Nginx和动静分离和平滑升级

时间:2023-02-10 16:02:54

实验步骤:把Nginx包拖进CentOS1里面

1、安装依赖程序和管理用户

)创建管理Nginx用户

 [root@centos01 ~]# useradd -M -s /sbin/nologin nginx

2)安装依赖程序  

 [root@centos01 ~]# yum -y install pcre-devel zlib-devel

3)解压配置Nginx

挂载光盘mount /dev/cdrom /mnt/

删除Centos # rm -rf /etc/yum.repos.d/CentOS-*

 [root@centos01 ~]# tar zxf ./nginx-1.12.0.tar.gz -C /usr/src/

 [root@centos01 ~]# cd /usr/src/nginx-1.12.0/

 [root@centos01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx

 --user=nginx --group=nginx --with-http_stub_status_module

4)编译安装nginx

[root@centos01 nginx-1.12.0]# make && make install

5)优化命令

[root@centos01 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

[root@centos01 nginx-1.12.0]# cd

2.安装LAMP平台

1)安装LAMP平台  

[root@centos02 ~]# yum -y install httpd php php-mysql mariadb-server

Nginx和动静分离和平滑升级

2)启动服务

[root@centos02 ~]# systemctl start httpd

[root@centos02 ~]# systemctl enable httpd

[root@centos02 ~]# systemctl enable mariadb

[root@centos02 ~]# systemctl start mariadb

Nginx和动静分离和平滑升级

3)设置apache网站主页

       [root@centos02 ~]# echo "www.benet.com" > /var/www/html/index.html

4)配置php测试页

       [root@centos02 ~]# vim /var/www/html/index.php

Nginx和动静分离和平滑升级

3、配置动静分离

       1)创建Nginx网站根目录设置主页

              [root@centos01 ~]# mkdir /www

              [root@centos01 ~]# echo "www.bdqn.com" > /www/index.html

       2)修改Nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf

              user  nginx;

              worker_processes  1;

              events {

                     worker_connections  1024;

              }

              http {

                     include       mime.types;

                     default_type  application/octet-stream;

 

                     access_log  logs/access.log;

                     sendfile        on;

                     keepalive_timeout  65;

                     server {

                            listen       192.168.100.10:80;

                            server_name  www.bdqn.com;

                            charset utf-8;

                            access_log  logs/www.bdqn.com.access.log;

                            location / {

                                   root   /www/;

                                   index  index.html index.htm;

                               }

                            location ~ .*\.(gif|jpg|jpeg|bmp|swf)$ {                    //图片本地Nginx处理

                               root /www/;

                               index index.html index.htm;

                            }

                            location ~ \.php$ {                                                               //php结尾文件转发到LAMP处理

                               proxy_pass   http://www.benet.com;

                            }

                }

              }

       3)重新启动Nginx服务

              [root@centos01 ~]# killall nginx

              [root@centos01~]#nginx

Nginx和动静分离和平滑升级

Nginx监控访问监控需要输入账户密码

       1)安装验证数据库程序

              [root@centos01 ~]# rpm -ivh /mnt/Packages/httpd-tools-2.4.6-67.el7.centos.x86_64.rpm

       2)生成验证数据库

              [root@centos01 ~]# htpasswd -c /usr/local/nginx/password admin

       3)修改Nginx配置文件支持监控

              [root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf

              user  nginx;

              worker_processes  1;

              events {

                     worker_connections  1024;

              }

              http {

                     include       mime.types;

                     default_type  application/octet-stream;

                     access_log  logs/access.log;

                     sendfile        on;

                     keepalive_timeout  65;

                     server {

                            listen       192.168.100.10:80;

                            server_name  www.bdqn.com;

                            charset utf-8;

                            access_log  logs/www.bdqn.com.access.log;

                            location / {

                                   root   /www/;

                                   index  index.html index.htm;

                               }

                            location ~ .*\.(gif|jpg|jpeg|bmp|swf)$ {

                               root /www/;

                               index index.html index.htm;

                            }

                            location ~ \.php$ {

                               proxy_pass   http://www.benet.com;

                            }

                            location /status {

                               stub_status on;

                         access_log off;

                               auth_basic "welcom Auth";

                               auth_basic_user_file /usr/local/nginx/password;

                            }

                }

              }

       4)重启Nginx服务

              [root@centos01 ~]# killall nginx

              [root@centos01~]#nginx

Nginx和动静分离和平滑升级

5、配置Nginx平滑升级

       1)停止老版本nginx服务

              [root@centos01 ~]# killall nginx

              nginx: no process found

Nginx和动静分离和平滑升级

2)解压新版本Nginx

              [root@centos01 ~]# tar zxf ./nginx-1.16.1.tar.gz -C /usr/src/

              [root@centos01 ~]# cd /usr/src/nginx-1.16.1/

3)配置新版本Nginx

              [root@centos01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx

              --group=nginx --with-http_stub_status_module

Nginx和动静分离和平滑升级

4)编译Nginx

              [root@centos01 nginx-1.16.1]# make

 

              [root@centos01 nginx-1.16.1]# cd

Nginx和动静分离和平滑升级

5)备份老版本进程

              [root@centos01 ~]# mv /usr/local/sbin/nginx /usr/local/sbin/nginx.bak

       6)生成新版本服务

              [root@centos01 ~]# ln -s /usr/src/nginx-1.16.1/objs/nginx /usr/local/sbin/

       7)启动服务

              [root@centos01 ~]# nginx

Nginx和动静分离和平滑升级