linux-nginx 怎么设置开机自启动

本文介绍了如何在使用systemd的系统(如CentOS7和Ubuntu16.04及以上)中管理和配置Nginx服务,包括直接使用默认配置和手动创建服务文件的情况。步骤包括启用Nginx、处理SELinux问题以及重启和加载systemd配置。
摘要由CSDN通过智能技术生成

使用systemd(适用于使用systemd的系统,如CentOS 7及更高版本、Ubuntu 16.04及更高版本等)

        1 直接运行下面代码,因为systemd有默认的nginx配置,所以直接使用下面代码就可以了

        系统自带的nginx配置的默认路径在/usr/lib/systemd/system/nginx.service

sudo systemctl enable nginx

        2 如果系统没有默认的nginx服务配置,则需要手动添加

  • 进入/etc/systemd/system文件夹,并创建一个新的服务文件,例如nginx.service
  • 手动添加的配置目录与系统自带的目录不同(便于区分系统文件和用户自定义的文件,当然放在系统默认目录也是可以的)
  • 在该文件中,添加以下内容
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

        3 在配置完 systemd 服务文件后,您需要重新加载 systemd 配置并启动您的服务:

sudo systemctl daemon-reload  
sudo systemctl start nginx

                
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值