Nginx常见的安装方式

1.Nginx概述

Nginx的安装版本分为开发版、稳定版和过期版, Nginx安装可以使用yum或源码安装,但是推荐使用源码安装,原因如下:
  1>.是yum的版本比较旧;
  2>.是编译安装可以更方便自定义相关路径;
  3>.是使用源码编译可以自定义相关功能,更方便业务的上的使用。

源码安装需要提前准备以下环境:
  标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective-C,java以及Ada等其他语言
  此外还需要Automake工具,以完成自动创建Makefile的工作
  Nginx的一些模块需要依赖第三方库,比如pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块)等。

2.yum方式安装nginx(生产环境中进来不要使用yum方式安装nginx,尽管它的安装方式很简单)

2.1.查看nginx官网提供的安装包

 2.2.如下图所示,选择相应的操作系统

 2.3.查看CentOS基于yum方式安装nginx的官网文档(http://nginx.org/en/linux_packages.html#RHEL-CentOS)

 2.4.根据官方文档基于yum方式安装nginx实战

[root@nginx-test-10e0e0e21 ~]# yum -y install yum-utils
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                                           | 3.6 kB  00:00:00     
extras                                                                                                         | 2.9 kB  00:00:00     
updates                                                                                                        | 2.9 kB  00:00:00     
(1/3): extras/7/x86_64/primary_db                                                                              | 225 kB  00:00:00     
(2/3): base/7/x86_64/primary_db                                                                                | 6.1 MB  00:00:00     
(3/3): updates/7/x86_64/primary_db                                                                             | 5.7 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-54.el7_8.noarch
--> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-54.el7_8.noarch
--> Running transaction check
---> Package libxml2-python.x86_64 0:2.9.1-6.el7.5 will be installed
--> Processing Dependency: libxml2 = 2.9.1-6.el7.5 for package: libxml2-python-2.9.1-6.el7.5.x86_64
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package libxml2.x86_64 0:2.9.1-6.el7_2.3 will be updated
---> Package libxml2.x86_64 0:2.9.1-6.el7.5 will be an update
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================
 Package                             Arch                        Version                              Repository                 Size
======================================================================================================================================
Installing:
 yum-utils                           noarch                      1.1.31-54.el7_8                      base                      122 k
Installing for dependencies:
 libxml2-python                      x86_64                      2.9.1-6.el7.5                        base                      247 k
 python-chardet                      noarch                      2.2.1-3.el7                          base                      227 k
 python-kitchen                      noarch                      1.1.1-5.el7                          base                      267 k
Updating for dependencies:
 libxml2                             x86_64                      2.9.1-6.el7.5                        base                      668 k

Transaction Summary
======================================================================================================================================
Install  1 Package  (+3 Dependent packages)
Upgrade             ( 1 Dependent package)

Total download size: 1.5 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/5): libxml2-python-2.9.1-6.el7.5.x86_64.rpm                                                                 | 247 kB  00:00:00     
(2/5): libxml2-2.9.1-6.el7.5.x86_64.rpm                                                                        | 668 kB  00:00:00     
(3/5): yum-utils-1.1.31-54.el7_8.noarch.rpm                                                                    | 122 kB  00:00:00     
(4/5): python-kitchen-1.1.1-5.el7.noarch.rpm                                                                   | 267 kB  00:00:00     
(5/5): python-chardet-2.2.1-3.el7.noarch.rpm                                                                   | 227 kB  00:00:00     
--------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                 2.2 MB/s | 1.5 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : libxml2-2.9.1-6.el7.5.x86_64                                                                                       1/6 
  Installing : libxml2-python-2.9.1-6.el7.5.x86_64                                                                                2/6 
  Installing : python-chardet-2.2.1-3.el7.noarch                                                                                  3/6 
  Installing : python-kitchen-1.1.1-5.el7.noarch                                                                                  4/6 
  Installing : yum-utils-1.1.31-54.el7_8.noarch                                                                                   5/6 
  Cleanup    : libxml2-2.9.1-6.el7_2.3.x86_64                                                                                     6/6 
  Verifying  : libxml2-python-2.9.1-6.el7.5.x86_64                                                                                1/6 
  Verifying  : python-chardet-2.2.1-3.el7.noarch                                                                                  2/6 
  Verifying  : python-kitchen-1.1.1-5.el7.noarch                                                                                  3/6 
  Verifying  : yum-utils-1.1.31-54.el7_8.noarch                                                                                   4/6 
  Verifying  : libxml2-2.9.1-6.el7.5.x86_64                                                                                       5/6 
  Verifying  : libxml2-2.9.1-6.el7_2.3.x86_64                                                                                     6/6 

