Browse Source

Add vhost del

lj2007331 9 years ago
parent
commit
61f8ad1f11
5 changed files with 196 additions and 55 deletions
  1. 7 0
      README.md
  2. 0 6
      config/nginx.conf
  3. 0 8
      config/nginx_apache.conf
  4. 15 8
      include/memory.sh
  5. 174 33
      vhost.sh

+ 7 - 0
README.md

@@ -46,6 +46,13 @@ cd ~/oneinstack    // Must enter the directory execution under oneinstack
 ./vhost.sh    // Do not sh vhost.sh or bash vhost.sh such execution
 ```
 
+## How to delete a virtual host
+
+```bash
+cd ~/oneinstack
+./vhost.sh del
+```
+
 ## How to add FTP virtual user
 
 ```bash

+ 0 - 6
config/nginx.conf

@@ -61,21 +61,15 @@ http {
         access_log /home/wwwlogs/access_nginx.log combined;
         root /home/wwwroot/default;
         index index.html index.php;
-        if ( $query_string ~* ".*[\;'\<\>].*" ){
-                return 404;
-	        }
-
         location ~ .*\.(php|php5)?$ {
 		#fastcgi_pass remote_php_ip:9000;
 	        fastcgi_pass unix:/dev/shm/php-cgi.sock;
 		fastcgi_index index.php;
 		include fastcgi.conf;
 		}
-
         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
 		expires 30d;
 	        }
-
         location ~ .*\.(js|css)?$ {
                 expires 7d;
                 }

+ 0 - 8
config/nginx_apache.conf

@@ -76,27 +76,19 @@ http {
         access_log /home/wwwlogs/access_nginx.log combined;
         root /home/wwwroot/default;
         index index.html index.php;
-        if ( $query_string ~* ".*[\;'\<\>].*" ){
-                return 404;
-	        }
-
 	location / {
 		try_files $uri @apache;
 		}
-
 	location @apache {
 		internal;
 		proxy_pass http://127.0.0.1:9090;
 		}
-
         location ~ .*\.(php|php5)?$  {
 		proxy_pass http://127.0.0.1:9090;
 		}
-
         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
                 expires 30d;
                 }
-
         location ~ .*\.(js|css)?$ {
                 expires 7d;
                 }

+ 15 - 8
include/memory.sh

@@ -34,17 +34,24 @@ elif [ $Mem -gt 8000 ];then
     Memory_limit=448
 fi
 
-# add swapfile
-if [ "$Swap" == '0' ] ;then
-    if [ $Mem -le 1024 ];then
-        dd if=/dev/zero of=/swapfile count=1024 bs=1M
-    elif [ $Mem -gt 1024 -a $Mem -le 2048 ];then
-        dd if=/dev/zero of=/swapfile count=2048 bs=1M
-    fi
+Make-swapfile() {
+    dd if=/dev/zero of=/swapfile count=$COUNT bs=1M
     mkswap /swapfile
     swapon /swapfile
     chmod 600 /swapfile
-    cat >> /etc/fstab << EOF
+    [ -z "`grep swapfile /etc/fstab`"] && cat >> /etc/fstab << EOF
 /swapfile    swap    swap    defaults    0 0
 EOF
+
+}
+
+# add swapfile
+if [ "$Swap" == '0' ] ;then
+    if [ $Mem -le 1024 ];then
+        COUNT=1024
+        Make-swapfile
+    elif [ $Mem -gt 1024 -a $Mem -le 2048 ];then
+        COUNT=2048
+        Make-swapfile
+    fi
 fi

+ 174 - 33
vhost.sh

@@ -20,10 +20,20 @@ printf "
 . ./options.conf
 . ./include/color.sh
 . ./include/check_web.sh
+. ./include/get_char.sh
 
 # Check if user is root
 [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; } 
 
