菜单
本页目录

10 LNMP

image-20221102230037080

主题软件:源码包安装

辅助软件:网络yum安装

一 准备

1、配置好IP,使得能联网

2、配置网络yum源,并安装扩展源 epel-release(glances:python写的系统使用情况工具)

yum -y install epel-release

3、安装编译工具、依赖

yum -y install gcc gcc-c++ pcre-devel openssl openssl-devel zlib-devel ncurses-devel cmake bison libxml2-devel libpng-devel

4、下载Nginx、Mysql、Php源码包

​ Nginx:http://nginx.org/en/download.html

​ MySQL:https://dev.mysql.com/downloads/mysql/

​ PHP:http://www.php.net/

版本选用:

​ Nginx: 1.12.* #选用软件的稳定版即可

​ Mysql: 5.5.* #5.5以上版本需要1G以上的内存,否则无法安装

​ PHP: 7.1.* #我们使用的是php7

注意:每次安装LNMP****时,软件包的小版本都不一样,官方会对其大版本下的小版本进行覆盖式更新,本文内部分链接会失效,切记按照下载版本进行安装。

二 源码安装

1、Nginx

1.创建 /lamp 目录,wget下载,解压,进入目录
[root@localhost ~]# mkdir /lnmp
[root@localhost ~]# cd /lnmp/
[root@localhost lnmp]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@localhost lnmp]# tar -xf nginx-1.12.2.tar.gz 
[root@localhost lnmp]# cd nginx-1.12.2/
2.创建nginx服务用户
[root@localhost nginx-1.12.2]# useradd -r -s /sbin/nologin nginx
3.指定安装位置,开启模块等、编译、安装
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module && make -j 8 && make install
4.创建软链接,方便命令调用

​ nginx(启动)

​ nginx -s stop (停止)

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
5.上传nginx启动脚本

​ 到 /etc/init.d/nginx、别忘了添加可执行权限

#!/bin/bash
#Author:YQ
#chkconfig: 2345 99 33
#description: nginx server control tools

ngxc="/usr/local/nginx/sbin/nginx"
pidf="/usr/local/nginx/logs/nginx.pid"
ngxc_fpm="/usr/local/php/sbin/php-fpm"
pidf_fpm="/usr/local/php/var/run/php-fpm.pid"
case "$1" in
    start)
        $ngxc -t &> /dev/null
        if [ $? -eq 0 ];then
                $ngxc
                $ngxc_fpm
                echo "nginx service start success!"
        else
                $ngxc -t
        fi
        ;;
    stop)
        kill -s QUIT $(cat $pidf)
        kill -s QUIT $(cat $pidf_fpm)
        echo "nginx service stop success!"
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        $ngxc -t &> /dev/null
        if [ $? -eq 0 ];then
                kill -s HUP $(cat $pidf)
                kill -s HUP $(cat $pidf_fpm)
                echo "reload nginx config success!"
        else
                $ngxc -t
        fi
        ;;
    *)
        echo "please input stop|start|restart|reload."
        exit 1
		;;
esac
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx

2、Mysql

