nginx简介
Nginx(发音 engine x)专为性能优化而开发的开源软件,是 HTTP 及较好的反向代理软件,由俄罗斯的作者 Igor Sysoev 开发,其最知名的优点是它的稳定性和低系统资源消耗(硬件资源占用较低),以及对 HTTP 并发连接的高处理能力(单台物理服务器可支持 30000~50000 个并发请求),是一个轻量级 WEB 服务器软件。正因为如此,大量提供社交网站、新闻资讯、电子商务以及虚拟主机等服务的企业纷纷选择 Ngnix 来提供 WEB 服务。如新浪,淘宝(Tengine),京东,金山,网易,腾讯,百度文库,51cto,人人网等。
部署 Nginx软件
1.安装支持软件: Nginx 的配置及运行需要 pcre、zlib 等软件包的支持,因此应预先安装这些软件的开发包(devel),以便提供相应的库和头文件,确保 Nginx 的安装顺利完成。
[root\@master\~]# systemctl stop firewalld
[root\@master \~]# iptables -F
[root\@master \~]# setenforce 0
[root\@master \~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
2.创建运行用户、组:
Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限,增加灵活性、降低安全风险。如:创建一个名为 nginx 的用户,不建立宿主目录,也禁止登录到 shell 环境。
[root\@master \~]# useradd -M -s /sbin/nologin nginx
3.编译安装 nginx:
释放 nginx 源码包
[root\@master \~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
4.配置编译:
[root\@master nginx-1.24.0]# tar xf nginx-1.24.0.tar.gz -C /usr/src/
[root\@master nginx-1.24.0]# cd /usr/src/nginx-1.24.0/
[root\@master nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http\_stub\_status\_module --with-http\_ssl\_module --with-http\_flv\_module --with-http\_gzip\_static\_module && make && make install
- --prefix 设定 Nginx 的安装目录
- --user 和--group 指定 Nginx 运行用户和组
- --with-http_stub_status_module 启用 http_stub_status_module 模块以支持状态统计
- --with-http_ssl_module 启用 SSL 模块
- --with-http_flv_module 启用 FLV 模块,提供寻求内存使用基于时间的偏移量文件
为了使 Nginx 服务器的运行更加方便,可以为主程序 nginx 创建链接文件,以便管理员直接执行 nginx 命令就可以调用 Nginx 的主程序。
[root@master nginx-1.24.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@master nginx-1.24.0]# ll /usr/local/bin/nginx
lrwxrwxrwx 1 root root 27 1月 4 17:13 /usr/local/bin/nginx -> /usr/local/nginx/sbin/nginx
5.Nginx 的运行控制: 与 Apache 的主程序 httpd 类似,Nginx 的主程序也提供了"-t"选项用来对配置文件进行检查, 以便找出不当或错误的配置。配置文件 nginx.conf 默认位于安装目录/usr/local/nginx/conf/目 录中。若要检查位于其他位置的配置文件,可使用"-c"选项来指定路径。
[root\@master nginx-1.24.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
6.启动、停止 Nginx:
直接运行 nginx 即可启动 Nginx 服务器,这种方式将使用默认的配置文件,若要改用其他配置文件,需添加"-c 配置文件路径"选项来指定路径。需要注意的是,若服务器中已安装有 httpd 等其他 WEB 服务软件,应采取措施(修改端口,停用或卸载其他软件)避免部突。
[root\@master nginx-1.24.0]# nginx
[root\@master nginx-1.24.0]# netstat -untpl | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:\* LISTEN 597270/nginx: maste
tcp 0 0 192.168.200.10:2380 0.0.0.0:\* LISTEN 2250/etcd
通过检查 Nginx 程序的监听状态,或者在浏览器中访问此 WEB 服务(默认页面将显示
"Welcome to nginx!"),可以确认 Nginx 服务是否正常运行。
主程序 Nginx 支持标准的进程信号,通过 kill 或者 killall 命令传送
HUP 重载配置 等同于-1
QUIT 退出进程 等同于-3
KILL 杀死进程 等同于-9
[root\@master nginx-1.24.0]# killall -s QUIT nginx
[root\@master nginx-1.24.0]# netstat -untpl |grep :80
[root\@master nginx-1.24.0]#
当 Nginx 进程运行时,PID 号默认存放在/usr/local/nginx/logs/目录下的 nginx.pid 文件中,因此若改用 kill 命令,也可以根据 nginx.pid 文件中的 PID 号来进行控制。为了使 Nginx 服务的启动、停止、重载等操作更加方便,可以编写 Nginx 服务脚本,使用 chkconfig 和 systemctl 工具来进行管理,也更加符合 RHEL 系统的管理习惯。
[root@master nginx-1.24.0]# vim /usr/lib/systemd/system/nginx.service
[root@master nginx-1.24.0]# cat /usr/lib/systemd/system/nginx.service
[Unit]
# 服务描述信息,systemctl status 时会显示
Description=Nginx - High Performance Web Server
# 指定该服务在哪些系统目标之后启动
After=network.target remote-fs.target nss-lookup.target
# network.target : 网络服务启动后
# remote-fs.target : 远程文件系统挂载完成后
# nss-lookup.target : 主机名解析服务可用后
[Service]
# 服务类型:forking 表示启动脚本会调用 fork() 创建子进程
Type=forking
# 指定 PID 文件路径(必须与 nginx.conf 中 pid 指令一致)
PIDFile=/usr/local/nginx/logs/nginx.pid
# 启动前执行的命令:检查配置文件是否正确
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 启动命令:启动 Nginx
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 重载配置文件(热更新,不中断服务)
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 停止服务的命令:发送 QUIT 信号给主进程,让 Nginx 优雅退出
ExecStop=/bin/kill -s QUIT $MAINPID
# 启用私有的临时目录,提高安全性
PrivateTmp=true
[Install]
# 指定服务安装到哪个目标(多用户模式)
WantedBy=multi-user.target
1. 重新加载 systemd 配置
[root@master nginx-1.24.0]# sudo systemctl daemon-reload
2. 设置开机自启
[root@master nginx-1.24.0]# sudo systemctl enable nginx
3. 启动 Nginx
[root@master nginx-1.24.0]#sudo systemctl start nginx
4. 查看状态
[root@master nginx-1.24.0]# systemctl status nginx
● nginx.service - Nginx - High Performance Web Server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2026-01-04 17:23:03 CST; 1s ago
Process: 9357 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Process: 9354 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 9358 (nginx)
Tasks: 2
Memory: 1.4M
CGroup: /system.slice/nginx.service
├─9358 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
└─9359 nginx: worker process
1月 04 17:23:03 master systemd[1]: Starting Nginx - High Performance Web Server...
1月 04 17:23:03 master nginx[9354]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
1月 04 17:23:03 master nginx[9354]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1月 04 17:23:03 master systemd[1]: Started Nginx - High Performance Web Server.