+Usage() {
+printf "
+Usage: $0 [ ${CMSG}add${CEND} | ${CMSG}del${CEND} ]
+${CMSG}add${CEND}    --->Add Virtualhost
+${CMSG}del${CEND}    --->Delete Virtualhost
+
+"
+}
+
 Choose_env()
 {
 if [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
@@ -123,7 +133,7 @@ elif [ "$NGX_FLAG" == 'hhvm' ];then
 fi
 }
 
-Input_domain()
+Input_Add_domain()
 {
 while :
 do
@@ -516,38 +526,169 @@ echo "`printf "%-32s" "Directory of:"`${CMSG}$vhostdir${CEND}"
 [ "$rewrite_yn" == 'y' ] && echo "`printf "%-32s" "Rewrite rule:"`${CMSG}$rewrite${CEND}" 
 }
 
-if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/modules/libphp5.so" ];then
-    Choose_env
-    Input_domain
-    Nginx_anti_hotlinking
-    if [ "$NGX_FLAG" == 'java' ];then
-        Nginx_log
-        Create_nginx_tomcat_conf
-    else
-        Nginx_rewrite
-        Nginx_log
-        Create_nginx_php-fpm_hhvm_conf
-    fi
-elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
-    Choose_env
-    Input_domain
-    Apache_log
-    Create_apache_conf
-elif [ -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
-    Choose_env
-    Input_domain
-    Nginx_anti_hotlinking
-    if [ "$NGX_FLAG" == 'java' ];then
-        Nginx_log
-        Create_nginx_tomcat_conf
-    elif [ "$NGX_FLAG" == 'hhvm' ];then
-        Nginx_rewrite
-        Nginx_log
-        Create_nginx_php-fpm_hhvm_conf
-    elif [ "$NGX_FLAG" == 'php' ];then
-        #Nginx_rewrite
-        Nginx_log
+Add_Vhost() {
+    if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/modules/libphp5.so" ];then
+        Choose_env
+        Input_Add_domain
+        Nginx_anti_hotlinking
+        if [ "$NGX_FLAG" == 'java' ];then
+            Nginx_log
+            Create_nginx_tomcat_conf
+        else
+            Nginx_rewrite
+            Nginx_log
+            Create_nginx_php-fpm_hhvm_conf
+        fi
+    elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
+        Choose_env
+        Input_Add_domain
         Apache_log
-        Create_nginx_apache_mod-php_conf
+        Create_apache_conf
+    elif [ -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
+        Choose_env
+        Input_Add_domain
+        Nginx_anti_hotlinking
+        if [ "$NGX_FLAG" == 'java' ];then
+            Nginx_log
+            Create_nginx_tomcat_conf
+        elif [ "$NGX_FLAG" == 'hhvm' ];then
+            Nginx_rewrite
+            Nginx_log
+            Create_nginx_php-fpm_hhvm_conf
+        elif [ "$NGX_FLAG" == 'php' ];then
+            #Nginx_rewrite
+            Nginx_log
+            Apache_log
+            Create_nginx_apache_mod-php_conf
+        fi
     fi
+}
+
+Del_NGX_Vhost() {
+    if [ -e "$web_install_dir/sbin/nginx" ];then
+        [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
+        if [ -n "$Domain_List" ];then
+            echo
+            echo "Virtualhost list:"
+	    echo ${CMSG}$Domain_List${CEND}
+            while :
+            do
+                echo
+                read -p "Please input a domain you want to delete: " domain
+                if [ -z "`echo $domain | grep '.*\..*'`" ]; then
+                    echo "${CWARNING}input error! ${CEND}"
+                else
+                    if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
+                        Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
+                        rm -rf $web_install_dir/conf/vhost/${domain}.conf
+                        $web_install_dir/sbin/nginx -s reload
+                        while :
+                        do
+                            echo ''
+                            read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_NGX_wwwroot_yn 
+                            if [ "$Del_NGX_wwwroot_yn" != 'y' ] && [ "$Del_NGX_wwwroot_yn" != 'n' ];then
+                                echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
+                            else
+                                break
+                            fi
+                        done
+                        if [ "$Del_NGX_wwwroot_yn" == 'y' ];then
+                            echo "Press Ctrl+c to cancel or Press any key to continue..."
+                            char=`get_char`
+                            rm -rf $Directory
+                        fi
+                        echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
+                    else
+                        echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
+                    fi
+                    break
+                fi
+            done
+
+        else
+            echo "${CWARNING}Virtualhost was not exist! ${CEND}"
+        fi
+    fi
+}
+
+Del_Apache_Vhost() {
+    if [ -e "$apache_install_dir/conf/httpd.conf" ];then
+        if [ -e "$web_install_dir/sbin/nginx" ];then
+            rm -rf $apache_install_dir/conf/vhost/${domain}.conf 
+            /etc/init.d/httpd restart
+        else
+            Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
+            if [ -n "$Domain_List" ];then
+                echo
+                echo "Virtualhost list:"
+                echo ${CMSG}$Domain_List${CEND}
+                while :
+                do
+                    echo
+                    read -p "Please input a domain you want to delete: " domain
+                    if [ -z "`echo $domain | grep '.*\..*'`" ]; then
+                        echo "${CWARNING}input error! ${CEND}"
+                    else
+                        if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
+                            Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
+                            rm -rf $apache_install_dir/conf/vhost/${domain}.conf
+                            /etc/init.d/httpd restart
+                            while :
+                            do
+                                echo ''
+                                read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Apache_wwwroot_yn
+                                if [ "$Del_Apache_wwwroot_yn" != 'y' ] && [ "$Del_Apache_wwwroot_yn" != 'n' ];then
+                                    echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
+                                else
+                                    break
+                                fi
+                            done
+
+                            if [ "$Del_Apache_wwwroot_yn" == 'y' ];then
+                                echo "Press Ctrl+c to cancel or Press any key to continue..."
+                                char=`get_char`
+                                rm -rf $Directory
+                            fi
+                            echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
+                        else
+                            echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
+                        fi
+                        break
+                    fi
+                done
+
+            else
+                echo "${CWARNING}Virtualhost was not exist! ${CEND}"
+            fi
+        fi
+    fi
+}
+
+Del_Tomcat_Vhost() {
+    if [ -e "$tomcat_install_dir/conf/server.xml" ];then
+        Num=`grep -n "docBase=\"$Directory\"" $tomcat_install_dir/conf/server.xml | awk -F: '{print $1}'`
+        [ -n "$Num" ] && { sed -i ${Num}d $tomcat_install_dir/conf/server.xml; /etc/init.d/tomcat restart; }
+    fi
+}
+
+if [ $# == 0 ];then
+    Add_Vhost 
+elif [ $# == 1 ];then
+    case $1 in
+    add)
+        Add_Vhost
+        ;;
+
+    del)
+        Del_NGX_Vhost
+        Del_Apache_Vhost
+        Del_Tomcat_Vhost
+        ;;
+
+    *)
+        Usage
+        ;;
+    esac
+else
+    Usage
 fi