Centos 7 Nginx 开机自启动
1.查看版本
nginx -v
[root@dev ~]# nginx -v nginx version: nginx/1.21.6 |
不同版本安装默认路径不太一样,所以必须 命令确认安装路径后再配置
2.查看nginx的安装路径
ps -aux|grep nginx
[root@dev ~]# ps -aux|grep nginx root 7633 0.0 0.0 112828 992 pts/0 S+ 13:41 0:00 grep --color=auto nginx root 27802 0.0 0.0 49040 1268 ? Ss 7月26 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf root 27803 0.0 0.0 51564 2492 ? S 7月26 0:00 nginx: worker process root 27804 0.0 0.0 51564 2480 ? S 7月26 0:00 nginx: worker process root 27805 0.0 0.0 51564 2508 ? S 7月26 0:00 nginx: worker process root 27806 0.0 0.0 51564 2516 ? S 7月26 0:00 nginx: worker process root 27807 0.0 0.0 51564 2512 ? S 7月26 0:00 nginx: worker process root 27808 0.0 0.0 51564 2504 ? S 7月26 0:00 nginx: worker process root 27809 0.0 0.0 51564 2516 ? S 7月26 0:00 nginx: worker process root 27810 0.0 0.0 51564 2516 ? S 7月26 0:00 nginx: worker process root 27811 0.0 0.0 51564 2516 ? S 7月26 0:00 nginx: worker process root 27812 0.0 0.0 51564 2500 ? S 7月26 0:00 nginx: worker process root 27813 0.0 0.0 51564 2508 ? S 7月26 0:00 nginx: worker process root 27814 0.0 0.0 51564 2492 ? S 7月26 0:00 nginx: worker process root 27815 0.0 0.0 51564 2516 ? S 7月26 0:00 nginx: worker process root 27816 0.0 0.0 51564 2512 ? S 7月26 0:00 nginx: worker process root 27817 0.0 0.0 51564 2516 ? S 7月26 0:00 nginx: worker process root 27818 0.0 0.0 51564 2516 ? S 7月26 0:00 nginx: worker process [root@dev ~]# cd |
说明:根据实际安装路径再创建 开机自启动配置
/usr/sbin/nginx
3.创建 nginx.service
vim /lib/systemd/system/nginx.service
使用下面的内容:
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target ~ ~ |
当前版本是 以上路径,相比其它少了一个“”local/nginx/“ 所以配置时,注意实际路径
其它的是:
|
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
保存退出。
4.设置开机启动
systemctl enable nginx.service
[root@dev system]# systemctl enable nginx.service Failed to execute operation: Cannot send after transport endpoint shutdown [root@dev system]# |
出现以上错误原因有两个,一个是上面广本中有错误,请检查看再保存,还有一种情况是
selinux没有关闭
正常的提示如下
:
[root@dev system]# systemctl enable nginx.service Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. |
systemctl status nginx.service
[root@dev system]# systemctl status nginx.service ● nginx.service - nginx Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: inactive (dead) Jul 28 14:14:29 dev.dltcmain systemd[1]: [/usr/lib/systemd/system/nginx.serv...' Jul 28 14:14:54 dev.dltcmain systemd[1]: [/usr/lib/systemd/system/nginx.serv...' Hint: Some lines were ellipsized, use -l to show in full. |
正常中,好像有不太对 Active: inactive (dead)?
6.杀死进程,然后重启
杀死nginx重启nginx
pkill -9 nginx
ps aux | grep nginx
systemctl start nginx
[root@dev system]# pkill -9 nginx [root@dev system]# ps aux | grep nginx root 4338 0.0 0.0 112812 980 pts/0 S+ 14:18 0:00 grep --color=auto nginx [root@dev system]# systemctl start nginx [root@dev system]# systemctl status nginx ● nginx.service - nginx Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2022-07-28 14:18:46 CST; 8s ago Process: 4351 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Main PID: 4352 (nginx) Tasks: 17 CGroup: /system.slice/nginx.service ├─4352 nginx: master process /usr/sbin/nginx ├─4353 nginx: worker process ├─4354 nginx: worker process ├─4355 nginx: worker process ├─4356 nginx: worker process ├─4357 nginx: worker process ├─4358 nginx: worker process ├─4359 nginx: worker process ├─4360 nginx: worker process ├─4362 nginx: worker process ├─4363 nginx: worker process ├─4365 nginx: worker process ├─4367 nginx: worker process ├─4368 nginx: worker process ├─4369 nginx: worker process ├─4370 nginx: worker process └─4371 nginx: worker process Jul 28 14:18:46 dev.dltcmain systemd[1]: Starting nginx... Jul 28 14:18:46 dev.dltcmain systemd[1]: Started nginx. [root@dev system]# |
至此开机启动就设置好了,过程中,感觉最搞的是 nginx的安装路径,和其它互联网的资料不一致。
---------------------------
最后返璞归真:
如果您是通过配置nginx源 且 通过yum install nginx 可以直接通过一行命令搞定
systemcel enable nginx.service
过程中如有异常,可以参考前面的,否则可以一行命令搞定.