LNMP搭建,nginx安装,php-fpm,版本升级,配置虚拟主机

环境,glibc安装mysql,yum源配好epel

1.安装依赖

# yum -y install pcre-devel zlib-devel openssl-devel

2.下载nginx

官网:http://www.nginx.org

http://nginx.org/download/nginx-1.16.1.tar.gz

# cd /software
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
# tar xf nginx-1.16.1.tar.gz
# cd nginx-1.16.1
# useradd -r -s /sbin/nologin www
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
# make && make install

启动Nginx软件
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

停止Nginx
# /usr/local/nginx/sbin/nginx -s stop

3.Nginx/Apache的区别

Apache与PHP的之间的关系:PHP是以模块的形势加载在Apache的内核中。在解析动态PHP代码时,其效率较高。LoadModule

Nginx与PHP之间的关系:Nginx与PHP相对而言都是独立的,只不过在应用过程中,解析动态脚本时,Nginx会自动源代码发送给PHP-FPM程序。Nginx在处理静态页面时,效率较高。

4.安装PHP-FPM

安装依赖库

#  yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
# cd /software
# wget https://www.php.net/distributions/php-7.2.23.tar.bz2
# tar xf php-7.2.23.tar.bz2
# cd php-7.2.23
# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts

Thank you for using PHP.
成功
# 请确保机器内存不小于512MB
# make && make install

...漫长的等待...

Build complete.
成功

5.配置PHP-FPM

# cp /software/php-7.2.23/php.ini-development /usr/local/php/etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# cp /software/php-7.2.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# service php-fpm start
# ss -nltp

9000端口被php-fpm占用
成功

加入环境变量
# echo 'export PATH=$PATH:/usr/local/php/bin' >> /etc/profile
# source /etc/profile

6.关联nginx与php

上项目之前先授权。
# chown www.www -R /usr/local/nginx

# cd /usr/local/nginx/html
# vim demo.php

<?php phpinfo(); ?>

# vim /usr/local/nginx/conf/nginx.conf

在
location / {
            root   html;
            index  index.html index.htm;
}
后面添加

location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
}       
保存退出

重启nginx
# /usr/local/nginx/sbin/nginx -s stop
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

访问 
http://10.1.1.10/demo.php
可以解析php代码啦~

7.升级nginx

接上文,升级nginx到1.17.5

# 查看当前nginx版本
# /usr/local/nginx/sbin/nginx -v
显示
nginx version: nginx/1.16.1

# 后续程序一样,下载一个更高版本,同样的配置,安装同样的目录

# cd /software
# wget http://nginx.org/download/nginx-1.17.5.tar.gz
# tar xf nginx-1.17.5.tar.gz
# cd nginx-1.17.5
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
# make && make install

# 原处安装两个nginx的反应就是

# ll /usr/local/nginx/sbin/
-rwxr-xr-x 1 root root 5991776 Nov  7 21:24 nginx
-rwxr-xr-x 1 root root 5983944 Nov  6 14:43 nginx.old
# nginx.old是原来nginx的,nginx是新版本的。
# 验证
# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.17.5
# /usr/local/nginx/sbin/nginx.old -v
nginx version: nginx/1.16.1

安装完毕之后,要优雅的切换版本。

# 原理:开启两个nginx进程,每一个nginx进程都有一个master和若干个worker。
# 先关闭worker进程,再优雅得退出master进程
# 优雅?如果直接关闭服务再开启也行,但是如果线上环境就需要不停服,优雅得升级

# 查看nginx的PID
# ps -ef | grep nginx
root      1216     1  0 21:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       1217  1216  0 21:32 ?        00:00:00 nginx: worker process
root      1219  1196  0 21:32 pts/0    00:00:00 grep --color=auto nginx

如上master的PID是1216

# 拉起两个nginx进程
# kill -usr2 1216
# ps -ef | grep nginx
root      1216     1  0 21:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       1217  1216  0 21:32 ?        00:00:00 nginx: worker process
root      1220  1216  2 21:33 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       1221  1220  0 21:33 ?        00:00:00 nginx: worker process
root      1223  1196  0 21:33 pts/0    00:00:00 grep --color=auto nginx
# 有两个nginx了

# 关闭旧nginx的worker
# kill -winch 1216(要输入master的PID来关闭worker)
# 优雅的关闭旧master
# kill -quit 1216

完毕

8.配置文件与虚拟主机

nginx.conf文件初探

可以精简nginx.conf文件
cd /usr/local/nginx/conf
grep -Ev '#|^$' nginx.conf > nginx.conf1
mv nginx.conf1 nginx.conf

关注重点http{}区域
首先,配置文件中仅可以存在一个http{}区域
其次,一个http{}中可以存在多个server{}区域

http {
    include       mime.types;     # include引入文件 mime.types里是nginx可处理的所有文件格式
    default_type  application/octet-stream;    # 默认以8位文件流模式传输文件
    sendfile        on;    # 略
    keepalive_timeout  65;    # 保持连接时间
    server {    # server区域,虚拟主机设置点
        listen       80;    # 监听端口
        server_name  localhost;    # 主机名称
        location / {    # 默认访问
            root   html;    # 项目程序路径
            index  index.html index.htm;    # 默认主页文件
        }
	    location ~ \.php$ {    # 需要解析php文件就写上这段
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
	}      
    error_page   500 502 503 504  /50x.html;     # 默认报错页面。
    location = /50x.html {
            root   html;
    }
}


# 修改配置要重启nginx
# /usr/local/nginx/sbin/nginx -s reload

服务器端结束

虚拟主机访问端需要修改host
windows下hosts在
C:\Windows\System32\drivers\etc\hosts
linux下hosts在 
/etc/hosts

修改方法一样
增加一行

# IP    上面设定的自定义域名
例如
10.1.1.10    www.h5.com

修改完毕保存,访问验证即可。

如果要设置虚拟主机,则增加一个server区域

http {
    ...略...
    server {
        原来的
    }

    server {
        listen       80;
        server_name  虚拟主机域名;
        location / {
            root   虚拟主机项目路径;
            index  index.html index.htm;
        }
	    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
	}      
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
            root   html;
    }


}

其本质就是复制了一遍,改俩地方

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值