Скрипт создания виртуального хоста

Вот скриптик, созданный собой и для себя давным давно. первый опыт написания шелл скриптов =)

Создаем файлик vh, ставим ему права на исполнение

usage: sudo vh test1.ru

#!/bin/bash  
# Automatic creation of virtualhosl
# author	        Horrower 
# date 		20.10.2009


if [ `whoami` != 'root' ]; then                                                                                                                       
                                                                                                                                                      
	echo "You must be running with root privileges to run this script."                                                                           
	echo "Rerun using: sudo $0 $*"                                                                                                                
	exit 1                                                                                                                                        
fi  

# my directory with virtualhosts files
APACHE_VH_DIR="/etc/apache2/sites"

# my directory with virtualhosts files
NGINX_VH_DIR="/etc/nginx/hosts"

#my workspace folder
ROOT="/www/"

# const ip address for virtualhost
IP_ADDRESS="127.0.0.1"

# hosts file path
HOSTS_FILE="/etc/hosts"

# apache control 
APACHE_CTL="/usr/sbin/apachectl"

# apache control 
NGINX_CTL="/usr/bin/nginx"


host_exists() {
    if grep -q -e $1$ /etc/hosts ; then
	return 0
    else
	return 1
    fi
}


create_nginx_vhost() {

date=`/bin/date` 

echo "           
server {         
                listen       80;
                server_name  $1 www.$1;
                access_log  $ROOT$1/nginx_access.log ;
                error_log   $ROOT$1/nginx_error.log debug;  

                # Main location 

                location / {                                                                                                                          
                    proxy_pass         http://$1:8080;                                                                                              
                    #proxy_redirect     off;                                                                                                          
                                                                                                                                                      
                    proxy_set_header   Host             \$host;                                                                                        
                    proxy_set_header   X-Real-IP        \$remote_addr;                                                                                 
                    proxy_set_header   X-Forwarded-For  \$proxy_add_x_forwarded_for;                                                                   
                                                                                                                                                      
                    client_max_body_size       10m;                                                                                                   
                    client_body_buffer_size    128k;                                                                                                  
                                                                                                                                                      
                    proxy_connect_timeout      90;                                                                                                    
                    proxy_send_timeout         90;                                                                                                    
                    proxy_read_timeout         90;                                                                                                    
                                                                                                                                                      
                    proxy_buffer_size          4k;                                                                                                    
                    proxy_buffers              4 32k;                                                                                                 
                    proxy_busy_buffers_size    64k;                                                                                                   
                    proxy_temp_file_write_size 64k;                                                                                                   
                } 





                                                                                                                      
                location /_nginx/ {                                                                                                                          
                    fastcgi_pass   127.0.0.1:9001;                                                                                                    
                    fastcgi_index  index.php;                                                                                                         
                    fastcgi_param  SCRIPT_FILENAME  $ROOT$1/public_html/\$fastcgi_script_name;                               
                    include        fastcgi_params;                                                                                                    
                  }

                # Static files location
                location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js)$ {
                    root   $ROOT$1/public_html/;
		    expires 1h;
                }

                location ~ /\.ht {
	            deny  all;
	        }

}
" >> $NGINX_VH_DIR/$1.conf
}




create_apache_vhost() {

date=`/bin/date` 

echo "           

# name : $1
# created : $date

<VirtualHost $IP_ADDRESS:80>                                                                                                                            
    ServerName $1                                                                                                                                 
    ServerAlias www.$1                                                                                                                            
    DocumentRoot $ROOT/$1/public_html                                                                                      
                                                                                                                                                      
    <Directory $ROOT/$1/public_html>                                                                                       
	Options Includes -Indexes FollowSymLinks
        AllowOverride All                                                                                                                             
        Order allow,deny                                                                                                                              
        Allow from all                                                                                                                                
    </Directory>                                                                                                                                      
                                                                                                                                                      
    ErrorLog $ROOT/$1/error_log                                                                                            
    CustomLog $ROOT/$1/access_log common   

#    <Location /svn/>                                                                                                                                  
#	DAV svn                                                                                                                                       
#	SVNParentPath /usr/local/svn/$1                                                                                                           
#	SVNListParentPath on                                                                                                                          
#    </Location>           

</VirtualHost>
" >> $APACHE_VH_DIR/$1
}

