Linux 添加Nginx 到 service 启动

本文介绍了一个用于管理Nginx服务的bash脚本,包括启动、停止、重启及状态检查等功能。此脚本适用于Linux环境,为Nginx的安装与维护提供便利。

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

1.vi /etc/rc.d/init.d/nginx

2复制下面脚本

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/nginx.pid

RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0


# Start nginx daemons functions.
start() {

if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi

echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL

}


# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}


# reload nginx service functions.
reload() {

echo -n $"Reloading $prog: "
$nginxd -s reload
#if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`"
RETVAL=$?
echo

}

# See how we were called.
case "$1" in
start)
start
;;

stop)
stop
;;

reload)
reload
;;

restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac

exit $RETVAL
### Linux 中设置 Nginx 开机自启动的方法 在 Linux 系统中,可以通过 systemd 或 SysV Init 来配置 Nginx 的开机自启动功能。以下是具体的操作步骤: #### 方法一:通过 Systemd 配置 Nginx 开机自启动 1. **进入指定目录并创建服务文件** 切换到 `/lib/systemd/system/` 目录下,并创建名为 `nginx.service` 的服务文件。 ```bash [root@localhost ~]# cd /lib/systemd/system/ [root@localhost system]# touch nginx.service ``` 2. **编辑服务文件内容** 使用文本编辑器(如 `vi`)打开 `nginx.service` 文件,并按照以下模板填写内容: ```ini [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 ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target ``` 上述内容定义了 Nginx 作为服务的行为方式。 3. **重新加载 systemd 并启用服务** 完成上述操作后,需要重新加载 systemd 配置以识别新添加的服务文件,然后启用该服务以便于开机自动启动。 ```bash [root@localhost system]# systemctl daemon-reload [root@localhost system]# systemctl enable nginx ``` 4. **启动 Nginx 服务** 手动启动 Nginx 服务来验证其正常工作状态。 ```bash [root@localhost system]# systemctl start nginx ``` 以上过程完成后,Nginx 将会在每次系统重启时自动启动[^1][^3]。 #### 方法二:通过 SysV Init 配置 Nginx 开机自启动 对于某些不支持 systemd 的旧版 Linux 发行版,可以采用 SysV Init 方式实现相同目标。 1. 编辑位于 `/etc/init.d/` 下的脚本文件或者确认已存在针对 Nginx 的初始化脚本; 2. 授权此脚本可执行权限; 3. 添加至默认运行级别中的启动项列表里去。 ```bash $ sudo update-rc.d nginx defaults ``` 此外也可以直接利用如下命令完成整个流程简化处理: ```bash $ sudo systemctl enable nginx $ sudo systemctl start nginx ``` 注意这里虽然写的是 `systemctl` 实际上兼容调用了底层 init 脚本机制[^2]。 --- ### 总结 无论是现代基于 systemd 架构还是传统依赖 sysvinit 的环境当中,都可以轻松达成让 webserver 类型软件比如 Nginx 达成随操作系统引导而自行激活的效果。只需依据实际使用的发行版本特性选取对应途径即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值