#!/bin/sh # 編譯安裝管理Nginx App=nginx AppName=Nginx AppBase=/App AppDir=$AppBase/$App AppProg=$AppDir/sbin/nginx AppConf=$AppDir/conf/nginx.conf AppSrcBase=/App/src AppSrcFile=$App-*.tar.* AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f 2> /dev/null | sed -e 's/.tar.*$//' -e 's/^.\///') AppUser=$(grep "^[[:space:]]*user" $AppConf 2> /dev/null | sed 's/;//g' | awk '{print $2}') AppGroup=$(grep "^[[:space:]]*user" $AppConf 2> /dev/null | sed 's/;//g' | awk '{print $3}') AppPidDir=$(dirname $(grep "^[[:space:]]*pid" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//') 2> /dev/null) AppErrorLogs=$(grep "^[[:space:]]*error_log" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//') AppAccessLogs=$(grep "^[[:space:]]*access_log" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//') AppProxyTempDir=$(grep "^[[:space:]]*proxy_temp_path" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//') AppProxyCacheDir=$(grep "^[[:space:]]*proxy_cache_path" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//') AppFastCGITempDir=$(grep "^[[:space:]]*fastcgi_temp_path" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//') AppFastCGICacheDir=$(grep "^[[:space:]]*fastcgi_cache_path" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//') AppUser=${AppUser:-nobody} AppGroup=${AppGroup:-nobody} AppPidDir=${AppPidDir:-$AppDir/logs} AppErrorLogs=${AppErrorLogs:-$AppDir/logs/error.log} AppAccessLogs=${AppAccessLogs:-$AppDir/logs/access.log} RemoveFlag=0 InstallFlag=0 # 獲取PID fpid() { AppMasterPid=$(ps ax | grep "nginx: master process" | grep -v "grep" | awk '{print $1}' 2> /dev/null) AppWorkerPid=$(ps ax | grep "nginx: worker process" | grep -v "grep" | awk '{print $1}' 2> /dev/null) AppCacheManagerPid=$(ps ax | grep "nginx: cache manager process" | grep -v "grep" | awk '{print $1}' 2> /dev/null) AppCacheLoaderPid=$(ps ax | grep "nginx: cache loader process" | grep -v "grep" | awk '{print $1}' 2> /dev/null) } # 查詢狀態 fstatus() { fpid if [ ! -f "$AppProg" ]; then echo "$AppName 未安裝" else echo "$AppName 已安裝" if [ -z "$AppMasterPid" ]; then echo "$AppName 未啓動" else echo "$AppName 正在運行" fi fi } # 刪除 fremove() { fpid RemoveFlag=1 if [ -z "$AppMasterPid" ]; then if [ -d "$AppDir" ]; then rm -rf $AppDir && echo "刪除 $AppName" else echo "$AppName 未安裝" fi else echo "$AppName 正在運行" && exit fi } # 備份 fbackup() { Day=$(date +%Y-%m-%d) BackupFile=$App.$Day.tgz if [ -f "$AppProg" ]; then cd $AppBase tar zcvf $BackupFile --exclude=logs/* $App --backup=numbered [ $? -eq 0 ] && echo "$AppName 備份成功" || echo "$AppName 備份失敗" else echo "$AppName 未安裝" fi } # 安裝 finstall() { fpid InstallFlag=1 if [ -z "$AppMasterPid" ]; then test -f "$AppProg" && echo "$AppName 已安裝" [ $? -ne 0 ] && fupdate && fcpconf else echo "$AppName 正在運行" fi } # 拷貝配置 fcpconf() { cp -vf --backup=numbered $ScriptDir/nginx.conf $AppConf cp -vf --backup=numbered $AppDir/conf/fastcgi.conf $AppDir/conf/fastcgi.conf sed -i 's#/$nginx_version##' $AppDir/conf/fastcgi.conf } # 更新 fupdate() { Operate="更新" [ $InstallFlag -eq 1 ] && Operate="安裝" [ $RemoveFlag -ne 1 ] && fbackup cd $AppSrcBase test -d "$AppSrcDir" && rm -rf $AppSrcDir tar zxf $AppSrcFile || tar jxf $AppSrcFile || tar Jxf $AppSrcFile cd $AppSrcDir ./configure \ "--prefix=$AppDir" \ "--with-http_stub_status_module" \ "--without-http_auth_basic_module" \ "--without-http_autoindex_module" \ "--without-http_browser_module" \ "--without-http_empty_gif_module" \ "--without-http_geo_module" \ "--without-http_limit_conn_module" \ "--without-http_limit_req_module" \ "--without-http_map_module" \ "--without-http_memcached_module" \ "--without-http_proxy_module" \ "--without-http_referer_module" \ "--without-http_scgi_module" \ "--without-http_split_clients_module" \ "--without-http_ssi_module" \ "--without-http_upstream_ip_hash_module" \ "--without-http_upstream_keepalive_module" \ "--without-http_upstream_least_conn_module" \ "--without-http_userid_module" \ "--without-http_uwsgi_module" \ "--without-mail_imap_module" \ "--without-mail_pop3_module" \ "--without-mail_smtp_module" \ "--without-poll_module" \ "--without-select_module" \ "--with-cc-opt='-O2'" [ $? -eq 0 ] && make && make install if [ $? -eq 0 ]; then echo "$AppName $Operate成功" else echo "$AppName $Operate失敗" exit 1 fi } # 初始化 finit() { echo "初始化 $AppName" id -gn $AppGroup &> /dev/null if [ $? -ne 0 ]; then groupadd $AppGroup && echo "新建 $AppName 運行組:$AppGroup" else echo "$AppName 運行組:$AppGroup 已存在" fi id -un $AppUser &> /dev/null if [ $? -ne 0 ]; then useradd -s /bin/false -M -g $AppGroup $AppUser if [ $? -eq 0 ]; then echo "新建 $AppName 運行用戶:$AppUser" echo "s0ngNg1nx69#1" | passwd --stdin $AppUser &> /dev/null fi else echo "$AppName 運行用戶:$AppUser 已存在" fi cd $AppDir if [ ! -e "$AppPidDir" ]; then mkdir -p $AppPidDir && echo "新建 $AppName PID 文件目錄:$AppPidDir" else echo "$AppName PID 文件目錄:$AppPidDir 已存在" fi for AppErrorLog in $AppErrorLogs do AppErrorLogDir=$(dirname $AppErrorLog) if [ ! -e "$AppErrorLogDir" ]; then mkdir -p $AppErrorLogDir && echo "新建 $AppName 錯誤日誌目錄:$AppErrorLogDir" else echo "$AppName 錯誤日誌目錄:$AppErrorLogDir 已存在" fi done for AppAccessLog in $AppAccessLogs do AppAccessLogDir=$(dirname $AppAccessLog) if [ ! -e "$AppAccessLogDir" ]; then mkdir -p $AppAccessLogDir && echo "新建 $AppName 訪問日誌目錄:$AppAccessLogDir" else echo "$AppName 訪問日誌目錄:$AppAccessLogDir 已存在" fi done if [ -n "$AppProxyTempDir" ]; then if [ ! -e "$AppProxyTempDir" ]; then mkdir -p $AppProxyTempDir && echo "新建 $AppName 代理臨時目錄:$AppProxyTempDir" else echo "$AppName 代理臨時目錄:$AppProxyTempDir 已存在" fi chown $AppUser $AppProxyTempDir fi if [ -n "$AppProxyCacheDir" ]; then if [ ! -e "$AppProxyCacheDir" ]; then mkdir -p $AppProxyCacheDir && echo "新建 $AppName 代理緩存目錄:$AppProxyCacheDir" else echo "$AppName 代理緩存目錄:$AppProxyCacheDir 已存在" fi chown $AppUser $AppProxyCacheDir fi if [ -n "$AppFastCGITempDir" ]; then if [ ! -e "$AppFastCGITempDir" ]; then mkdir -p $AppFastCGITempDir && echo "新建 $AppName FastCGI臨時目錄:$AppFastCGITempDir" else echo "$AppName FastCGI臨時目錄:$AppFastCGITempDir 已存在" fi chown $AppUser $AppFastCGITempDir fi if [ -n "$AppFastCGICacheDir" ]; then if [ ! -e "$AppFastCGICacheDir" ]; then mkdir -p $AppFastCGICacheDir && echo "新建 $AppName FastCGI緩存目錄:$AppFastCGICacheDir" else echo "$AppName FastCGI緩存目錄:$AppFastCGICacheDir 已存在" fi chown $AppUser $AppFastCGICacheDir fi } # 檢查配置 ftest() { $AppProg -t && echo "$AppName 配置正確" || echo "$AppName 配置錯誤" } # 啓動 fstart() { fpid if [ -n "$AppMasterPid" ]; then echo "$AppName 正在運行" else $AppProg && echo "啓動 $AppName" || echo "$AppName 啓動失敗" fi } # 中止 fstop() { fpid if [ -n "$AppMasterPid" ]; then $AppProg -s stop && echo "中止 $AppName" || echo "$AppName 中止失敗" else echo "$AppName 未啓動" fi } # 重載配置 freload() { fpid if [ -n "$AppMasterPid" ]; then $AppProg -s reload && echo "重載 $AppName 配置" || echo "$AppName 重載配置失敗" else echo "$AppName 未啓動" fi } # 重啓 frestart() { fpid [ -n "$AppMasterPid" ] && fstop && sleep 1 fstart } # 終止進程 fkill() { fpid if [ -n "$AppMasterPid" ]; then echo "$AppMasterPid" | xargs kill -9 if [ $? -eq 0 ]; then echo "終止 $AppName 主進程" else echo "終止 $AppName 主進程失敗" fi else echo "$AppName 主進程未運行" fi if [ -n "$AppWorkerPid" ]; then echo "$AppWorkerPid" | xargs kill -9 if [ $? -eq 0 ]; then echo "終止 $AppName 工做進程" else echo "終止 $AppName 工做進程失敗" fi else echo "$AppName 工做進程未運行" fi if [ -n "$AppCacheManagerPid" ]; then echo "$AppCacheManagerPid" | xargs kill -9 if [ $? -eq 0 ]; then echo "終止 $AppName 緩存管理進程" else echo "終止 $AppName 緩存管理進程失敗" fi else echo "$AppName 緩存管理進程未運行" fi if [ -n "$AppCacheLoaderPid" ]; then echo "$AppCacheLoaderPid" | xargs kill -9 if [ $? -eq 0 ]; then echo "終止 $AppName 緩存加載進程" else echo "終止 $AppName 緩存加載進程失敗" fi else echo "$AppName 緩存加載進程未運行" fi } # 切割日誌 fcutlog() { Time=$(date +'%Y-%m-%d %H:%M:%S') Day=$(date -d '-1 days' +'%Y-%m-%d') SaveDays=30 echo "$Time" for AppAccessLog in $AppAccessLogs do [ $AppAccessLog == "off" ] && continue echo "$AppAccessLog" | grep -q "^/" if [ $? -eq 1 ]; then AppAccessLog=$AppDir/$AppAccessLog fi CutLog=$(echo "$AppAccessLog" | sed "s#\.log#.$Day.log#") find $(dirname $CutLog) -name "*.log" -type f -ctime +$SaveDays -exec rm -f {} \; mv -vf --backup=numbered $AppAccessLog $CutLog && echo "切割 $AppName 訪問日誌" done for AppErrorLog in $AppErrorLogs do echo "$AppErrorLog" | grep -q "^/" if [ $? -eq 1 ]; then AppErrorLog=$AppDir/$AppErrorLog fi CutLog=$(echo $AppErrorLog | sed "s#\.log#.$Day.log#") find $(dirname $CutLog) -name "*.log" -type f -ctime +$SaveDays -exec rm -f {} \; mv -vf --backup=numbered $AppErrorLog $CutLog && echo "切割 $AppName 錯誤日誌" done $AppProg -s reopen } ScriptDir=$(cd $(dirname $0); pwd) ScriptFile=$(basename $0) case "$1" in "install" ) finstall;; "update" ) fupdate;; "reinstall" ) fremove && finstall;; "remove" ) fremove;; "backup" ) fbackup;; "init" ) finit;; "start" ) fstart;; "stop" ) fstop;; "restart" ) frestart;; "status" ) fstatus;; "cpconf" ) fcpconf;; "test" ) ftest;; "reload" ) freload;; "kill" ) fkill;; "cutlog" ) fcutlog;; * ) echo "$ScriptFile install 安裝 $AppName" echo "$ScriptFile update 更新 $AppName" echo "$ScriptFile reinstall 重裝 $AppName" echo "$ScriptFile remove 刪除 $AppName" echo "$ScriptFile backup 備份 $AppName" echo "$ScriptFile init 初始化 $AppName" echo "$ScriptFile start 啓動 $AppName" echo "$ScriptFile stop 中止 $AppName" echo "$ScriptFile restart 重啓 $AppName" echo "$ScriptFile status 查詢 $AppName 狀態" echo "$ScriptFile cpconf 拷貝 $AppName 配置" echo "$ScriptFile test 檢查 $AppName 配置" echo "$ScriptFile reload 重載 $AppName 配置" echo "$ScriptFile kill 終止 $AppName 進程" echo "$ScriptFile cutlog 切割 $AppName 日誌" ;; esac