[root@localhost src]# tar xf nginx-1.14.2.tar.gz 
[root@localhost src]# ls
nginx-1.14.2 nginx-1.14.2.tar.gz
[root@localhost nginx-1.14.2]# yum -y install gcc gcc-c++ autoconf automake make

#-----------./configure --help查看编译安装时nginx支持的参数
[root@localhost src]# cd nginx-1.14.2
[root@localhost nginx-1.14.2]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@localhost nginx-1.14.2]# ./configure --help


[root@localhost nginx-1.14.2]# ./configure --prefix=/apps/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_modul
(./configure --prefix=/apps/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)

/*
#-------------在安装过程中可能会出现一些错误例如:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
此时不要着急需要执行如下命令,然后在对configure目录进行编译
*/
[root@localhost nginx-1.14.2]# yum -y install pcre-devel
[root@localhost nginx-1.14.2]# yum -y install openssl openssl-devel


[root@localhost nginx-1.14.2]# make
[root@localhost nginx-1.14.2]# make install

#创建nginx用户以及nginx用户组并且nginx用户和nginx用户组需要具有/apps/nginx/的权限
[root@localhost apps]# useradd nginx -s /sbin/nologin -u 2000


建议搭建环境时,特别是搭建nginx集群的时候建议所有机器的nginx用户以及nginx用户组的id都是相同且权限相同,以避免因用户权限而导致有些文件不能读取;例如上传文件时,当经过某一台nginx的服务器时,其所属权限为当前nginx的权限;如果其他服务器上的nginx与当前服务器的nginx用户权限不同就会导致如不能查看该文件,或者执行,读或者写的权限,造成应用出现一些非必现的bug

[root@localhost apps]# chown nginx.nginx -R /apps/nginx/

#创建nginx自启动脚本
[root@localhost system]# vim 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=/apps/nginx/logs/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

#验证nginx自启动
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port 
LISTEN 0 100 127.0.0.1:25 *:* 
LISTEN 0 128 *:80 *:* 
LISTEN 0 128 *:22 *:* 
LISTEN 0 100 [::1]:25 [::]:* 
LISTEN 0 128 [::]:22 [::]:* 
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port 
LISTEN 0 100 127.0.0.1:25 *:* 
LISTEN 0 128 *:22 *:* 
LISTEN 0 100 [::1]:25 [::]:* 
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# ps -ef|grep nginx
root 1108 1 0 00:13 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nobody 1117 1108 0 00:14 ? 00:00:00 nginx: worker process
root 1120 1052 0 00:15 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]# systemctl reload nginx
[root@localhost ~]# ps -ef|grep nginx
root 1108 1 0 00:13 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nobody 1128 1108 0 00:15 ? 00:00:00 nginx: worker process
root 1132 1052 0 00:15 pts/0 00:00:00 grep --color=auto nginx
###对比发现worker进程的启动时间发生变化

#将nginx命令软连接到sbin目录下
[root@localhost ~]# ln -sv /apps/nginx/sbin/nginx /usr/sbin/
?.usr/sbin/nginx?.-> ?.apps/nginx/sbin/nginx?
[root@localhost ~]# nginx -s stop
[root@localhost ~]# nginx 
[root@localhost ~]# ps -ef|grep nginx
root 1139 1 0 00:19 ? 00:00:00 nginx: master process nginx
nobody 1140 1139 0 00:19 ? 00:00:00 nginx: worker process
root 1142 1052 0 00:20 pts/0 00:00:00 grep --color=auto nginx