Chinaunix首页 | 论坛 | 博客
  • 博客访问: 224637
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1506
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-25 10:52
文章分类
文章存档

2014年(22)

2013年(31)

我的朋友

分类: 网络与安全

2014-02-14 10:41:06

#!/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
阅读(2589) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

CU博客助理2014-03-10 16:27:48

专家点评:脚本我并没有执行,不过看脚本还是比较实用的。特别是对一些入门的操作员来说,很实用。
"


(感谢您参与“原创博文评选”获奖结果即将公布)