centos7 nginx 开机自启动

参考:

http://www.centoscn.com/CentOS/config/2015/0507/5374.html

vi添加nginx.service

[root@klmy-460-res01 ~]# vi /usr/lib/systemd/system/nginx.service

编辑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

应用nginx自启动服务:

[root@klmy-460-res01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

开启服务:

[root@klmy-460-res01 ~]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

有错误,没有开启成功。

 查看服务状态:

[root@klmy-460-res01 ~]# systemctl status nginx
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 五 2016-12-16 17:25:07 CST; 3min 55s ago
  Process: 34804 ExecStart=/usr/etc/ngin/sbin/nginx (code=exited, status=1/FAILURE)

12月 16 17:25:05 klmy-460-res01 nginx[34804]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
12月 16 17:25:05 klmy-460-res01 nginx[34804]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
12月 16 17:25:06 klmy-460-res01 nginx[34804]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
12月 16 17:25:06 klmy-460-res01 nginx[34804]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
12月 16 17:25:07 klmy-460-res01 nginx[34804]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
12月 16 17:25:07 klmy-460-res01 nginx[34804]: nginx: [emerg] still could not bind()
12月 16 17:25:07 klmy-460-res01 systemd[1]: nginx.service: control process exited, code=exited status=1
12月 16 17:25:07 klmy-460-res01 systemd[1]: Failed to start nginx.
12月 16 17:25:07 klmy-460-res01 systemd[1]: Unit nginx.service entered failed state.
12月 16 17:25:07 klmy-460-res01 systemd[1]: nginx.service failed.

状态信息为,端口占用。

关闭已启动的nginx

[root@klmy-460-res01 ~]# /usr/etc/ngin/sbin/nginx -s stop

重新执行systemctl start命令开启服务:

[root@klmy-460-res01 ~]# systemctl start nginx
[root@klmy-460-res01 ~]# systemctl status nginx
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2016-12-16 17:32:58 CST; 17s ago
  Process: 34825 ExecStart=/usr/etc/ngin/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 34826 (nginx)
   CGroup: /system.slice/nginx.service
           ├─34826 nginx: master process /usr/etc/ngin/sbin/nginx
           └─34827 nginx: worker process

12月 16 17:32:58 klmy-460-res01.klmyedu.cn systemd[1]: Starting nginx...
12月 16 17:32:58 klmy-460-res01.klmyedu.cn systemd[1]: Started nginx.

重启服务器:

[root@klmy-460-res01 ~]# reboot

测试是否已经启动了

windows 命令行:

telnet 服务器ip 端口

 看到黑屏光标闪烁为已经启动

相关推荐