首页 > 系统相关 >一步一步教你Nginx平滑升级和动静分离

一步一步教你Nginx平滑升级和动静分离

时间:2023-02-19 19:00:38浏览次数:42  
标签:www nginx centos01 平滑 一步 Nginx com root usr

一、在Centos01安装Nginx服务,配置网站主页,使用www.szt.com访问Nginx

1、在Centos01上安装Nginx依赖

1)挂载光盘配置本地yum仓库

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*

2)安装依赖

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

3)创建管理Nginx用户

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

2、配置安装Nginx服务

1)切换光盘

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/

2)解压配置安装nginx

[root@centos01 ~]# tar zxf
/mnt/nginx-1.6.0.tar.gz -C /usr/src/
[root@centos01 nginx-1.6.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

3)编译安装Nginx

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

4)优化nginx命令检查配置文件

[root@centos01 ~]# ln -s
/usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3、设置Nginx服务器网站根目录设置主页修改配置文件

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

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

2)修改Nginx主配置文件

[root@centos01 ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user
nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
worker_connections 1024;
}
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
}

3)启动Nginx服务监听端口号

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3740/nginx: master

二、在Centos02配置DNS服务器使用,创建stz.com域和Benet.com域分别添加主机指向Nginx主机IP地址和lamp主机IP地址

1、安装DNS服务设置开机自动启动

1)挂载系统光盘安装DNS服务

[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt/
CentOS_BuildTag images repodata
EFI isolinux RPM-GPG-KEY-CentOS-7
EULA LiveOS RPM-GPG-KEY-CentOS-Testing-7
GPL Packages TRANS.TBL

2)安装DNS服务

[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-chroot-9.9.4-50.el7.x86_64.rpm

3)设置服务开机自动启动

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

2、修改主配置文件和区域配置文件

1)修改DNS主配置文件

[root@centos02 ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory "/var/named";
};
zone "stz.com" IN {
type master;
file "/var/named/stz.com.zone";
};
zone "benet.com" IN {
type master;
file "/var/named/benet.com.zone";
};

2)检查主配置文件是否错误

[root@centos02 ~]# named-checkconf /etc/named.conf

3)修改区域配置文件

[root@centos02 ~]# vim
/var/named/stz.com.zone
$TTL 86400
@ SOA stz.com. root.stz.com. (
2022021609
1H
15M
1W
1D
)
@ NS centos02.stz.com.
centos02 A 192.168.100.20
www A 192.168.100.10
[root@centos02 ~]# vim
/var/named/benet.com.zone
$TTL 86400
@ SOA benet.com. root.benet.com. (
2022021609
1H
15M
1W
1D
)
@ NS centos02.benet.com.
centos02 A 192.168.100.20
www A 192.168.100.30

4)检查区域配置文件是否错误

[root@centos02 ~]# named-checkzone stz.com /var/named/stz.com.zone
zone stz.com/IN: loaded serial 2022021609
OK
[root@centos02 ~]# named-checkzone benet.com /var/named/benet.com.zone
zone benet.com/IN: loaded serial 2022021609
OK

3、启动DNS服务查看服务运行状态

1)启动DNS服务

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

2)查看服务运行状态

[root@centos02 ~]# netstat -anptu | grep named
tcp
0 0 127.0.0.1:53 0.0.0.0:* LISTEN 1449/named
tcp
0 0 127.0.0.1:953 0.0.0.0:* LISTEN 1449/named
tcp6
0 0 ::1:53 :::* LISTEN 1449/named
tcp6
0 0 ::1:953 :::* LISTEN
1449/named
udp
0 0 127.0.0.1:53 0.0.0.0:* 1449/named
udp6
0 0 ::1:53 :::* 1449/named

3)测试域名解析

[root@centos02 ~]# nslookup www.stz.com
Server: 127.0.0.1
Address: 127.0.0.1#53

Name: www.stz.com
Address: 192.168.100.10
[root@centos02 ~]# nslookup www.benet.com
Server: 127.0.0.1
Address: 127.0.0.1#53

Name: www.benet.com
Address: 192.168.100.30

三、在Centos03使用yum安装LAMP服务配置,使用域名wwwbenet.Com访问lamp,在Nginx配置动静分离,静态Nginx处理动态LAMP处理

1、配置yum安装LAMP平台

1)挂载系统光盘配置yum仓库

[root@centos03 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos03 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos03 ~]# cat /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt
enabled=1
gpgcheck=0

2)使用yum安装LAMP平台

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

3)启动服务设置开机自动启动

[root@centos03 ~]# systemctl start httpd
[root@centos03 ~]# systemctl enable httpd
[root@centos03 ~]# systemctl start mariadb
[root@centos03 ~]# systemctl enable mariadb

4)设置网站主页

[root@centos03 ~]# vim /var/www/html/index.html
www.benet.com

5)设置php测试页

[root@centos03 ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>

2、访问Nginx和LAPM平台测试

1)给客户端配置ip

一步一步教你Nginx平滑升级和动静分离_centos

2)访问Nginx

一步一步教你Nginx平滑升级和动静分离_centos_02

3)访问LAMP平台

一步一步教你Nginx平滑升级和动静分离_centos_03

3)访问LAMP的php

一步一步教你Nginx平滑升级和动静分离_nginx_04

3、在Nginx服务器配置动静分离,静态数据图片访问Nginx动态php访问跳转到LAMP