Installed:
  yum-utils.noarch 0:1.1.31-54.el7_8                                                                                                  

Dependency Installed:
  libxml2-python.x86_64 0:2.9.1-6.el7.5        python-chardet.noarch 0:2.2.1-3.el7        python-kitchen.noarch 0:1.1.1-5.el7       

Dependency Updated:
  libxml2.x86_64 0:2.9.1-6.el7.5                                                                                                      

Complete!
[root@nginx-test-10e0e0e21 ~]# yum -y install yum-utils
[root@nginx-test-10e0e0e21 ~]# vim /etc/yum.repos.d/nginx.repo
[root@nginx-test-10e0e0e21 ~]# 
[root@nginx-test-10e0e0e21 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@nginx-test-10e0e0e21 ~]# yum list nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Available Packages
nginx.x86_64                                              1:1.18.0-2.el7.ngx                                              nginx-stable
[root@nginx-test-10e0e0e21 ~]# vim /etc/yum.repos.d/nginx.repo
[root@nginx-test-10e0e0e21 ~]# yum list nginx*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Available Packages
nginx.x86_64                                                          1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-debug.x86_64                                                    1:1.8.0-1.el7.ngx                                   nginx-stable
nginx-debuginfo.x86_64                                                1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-geoip.x86_64                                             1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-geoip-debuginfo.x86_64                                   1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-image-filter.x86_64                                      1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-image-filter-debuginfo.x86_64                            1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-njs.x86_64                                               1:1.18.0+0.5.1-1.el7.ngx                            nginx-stable
nginx-module-njs-debuginfo.x86_64                                     1:1.18.0+0.5.1-1.el7.ngx                            nginx-stable
nginx-module-perl.x86_64                                              1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-perl-debuginfo.x86_64                                    1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-xslt.x86_64                                              1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-module-xslt-debuginfo.x86_64                                    1:1.18.0-2.el7.ngx                                  nginx-stable
nginx-nr-agent.noarch                                                 2.0.0-12.el7.ngx                                    nginx-stable
[root@nginx-test-10e0e0e21 ~]# yum list nginx*
[root@nginx-test-10e0e0e21 ~]# yum -y install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.18.0-2.el7.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================
 Package                   Arch                       Version                                  Repository                        Size
======================================================================================================================================
Installing:
 nginx                     x86_64                     1:1.18.0-2.el7.ngx                       nginx-stable                     769 k

Transaction Summary
======================================================================================================================================
Install  1 Package

Total download size: 769 k
Installed size: 2.7 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/nginx-stable/packages/nginx-1.18.0-2.el7.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Public key for nginx-1.18.0-2.el7.ngx.x86_64.rpm is not installed
nginx-1.18.0-2.el7.ngx.x86_64.rpm                                                                              | 769 kB  00:00:48     
Retrieving key from https://nginx.org/keys/nginx_signing.key
Importing GPG key 0x7BD9BF62:
 Userid     : "nginx signing key <signing-key@nginx.com>"
 Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
 From       : https://nginx.org/keys/nginx_signing.key
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:nginx-1.18.0-2.el7.ngx.x86_64                                                                                    1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.18.0-2.el7.ngx.x86_64                                                                                    1/1 

Installed:
  nginx.x86_64 1:1.18.0-2.el7.ngx                                                                                                     

