1. Nginx如何选择安装方式
对于LNMP环境来说,中小企业可以选择yum或者编译安装,大型企业可以选择综合安装方式。对于使用yum安装Nginx也有两种方式,一种是采用epel源进行yum,缺点是其提供的Nginx软件版本往往要落后一些;另一种是直接采用Nginx官方源进行yum,特点是Nginx版本更新。这里使用配置nginx官方源yum的安装过程。
2. 编译安装Nginx
2.1 安装Nginx所需的PCRE库
PCRE(Perl Compatible Regular Expressions)意为“perl兼容正则表达式”,官方站点为http://www.pcre.org/,安装PCRE库是为了使Nginx支持具备URI重写功能的Rewrite模块,如果不安装PCRE库,则Nginx无法使用Rewrite模块功能,Nginx的Rewrite模块功能几乎是企业应用必须。安装PCRE库的过程如下。
1)当前Linux系统环境情况:
[root@web01 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@web01 ~]# uname -r
3.10.0-957.el7.x86_64
[root@web01 ~]# uname -m
x86_64
2)操作命令。采用yum安装方式安装PCRE(推荐),命令如下。
[root@web01 ~]# yum install pcre pcre-devel -y
yum安装操作后检查安装结果:
[root@web01 ~]# rpm -qa pcre pcre-devel
pcre-devel-8.32-17.el7.x86_64
pcre-8.32-17.el7.x86_64
2.2 安装Nginx
1)检查及安装Nginx基础依赖包pcre-devel、openssl-devel。
要想正确安装Nginx,首先必须安装好pcre-devel、openssl-devel包,因此首先要检查这些Nginx基础依赖包是否安装,操作命令如下:
[root@web01 ~]# rpm -qa openssl-devel openssl
openssl-1.0.2k-16.el7.x86_64 ---这里没有openssl “devel”字符串的包
2)安装openssl-devel。
Nginx在使用HTTPS功能的时候要用到此模块,如果不安装openssl相关包,安装Nginx的过程中也会报错。安装openssl-devel的命令及检查命令如下:
[root@web01 ~]# yum install openssl openssl-devel -y
[root@web01 ~]# rpm -qa openssl openssl-devel
openssl-1.0.2k-19.el7.x86_64
openssl-devel-1.0.2k-19.el7.x86_64
3)开始安装Nginx,操作命令及过程如下:
[root@web01 ~]# mkdir -p /home/oldboy/tools
[root@web01 ~]# cd /home/oldboy/tools/
[root@web01 tools]# wget -q http://nginx.org/download/nginx-1.16.0.tar.gz ---下载软件包
[root@web01 tools]# ls -lh
总用量 1012K
-rw-r--r-- 1 root root 1009K 4月 23 2019 nginx-1.16.0.tar.gz
[root@web01 tools]# useradd nginx -u 1111 -s /sbin/nologin -M ---添加Nginx用户,服务于Nginx
[root@web01 tools]# tar xf nginx-1.16.0.tar.gz ---解压软件包
[root@web01 tools]# cd nginx-1.16.0/
[root@web01 nginx-1.16.0]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module
---指定用户和用户组,然后指定路径,和功能模块对Nginx进行配置,为编译做准备
[root@web01 nginx-1.16.0]# echo $?
0
[root@web01 nginx-1.16.0]# make ---编译Nginx
[root@web01 nginx-1.16.0]# echo $?
0
[root@web01 nginx-1.16.0]# make install ---安装Nginx
[root@web01 nginx-1.16.0]# ln -s /application/nginx-1.16.0 /application/nginx
[root@web01 nginx-1.16.0]# ls /application/nginx
conf html logs sbin
检查链接及目录状态:
[root@web01 tools]# ll /application/
总用量 0
lrwxrwxrwx 1 root root 25 3月 22 01:58 nginx -> /application/nginx-1.16.0
drwxr-xr-x 6 root root 54 3月 22 01:57 nginx-1.16.0
到此,Nginx的安装工作就完成了。
编译Nginx软件时,可以使用./configure --help查看相关参数帮助,下面是本次编译时指定的参数及简单说明:
--prefix=PATH ---设置安装路径
--user=USER ---进程用户权限
--group=GROUP ---进程用户组权限
--with-http_stub_status_module ---激活状态信息
--with-http_ssl_module ---激活ssl功能
提示:Nginx的大部分模块功能都会默认编译到软件中,不需要单独指定编译参数。
2.3 启动并检查安装结果
安装完Nginx后,并不能直接对外提供服务,需要先启动Nginx服务才行,操作如下:
1)启动前检查配置文件语法:
[root@web01 tools]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
提示:在启动服务前检查语法,可防止因配置错误导致网站重启而重新加载配置对用户造成影响。
2)启动Nginx服务,命令如下:
[root@web01 tools]# /application/nginx/sbin/nginx
注意:为了减少输入Nginx启动命令的路径,可以将路径加入到PATH环境变量里,步骤如下:
[root@web01 tools]# /application/nginx/sbin/nginx
[root@web01 tools]# echo 'export PATH="/application/nginx/sbin:$PATH"' >> /etc/profile
[root@web01 tools]# tail -1 /etc/profile
export PATH="/application/nginx/sbin:$PATH"
[root@web01 tools]# source /etc/profile
[root@web01 tools]# echo $PATH
/application/nginx/sbin:/application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@web01 tools]# nginx -t ---去掉路径执行成功
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
3)查看Nginx服务对应的端口是否成功,命令如下:
[root@web01 tools]# netstat -lntup| grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9584/nginx: master
4)检查Nginx启动的实际效果。在Windows系统下检测。