1)修改Nginx主配置文件支持动静分离

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location ~ .*\.(gif|jpg|bmp|swf)$ {
root /www;
index index.html index.htm;
}
location ~ \.php$ {
proxy_pass http://www.benet.com;
}
}
}

2)检查配置出错没有

[root@centos01 ~]# nginx -t
nginx: the configuration file
/usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file
/usr/local/nginx/conf/nginx.conf test is successful

3)重新启动nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

4)客户端访问php动态数据查看抓包信息

四、在Centos01上安装nginx实现平滑升级

1、安装老版本nginx

1)安装依赖程序创建管理nginx用户

[root@centos01 ~]# yum -y install pcre-devel zlib-devel
[root@centos01 ~]# useradd -M -s /sbin/nologin nginx

2)解压配置老版本nginx

[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

3)编译安装

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

4)优化Nginx命令

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

2、配置老版本nginx网站服务器

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

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

2)修改Nginx配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
worker_connections 1024;
}
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
}

3)启动nginx服务

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 47091/nginx: master

4)客户端访问

一步一步教你Nginx平滑升级和动静分离_centos_05

3、安装新版nginx服务器平滑升级,配置nginx监控访问监控进程基本身份验证

1)解压配置nginx

[root@centos01 ~]# ls
anaconda-ks.cfg nginx-1.16.1.tar.gz 视频 下载
initial-setup-ks.cfg 公共 图片 音乐
nginx-1.12.0.tar.gz 模板 文档 桌面
[root@centos01 ~]# tar -zxf ./nginx-1.16.1.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nginx-1.6.0/
[root@centos01 nginx-1.6.0]# ./configure \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

2)编译安装

[root@centos01 nginx-1.6.0]# make

3)关闭老版本服务

[root@centos01 nginx-1.6.0]# killall -I -QUIT nginx

4)生成管理服务文件

[root@centos01 nginx-1.6.0]# cp objs/nginx /usr/local/sbin

5)查看nginx运行版本

[root@centos01 ~]# nginx -V
nginx version: nginx/1.6.0
built by gcc 4.8.5 20150623 (Red Hat
4.8.5-16) (GCC)
configure arguments: --user=nginx
--group=nginx --with-http_stub_status_module

6)启动新版

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 49485/nginx: master

7)查看升级后的版本

一步一步教你Nginx平滑升级和动静分离_nginx_06

4、配置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 bob
New password:
Re-type new password:
Adding password for user bob

3)修改Nginx主配置文件加载验证和采集监控数据

[root@centos01 ~]# vim
/usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events {
worker_connections 1024;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
location /status {
stub_status on;
access_log off;
auth_basic "welcom tomauth";
auth_basic_user_file /usr/local/nginx/password;

}
}
}

4)重新启动nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

5)客户端访问验证

一步一步教你Nginx平滑升级和动静分离_centos_07

6)查看监控

一步一步教你Nginx平滑升级和动静分离_Nginx_08

标签:www,nginx,centos01,平滑,一步,Nginx,com,root,usr
From: https://blog.51cto.com/u_15946809/6066798

相关文章

  • Docker 中使用Nginx网站的搭建
    Docker中使用Nginx网站的搭建使用Nginx来搭建完整的前置站点,实现后向的代理,这篇文章中简单介绍一个搭建的步骤,至于Nginx的知识,Docker的使用可以参考对应的文档。前提......
  • Nginx+Rtmp推流服务器方案
    推流服务器方案一一、前期准备操作系统:Centos7Linux系统Nginx版本:nginx-1.22.0.tar.gzRTMP模块:nginx-rtmp-module推流工具:OBS-Studio拉流工具二、环境搭建1.安......
  • 对fork函数的进一步分析
       在fork之前的printf和write函数只会父进程调用一次,子进程不会调用,因为那时子进程还没有创建出来。当fork时,子进程被创建,程序只会往下顺序执行,但是前面父进程分配......
  • LVS(Linux Virtual Server)+Nginx 高可用集群
    LVS(Linux虚拟服务器)LVS(LinuxVirtualServer)是一个开源的负载均衡项目,是国内最早出现的开源项目之一,目前已被集成到Linux内核模块中。该项目在Linux内核中实现了基于......
  • Nginx location
                                                 c......
  • nginx之rewrite四种flag
    利用nginx的rewrite命令,可以实现URL的重写,可在nginx配置文件的server、location、if部分使用,对于rewrite有四种不同的flag。redirect:返回302临时重定向,浏览器地址栏会显示......
  • nginx 配置 详解
    2.nginx.conf配置文件Nginx配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和location(URL匹配特定位置......
  • Nginx匹配@符号的作用
    Nginx匹配@符号的作用@符号,用于定义一个Location块,且该块不能被外部Client所访问,只能被Nginx内部配置指令所访问,比如try_files或error_page.error_page400=@......
  • nginx原理学习--6
    nginx的请求处理阶段 接收请求流程  http请求格式简介 首先介绍一下rfc2616中定义的http请求基本格式: Request=Request-Line*((general-hea......
  • nginx配置无限个子域名
    nginx配置多个子域名功能:不管多少个子域名demo.liaosp.top/还是demo2.liaosp.top在servername添加域名,*的域名在前面修改:server_name*.daishua.liaosp.topdaishua.li......