Complete!
[root@nginx-test-10e0e0e21 ~]# yum -y install nginx
[root@nginx-test-10e0e0e21 ~]# rpm -ql  nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.18.0
/usr/share/doc/nginx-1.18.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
[root@nginx-test-10e0e0e21 ~]# rpm -ql nginx
[root@nginx-test-10e0e0e21 ~]# rpm -qi  nginx
Name        : nginx
Epoch       : 1
Version     : 1.18.0
Release     : 2.el7.ngx
Architecture: x86_64
Install Date: Mon 08 Mar 2021 06:54:09 AM CST
Group       : System Environment/Daemons
Size        : 2830200
License     : 2-clause BSD-like license
Signature   : RSA/SHA1, Thu 29 Oct 2020 11:35:52 PM CST, Key ID abf5bd827bd9bf62
Source RPM  : nginx-1.18.0-2.el7.ngx.src.rpm
Build Date  : Thu 29 Oct 2020 11:25:20 PM CST
Build Host  : ip-10-1-17-226.eu-central-1.compute.internal
Relocations : (not relocatable)
Vendor      : Nginx, Inc.
URL         : http://nginx.org/
Summary     : High performance web server
Description :
nginx [engine x] is an HTTP and reverse proxy server, as well as
a mail proxy server.
[root@nginx-test-10e0e0e21 ~]# rpm -qi nginx
[root@nginx-test-10e0e0e21 ~]# cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target
[root@nginx-test-10e0e0e21 ~]# cat /usr/lib/systemd/system/nginx.service
[root@nginx-test-10e0e0e21 ~]# ss -lnt
State      Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN     0      128                                     *:111                                                 *:*                  
LISTEN     0      128                                     *:20048                                               *:*                  
LISTEN     0      128                                     *:22                                                  *:*                  
LISTEN     0      100                             127.0.0.1:25                                                  *:*                  
LISTEN     0      128                                     *:40799                                               *:*                  
LISTEN     0      64                                      *:44704                                               *:*                  
LISTEN     0      64                                      *:2049                                                *:*                  
LISTEN     0      64                                     :::33514                                              :::*                  
LISTEN     0      128                                    :::111                                                :::*                  
LISTEN     0      128                                    :::20048                                              :::*                  
LISTEN     0      128                                    :::36146                                              :::*                  
LISTEN     0      128                                    :::22                                                 :::*                  
LISTEN     0      100                                   ::1:25                                                 :::*                  
LISTEN     0      64                                     :::2049                                               :::*                  
[root@nginx-test-10e0e0e21 ~]# systemctl start nginx
[root@nginx-test-10e0e0e21 ~]# ss -lnt
State      Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN     0      128                                     *:111                                                 *:*                  
LISTEN     0      128                                     *:80                                                  *:*                  
LISTEN     0      128                                     *:20048                                               *:*                  
LISTEN     0      128                                     *:22                                                  *:*                  
LISTEN     0      100                             127.0.0.1:25                                                  *:*                  
LISTEN     0      128                                     *:40799                                               *:*                  
LISTEN     0      64                                      *:44704                                               *:*                  
LISTEN     0      64                                      *:2049                                                *:*                  
LISTEN     0      64                                     :::33514                                              :::*                  
LISTEN     0      128                                    :::111                                                :::*                  
LISTEN     0      128                                    :::20048                                              :::*                  
LISTEN     0      128                                    :::36146                                              :::*                  
LISTEN     0      128                                    :::22                                                 :::*                  
LISTEN     0      100                                   ::1:25                                                 :::*                  
LISTEN     0      64                                     :::2049                                               :::*                  
[root@nginx-test-10e0e0e21 ~]# systemctl start nginx

 3.编译方式安装nginx

3.1.下载nginx长期支持版本

博主推荐阅读:
  https://nginx.org/en/download.html

 

 3.2.如上图所示,将下载的软件上传到服务器进行后续的安装操作,如果服务器有网络的情况下,也可以直接在互联网下载

[root@nginx-test-10e0e0e22 src]# yum install wget 
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
base                                                                                                                                                | 3.6 kB  00:00:00     
extras                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                             | 2.9 kB  00:00:00     
(1/3): extras/7/x86_64/primary_db                                                                                                                   | 225 kB  00:00:00     
(2/3): updates/7/x86_64/primary_db                                                                                                                  | 5.7 MB  00:00:01     
(3/3): base/7/x86_64/primary_db                                                                                                                     | 6.1 MB  00:00:02     
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================
 Package                              Arch                                   Version                                            Repository                            Size
===========================================================================================================================================================================
Installing:
 wget                                 x86_64                                 1.14-18.el7_6.1                                    base                                 547 k

Transaction Summary
===========================================================================================================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Is this ok [y/d/N]: y
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm                                                                                                                     | 547 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-18.el7_6.1.x86_64                                                                                                                             1/1 
  Verifying  : wget-1.14-18.el7_6.1.x86_64                                                                                                                             1/1 

Installed:
  wget.x86_64 0:1.14-18.el7_6.1                                                                                                                                            

