1. 安装Nginx
1.1. 源码编译安装
1.1.1. 安装(仅安装基础模块)
[root@centos-81 ~]# yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@centos-81 ~]# useradd -u 8080 -M -s /sbin/nologin nginx
[root@centos-81 ~]# cd /usr/local/src/ ; wget https://nginx.org/download/nginx-1.14.2.tar.gz
[root@centos-81 src]# tar -xf nginx-1.14.2.tar.gz
[root@centos-81 src]# cd nginx-1.14.2/
[root@centos-81 nginx-1.14.2]# ./configure --prefix=/opt/apps/nginx --user=nginx --group=nginx
[root@centos-81 nginx-1.14.2]# make -j 4 && make install
1.1.2. 版本信息
[root@centos-81 nginx-1.14.2]# /opt/apps/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/opt/apps/nginx --user=nginx --group=nginx
[root@centos-81 ~]# /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf ## start
1.1.3. 启动脚本配置(可选)
[root@centos-81 ~]# vim /etc/init.d/nginx
#!/bin/bash
# Author=heyingsheng
# Date=2018-03-02
# Version=1.0
# Instorduction: Manage nginx process.
APP="/opt/apps/nginx/sbin/nginx"
PIDF="--pid-path=/opt/logs/nginx/nginx.pid" # 根据实际情况选择
case "$1" in
start)
[ -f $PIDF ] && exit || $APP
;;
stop)
[ -f $PIDF ] && kill -3 $(cat $PIDF)
;;
restart)
[ -f $PIDF ] && kill -3 $(cat $PIDF)
$APP
;;
reload)
if [ -f $PIDF ];then
$APP -s reload
else
$APP && sleep 2 && $APP -s reload
fi
;;
-v)
$APP -v
;;
-V)
$APP -V
;;
-t)
$APP -t
;;
*)
echo "Usage: $0 { start | stop | restart | reload } # Start or stop service."
echo " $0 { -v | -V } # Show version of nginx"
echo " $0 -t # Check profile."
exit
esac
[root@centos-81 ~]# chmod u+x /etc/init.d/nginx
[root@centos-81 ~]# echo '/etc/init.d/nginx restart' >> /etc/rc.d/rc.local && chmod u+x /etc/rc.d/rc.local # 可选
1.1.5. 编译参数
—prefix= |
Specify install directory of nginx.The default path is /usr/local/nginx. |
---|---|
—sbin-path= |
Specify path that save executables file.The default path is |
—conf-path= |
Specify profiles path.The default path is |
—user= |
Specify user of process. |
—group= |
Specify group of process. |
—with-poll_module | Specify open poll module. |
—without-poll_module | Specify close poll module. |
—with-select_module | Specify open select module,it is default module. |
—without-select_module | Specify close select module. |
—with-http_ssl_module | Specify open ssl module for https. |
—with-http_realip_module | Specify open realip module.It close default. |
—with-http_sub_module | Specify open sub module.It close default. |
—with-http_addition_module | Specify open addition module.It close default. |
—with-http_dav_module | Specify open dav module.It close default. |
—with-http_flv_module | Specify open flv module.It close default. |
—with-http_stub_status_module | Specify open server status.It close default. |
1.2. 编译新模块(gunzip)
1.2.1. 编译
[root@centos-81 src]# rm -rf nginx-1.14.2 && tar -xf nginx-1.14.2.tar.gz && cd nginx-1.14.2/
[root@centos-81 nginx-1.14.2]# ./configure —help | grep gunzip
--with-http_gunzip_module enable ngx_http_gunzip_module
[root@centos-81 nginx-1.14.2]# /opt/apps/nginx/sbin/nginx -V ## 确认编译参数
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/opt/apps/nginx --user=nginx --group=nginx
[root@centos-81 nginx-1.14.2]# ./configure —prefix=/opt/apps/nginx —user=nginx —group=nginx —with-http_gunzip_module
[root@centos-81 nginx-1.14.2]# make -j 4
1.2.2. 直接替换(推荐)
[root@centos-81 nginx-1.14.2]# /opt/apps/nginx/sbin/nginx -s stop
[root@centos-81 nginx-1.14.2]# mv /opt/apps/nginx/sbin/nginx /opt/apps/nginx/sbin/nginx.old
[root@centos-81 nginx-1.14.2]# cp objs/nginx /opt/apps/nginx/sbin/nginx
[root@centos-81 nginx-1.14.2]# /opt/apps/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/opt/apps/nginx --user=nginx --group=nginx --with-http_gunzip_module
1.2.3. 平滑更新
官方文档:https://nginx.org/en/docs/control.html
图解:https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/
- 准备
[root@centos-81 nginx-1.14.2]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep ‘(nginx|PID)’|grep -v grep
PID PPID USER %CPU VSZ WCHAN COMMAND
10069 1 root 0.0 20544 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
10070 10069 nginx 0.0 20988 ep_pol nginx: worker process
- 替换nginx可执行文件
[root@centos-81 nginx-1.14.2]# mv /opt/apps/nginx/sbin/nginx /opt/apps/nginx/sbin/nginx.old
[root@centos-81 nginx-1.14.2]# cp objs/nginx /opt/apps/nginx/sbin/nginx
- 启动新的nginx进程(新老进程同时提供服务)
[root@centos-81 nginx-1.14.2]# kill -USR2 10069
[root@centos-81 nginx-1.14.2]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep ‘(nginx|PID)’|grep -v grep
PID PPID USER %CPU VSZ WCHAN COMMAND
10069 1 root 0.0 20544 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
10070 10069 nginx 0.0 20988 ep_pol nginx: worker process
10160 10069 root 0.0 20552 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
10161 10160 nginx 0.0 20996 ep_pol nginx: worker process
[root@centos-81 nginx-1.14.2]# tail /opt/apps/nginx/logs/nginx.pid*
==> /opt/apps/nginx/logs/nginx.pid <==
10160
==> /opt/apps/nginx/logs/nginx.pid.oldbin <==
10069
- 停止老nginx的工作进程
[root@centos-81 nginx-1.14.2]# kill -WINCH 10069
[root@centos-81 nginx-1.14.2]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep ‘(nginx|PID)’|grep -v grep
PID PPID USER %CPU VSZ WCHAN COMMAND
10069 1 root 0.0 20544 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
10160 10069 root 0.0 20552 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
10161 10160 nginx 0.0 20996 ep_pol nginx: worker process
- 停止老nginx的master进程
[root@centos-81 nginx-1.14.2]# kill -QUIT 10069
[root@centos-81 nginx-1.14.2]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep ‘(nginx|PID)’|grep -v grep
PID PPID USER %CPU VSZ WCHAN COMMAND
10160 1 root 0.0 20552 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
10161 10160 nginx 0.0 20996 ep_pol nginx: worker process
1.3. yum 安装Nginx
官方文档:https://nginx.org/en/linux_packages.html#RHEL-CentOS
一般都使用源码编译安装,很少采用yum安装。
2. 启动选项与信号
2.1 Nginx启动选项
[root@centos-81 nginx-1.14.2]# /opt/apps/nginx/sbin/nginx -h
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /opt/apps/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
常用的几个命令选项:
/opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf # 指定配置文件启动
/opt/apps/nginx/sbin/nginx -t # 检查配置文件
/opt/apps/nginx/sbin/nginx -V # 查看版本与编译选项
/opt/apps/nginx/sbin/nginx -s stop # 停止nginx
/opt/apps/nginx/sbin/nginx -s reload # 重载配置
/opt/apps/nginx/sbin/nginx -s reopen # 重新打开日志
/opt/apps/nginx/sbin/nginx -g directives # 临时指定运行指令
2.2 常用信号
kill -s INT pid | Stop nginx quickly.It like “-s stop”. |
---|---|
kill -s QUIT pid | Stop nginx slowly.It like “-s quit”. |
kill -s HUP pid | Start workers with new profile if it correct, then stop old workers slowly.It like “-s reload”. |
kill -s USR1 pid | Reopen log files.It uses for rotate log files.It like “-s reopen”. |