1. 安装Nginx

1.1. 源码编译安装

1.1.1. 安装(仅安装基础模块)

  1. [root@centos-81 ~]# yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  2. [root@centos-81 ~]# useradd -u 8080 -M -s /sbin/nologin nginx
  3. [root@centos-81 ~]# cd /usr/local/src/ ; wget https://nginx.org/download/nginx-1.14.2.tar.gz
  4. [root@centos-81 src]# tar -xf nginx-1.14.2.tar.gz
  5. [root@centos-81 src]# cd nginx-1.14.2/
  6. [root@centos-81 nginx-1.14.2]# ./configure --prefix=/opt/apps/nginx --user=nginx --group=nginx
  7. [root@centos-81 nginx-1.14.2]# make -j 4 && make install

1.1.2. 版本信息

  1. [root@centos-81 nginx-1.14.2]# /opt/apps/nginx/sbin/nginx -V
  2. nginx version: nginx/1.14.2
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
  4. configure arguments: --prefix=/opt/apps/nginx --user=nginx --group=nginx
  5. [root@centos-81 ~]# /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf ## start

1.1.3. 启动脚本配置(可选)

  1. [root@centos-81 ~]# vim /etc/init.d/nginx
  2. #!/bin/bash
  3. # Author=heyingsheng
  4. # Date=2018-03-02
  5. # Version=1.0
  6. # Instorduction: Manage nginx process.
  7. APP="/opt/apps/nginx/sbin/nginx"
  8. PIDF="--pid-path=/opt/logs/nginx/nginx.pid" # 根据实际情况选择
  9. case "$1" in
  10. start)
  11. [ -f $PIDF ] && exit || $APP
  12. ;;
  13. stop)
  14. [ -f $PIDF ] && kill -3 $(cat $PIDF)
  15. ;;
  16. restart)
  17. [ -f $PIDF ] && kill -3 $(cat $PIDF)
  18. $APP
  19. ;;
  20. reload)
  21. if [ -f $PIDF ];then
  22. $APP -s reload
  23. else
  24. $APP && sleep 2 && $APP -s reload
  25. fi
  26. ;;
  27. -v)
  28. $APP -v
  29. ;;
  30. -V)
  31. $APP -V
  32. ;;
  33. -t)
  34. $APP -t
  35. ;;
  36. *)
  37. echo "Usage: $0 { start | stop | restart | reload } # Start or stop service."
  38. echo " $0 { -v | -V } # Show version of nginx"
  39. echo " $0 -t # Check profile."
  40. exit
  41. esac
  42. [root@centos-81 ~]# chmod u+x /etc/init.d/nginx
  43. [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 /sbin.
—conf-path= Specify profiles path.The default path is /conf.
—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

  1. --with-http_gunzip_module enable ngx_http_gunzip_module

[root@centos-81 nginx-1.14.2]# /opt/apps/nginx/sbin/nginx -V ## 确认编译参数

  1. nginx version: nginx/1.14.2
  2. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
  3. 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

  1. nginx version: nginx/1.14.2
  2. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
  3. 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/
01-Nginx的安装与启动 - 图1

  • 准备

[root@centos-81 nginx-1.14.2]# ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep ‘(nginx|PID)’|grep -v grep

  1. PID PPID USER %CPU VSZ WCHAN COMMAND
  2. 10069 1 root 0.0 20544 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
  3. 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

  1. PID PPID USER %CPU VSZ WCHAN COMMAND
  2. 10069 1 root 0.0 20544 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
  3. 10070 10069 nginx 0.0 20988 ep_pol nginx: worker process
  4. 10160 10069 root 0.0 20552 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
  5. 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*

  1. ==> /opt/apps/nginx/logs/nginx.pid <==
  2. 10160
  3. ==> /opt/apps/nginx/logs/nginx.pid.oldbin <==
  4. 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

  1. PID PPID USER %CPU VSZ WCHAN COMMAND
  2. 10069 1 root 0.0 20544 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
  3. 10160 10069 root 0.0 20552 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
  4. 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

  1. PID PPID USER %CPU VSZ WCHAN COMMAND
  2. 10160 1 root 0.0 20552 sigsus nginx: master process /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf
  3. 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

  1. Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
  2. Options:
  3. -?,-h : this help
  4. -v : show version and exit
  5. -V : show version and configure options then exit
  6. -t : test configuration and exit
  7. -T : test configuration, dump it and exit
  8. -q : suppress non-error messages during configuration testing
  9. -s signal : send signal to a master process: stop, quit, reopen, reload
  10. -p prefix : set prefix path (default: /opt/apps/nginx/)
  11. -c filename : set configuration file (default: conf/nginx.conf)
  12. -g directives : set global directives out of configuration file

常用的几个命令选项:

  1. /opt/apps/nginx/sbin/nginx -c /opt/apps/nginx/conf/nginx.conf # 指定配置文件启动
  2. /opt/apps/nginx/sbin/nginx -t # 检查配置文件
  3. /opt/apps/nginx/sbin/nginx -V # 查看版本与编译选项
  4. /opt/apps/nginx/sbin/nginx -s stop # 停止nginx
  5. /opt/apps/nginx/sbin/nginx -s reload # 重载配置
  6. /opt/apps/nginx/sbin/nginx -s reopen # 重新打开日志
  7. /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”.