Ubuntu18.04 将nginx设置成服务并开机自启

Ubuntu18.04 将nginx设置成服务并开机自启
1、创建nginx.service文件
在这里插入图片描述


路径:/lib/systemd/system
命令:touch nginx.service
Chmod 777 nginx.service

2、编辑文件nginx.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
[Unit]
Description=/home/user/service/nginx.sh Compatibility
ConditionFileIsExecutable=/home/user/service/nginx.sh
After=network.target
[Service]
Type=forking
ExecStart=/home/user/service/nginx.sh
TimeoutSec=0
RemainAfterExit=yes
#### 添加 ####
[Install]
WantedBy=multi-user.target
Alias=nginx.service

3、在/home/user/service/下创建nginx.sh 文件同时给与权限:chmod 677 nginx.sh
4、编辑nginx.sh

#!/bin/sh -e
cd /usr/local/nginx/sbin
./nginx# 一定要加最上面那否则容易报错#!/bin/sh -e
                              **一定要加最上面那否则容易报错#!/bin/sh -e**

5、创建链接:sudo ln -s /lib/systemd/system/nginx.service /etc/systemd/system
6、设置开机自启:systemctl enable nginx.service
7、启动服务:systemctl start nginx.service
8、查看服务状态:systemctl status nginx.service

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40267002/article/details/122478251
今日推荐