在Linux下也可以使用wget或curl命令检测。
[root@web01 tools]# wget 127.0.0.1
--2020-03-22 02:32:45-- http://127.0.0.1/
正在连接 127.0.0.1:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:612 [text/html]
正在保存至: “index.html”
100%[==============================================>] 612 --.-K/s 用时 0s
2020-03-22 02:32:45 (157 MB/s) - 已保存 “index.html” [612/612])
[root@web01 tools]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
提示:192.168.9.7为服务器的IP地址,也可以通过netstat -lnt|grep 80、lsof -i:80检查Nginx服务端口(默认为80)来判断Nginx是否已启动,或通过ps -ef|grep nginx检查Nginx服务进程来判断,当然最稳妥的方法还是通过URL地址来检查。在后面的监控服务中还会提到这个问题。
Nginx启动的疑难杂症汇总
问题1:启动Nginx报错nginx:[emerg] getpwnam("nginx")failed。
解答:这是因为没有对应的Nginx服务用户所致,执行useradd nginx -u 1111 -s /sbin/nologin -M创建Nginx用户即可。
问题2:如何查看Nginx编译时的参数?
解答:可采用如下命令查看:
[root@web01 tools]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
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: --user=nginx --group=nginx --prefix=/application/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module
问题3:浏览器、wget或者curl等软件访问不了Nginx页面。
解答:此类问题的排查思路又分在Nginx服务端和客户端排查,服务端排查过程如下。
首先关闭selinux:
[root@web01 tools]# setenforce 0 ---临时关闭selinux的方法
setenforce: SELinux is disabled ---如果之前就已经是关闭状态,则显示disabled
[root@web01 tools]# grep SELINUX=disabled /etc/selinux/config ---修改配置文件永久关闭
# SELINUX=disabled
SELINUX=disabled
然后关闭防火墙,命令如下:
[root@web01 tools]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) ---已经处于dead状态,并且开机状态是disabled
Docs: man:firewalld(1)
再到服务器本地检查端口、进程和URL。在此过程中,又分4步。
一是确认端口80是否存在,命令如下:
[root@web01 tools]# netstat -lnt|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
二是查看是否有HTTP进程存在,命令如下:
[root@web01 tools]# ps -ef | grep nginx | grep -v grep
root 9584 1 0 02:24 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 9585 9584 0 02:24 ? 00:00:00 nginx: worker process
三是在服务器本地进行wget http://192.168.9.7测试。
这里把服务器本地当作客户端来模拟用户检查http服务,此条的检查比步骤一和二的更准确,同时可以排除防火墙的干扰,如果可正常访问,说明Nginx服务没问题,那么就是网络或防火墙等的问题了。
四是查看Nginx的错误日志,确定是否有特殊异常。
如果以上四步都检查了,问题基本上就迎刃而解了,如果是步骤一和二异常,则可以查看Web错误日志,获取信息。例如,刚才重现的错误日志里的提示为:
[root@web01 tools]# cat /application/nginx/logs/error.log
2020/03/22 02:34:49 [error] 9585#0: *2 open() "/application/nginx-1.16.0//html/favicon.ico" failed (2: No such file or directory), client: 192.168.9.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.9.7", referrer: "http://192.168.9.7/"
下面是客户端排查的思路。
第一步,在客户端上telnet服务器端IP,命令如下:
ping 192.168.9.7 ---排除物理线路问题的影响
第二步,在客户端上telnet服务器端IP、端口,如下:
telnet 192.168.9.7 80 ---排除防火墙的影响
第三步,在客户端使用wget命令检测。如下:
wget 192.168.9.7(curl -I 192.168.9.7) ---模拟用户访问,排除HTTP服务自身问题,根据输出再排错。
2.4 部署一个Web站点
Nginx的默认站点目录是Nginx安装目录/application/nginx/下的html目录,这可以从Nginx主配置文件/application/nginx/conf/nginx.conf中查到,内容如下:
[root@web01 tools]# sed -n '43,46p' /application/nginx/conf/nginx.conf
location / {
root html; ---默认站点目录html,全路径是/application/nginx/html
index index.html index.htm; ---这是站点的默认首页文件
}
此时,如果要部署网站业务数据,只需把开发好的程序全部放置到/application/nginx/html目录下即可。
这里进入到/application/nginx/html下,删除掉Nginx默认的首页index.html(原始内容为Welcome to Nginx等),然后新建立一个index.html,加入如下网页内容,然后保存:
<html>
<head><title>My Nginx server blog.</title></head>
<body>Hi,I am Nginx.My blog address is <a href="www.baidu.com">http://www.baidu.com</a></body>
</html>
操作过程如下:
[root@web01 nginx]# cd /application/nginx/html/
[root@web01 html]# mv index.html index.html.ori
[root@web01 html]# vi index.html
<html>
<head><title>My Nginx server blog.</title></head>
<body>Hi,I am Nginx.My blog address is <a href="www.baidu.com">http://www.baidu.com</a></body>
</html>
此时,打开浏览器输入http://192.168.9.7/,然后回车,应该可以看到如下图所示的内容。

