Nginx 운영 스 크 립 트
#!/bin/sh
# Nginx
App=nginx
#
AppName=Nginx
#
AppBase=/App
# , "--prefix="
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}')
# Pid
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/;$//')
# FastCGI
AppFastCGITempDir=$(grep "^[[:space:]]*fastcgi_temp_path" $AppConf 2> /dev/null | awk '{print $2}' | sed 's/;$//')
# FastCGI
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
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"
[ $? -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
fi
if [ -n "$AppProxyCacheDir" ]; then
if [ ! -e "$AppProxyCacheDir" ]; then
mkdir -p $AppProxyCacheDir && echo " $AppName :$AppProxyCacheDir"
else
echo "$AppName :$AppProxyCacheDir "
fi
fi
if [ -n "$AppFastCGITempDir" ]; then
if [ ! -e "$AppFastCGITempDir" ]; then
mkdir -p $AppFastCGITempDir && echo " $AppName FastCGI :$AppFastCGITempDir"
else
echo "$AppName FastCGI :$AppFastCGITempDir "
fi
fi
if [ -n "$AppFastCGICacheDir" ]; then
if [ ! -e "$AppFastCGICacheDir" ]; then
mkdir -p $AppFastCGICacheDir && echo " $AppName FastCGI :$AppFastCGICacheDir"
else
echo "$AppName FastCGI :$AppFastCGICacheDir "
fi
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
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
프로그램 원숭이 의 낭만적 인 이 진 표백편그날 발 렌 타인 데 이에 나 는 그녀 에 게 숫자 (01001001 00100000 011011011011010 01101001 00100000 0111001001 011011011011011101) 를 보 냈 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.