usage() {
echo "                                                                                                                                  
Usage: sudo vh <name>                                                                                                                     
"                                                                                                                                                 
exit 1 
}


VIRTUALHOST=$1

if [ -z $1 ]; then 
  usage
fi

if ! echo $VIRTUALHOST | grep -q -E '^[A-Za-z0-9]+' ; then                                                                                            
    echo "Sorry, '$VIRTUALHOST' is not a valid host name to use. It must start with a letter or number."                                          
    exit 1                                                                                                                                        
fi                                                                                                                                                    
                                                                                                                                                    
echo -n "
Create http://$VIRTUALHOST/? [Y/n]: "                                                                                                        
                                                                                                                                                      
read continue                                                                                                                                         
                                                                                                                                                      
case $continue in                                                                                                                                     
n*|N*) exit                                                                                                                                           
esac                                                                                                                                                  
                                                                                                                                                      
                                                                                                                                                      
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -                                                                       
# If the host is not already defined in /etc/hosts, define it...                                                                                      
#                                                                                                                                                     
                                                                                                                                                      
if ! host_exists $VIRTUALHOST ; then                                                                                                                  
                                                                                                                                                      
echo  "
Creating a virtualhost for '$VIRTUALHOST'... "                                                                                             
echo -n "+ Adding '$VIRTUALHOST' to /etc/hosts... "                                                                                             
echo -n "$IP_ADDRESS	$VIRTUALHOST" >> /etc/hosts                                                                                                     
echo "done"                
echo -n "+ Creating apache virtualhost file in $APACHE_VH_DIR/$VIRTUALHOST... "
create_apache_vhost $VIRTUALHOST
echo "done"  
#echo -n "+ Creating nginx virtualhost file in $NGINX_VH_DIR/$VIRTUALHOST.conf... "
#create_nginx_vhost $VIRTUALHOST
echo "done"  
if ! [ -d $ROOT/$VIRTUALHOST/public_html ] ; then 
echo -n "+ Creating web directory and index file... "
mkdir $ROOT/$VIRTUALHOST
mkdir $ROOT/$VIRTUALHOST/public_html
fi
if ! [ -f $ROOT/$VIRTUALHOST/public_html/index.php ] ; then 
echo "<? 
phpinfo(); 
?>" >> $ROOT/$VIRTUALHOST/public_html/index.php
echo "done"
fi
echo -n "+ Setting up permissions for web directory..."
chown -R $SUDO_USER $ROOT/$VIRTUALHOST   
chmod -R 777 $ROOT/$VIRTUALHOST
#find $ROOT/$VIRTUALHOST/. -type d -exec chmod +a "_www allow read,write" {} \;
echo "done"
                                                                                                                                                   
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -                                                                       
# Restart apache for the changes to take effect                                                                                                       
#                                                                                                                                                     
echo -n "+ Flushing cache... "                                                                                                                        
dscacheutil -flushcache                                                                                                                               
sleep 1                                                                                                                                               
curl --silent http://$VIRTUALHOST/ 2>&1 >/dev/null                                                                                                    
echo "done"                                                                                                                                           
                                                                                                                                                      
dscacheutil -q host | grep -q $VIRTUALHOST                                                                                                            
                                                                                                                                                      
sleep 1                                                                                                                                               
                                                                                                                                                      
echo -n "+ Restarting Apache... "                                                                                                                     
$APACHE_CTL graceful 1>/dev/null 2>/dev/null                                                                                                           
echo "done"                                                                                                                                           

echo -n "+ Restarting Nginx... "                                                                                                                     
$NGINX_CTL -s reload 1>/dev/null 2>/dev/null                                                                                                           
echo "done"   
                                                                                                                                                      
echo -n "                                                                                                                                         
                                                                                                                                                      
http://$VIRTUALHOST/ is setup and ready for use.
"                    

else
echo -n "Same virtualhost config already exists
"
                                                                                                                 
fi