nginx安装

2022/3/8 7:15:00

本文主要是介绍nginx安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Nginx安装

[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

Nginx安装参数详解

#-----------查看nginx的版本
[root@localhost nginx-1.14.2]# /apps/nginx/sbin/nginx -v
nginx version: nginx/1.14.2

#-----------查看nginx的版本以及nginx编译安装时所添加的参数
[root@localhost nginx-1.14.2]# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --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

#-----------------nginx -t查看当前nginx配置是否正确,在每次修改nginx之后建议都要进行nginx -t以保证在热启动nginx之前,nginx的配置是正确的
[root@localhost ~]# /apps/nginx/sbin/nginx -t        
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
#-----------------(将nginx的配置修改错误使用nginx -t显示如下)直接告诉我们错误发生在地18行
[root@localhost ~]# vi /apps/nginx/conf/nginx.conf
[root@localhost ~]# /apps/nginx/sbin/nginx -t
nginx: [emerg] directive "http" has no opening "{" in /apps/nginx/conf/nginx.conf:18
nginx: configuration file /apps/nginx/conf/nginx.conf test failed

#----------------nginx的停止
[root@localhost ~]# ps -ef|grep nginx
root       8442      1  0 22:49 ?        00:00:00 nginx: master process nginx
nginx      8443   8442  0 22:49 ?        00:00:00 nginx: worker process
root       8445   8411  0 22:49 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# nginx -s stop
[root@localhost ~]# ps -ef|grep nginx
root       8448   8411  0 22:49 pts/0    00:00:00 grep --color=auto nginx

#----------------nginx的热启动(只会重启woker进程)
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ps -ef |grep nginx
root       1162      1  0 00:00 ?        00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      1221   1162  0 00:18 ?        00:00:00 nginx: worker process
root       1223   1044  0 00:18 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ps -ef |grep nginx
root       1162      1  0 00:00 ?        00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      1225   1162  0 00:19 ?        00:00:00 nginx: worker process
root       1227   1044  0 00:19 pts/0    00:00:00 grep --color=auto nginx

 



这篇关于nginx安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程