Complete!
[root@nginx-test-10e0e0e22 src]# yum install wget
[root@nginx-test-10e0e0e22 src]# wget wget https://nginx.org/download/nginx-1.16.1.tar.gz
--2021-03-08 22:11:12--  http://wget/
Resolving wget (wget)... failed: Name or service not known.
wget: unable to resolve host address ‘wget’
--2021-03-08 22:11:12--  https://nginx.org/download/nginx-1.16.1.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1032630 (1008K) [application/octet-stream]
Saving to: ‘nginx-1.16.1.tar.gz’

100%[=================================================================================================================================>] 1,032,630   35.3KB/s   in 31s    

2021-03-08 22:11:44 (32.4 KB/s) - ‘nginx-1.16.1.tar.gz’ saved [1032630/1032630]

FINISHED --2021-03-08 22:11:44--
Total wall clock time: 32s
Downloaded: 1 files, 1008K in 31s (32.4 KB/s)
[root@nginx-test-10e0e0e22 src]# wget wget https://nginx.org/download/nginx-1.16.1.tar.gz

3.3.解压nginx文件

[root@nginx-test-10e0e0e22 src]# ls
nginx-1.16.1.tar.gz
[root@nginx-test-10e0e0e22 src]# tar xf nginx-1.16.1.tar.gz 
[root@nginx-test-10e0e0e22 src]# ls
nginx-1.16.1  nginx-1.16.1.tar.gz
[root@nginx-test-10e0e0e22 src]# tar xf nginx-1.16.1.tar.gz

3.4.准备编译安装的基础环境

[root@nginx-test-10e0e0e22 src]# yum -y install vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
gcc为GNU Compiler Collection的缩写,可以编译C和C++源代码等,它是GNU开发的C和C++以及其他很多种语言的编译器(最早的时候只能编译C,后来很快进化成一个编译多种语言的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。)

  gcc 在编译C++源代码的阶段,只能编译 C++ 源文件,而不能自动和 C++ 程序使用的库链接(编译过程分为编译、链接两个阶段,注意不要和可执行文件这个概念搞混,相对可执行文件来说有三个重要的概念:编译(compile)、链接(link)、加载(load)。源程序文件被编译成目标文件,多个目标文件连同库被链接成一个最终的可执行文件,可执行文件被加载到内存中运行)。因此,通常使用 g++ 命令来完成 C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。

  gcc-c++也能编译C源代码,只不过把会把它当成C++源代码,后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。

  automake是一个从Makefile.am文件自动生成Makefile.in的工具。为了生成Makefile.in,automake还需用到perl,由于automake创建的发布完全遵循GNU标准,所以在创建中不需要perl。libtool是一款方便生成各种程序库的工具。
  
  pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx的Rewrite模块和HTTP 核心模块会使用到PCRE正则表达式语法。

  zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持。

  openssl openssl-devel:开启SSL的时候需要此模块的支持。

3.5.配置nginx模块(该步骤会检查nginx编译时需要的环境,如依赖包等,若不满足则会在配置过程中失败,并且会生成一个)

[root@nginx-test-10e0e0e22 src]# cd nginx-1.16.1
[root@nginx-test-10e0e0e22 nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx-test-10e0e0e22 nginx-1.16.1]# file configure 
configure: POSIX shell script, ASCII text executable
[root@nginx-test-10e0e0e22 nginx-1.16.1]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_mirror_module       disable ngx_http_mirror_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_grpc_module         disable ngx_http_grpc_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_random_module
                                     disable ngx_http_upstream_random_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_random_module
                                     disable ngx_stream_upstream_random_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging
[root@nginx-test-10e0e0e22 nginx-1.16.1]# ./configure --help
 ./configure --prefix=/yinzhengjie/softwares/nginx       #指定编译安装时的路径,这些编译参数在帮助信息均有记录哟~
> --user=nginx                     #指定以nginx用户运行服务
> --group=nginx                     #指定以nginx组运行服务
> --with-http_ssl_module               #支持ssl功能
> --with-http_v2_module               #支持http2功能
> --with-http_realip_module             #做反向代理时可以转发真实的IP地址
> --with-http_stub_status_module          #开启状态页
> --with-http_gzip_static_module          #开启静态压缩功能
> --with-pcre                     #支持正则
> --with-stream                    #支持TCP代理的方式
> --with-stream_ssl_module             
> --with-stream_realip_module

