配置nginx的service服务,快捷启动nginx

1)在编辑nginx服务之前先关闭nginx。

使用命令查看nginx启动进程ID。

[root@localhost ~]# ps -ef |grep nginx
root      10759      1  0 15:05 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www       10760  10759  0 15:05 ?        00:00:00 nginx: worker process
www       10761  10759  0 15:05 ?        00:00:00 nginx: worker process
www       10762  10759  0 15:05 ?        00:00:00 nginx: worker process
www       10763  10759  0 15:05 ?        00:00:00 nginx: worker process
root      10985  10332  0 15:17 pts/0    00:00:00 grep --color=auto nginx

nginx的进程ID是10759,使用kill命令关闭nginx,并再次查看进程是否已关闭。

[root@localhost ~]# kill 10759
[root@localhost ~]#
[root@localhost ~]# ps -ef |grep nginx
root      11203  10332  0 15:35 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

2)编辑nginx服务

默认情况下,所有程序的服务都存在/lib/systemd/system/下,所以我们也在此目录下创建nginx的服务文件nginx.service

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target


[Service]
Type=forking
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=multi-user.target

3)重新加载服务

编辑好服务文件后,需要重新载入才能生效。

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]#

4)测试启动服务

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ps -ef |grep nginx
root      11334      1  0 15:44 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www       11335  11334  0 15:44 ?        00:00:00 nginx: worker process
www       11336  11334  0 15:44 ?        00:00:00 nginx: worker process
www       11337  11334  0 15:44 ?        00:00:00 nginx: worker process
www       11338  11334  0 15:44 ?        00:00:00 nginx: worker process
root      11342  10332  0 15:44 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

5)测试停止服务

[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# ps -ef |grep nginx
root      11385  10332  0 15:47 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

6)测试重启服务

重启服务的测试,主要是通过nginx两次运行的ID来分辨,每次启动的ID均不同。
开始启动后ID是11412,重启后变成11443,说明重启成功。

[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ps -ef |grep nginx
root      11412      1  0 15:49 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www       11413  11412  0 15:49 ?        00:00:00 nginx: worker process
www       11414  11412  0 15:49 ?        00:00:00 nginx: worker process
www       11415  11412  0 15:49 ?        00:00:00 nginx: worker process
www       11416  11412  0 15:49 ?        00:00:00 nginx: worker process
root      11421  10332  0 15:49 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# ps -ef |grep nginx
root      11443      1  0 15:50 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www       11444  11443  0 15:50 ?        00:00:00 nginx: worker process
www       11445  11443  0 15:50 ?        00:00:00 nginx: worker process
www       11446  11443  0 15:50 ?        00:00:00 nginx: worker process
www       11447  11443  0 15:50 ?        00:00:00 nginx: worker process
root      11467  10332  0 15:50 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

##7)设置开机启动

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

8)重启系统,测试nginx是否启动

查看进程,nginx成功启动。

Last login: Fri Jul 28 14:37:38 2023 from 192.168.88.134
[root@localhost ~]# ps -ef |grep nginx
root      11443      1  0 15:50 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www       11444  11443  0 15:50 ?        00:00:00 nginx: worker process
www       11445  11443  0 15:50 ?        00:00:00 nginx: worker process
www       11446  11443  0 15:50 ?        00:00:00 nginx: worker process
www       11447  11443  0 15:50 ?        00:00:00 nginx: worker process
root      11638  11573  0 15:55 pts/1    00:00:00 grep --color=auto nginx
[root@localhost ~]#
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lht631935612

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值