代码
目录结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | :nginx-configs tianjiaguo$ tree . ├── 01-site-nginx │ ├── README.MD # 配置文件详情,标示目录下配置文件部署节点及各域名使用场景 │ ├── build_shell.sh │ ├── conf │ │ ├── browsers │ │ ├── fastcgi.conf │ │ ├── fastcgi_params │ │ ├── koi-utf │ │ ├── koi-win │ │ ├── mime.types │ │ ├── module_stubs │ │ ├── nginx.conf │ │ ├── scgi_params │ │ ├── uwsgi_params │ │ ├── vhost │ │ │ ├── README.MD # 描述文件,内容是警告不要直接修改此文件发布,应该作用发布部署系统发布 │ │ │ ├── www.tianjiaguo.com_server.conf │ │ │ ├── www.tianjiaguo.com_upstream.conf │ │ │ ├── image.tianjiaguo.com_server.conf │ │ │ └── image.tianjiaguo.com_upstream.conf │ │ └── win-utf │ └── controller.sh └── output # build执行后配置文件复制到output目录,部署程序需要把output目录打包并发送到目的机器 ├── conf │ ├── browsers │ ├── fastcgi.conf │ ├── fastcgi_params │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── module_stubs │ ├── nginx.conf │ ├── scgi_params │ ├── uwsgi_params │ ├── vhost │ │ ├── README.MD │ │ ├── www.tianjiaguo.com_server.conf │ │ ├── www.tianjiaguo.com_upstream.conf │ │ ├── image.tianjiaguo.com_server.conf │ │ └── image.tianjiaguo.com_upstream.conf │ ├── vhost-3.40.tar.gz │ └── win-utf └── controller.sh |
1 2 3 4 | # 打包 ./01-site-nginx/build_shell.sh # 部署 ./controller.sh |
打包脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #!/bin/bash source /etc/profile #通过脚本路径计算APP相关变量 11 if readlink -f "$0" > /dev/null 2>&1 then SHELL_BIN=$(readlink -f "$0") else SHELL_BIN="$0" fi # BIN_HOME=`cd $(dirname ${SHELL_BIN});pwd` PRJ_HOME=`cd ${BIN_HOME}/../;pwd` cd ${PRJ_HOME} rm -rf ./output/* mkdir ./output cp -r ${BIN_HOME}/conf ./output/ cp ${BIN_HOME}/controller.sh ./output/ chmod u+x ./output/controller.sh echo 'finish' |
部署脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #!/bin/bash source /etc/profile #通过脚本路径计算APP相关变量 11 if readlink -f "$0" > /dev/null 2>&1 then SHELL_BIN=$(readlink -f "$0") else SHELL_BIN="$0" fi # BIN_HOME=`cd $(dirname ${SHELL_BIN});pwd` TIME_STR=`date '+%Y%m%d%H%M%S'` NGINX_DIR=/usr/local/nginx LOG_DIR=/data1/logs/nginx/ LOG_FILE=${LOG_DIR}/deploy.log mkdir -p ${LOG_DIR} deploy_config() { echo "start deploy nginx" |tee -a ${LOG_FILE} echo `ls ${BIN_HOME}` |tee -a ${LOG_FILE} # cp echo "sudo mv ${NGINX_DIR}/conf ${BIN_HOME}/conf_${TIME_STR}" |tee -a ${LOG_FILE} sudo mv ${NGINX_DIR}/conf ${BIN_HOME}/conf_${TIME_STR} echo "sudo cp -r ${BIN_HOME}/conf ${NGINX_DIR}/conf" |tee -a ${LOG_FILE} sudo cp -r ${BIN_HOME}/conf ${NGINX_DIR}/conf } check_config() { # check echo "sudo ${NGINX_DIR}/sbin/nginx -t" |tee -a ${LOG_FILE} sudo ${NGINX_DIR}/sbin/nginx -t ret=$? if [ ${ret} -ne 0 ];then echo "===== nginx配置文件检查错误,无法部署 =====" |tee -a ${LOG_FILE} echo "sudo mv ${BIN_HOME}/conf_${TIME_STR} ${NGINX_DIR}/conf" |tee -a ${LOG_FILE} sudo rm -rf ${NGINX_DIR}/conf sudo mv ${BIN_HOME}/conf_${TIME_STR} ${NGINX_DIR}/conf exit ${ret} else echo "===== nginx配置文件检查无误 =====" |tee -a ${LOG_FILE} fi } psid=0 checkpid() { javaps=$(ps aux |grep "${NGINX_DIR}/sbin/nginx" |grep -v 'grep') if [ -n "$javaps" ]; then psid=$(echo $javaps | awk '{print $2}') else psid=0 fi } reload_config() { # reload echo "sudo ${NGINX_DIR}/sbin/nginx -s reload" |tee -a ${LOG_FILE} sudo ${NGINX_DIR}/sbin/nginx -s reload ret=$? if [ ${ret} -ne 0 ];then echo "===== nginx重启失败 =====" |tee -a ${LOG_FILE} echo "sudo mv ${BIN_HOME}/conf_${TIME_STR} ${NGINX_DIR}/conf" |tee -a ${LOG_FILE} sudo rm -rf ${NGINX_DIR}/conf sudo mv ${BIN_HOME}/conf_${TIME_STR} ${NGINX_DIR}/conf exit ${ret} else echo "===== nginx重启成功 =====" |tee -a ${LOG_FILE} fi } start_nginx() { # reload echo "sudo ${NGINX_DIR}/sbin/nginx" |tee -a ${LOG_FILE} sudo ${NGINX_DIR}/sbin/nginx ret=$? if [ ${ret} -ne 0 ];then echo "===== nginx启动失败 =====" |tee -a ${LOG_FILE} echo "sudo mv ${BIN_HOME}/conf_${TIME_STR} ${NGINX_DIR}/conf" |tee -a ${LOG_FILE} sudo rm -rf ${NGINX_DIR}/conf sudo mv ${BIN_HOME}/conf_${TIME_STR} ${NGINX_DIR}/conf exit ${ret} else echo "===== nginx启动成功 =====" |tee -a ${LOG_FILE} fi } start() { # 部署新配置文件 deploy_config # 检查新配置文件 check_config checkpid if [ $psid -ne 0 ]; then echo "info: reload nginx config! (pid=$psid)" # 重新载入配置文件 reload_config else echo "info: start nginx" # 重新启动nginx start_nginx fi } stop() { # nginx重启时不需要关闭旧进程 echo "do nothing" } status(){ checkpid if [ $psid -ne 0 ]; then echo "nginx start with pid $psid" else echo "nginx not start" fi } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo "Usage Command: {start|stop|restart|status}" exit 1 ;; esac |