linux新增nginx系統服務
阿新 • • 發佈:2021-01-21
技術標籤:linuxnginxlinuxnginxshell運維
導航
將nginx新增為linux系統服務
在Linux上安裝好nginx後,除了通常需要配置的nignx conf檔案外,將nginx 加入service 系統服務可以更便捷的管理nginx 服務。具體操作很簡單,網上一搜一大把,這裡把我整理的方法總結一下,做個備忘。
第一步 寫指令碼
在 /etc/init.d/ 目錄下新建 nginx 檔案, 將下面指令碼寫入:
#! /bin/bash
# chkconfig: 35 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d_start(){
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON -s quit || echo -n " not running"
}
d_reload() {
$DAEMON -s reload || echo -n " counld not reload"
}
case "$1" in
start)
echo -n "Starting $DESC:$NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC:$NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
以上指令碼的幾點說明:
- 網上找的,親測可用
- 裡面 DAEMON 欄位的值是您nginx所在目錄,這個因人而異
- 其他提示欄位及功能可自行新增,這幾個基本也夠用了
第二步 配置一下
在上一步目錄 /etc/init.d/ 下 執行
- 新增可執行許可權
chmod +x nginx
- 新增系統服務
chkconfig --add nginx
搞定收工,執行如下命令驗證一下吧:
service nginx start
service nginx stop
service nginx reload
service nginx restart