Centos7.x设置nginx开机启动
Systemctl 知识简介:
从CentOS7 开始,系统里的网络服务启动已经从传统的service改成了systemctl(一个systemd工具,主要负责控制systemd系统和服务管理器。),管理开机自启动的命令也从chkconfig改为了systemctl,由systemctl一个命令代替了CentOS7以前系统中的service和chkconfig两个命令。
系统服务的脚本也从传统的路径的/etc/init.d(/etc/rc.d/init.d/),改到了/usr/lib/systemd(除此之外还有/etc/systemd/system),需要自启动运行的程序,一般存在这个系统服务目录下,即:/usr/lib/systemd/system目录,每一个服务以“服务名.service”结尾,该文件的内容一般分为3部分:即[Unit]、[Service]和[Install]。
1. 第一步:进入到 /lib/systemd/system/ 目录
[root@iz2z init.d]# cd /lib/systemd/system/
2. 第二步:创建nginx.service文件,并编辑
# vim nginx.service、
内如如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/var/nginx
ExecReload=/var/nginx/nginx -s reload
ExecStop=/var/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]:服务的说明,对该系统服务描述及说明模块
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式,服务类型,可选有forking、notify、simple等
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
保存退出
3. 加入开机自启动
# systemctl enable nginx.service
如果不想开机自启动了,可以使用下面的命令取消开机自启动。
# systemctl disable nginx.service
systemctl start nginx.service 启动nginx
systemctl stop nginx.service 结束nginx
systemctl restart nginx.service 重启nginx
4. 说明(第一次启动会出现的问题)
说明第一次启动会提示以下报错:
[root@server nginx-1.8.1]# systemctl restart nginx.service
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
执行下面的命令重新载入 systemd,扫描新的或有变动的单元即可:
systemctl daemon-reload
#重新载入 systemd,扫描新的或有变动的单元
chmod -R 777 nginx/