[root@nginx-test-10e0e0e22 nginx-1.16.1]# ./configure --prefix=/yanhuihuang/softwares/nginx  --user=nginx  --group=nginx  --with-http_ssl_module  --with-http_v2_module  --with-http_realip_module  --with-http_stub_status_module  --with-http_gzip_static_module  --with-pcre  --with-stream  --with-stream_ssl_module   --with-stream_realip_module  
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/yanhuihuang/softwares/nginx"
  nginx binary file: "/yanhuihuang/softwares/nginx/sbin/nginx"
  nginx modules path: "/yanhuihuang/softwares/nginx/modules"
  nginx configuration prefix: "/yanhuihuang/softwares/nginx/conf"
  nginx configuration file: "/yanhuihuang/softwares/nginx/conf/nginx.conf"
  nginx pid file: "/yanhuihuang/softwares/nginx/logs/nginx.pid"
  nginx error log file: "/yanhuihuang/softwares/nginx/logs/error.log"
  nginx http access log file: "/yanhuihuang/softwares/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@nginx-test-10e0e0e22 nginx-1.16.1]# echo $?
0
[root@nginx-test-10e0e0e22 nginx-1.16.1]# ll Makefile
-rw-r--r--. 1 root root 424 Mar  8 22:52 Makefile
[root@nginx-test-10e0e0e22 nginx-1.16.1]# cat Makefile

default:    build

clean:
    rm -rf Makefile objs

build:
    $(MAKE) -f objs/Makefile

install:
    $(MAKE) -f objs/Makefile install

modules:
    $(MAKE) -f objs/Makefile modules

upgrade:
    /yanhuihuang/softwares/nginx/sbin/nginx -t

    kill -USR2 `cat /yanhuihuang/softwares/nginx/logs/nginx.pid`
    sleep 1
    test -f /yanhuihuang/softwares/nginx/logs/nginx.pid.oldbin

    kill -QUIT `cat /yanhuihuang/softwares/nginx/logs/nginx.pid.oldbin`

3.6.编译(该步骤会根据Makefile文件生成相应的模块)

[root@nginx-test-10e0e0e22 nginx-1.16.1]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 142
Model name:            Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Stepping:              10
CPU MHz:               1991.999
BogoMIPS:              3983.99
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              8192K
NUMA node0 CPU(s):     0
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat spec_ctrl intel_stibp flush_l1d arch_capabilities
[root@nginx-test-10e0e0e22 nginx-1.16.1]# lscpu
[root@nginx-test-10e0e0e22 nginx-1.16.1]# make -j 1
[root@nginx-test-10e0e0e22 nginx-1.16.1]# echo $?
0

3.7.安装(该步骤会创建目录,并将生成的模块和文件复制到相应的目录)

