centos7 nginx 开机自启动

本文介绍如何在CentOS系统中设置Nginx为开机自启动服务,并解决启动过程中遇到的问题,如端口冲突等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:

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 端口

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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值