3. yum安装Nginx完整步骤
1)配置Nginx官方yum源:
[root@web02 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=Nginx repo by zheng
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2)yum安装Nginx:
[root@web02 ~]# rpm -qa nginx ---提前检查,发现没有
[root@web02 ~]# yum install nginx -y
[root@web02 ~]# rpm -qa nginx
nginx-1.16.1-1.el7.ngx.x86_64
3)启动并检查Nginx状态:
[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web02 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2020-03-25 11:16:46 CST; 13s ago
Docs: http://nginx.org/en/docs/
Main PID: 6888 (nginx)
CGroup: /system.slice/nginx.service
├─6888 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─6889 nginx: worker process
3月 25 11:16:46 web02 systemd[1]: Starting nginx - high performance web server...
3月 25 11:16:46 web02 systemd[1]: PID file /var/run/nginx.pid not readable (yet?)...rt.
3月 25 11:16:46 web02 systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@web02 ~]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6888/nginx: master
4)测试访问是否成功:
[root@web02 ~]# wget 127.0.0.1
--2020-03-25 11:17:48-- http://127.0.0.1/
正在连接 127.0.0.1:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:612 [text/html]
正在保存至: “index.html”
100%[==============================================>] 612 --.-K/s 用时 0s
2020-03-25 11:17:48 (157 MB/s) - 已保存 “index.html” [612/612])
网友评论