当前位置: 首页 > 编程文章 >

CentOS 7下Nginx自启动服务配置指南

CentOS 7下Nginx自启动服务配置指南

作者:陆知时间:2026-09-27 09:26:46浏览:229

在CentOS 7环境中,Nginx服务器重启后需手动配置自启动以确保服务可用性。本文介绍两种场景:通过yum安装的Nginx可直接利用rpm包自带的nginx.service文件启用;而编译安装的Nginx需根据实际路径(如PID、二进制位置)自定义编写nginx.service文件,并通过systemctl enable命令注册为系统服务。

本文目录展开

CentOS 7下Nginx自启动服务配置指南

Nginx作为一款广受欢迎的轻量级Web服务器软件,具备开源、精简且易用的特点,深受互联网企业及IT运维团队青睐。在生产环境中,若采用编译方式安装Nginx,通常需手动配置开机自启动服务,以防止服务器异常宕机后无法自动恢复。下文将基于CentOS 7系统,详细介绍Nginx自启动服务的配置方法。

一、通过Yum安装方式的Nginx自启动配置

当前运行环境如下所示:


[root@node142 ~]# more /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

首先检查系统中是否已安装Nginx的RPM包。


[root@node142 ~]# rpm -qa|grep nginx
nginx-mod-http-geoip-1.12.2-2.el7.x86_64
nginx-1.12.2-2.el7.x86_64
nginx-filesystem-1.12.2-2.el7.noarch
nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
nginx-mod-stream-1.12.2-2.el7.x86_64
nginx-mod-http-perl-1.12.2-2.el7.x86_64
nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
nginx-all-modules-1.12.2-2.el7.noarch
nginx-mod-mail-1.12.2-2.el7.x86_64

接着查看是否存在相关的系统服务,输出结果显示存在nginx.service。


[root@node142 ~]# systemctl list-unit-files |grep nginx
nginx.service               disabled

将该服务设置为开机自动启动。


[root@node142 ~]# systemctl enable nginx.service

查看nginx.service的具体配置文件内容。


[root@node142 ~]# more /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

该配置文件的内容与官方提供的标准配置完全一致。

参考官方文档:https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

二、编译安装后的自启动配置

由于采用的是编译安装方式,因此需要自行创建并配置自启动脚本。

具体操作可参考上述链接或前述nginx.service文件的模板内容。

将配置好的文件保存至 /lib/systemd/system/nginx.service。

具体示例如下:


# more /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

# ps -ef|grep nginx
root  10092 10014 0 16:23 pts/0  00:00:00 grep --color=auto nginx
root  20791  1 0 Mar20 ?    00:00:00 nginx: master process ./sbin/nginx -c /u01/app/nginx/nginx.conf
nobody 20792 20791 0 Mar20 ?    00:00:44 nginx: worker process
nobody 20793 20791 0 Mar20 ?    00:00:42 nginx: worker process
nobody 20794 20791 0 Mar20 ?    00:00:50 nginx: worker process
nobody 20795 20791 0 Mar20 ?    00:00:44 nginx: worker process
nobody 20796 20791 0 Mar20 ?    00:00:43 nginx: worker process
nobody 20797 20791 0 Mar20 ?    00:00:43 nginx: worker process
nobody 20798 20791 0 Mar20 ?    00:00:37 nginx: worker process
nobody 20799 20791 0 Mar20 ?    00:00:48 nginx: worker process
nobody 20800 20791 0 Mar20 ?    00:00:04 nginx: cache manager process
# 

此处无相应的RPM包,查询结果如下,表明为编译安装版本。


# rpm -qa|grep nginx

同时也未添加相应的自启动服务。


# systemctl list-unit-files |grep nginx

确认Nginx的版本信息。


# /u01/app/nginx/sbin/nginx -v
nginx version: nginx/1.8.1

获取Nginx的编译模块参数,记录PID文件路径及二进制程序位置,以便后续修改启动文件。


# /u01/app/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20260623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips 26 Jan 2026
TLS SNI support enabled
configure arguments: --prefix=/u01/app/nginx --sbin-path=/u01/app/nginx/sbin/nginx 
--conf-path=/u01/app/nginx/nginx.conf --error-log-path=/u01/app/nginx/log/error.log 
--http-log-path=/u01/app/nginx/log/access.log --pid-path=/u01/app/nginx/nginx.pid 
--lock-path=/u01/app/nginx/nginx.lock --http-client-body-temp-path=/u01/app/nginx/client_temp 
--http-proxy-temp-path=/u01/app/nginx/proxy_temp 
--http-fastcgi-temp-path=/u01/app/nginx/fastcgi_temp 
--http-uwsgi-temp-path=/u01/app/nginx/uwsgi_temp --http-scgi-temp-path=/u01/app/nginx/scgi_temp
--user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module 
--with-http_addition_module --with-http_sub_module --with-http_dav_module
--with-http_flv_module --with-http_mp4_module --with-http_gunzip_module 
--with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module 
--with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module
--with-file-aio --with-http_spdy_module --with-ipv6

接下来生成一个新的nginx.service文件。


# vim /lib/systemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/u01/app/nginx/nginx.pid
ExecStartPre=/u01/app/nginx/sbin/nginx -t
ExecStart=/u01/app/nginx/sbin/nginx 
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

首先手工停止正在运行的Nginx服务。


# /u01/app/nginx/sbin/nginx -s stop

