nginx注册为系统服务

  • 编辑/etc/init.d/nginx
#! /bin/sh
#chkconfig: - 85 15

PATH=/home/soft/nginx/sbin

DESC="nginx service"
NAME=nginx
DAEMON=/home/soft/nginx/sbin/$NAME
CONFIGFILE=/home/soft/nginx/conf/$NAME.conf
PIDFILE=/home/soft/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}

do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}

do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac

exit 0
  • 修改执行权限

chmod -R 755 /etc/init.d/nginx

  • 添加到系统服务

chkconfig --add nginx

  • 开机自启动
    chkconfig nginx on

  • 命令
    service nginx start
    service nginx stop
    service nginx restart
    service nginx reload

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将nginx注册系统服务,并且在启动时使用命令`nginx -s reload`,你可以按照以下步骤进行操作: 1. 首先,确保已经安装了nginx,并且路径已经配置到系统的环境变量中。 2. 打开终端或命令提示符,并以管理员身份运行。 3. 进入nginx的安装目录,通常是`/etc/nginx`或`/usr/local/nginx`。 4. 执行命令`sudo nano /etc/systemd/system/nginx.service`来创建一个nginx的systemd服务配置文件。如果使用的是其他编辑器,请将`nano`替换为相应的编辑器。 5. 在打开的文件中,输入以下内容: ``` [Unit] Description=nginx HTTP and reverse proxy server After=network.target [Service] ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop Restart=on-failure [Install] WantedBy=multi-user.target ``` 请确保`ExecStart`、`ExecReload`和`ExecStop`的路径与你的nginx安装路径匹配。 6. 保存并关闭文件。 7. 执行命令`sudo systemctl daemon-reload`以重新加载systemd配置。 8. 执行命令`sudo systemctl enable nginx`以启用nginx服务。 9. 最后,执行命令`sudo systemctl start nginx`以启动nginx服务。 现在,你可以使用命令`sudo systemctl status nginx`来检查nginx服务的状态,以及使用命令`sudo systemctl restart nginx`来重新启动nginx服务。当你需要重新加载nginx配置时,可以直接使用命令`sudo systemctl reload nginx`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值