一、安装nginx
[root@tiejiang ~]# yum -y install gcc gcc-c++ wget pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@tiejiang ~]# cd /usr/local/src/
[root@tiejiang src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@tiejiang src]# useradd nginx -s /sbin/nologin -M
[root@tiejiang src]# tar zxvf nginx-1.14.0.tar.gz
[root@tiejiang src]# cd nginx-1.14.0
[root@tiejiangSRC1 ~]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-stream --with-http_realip_module
[root@tiejiang nginx-1.14.0]# make && make install
[root@tiejiang nginx-1.14.0]# cd /usr/local/nginx/
[root@tiejiang nginx]# useradd -s /sbin/nologin -M nginx
[root@tiejiang nginx]# ./sbin/nginx -c /usr/local/nginx/conf/nginx.conf //加载指定的配置文件
[root@tiejiang ~]# vim /lib/systemd/system/nginx.service
#服务的说明
[Unit]
#描述服务
Description=nginx
#描述服务类别
After=network.target
<span class="hljs-meta">#<span class="zh-hans">服务运行参数的设置</span></span>
[Service]
<span class="hljs-meta">#forking<span class="zh-hans">是后台运行的形式</span></span>
Type=forking
<span class="hljs-meta">#ExecStart<span class="zh-hans">为服务的具体运行命令</span></span>
ExecStart=<span class="hljs-meta-keyword">/usr/</span>local<span class="hljs-meta-keyword">/nginx/</span>sbin/nginx
<span class="hljs-meta">#ExecReload<span class="zh-hans">为重启命令</span></span>
ExecReload=<span class="hljs-meta-keyword">/usr/</span>local<span class="hljs-meta-keyword">/nginx/</span>sbin/nginx -s reload
<span class="hljs-meta">#ExecStop<span class="zh-hans">为停止命令</span></span>
ExecStop=<span class="hljs-meta-keyword">/usr/</span>local<span class="hljs-meta-keyword">/nginx/</span>sbin/nginx -s quit
<span class="hljs-meta">#PrivateTmp=True<span class="zh-hans">表示给服务分配独立的临时空间</span></span>
PrivateTmp=true
<span class="hljs-meta">#[Install]<span class="zh-hans">运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为</span>3</span>
[Install]
WantedBy=multi-user.target
[root@tiejiang ~]
# systemctl enable nginx.service //设置开机启动[root@tiejiang ~]
# systemctl start nginx.service //启动nginx命令[root@tiejiang ~]
# systemctl disable nginx.service //停止开机自启[root@tiejiang ~]
# systemctl status nginx.service //查看当前服务状态[root@tiejiang ~]
# systemctl restart nginx.service //重启nginx服务
2、检查nginx配置文件
[root@tiejiang ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
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@tiejiang ~]# pkill -9 nginx
4、平滑重启Nginx
如果改变了nginx的配置文件(nginx.conf),想重启nginx
1、先检查配置文件是否正确
[root@tiejiang ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
2、平滑重启
[root@tiejiang ~]# kill -HUP `/usr/local/nginx/logs/nginx.pid`
当nginx接收到hup信号时,他会尝试先解析配置文件(如果指定配置文件,就能使用指定的话,否则使用默认的),如果成功,就应用新的配置文件。之后nginx运行新的工作进程并从容关闭旧的工作进程。通知工作进程关闭监听套接字,但是继续为当连接的客户提供服务。所有客户的的服务完成后,旧的工作进程被关闭。如果新的配置文件应用失败,nginx将继续使用旧的配置进行工作。
5、Nginx的信号控制
TERM,INT快速关闭
QUIT从容关闭
HUP平滑重启,重新加载配置文件
USR1重新打开日志文件,在切割日志时用途比较大
USR2平滑升级可执行程序
WINCH从容关闭工作进程
6、nginx的平滑升级
1、使用新的可执行程序替换旧的可执行程序,对于编译安装的nginx,可以将新版本编译安装到旧版本的nginx安装路径中,替换之前,最好备份旧的。
2、发送指令:"kill -USR2 旧版本的nginx主进程号"
3、旧版本Nginx的主进程将重命名它的pid文件为oldbin。然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程
4、此时,新、旧版本的nginx实例会同时运行,共同处理输入的请求。要逐步停止旧版本的nginx实例。你必须发生WINCH信号给旧的主进程。然后,它的工作进程就将开始从容关闭;kill -WINCH 旧版本的nginx主进程号
5、一段时间后,旧的工作进入(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求
6、这时候,我们可以决定是使用新版本,还是恢复旧版本
kill -HUP 旧的主进程号:nginx将在不重载配置文件的情况下启动它的工作进程;
kill -QUIT 新的主进程号:从容关闭其工作进程(worker process);
kill -TERM 新的主进程号:强制退出
kill 新的主进程号或旧的主进程号:如果因为某原因新的工作进程不能退出,则向其发送kill信号。
新的主进程退出后,旧的主进程会溢出.oldbin前缀,恢复它为.pid文件,这样一切都恢复到升级之前了。如果尝试升级成功,而你也希望保留新的服务器时,可发生QUIT信号给旧的主进程,使其退出而只留下新的服务器运行;