jjzjj

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

邂逅云边的落日 2023-03-28 原文

一、在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

2)访问Nginx

3)访问LAMP平台

3)访问LAMP的php

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)客户端访问

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)查看升级后的版本

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)客户端访问验证

6)查看监控

有关一步一步教你Nginx平滑升级和动静分离的更多相关文章

  1. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  4. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  5. ruby - 在不使用 RVM 的情况下在 Mac 上卸载和升级 Ruby - 2

    我最近决定从我的系统中卸载RVM。在thispage提出的一些论点说服我:实际上,我的决定是,我根本不想担心Ruby的多个版本。我只想使用1.9.2-p290版本而不用担心其他任何事情。但是,当我在我的Mac上运行ruby--version时,它告诉我我的版本是1.8.7。我四处寻找如何简单地从我的Mac上卸载这个Ruby,但奇怪的是我没有找到任何东西。似乎唯一想卸载Ruby的人运行linux,而使用Mac的每个人都推荐RVM。如何从我的Mac上卸载Ruby1.8.7?我想升级到1.9.2-p290版本,并且我希望我的系统上只有一个版本。 最佳答案

  6. ruby - 我正在学习编程并选择了 Ruby。我应该升级到 Ruby 1.9 吗? - 2

    我完全不是程序员,正在学习使用Ruby和Rails框架进行编程。我目前正在使用Ruby1.8.7和Rails3.0.3,但我想知道我是否应该升级到Ruby1.9,因为我真的没有任何升级的“遗留”成本。缺点是什么?我是否会遇到与普通gem的兼容性问题,或者甚至其他我不太了解甚至无法预料的问题? 最佳答案 你应该升级。不要坚持从1.8.7开始。如果您发现不支持1.9.2的gem,请避免使用它们(因为它们很可能不被维护)。如果您对gem是否兼容1.9.2有任何疑问,您可以在以下位置查看:http://www.railsplugins.or

  7. ruby-on-rails - 如何用不同的用户运行nginx主进程 - 2

    A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(

  8. ruby-on-rails - 从 Rails 2.3 升级到 Rails 4.0 - 2

    我们有一个目前在Rails2.3.12版和Ruby1.8.7版上运行的应用程序。我们想将我们的应用程序更新到Rails4.0和Ruby2.1.0。我们有大约200个模型和150个Controller。我想知道升级过程需要多大的努力。您还可以提供升级可以遵循的步骤。我们应该先升级Ruby然后再升级Rails还是相反? 最佳答案 您想要实现的目标将是史诗般的努力。我无法为您提供分步说明,因为不可能在一个答案中涵盖所有情况。我建议不要同时升级Ruby和Rails,而是分步升级。升级本身的复杂性是巨大的,但只要您的应用程序具有合理的测试覆盖

  9. ruby-on-rails - Rails 4.1.0.beta1 升级失败 - 2

    我最近尝试安装rails4.1.0.beta1,但是railss导致以下错误。[RVM]/gems/ruby-2.0.0-p247/gems/activesupport-4.1.0.beta1/lib/active_support/core_ext/module/aliasing.rb:32:in`alias_method':undefinedmethod`graft'forclass`ActiveRecord::Associations::JoinDependency'(NameError)[RVM]/gems/ruby-2.0.0-p247/gems/activesupport-4

  10. ruby-on-rails - ActionCable 无需升级到 Rails 5 beta - 2

    我目前有一个运行在4.2.5上的Rails应用程序,我想使用ActionCable而不必将整个应用程序升级到Rails5.0.0.beta3版本并冒破坏所有其他gem的风险。按照我在互联网上看到的指南,我已经尝试过gem'actioncable',github:'rails/actioncable'这不起作用,因为ActionCable存储库已合并到Rails存储库中。我什至试过gem'actioncable',github:'rails/rails'但这似乎不适用于ActionCable合并到Rails时发生的版本重新编号。(唯一低于5.0.0.beta*的版本是0.0.0,这似乎是

随机推荐