将nginx设置为开启自启动的配置

一 操作步骤

1.1 背景描述

项目现场项目需求就是需要重启机器之后,机器上的服务能够自动启动并运行。这里介绍Nginx的开机自启动。

1.2 操作步骤

1.进入 /lib/systemd/system/ 目录下

[root@localhost ~]# cd /lib/systemd/system/

2.创建文件Nginx.service

[root@localhost system]# touch Nginx.service
[root@localhost system]# vi Nginx.service 

3.编辑文件内容

[Unit]
Description=Nginx service
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 quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target

4.开机自启动

[root@localhost system]# systemctl enable Nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/Nginx.service to /usr/lib/systemd/system/Nginx.service.
[root@localhost system]# 

5.查看进程
[root@localhost system]# ps -ef|grep Nginx
root       3544      1  0 18:11 ?        00:00:00 Nginx: master process ./Nginx
nobody     3546   3544  0 18:11 ?        00:00:00 Nginx: worker process
root       3676   2606  0 18:16 pts/0    00:00:00 grep --color=auto Nginx
[root@localhost system]# 

6.关闭机器重新启动机器后,再次查看进程

[root@localhost ~]# ps -ef|grep Nginx
root       1013      1  0 18:19 ?        00:00:00 Nginx: master process /usr/local/Nginx/sbin/Nginx
nobody     1015   1013  0 18:19 ?        00:00:00 Nginx: worker process
root       2702   2653  0 18:22 pts/0    00:00:00 grep --color=auto Nginx
[root@localhost ~]# 


7.页面访问

 注意出现如下错误
Warning: Nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示执行命令systemctl daemon-reload 即可。

# systemctl daemon-reload
 

1.3  服务的启动/停止/刷新配置文件/查看状态

# systemctl start Nginx.service          启动Nginx服务

# systemctl stop Nginx.service           停止服务

# systemctl restart Nginx.service        重新启动服务

# systemctl list-units --type=service     查看所有已启动的服务

# systemctl status Nginx.service          查看服务当前状态

# systemctl enable Nginx.service          设置开机自启动

# systemctl disable Nginx.service         停止开机自启动

 

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...