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 ~]#