Ubuntu设置nginx开机启动

提示:版本:ubuntu16.0.4,不同版本号方式可能不同;
每次服务器重启以后,都需要去手动重启很多服务,一是麻烦,二是容易有遗漏的服务,等项目运行起来出问题了,才知道某个服务忘记开启了,本文主要记录nginx开启启动


前言

nginx的安装及配置在之前的文章已经介绍 Ubuntu安装配置nginx


提示:以下是本篇文章正文内容,下面案例可供参考,linux 版本 Linux ubuntu 16.0.4

一、配置

1.编辑rc.local.service

代码如下(示例):

cd /lib/systemd/system/
vim rc.local.service 

添加相应配置,保存

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes

#以下为添加的配置
[Install]
WantedBy=multi-user.target
Alias=rc-local.service

设置软连接,开机启动查找 /etc/……文件

ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/rc.local.service

修改rc.local

vim /etc/rc.local

添加如下配置

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sudo -S /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf << EOF
root Wisdom,.lms2020
EOF
/etc/init.d/start.sh    #这个是自定义的一些自启项,例如redis、jar、svn等,本文可省略
exit 0

修改权限

sudo chmod +x rc.local

添加优先权

 sudo update-rc.d  nginx defaults

重启

reboot

这样,nginx开机自动启动就配置好了。配置jar、redis开机自启可参考 linux项目开机自启

Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