35 Nginx配置成系统服务

35 Nginx配置成系统服务

经过前面操作,想要启动、关闭或重新加载nginx配置文件,需要先进入到nginx安装目录的sbin目录下,再使用nginx的二进制可执行文件操作,操作较繁琐,如何优化?

将nginx设置成随服务器启动就自动完成启动操作,如何实现?

1.Nginx配置成系统服务

2.Nginx命令配置到系统环境

35.1 配置nginx.service

[root@nginx-100 ~]# cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=default.target

35.2 修改权限

[root@nginx-100 ~]# chmod 755 /usr/lib/systemd/system/nginx.service

35.3 系统命令

启动:systemctl start nginx.service
停止:systemctl stop nginx.service
重启:systemctl restart nginx.service
重新加载配置文件:systemctl reload nginx.service
查看nginx状态:systemctl status nginx.service
开机自启:systemctl enable nginx.service
关闭开机自启:systemctl disable nginx.service

35.4 演示

# 启动
[root@nginx-100 ~]# systemctl start nginx
# 查看进程
[root@nginx-100 ~]# ps -ef|grep nginx
root       1806      1  0 10:42 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        1807   1806  0 10:42 ?        00:00:00 nginx: worker process
www        1808   1806  0 10:42 ?        00:00:00 nginx: worker process
root       1810   1607  0 10:42 pts/1    00:00:00 grep --color=auto nginx
# 查看状态
[root@nginx-100 ~]# systemctl status nginx
● nginx.service - nginx web service
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2026-05-09 10:42:39 CST; 12s ago
     Docs: http://nginx.org/en/docs/
  Process: 1804 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1803 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 1806 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1806 nginx: master process /usr/local/nginx/sbin/nginx
           ├─1807 nginx: worker process
           └─1808 nginx: worker process

May 09 10:42:38 nginx-100 systemd[1]: Starting nginx web service...
May 09 10:42:38 nginx-100 nginx[1803]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
May 09 10:42:38 nginx-100 nginx[1803]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
May 09 10:42:39 nginx-100 systemd[1]: Started nginx web service.
# 开机自启
[root@nginx-100 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/default.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 关闭
[root@nginx-100 ~]# systemctl stop nginx
# 查看进程 [root@nginx
-100 ~]# ps -ef|grep nginx root 1856 1607 0 10:43 pts/1 00:00:00 grep --color=auto nginx

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-05-08 16:21  马俊南  阅读(5)  评论(0)    收藏  举报