[root@nginx-test-10e0e0e22 nginx-1.16.1]# make install 
make -f objs/Makefile install
make[1]: Entering directory `/usr/local/src/nginx-1.16.1'
test -d '/yanhuihuang/softwares/nginx' || mkdir -p '/yanhuihuang/softwares/nginx'
test -d '/yanhuihuang/softwares/nginx/sbin' 
    || mkdir -p '/yanhuihuang/softwares/nginx/sbin'
test ! -f '/yanhuihuang/softwares/nginx/sbin/nginx' 
    || mv '/yanhuihuang/softwares/nginx/sbin/nginx' 
        '/yanhuihuang/softwares/nginx/sbin/nginx.old'
cp objs/nginx '/yanhuihuang/softwares/nginx/sbin/nginx'
test -d '/yanhuihuang/softwares/nginx/conf' 
    || mkdir -p '/yanhuihuang/softwares/nginx/conf'
cp conf/koi-win '/yanhuihuang/softwares/nginx/conf'
cp conf/koi-utf '/yanhuihuang/softwares/nginx/conf'
cp conf/win-utf '/yanhuihuang/softwares/nginx/conf'
test -f '/yanhuihuang/softwares/nginx/conf/mime.types' 
    || cp conf/mime.types '/yanhuihuang/softwares/nginx/conf'
cp conf/mime.types '/yanhuihuang/softwares/nginx/conf/mime.types.default'
test -f '/yanhuihuang/softwares/nginx/conf/fastcgi_params' 
    || cp conf/fastcgi_params '/yanhuihuang/softwares/nginx/conf'
cp conf/fastcgi_params 
    '/yanhuihuang/softwares/nginx/conf/fastcgi_params.default'
test -f '/yanhuihuang/softwares/nginx/conf/fastcgi.conf' 
    || cp conf/fastcgi.conf '/yanhuihuang/softwares/nginx/conf'
cp conf/fastcgi.conf '/yanhuihuang/softwares/nginx/conf/fastcgi.conf.default'
test -f '/yanhuihuang/softwares/nginx/conf/uwsgi_params' 
    || cp conf/uwsgi_params '/yanhuihuang/softwares/nginx/conf'
cp conf/uwsgi_params 
    '/yanhuihuang/softwares/nginx/conf/uwsgi_params.default'
test -f '/yanhuihuang/softwares/nginx/conf/scgi_params' 
    || cp conf/scgi_params '/yanhuihuang/softwares/nginx/conf'
cp conf/scgi_params 
    '/yanhuihuang/softwares/nginx/conf/scgi_params.default'
test -f '/yanhuihuang/softwares/nginx/conf/nginx.conf' 
    || cp conf/nginx.conf '/yanhuihuang/softwares/nginx/conf/nginx.conf'
cp conf/nginx.conf '/yanhuihuang/softwares/nginx/conf/nginx.conf.default'
test -d '/yanhuihuang/softwares/nginx/logs' 
    || mkdir -p '/yanhuihuang/softwares/nginx/logs'
test -d '/yanhuihuang/softwares/nginx/logs' 
    || mkdir -p '/yanhuihuang/softwares/nginx/logs'
test -d '/yanhuihuang/softwares/nginx/html' 
    || cp -R html '/yanhuihuang/softwares/nginx'
test -d '/yanhuihuang/softwares/nginx/logs' 
    || mkdir -p '/yanhuihuang/softwares/nginx/logs'
make[1]: Leaving directory `/usr/local/src/nginx-1.16.1'
[root@nginx-test-10e0e0e22 nginx-1.16.1]# echo $?
0
[root@nginx-test-10e0e0e22 nginx-1.16.1]# make install
[root@nginx-test-10e0e0e22 ~]#  ll /yanhuihuang/softwares/nginx/
total 4
drwxr-xr-x. 2 root root 4096 Mar  8 23:28 conf   #保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
drwxr-xr-x. 2 root root   40 Mar  8 23:28 html   #保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面
drwxr-xr-x. 2 root root    6 Mar  8 23:28 logs   #用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
drwxr-xr-x. 2 root root   19 Mar  8 23:28 sbin   #保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

3.8.创建nginx并授权

[root@nginx-test-10e0e0e22 ~]# useradd nginx -s /sbin/nologin -u 2000
[root@nginx-test-10e0e0e22 ~]# chown nginx:nginx -R /yanhuihuang/softwares/nginx/

3.9.启动nginx服务

