Nginx 운영 관리 스 크 립 트
#!/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
본문 은 '송 송' 블 로그 에서 나 온 것 이 니, 반드시 이 출처 를 보존 해 주 십시오.http://dongsong.blog.51cto.com/916653/1626625
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.