1.wget下载、解压、进入
wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.62.tar.gz && tar -xf mysql-5.5.62.tar.gz && cd mysql-5.5.62
2.创建mysql服务用户
[root@localhost mysql-5.5.62]# useradd -r -s /sbin/nologin mysql
3.指定安装位置,开启模块、编译、安装、创建软链接,方便命令调用
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 && make -j 8 && make install && ln -s /usr/local/mysql/bin/*  /usr/local/bin
4.配置mysql

​ 1.修改主目录下的data权限:方便后期调用数据库文件用mysql身份

​ 2.生成配置文件:将源码安装后的路径下配置文件,放到 /etc/my.conf 中

​ 3.指定用户安装:指定mysql用户身份

​ 4.启动mysql

​ 5.创建用户密码

chown -R mysql:mysql /usr/local/mysql/data
cp -a /lnmp/mysql-5.5.62/support-files/my-medium.cnf /etc/my.cnf << EOF
y
EOF
cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
service mysqld start
mysqladmin -uroot password '123456'

3、PHP

1.wget下载、解压、进入
cd /lnmp
wget https://www.php.net/distributions/php-7.1.29.tar.gz && tar -xf php-7.1.29.tar.gz && cd php-7.1.29
2.安装

​ 指定安装位置,开启模块、编译、安装、创建软链接

./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-soap --enable-mbstring=all --enable-sockets --with-pdo-mysql=/usr/local/mysql --with-gd --without-pear --enable-fpm && make -j 8 &&make install
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/

报错提示:若遇到libpng.so not found .报错(老版本的PHP会出现此问题) 解决方案: ln –s /usr/lib64/libpng.so /usr/lib

3.生成php配置文件
[root@localhost ~]# cp -a /lnmp/php-7.1.29/php.ini-production /usr/local/php/etc/php.ini

4、配置 nginx 连接 php

4.1 php-fpm

​ 1.进入 /usr/local/php/etc 中 将模板配置文件复制,去掉default 作为主配置文件

​ 2.进入 /usr/local/php/etc/php-fpm.d/ 将模板文件复制,去掉defaul 作为次配置文件

​ 3.修改 php-fpm 主配置文件:去掉pid注释

[root@localhost etc]# cd /usr/local/php/etc/
[root@localhost etc]# cp -a php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
pid = run/php-fpm.pid

​ 4.修改 php-fpm 次配置文件:将用户,组改为 nginx

[root@localhost php-fpm.d]# cp -a www.conf.default www.conf
[root@localhost etc]# vim /usr/local/php/etc/php-fpm.d/www.conf
user = nginx
group = nginx
4.2 修改nginx主配置文件,使 nginx 识别 php

​ /usr/local/nginx/conf/nginx.conf

拓展:压力测试命令: ab (属于 httpd-tools)

ab -c 10000 -n 1000 http://192.168.18.201/index.html #总共访问一万次,每次点击一千次

​ 1.修改用户为 nginx

user  nginx nginx;

​ 2.取消 pid 行的注释

pid        logs/nginx.pid;

​ 3.取消日志记录规则注释,并在 server 标签中根据域名取消日志记录项

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    server {
...
        access_log  logs/host.access.log  main;
...

​ 4.取消识别 php 规则的代码注释部分,并将调用的子配置文件改为 fastcgi.conf

        location ~ \.php(/.*)*$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;										#修改调用子配置文件为 fastcgi.conf
        }

5、LNMP配置后相关设置

​ 1.启动 nginx、mysql、php-fpm

[root@localhost ~]# nginx 
[root@localhost ~]# php-fpm 
[root@localhost ~]# service mysqld start
Starting MySQL.. SUCCESS! 
[root@localhost ~]# netstat -anpt |egrep "nginx|php-fpm|mysql"
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      9798/php-fpm: maste 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      10083/mysqld        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9794/nginx: master 

​ 2.将 nginx、mysql、php-fpm加入自启动

[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# vim /etc/rc.d/rc.local
[root@localhost ~]# grep "&" /etc/rc.d/rc.local
/usr/local/sbin/nginx &
/usr/local/sbin/php-fpm &
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on

三 实验

proxy:反向代理 负责接待、分配用户请求,负责均衡负载

​ 部分反向代理:nginx对请求进行处理(接待且处理)

​ 完全反向代理:nginx对请求不做处理(仅接待,然后交给apache)

nginx -s reload #不重启服务,仅加载配置

功能httpnginx
识别后缀的文件mime.typesmime.types
声明主目录DocumentRootroot
声明首页文件DirectoryIndexindex
声明端口Listenlisten
声明域名ServerNameserver_name
声明日志格式CustomLoglog
虚拟主机标签VirtualHostserver

1、 nginx作为反向代理

1)部分反向代理:

​ 在nginx.conf中配置 (IP为代理的apache服务器IP)

        location ~ \.php$ {
            proxy_pass   http://192.168.18.201;	
        }

实验:

#202nginx端
[root@localhost ~]# echo "this is 202 html" > /usr/local/nginx/html/index.html
[root@localhost ~]# cat << EOF > /usr/local/nginx/html/index.php
> <?php
>    echo "this is 202 php";
> ?>
> EOF
[root@localhost ~]# nginx -s reload

#201apache端
[root@localhost ~]# echo "this is 201 html" > /usr/local/apache2/htdocs/index.html 
[root@localhost ~]# cat << EOF > /usr/local/apache2/htdocs/index.php
> <?php
>    echo "this is 201 php";
> ?>
> EOF
[root@localhost ~]# apachectl restart

测试:201apache端查看日志,客户端访问202nginx

部分反向代理

2)完全反向代理:

​ 在nginx.conf中配置,将 proxy_pass 行加入 location / 标签中

        location / {
            root   html;
            index  index.html index.htm index.php;
            proxy_pass   http://192.168.18.201;
        }

测试:201apache端查看日志,客户端访问202nginx

完全反向代理

2、 让nginx识别php

​ 在nginx.conf中配置,重新配置 nginx 服务

        location ~ \.php(/.*)*$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

nginx识别php