[root@nginx-test-10e0e0e22 ~]# ss -lnt
State       Recv-Q Send-Q                                                                  Local Address:Port                                                                                 Peer Address:Port              
LISTEN      0      128                                                                                 *:42344                                                                                           *:*                  
LISTEN      0      64                                                                                  *:42411                                                                                           *:*                  
LISTEN      0      128                                                                                 *:111                                                                                             *:*                  
LISTEN      0      128                                                                                 *:20048                                                                                           *:*                  
LISTEN      0      128                                                                                 *:22                                                                                              *:*                  
LISTEN      0      100                                                                         127.0.0.1:25                                                                                              *:*                  
LISTEN      0      64                                                                                  *:2049                                                                                            *:*                  
LISTEN      0      128                                                                                :::37356                                                                                          :::*                  
LISTEN      0      128                                                                                :::111                                                                                            :::*                  
LISTEN      0      128                                                                                :::20048                                                                                          :::*                  
LISTEN      0      128                                                                                :::22                                                                                             :::*                  
LISTEN      0      100                                                                               ::1:25                                                                                             :::*                  
LISTEN      0      64                                                                                 :::44187                                                                                          :::*                  
LISTEN      0      64                                                                                 :::2049                                                                                           :::*                
[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx -t
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful
[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx 
[root@nginx-test-10e0e0e22 ~]# ss -lntp 
State       Recv-Q Send-Q                                                                  Local Address:Port                                                                                 Peer Address:Port              
LISTEN      0      128                                                                                 *:42344                                                                                           *:*                   users:(("rpc.statd",pid=7107,fd=9))
LISTEN      0      64                                                                                  *:42411                                                                                           *:*                  
LISTEN      0      128                                                                                 *:111                                                                                             *:*                   users:(("rpcbind",pid=6177,fd=8))
LISTEN      0      128                                                                                 *:80                                                                                              *:*                   users:(("nginx",pid=13609,fd=6),("nginx",pid=13608,fd=6))
LISTEN      0      128                                                                                 *:20048                                                                                           *:*                   users:(("rpc.mountd",pid=8266,fd=8))
LISTEN      0      128                                                                                 *:22                                                                                              *:*                   users:(("sshd",pid=7101,fd=3))
LISTEN      0      100                                                                         127.0.0.1:25                                                                                              *:*                   users:(("master",pid=7291,fd=13))
LISTEN      0      64                                                                                  *:2049                                                                                            *:*                  
LISTEN      0      128                                                                                :::37356                                                                                          :::*                   users:(("rpc.statd",pid=7107,fd=11))
LISTEN      0      128                                                                                :::111                                                                                            :::*                   users:(("rpcbind",pid=6177,fd=11))
LISTEN      0      128                                                                                :::20048                                                                                          :::*                   users:(("rpc.mountd",pid=8266,fd=10))
LISTEN      0      128                                                                                :::22                                                                                             :::*                   users:(("sshd",pid=7101,fd=4))
LISTEN      0      100                                                                               ::1:25                                                                                             :::*                   users:(("master",pid=7291,fd=14))
LISTEN      0      64                                                                                 :::44187                                                                                          :::*                  
LISTEN      0      64                                                                                 :::2049                                                                                           :::*                  
start nginx

 3.10.CentOS 7.6编写nginx启动脚本

[root@nginx-test-10e0e0e22 ~]# vim /usr/lib/systemd/system/nginx.service 
[root@nginx-test-10e0e0e22 ~]# 
[root@nginx-test-10e0e0e22 ~]# 
[root@nginx-test-10e0e0e22 ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=Yanhuihuang's nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/yanhuihuang/softwares/nginx/logs/nginx.pid
ExecStart=/yanhuihuang/softwares/nginx/sbin/nginx -c /yanhuihuang/softwares/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
[root@nginx-test-10e0e0e22 ~]# vim /usr/lib/systemd/system/nginx.service
[root@nginx-test-10e0e0e22 ~]# systemctl daemon-reload
[root@nginx-test-10e0e0e22 ~]# 
[root@nginx-test-10e0e0e22 ~]# 
[root@nginx-test-10e0e0e22 ~]# systemctl start  nginx.service
[root@nginx-test-10e0e0e22 ~]# systemctl status  nginx.service
● nginx.service - Yanhuihuang's nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-03-09 03:07:04 CST; 32s ago
     Docs: http://nginx.org/en/docs/
 Main PID: 15562 (nginx)
   CGroup: /system.slice/nginx.service
           ├─15562 nginx: master process /yanhuihuang/softwares/nginx/sbin/nginx -c /yanhuihuang/softwares/nginx/conf/nginx.conf
           └─15563 nginx: worker process

Mar 09 03:07:04 nginx-test-10e0e0e22 systemd[1]: Starting Yanhuihuang's nginx - high performance web server...
Mar 09 03:07:04 nginx-test-10e0e0e22 systemd[1]: Can't open PID file /yanhuihuang/softwares/nginx/logs/nginx.pid (yet?) after start: No such file or directory
Mar 09 03:07:04 nginx-test-10e0e0e22 systemd[1]: Started Yanhuihuang's nginx - high performance web server.

4.nginx命令常用参数介绍

4.1.查看帮助信息

[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx -h 
nginx version: nginx/1.16.1
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: /yanhuihuang/softwares/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

4.2."-v"参数使用案例

[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx -v 
nginx version: nginx/1.16.1

4.3."-V"参数使用案例

[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx -V 
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/yanhuihuang/softwares/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

4.4."-t"参数使用案例

[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx -t
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful

4.5."-T"参数使用案例

[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx -T    #检查mnginx的配置文件语法是否正确并列出生效的文件内容
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful
# configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

# configuration file /yanhuihuang/softwares/nginx/conf/mime.types:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}
/yanhuihuang/softwares/nginx/sbin/nginx -T

4.6."-c"参数使用案例

[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx  -c /yanhuihuang/softwares/nginx/conf/nginx.conf
[root@nginx-test-10e0e0e22 ~]# 
原文地址:https://www.cnblogs.com/huihuangyan/p/14501280.html