配置服务为自启动状态。


# systemctl enable nginx.service

使用systemctl工具启动Nginx服务以验证配置。


# systemctl start nginx.service

# systemctl status nginx.service
● nginx.service - The NGINX HTTP and reverse proxy server
 Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
 Active: active (running) since Thu 2026-03-29 16:37:47 CST; 6s ago
 Process: 10588 ExecStart=/u01/app/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Process: 10586 ExecStartPre=/u01/app/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 10590 (nginx)
 CGroup: /system.slice/nginx.service
     ├─10590 nginx: master process /u01/app/nginx/sbin/nginx
     ├─10591 nginx: worker process # Author : Leshami
     ├─10592 nginx: worker process # Blog : https://blog.csdn.net/leshami
     ├─10593 nginx: worker process
     ├─10594 nginx: worker process
     ├─10595 nginx: worker process
     ├─10596 nginx: worker process
     ├─10597 nginx: worker process
     ├─10598 nginx: worker process
     ├─10599 nginx: cache manager process
     └─10600 nginx: cache loader process

Mar 29 16:37:47 ydq-std systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Mar 29 16:37:47 ydq-std nginx[10586]: nginx: the configuration file /u01/app/nginx/nginx.conf syntax is ok
Mar 29 16:37:47 ydq-std nginx[10586]: nginx: configuration file /u01/app/nginx/nginx.conf test is successful
Mar 29 16:37:47 ydq-std systemd[1]: Started The NGINX HTTP and reverse proxy server.

以上即为本文的全部内容,希望对各位的学习有所帮助,也欢迎大家继续支持本站。

上一篇
MySQL 5.6 修改密码后执行语句报错 ERROR 1820 的解决方法

在 MySQL 5.6 中,用户成功设置或修改密码后,若立即尝试创建数据库或执行其他 SQL 语句,系统会返回 ERROR 1820 (HY000) 错误,提示 "You must SET PASSWORD before executing this statement"。该错误表明当前会话尚未应用新密码策略,需通过执行 `SET PASSWORD = PASSWORD('新密码');` 命令重新确认密码,即可恢复正常操作。

下一篇
Nginx+Tomcat高性能负载均衡集群搭建教程

本文介绍基于Nginx与Tomcat构建高性能负载均衡集群的完整流程。通过配置Nginx作为反向代理,将请求分发至多个Tomcat实例,突破单节点500并发限制。文中详细涵盖Nginx安装、核心参数调优(进程数、连接数、权重分配)及测试验证步骤,适用于Windows环境下的生产级部署参考。

相关文章

更多
CentOS 7 将 Nginx 添加为系统服务并设置开机自启

在 CentOS 7 中,编译安装 Nginx 后默认未注册为系统服务。本文介绍如何通过创建 /usr/lib/systemd/system/nginx.service 文件,利用 systemctl 命令管理 Nginx 状态、启动服务,并配置开机自动启动,实现服务的标准化运维。

2026.02.22 698
CentOS 7 配置 Nginx 开机自启动服务详解

在 CentOS 7 系统中,通过创建 systemd 服务文件可实现 Nginx 的开机自启动。需将包含 Unit、Service 及 Install 段落的配置文件保存至 /lib/systemd/system/nginx.service,并赋予 754 权限。随后使用 systemctl enable nginx.service 命令启用自启功能。相比旧版 chkconfig,systemctl 提供了更统一的服务管理接口,支持 start、stop、restart 及 status 等操作。

2026.08.06 443
CentOS 7 安装 Nginx 并配置 systemd 自启指南

本文介绍在 CentOS 7 系统中通过源码编译方式安装 Nginx 1.11.5 的完整流程。内容涵盖依赖库处理、configure 常见错误排查、make 编译安装以及利用 systemd 配置 nginx.service 实现开机自动启动和常用服务管理命令,最后通过访问服务器 IP 验证安装结果。

2026.05.21 128
CentOS 7 编译安装 PHP 7.4 与 Nginx 1.17.6 完整指南

本文介绍在 CentOS 7 系统中通过源码编译方式安装 PHP 7.4.0 和 Nginx 1.17.6 的详细步骤。内容涵盖依赖包安装、源码解压配置、参数调整及配置文件修改,重点包括 php.ini 的上传大小与时区设置,以及 php-fpm.conf 的 PID 路径和日志配置,最后提供 Nginx 反向代理 PHP 的配置示例及服务启动测试方法。

2025.10.22 932
CentOS 7.2 基于 Nginx 与 vsftpd 搭建图片服务器指南

本文介绍在 CentOS 7.2 环境下,通过安装配置 Nginx 和 vsftpd 来构建图片服务器的完整流程。主要涵盖 Nginx 源码编译安装、进程管理、配置文件编写以及 FTP 目录映射,最终实现通过 HTTP 访问存储在 FTP 服务中的图片资源。

2025.11.21 920
CentOS 7 安装 Nginx 整合 Lua 环境教程

本文介绍在 CentOS 7 虚拟机环境中,通过源码编译方式安装 Nginx 并整合 Lua 运行环境的完整流程。主要涵盖网络配置、依赖库安装、LuaJIT 部署、Nginx 编译参数设置以及解决 libluajit-5.1.so.2 缺失报错等关键步骤,最终实现 HTTP 请求下的 Lua 脚本执行验证。

2025.06.08 845

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。