环境:
[root@nginx local]# uname -a
Linux nginx 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@nginx local]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
过程:
第一步:安装Nginx所需pcre库,官方网站(www.pcre.org)。安装pcre库是为了使Nginx支持具备URL重写功能的Rewite模块,如不安装则Nginx无法使用此模块功能(典型功能为伪静态)。
命令:yum -y install pcre-devel
步骤:
[root@nginx ~]# yum -y install pcre-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
--------部分忽略--------
Complete!
第二步:安装编译需要的组件
命令:yum -y install zlib-devel openssl-devel
步骤:
[root@nginx ~]# yum -y install zlib-devel openssl-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
--------部分忽略--------
Complete!
第三步:创建软件配套专用的用户(禁止登录及不创建家目录)
命令:useradd www -s /sbin/nologin -M
步骤:
[root@nginx ~]# useradd www -s /sbin/nologin -M
第四步:进入/usr/src目录下载并解压软件包
命令:cd /usr/src/ && wget http://nginx.org/download/nginx-1.12.2.tar.gz && tar -zxf nginx-1.12.2.tar.gz
步骤:
[root@nginx ~]# cd /usr/src/ && wget http://nginx.org/download/nginx-1.12.2.tar.gz && tar -zxf nginx-1.12.2.tar.gz
--2019-04-09 22:34:31-- http://nginx.org/download/nginx-1.12.2.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
--------部分忽略--------
Saving to: ‘nginx-1.12.2.tar.gz’
100%[==============================================================================================================================>] 981,687 1023KB/s in 0.9s
2019-04-09 22:34:34 (1023 KB/s) - ‘nginx-1.12.2.tar.gz’ saved [981687/981687]
第五步:进入解压的软件目录并编译配置文件
命令:cd nginx-1.12.2 && ./configure --user=www --group=www --prefix=/usr/local/nginx-1.12.2 --with-mail_ssl_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module
步骤:
[root@nginx src]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx-1.12.2 --with-mail_ssl_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module
checking for OS
+ Linux 3.10.0-957.el7.x86_64 x86_64
--------部分忽略--------
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
第六步:编译及安装软件
命令:make && make install
步骤:
[root@nginx nginx-1.12.2]# make && make install
make -f objs/Makefile
make[1]: Entering directory `/usr/src/nginx-1.12.2'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
--------部分忽略--------
test -d '/usr/local/nginx-1.12.2/logs' \
|| mkdir -p '/usr/local/nginx-1.12.2/logs'
make[1]: Leaving directory `/usr/src/nginx-1.12.2'
第七步:创建软链接
命令: ln -s /usr/local/nginx-1.12.2/ /usr/local/nginx
步骤:
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx-1.12.2/ /usr/local/nginx
[root@nginx nginx-1.12.2]# cd ..
[root@nginx src]# ls
debug kernels nginx-1.12.2 nginx-1.12.2.tar.gz
[root@nginx src]# cd ..
[root@nginx usr]# ls
bin etc games include lib lib64 libexec local sbin share src tmp
[root@nginx usr]# cd local/
[root@nginx local]# ls
bin etc games include lib lib64 libexec nginx nginx-1.12.2 sbin share src
第八步:检查Nginx配置文件语法
命令:/usr/local/nginx/sbin/nginx -t
步骤:
[root@nginx local]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
第九步:把Nginx服务添加进启动自定义文件内并启动Nginx软件
命令:echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local && /usr/local/nginx/sbin/nginx
步骤:
[root@nginx local]# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local && /usr/local/nginx/sbin/nginx
第十步:检查端口情况判断Nginx软件是否运行正常
命令:ps -ef | grep nginx ; ss -lnat|grep 80 ; crul 10.0.0.180(此处是主机IP地址)
步骤:
[root@nginx local]# ps -ef | grep nginx
root 12984 1 0 22:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www 12985 12984 0 22:41 ? 00:00:00 nginx: worker process
root 13002 10292 0 22:41 pts/0 00:00:00 grep --color=auto nginx
[root@nginx local]# ss -lnat|grep 80
LISTEN 0 128 *:80 *:*
ESTAB 0 0 10.0.0.180:22 10.0.0.6:13842
[root@nginx local]# curl 10.0.0.180
<!DOCTYPE html>
<html>
<head>
--------部分忽略--------
</body>
</html>
第十一步:检查selinux和防火墙是否关闭
命令:getenforce ; systemctl status firewalld.service
配置:
[root@nginx local]# getenforce
Permissive
[root@nginx local]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
--------部分忽略--------
Apr 09 22:44:29 nginx systemd[1]: Stopped firewalld - dynamic firewall daemon.
如果没有关闭,将调整相关参数进行关闭