Nginx交互式管理腳本
#!/bin/bash
nginxdir=`find / -name nginx|grep nginx/sbin/nginx|awk -F sbin '{print $1}'`
while true
do
clear
# menu
echo "
本機Nginx路徑爲: $nginxdir"
echo "
****************** Nginx tool *******************
* *"
echo "* (1) 啓動Nginx *"
echo "* (2) 關閉Nginx *"
echo "* (3) 重啓Nginx *"
echo "* (4) 查看Nginx運行進程數 *"
echo "* (5) 查看TCP連接狀態 *"
echo "* (10) 添加虛擬主機 *"
echo "* (0) 退出本程序 *"
echo "* *
*************************************************"
read -p "請輸入對應數字: " caozuo
case $caozuo in
# start
1) if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
$nginxdir/sbin/nginx
sleep 1
if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
read -p "Nginx 啓動失敗!"
else
read -p "Nginx 啓動完成!回車繼續!"
fi
else
read -p "Nginx is Running! 回車繼續!"
fi
;;
#stop
2) killall nginx
sleep 1
if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
read -p "Nginx關閉完成!回車繼續!"
else
read -p "Nginx關閉失敗!回車繼續!"
fi
;;
#restart
3) if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
$nginxdir/sbin/nginx
read -p "Nginx啓動完成!回車繼續!"
else
killall nginx
sleep 1
$nginxdir/sbin/nginx
read -p "Nginx重啓完成!回車繼續!"
fi
;;
#process
4) read -p "Nginx運行進程數: `ps -ef|grep nginx|grep -v nginx.sh|grep -v grep|wc -l`"
;;
#TCP
5) read -p "TCP連接狀態:
`netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"t",state[key]}'`"
;;
#vhost
10)
while true
do
clear
read -p "請輸入要添加的虛擬主機完整域名: " vhost
read -p "請輸入該域名使用的端口: " prot
read -p "請輸入域名對應的root目錄: " hostdir
read -p "請輸入訪問日誌目錄: " logdir
echo "
################### 確認以下信息 ########################
"
read -p "Nginx的目錄爲: $nginxdir
要添加的虛擬主機爲: $vhost
該域名對應的端口爲: $prot
域名對應的root目錄爲: $hostdir
訪問日誌文件爲: $logdir/$vhost.log
#########################################################
[回車繼續,如有誤請輸入0返回]:" queren2
case $queren2 in
0) break
;;
*)
[ ! -d $nginxdir/conf/conf.d/ ]&& mkdir $nginxdir/conf/conf.d/
touch $nginxdir/conf/conf.d/$vhost.conf
sed -i "/include * mime.types/ a \include $nginxdir\/conf\/conf.d\/$vhost.conf;" $nginxdir/conf/nginx.conf
echo 'server
{
listen '$prot';
server_name '$vhost';
index index.php index.html index.htm;
root '$hostdir';
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=/$1 last;
rewrite ^(.*)$ /index.php/$1 last;
}
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log '$logdir/$vhost.log';
}' >>$nginxdir/conf/conf.d/$vhost.conf
read -p "添加完成,需重啓Nginx生效,回車返回!"
break
;;
esac
done
;;
0) break
;;
*) read -p "請輸入對應數字!或者Ctrl+C退出!回車繼續!"
;;
esac
done
nginxdir=`find / -name nginx|grep nginx/sbin/nginx|awk -F sbin '{print $1}'`
while true
do
clear
# menu
echo "
本機Nginx路徑爲: $nginxdir"
echo "
****************** Nginx tool *******************
* *"
echo "* (1) 啓動Nginx *"
echo "* (2) 關閉Nginx *"
echo "* (3) 重啓Nginx *"
echo "* (4) 查看Nginx運行進程數 *"
echo "* (5) 查看TCP連接狀態 *"
echo "* (10) 添加虛擬主機 *"
echo "* (0) 退出本程序 *"
echo "* *
*************************************************"
read -p "請輸入對應數字: " caozuo
case $caozuo in
# start
1) if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
$nginxdir/sbin/nginx
sleep 1
if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
read -p "Nginx 啓動失敗!"
else
read -p "Nginx 啓動完成!回車繼續!"
fi
else
read -p "Nginx is Running! 回車繼續!"
fi
;;
#stop
2) killall nginx
sleep 1
if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
read -p "Nginx關閉完成!回車繼續!"
else
read -p "Nginx關閉失敗!回車繼續!"
fi
;;
#restart
3) if [ -z "`ps ax|grep nginx|grep -v grep|grep -v nginx.sh|awk '{print $1}'`" ];
then
$nginxdir/sbin/nginx
read -p "Nginx啓動完成!回車繼續!"
else
killall nginx
sleep 1
$nginxdir/sbin/nginx
read -p "Nginx重啓完成!回車繼續!"
fi
;;
#process
4) read -p "Nginx運行進程數: `ps -ef|grep nginx|grep -v nginx.sh|grep -v grep|wc -l`"
;;
#TCP
5) read -p "TCP連接狀態:
`netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"t",state[key]}'`"
;;
#vhost
10)
while true
do
clear
read -p "請輸入要添加的虛擬主機完整域名: " vhost
read -p "請輸入該域名使用的端口: " prot
read -p "請輸入域名對應的root目錄: " hostdir
read -p "請輸入訪問日誌目錄: " logdir
echo "
################### 確認以下信息 ########################
"
read -p "Nginx的目錄爲: $nginxdir
要添加的虛擬主機爲: $vhost
該域名對應的端口爲: $prot
域名對應的root目錄爲: $hostdir
訪問日誌文件爲: $logdir/$vhost.log
#########################################################
[回車繼續,如有誤請輸入0返回]:" queren2
case $queren2 in
0) break
;;
*)
[ ! -d $nginxdir/conf/conf.d/ ]&& mkdir $nginxdir/conf/conf.d/
touch $nginxdir/conf/conf.d/$vhost.conf
sed -i "/include * mime.types/ a \include $nginxdir\/conf\/conf.d\/$vhost.conf;" $nginxdir/conf/nginx.conf
echo 'server
{
listen '$prot';
server_name '$vhost';
index index.php index.html index.htm;
root '$hostdir';
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=/$1 last;
rewrite ^(.*)$ /index.php/$1 last;
}
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log '$logdir/$vhost.log';
}' >>$nginxdir/conf/conf.d/$vhost.conf
read -p "添加完成,需重啓Nginx生效,回車返回!"
break
;;
esac
done
;;
0) break
;;
*) read -p "請輸入對應數字!或者Ctrl+C退出!回車繼續!"
;;
esac
done
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.