前言
ubuntu服务器自带了apache,如果按照之前的方法配置了nginx,当重启服务器后,会发现nginx并没有启动,取而代之的却是apache,这个问题其实很简单,无非是自启没有设置的问题。那么问题又来了,ubuntu18.04下,好像之前版本的设置方法对其无效,那么又该如何设置自启呢?
正文
1、建立rc-local.service文件
1 |
sudo vi /etc/systemd/system/rc-local.service |
2、将下列内容复制进rc-local.service文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=/etc/rc.local Compatibility ConditionPathExists=/etc/rc.local [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 StandardOutput=tty RemainAfterExit=yes SysVStartPriority=99 [Install] WantedBy=multi-user.target |
3、创建文件rc.local
1 |
sudo vi /etc/rc.local |
4、将下列内容复制进rc.local文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/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. echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log exit 0 |
5、给rc.local加上权限
1 |
sudo chmod +x /etc/rc.local |
6、启用服务
1 |
sudo systemctl enable rc-local |
7、启动服务并检查状态
1 2 |
sudo systemctl start rc-local.service sudo systemctl status rc-local.service |
8、重启并检查test.log文件
1 |
vi /usr/local/test.log |
如果发现test.log中有“看到这行字,说明添加自启动脚本成功。”即表示设置成功
9、设置nginx自启
1 |
sudo vi /etc/rc.local |
在exit0前面添加
1 2 |
sudo /etc/init.d/apache2 stop sudo systemctl start nginx |
再次重启,即可看到nginx正常的自启,而apache则被封印了。