MENU

CentOS7 设置 openresty 开机启动

December 17, 2019 • linux相关

myapp 工程启动脚本 run.sh

#!/bin/bash

if [ -n "$2" ];then
    PROFILE="$1"
else
    PROFILE=dev
fi

# work root
#BASE_ROOT=$(cd $(dirname $0); pwd)
# service中openresty工作目录得分开写
BASE_ROOT=$(dirname $0)
cd ${BASE_ROOT}

# date time
DATE_TIME=`date +%Y%m%d%H%M%S`

# conf path
PATH_CONF=${BASE_ROOT}/conf

# logs path
PATH_LOGS=${BASE_ROOT}/logs

# conf file
FILE_NGINX_CONF=${PATH_CONF}/nginx-${PROFILE}.conf

# pid file
FILE_NGINX_PID=${PATH_LOGS}/nginx-${PROFILE}.pid

# nginx file
FILE_NGINX_BIN=/usr/local/openresty/nginx/sbin

mkdir -p ${PATH_LOGS}
echo "app work root : "${BASE_ROOT}
echo "nginx bin root : "${FILE_NGINX_BIN}
echo "start app with profile: "${PROFILE}

app_start(){
    echo '------ begin app_start ------'
    ${FILE_NGINX_BIN}/nginx -p ${BASE_ROOT}/ -c ${FILE_NGINX_CONF}
    echo '------ end app_start ------'
}

app_stop(){
    echo '------ begin app_stop ------'
    ${FILE_NGINX_BIN}/nginx -s stop -p ${BASE_ROOT}/ -c ${FILE_NGINX_CONF}
    echo '------ end app_stop ------'
}

app_restart(){
    echo '------ begin app_restart ------'
    kill -HUP `cat ${FILE_NGINX_PID}`
    echo '------ end app_restart ------'
}

case "$1" in
  start)
    echo "start app..."
    app_start
    ;;
    
  stop)
    echo "stop app!!!"
    app_stop
    ;;
    
  restart)
    echo "restart app!!!"
    app_restart
    ;;
    
  *)
    echo "usage: $0 {start|stop|restart} {dev}"
    exit 1
esac

加入服务项

vim /usr/lib/systemd/system/myapp.service

保存以下内容:

[Unit]
Description=nginx-openresty-myapp
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/home/wwwroot/myapp/logs/nginx-dev.pid
ExecStart=/home/wwwroot/myapp/run.sh start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
ExecStartPost=/bin/sleep 0.1
PrivateTmp=true
[Install]
WantedBy=multi-user.target

激活|启动|停止|查询服务:

systemctl enable|start|stop|status myapp
Last Modified: November 19, 2021