add web server(nginx+apache)

#!/bin/bash
#
# Web Server Install Script
# Last Updated 2012.09.24
# 
##### modify by WanJie 2012.09.24 #####

conf_dir1="/usr/local/nginx/conf/vhost.d"
conf_dir2="/usr/local/apache2/conf/vhost.d"
rewrite_dir='/usr/local/nginx/conf/rewrite.d'
web_dir="/data/www/vhosts"

function dis_info {
        clear
        echo 
        echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        echo "Add Virtual Host"
        echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        echo
}

if [ $# -eq 0 ];then
	dis_info;
	echo "Pleast input domain:"
	read -p "(Default or Example domain: www.example.com):" domain ;
fi

if [ $# -eq 1 ];then
	domain=$1
fi
#else
#	echo "parameter is error."
#	exit 1
#fi

if [[ $domain == "" ]];then
        domain="example.com"
else if [[ $domain =~ ^www\.(.*)$ ]];then
	nonwww_domain=${domain#www.}
	domain=$nonwww_domain
	wwwdomain=www.$nonwww_domain
	oriwwwdomain=ori-$wwwdomain
fi

fi


echo 
echo "domain:$domain"
echo "-----------------------------------------"
echo

if [[ -f "$conf_dir1/$domain.conf" ]];then
        echo "$conf_dir1/$domain.conf is exists!"
        exit
fi

if [[ -f "$conf_dir2/$domain.conf" ]];then
        echo "$conf_dir2/$domain.conf is exists!"
        exit
fi

if [[ -f "$rewrite_dir/$domain.conf" ]];then
	echo "$rewrite_dir/$domain.conf is exists!"
	exit
fi


#echo "Do you want to add ftp Account? (y/n)"
#read ftp_account
#if [[ $ftp_account == "y" || $ftp_account == "Y" ]];then
#       read -p "ftp user name:" ftp_user
#       read -p "ftp user password:" ftp_pass
#       echo "Create FTP virtual host directory."
#       mkdir -p $web_dir/$domain/httpdocs
#       useradd -d $web_dir/$domain/httpdocs/ -s /sbin/nologin $ftp_user -g users
#       echo "$ftp_pass" | passwd --stdin $ftp_user
#       chown -R apache:users $web_dir/$domain/httpdocs
#	chmod 775 apache:users $web_dir/$domain/httpdocs
#       chown apache:apache $web_dir/$domain
#       chmod 755 $web_dir/$domain
#       echo
#fi

mkdir -p $web_dir/$domain/httpdocs
if [[ ! -d $conf_dir1 ]];then
	mkdir -p $conf_dir1
fi
if [[ ! -d $conf_dir1 ]];then
	mkdir -p $conf_dir2
fi
if [[ ! -d $rewrite_dir ]];then
	mkdir -p $rewrite_dir
fi




chown -R apache.users $web_dir/$domain
chown -R apache:users $web_dir/$domain/httpdocs

if [[ $nonwww_domain != "" ]];then
	apache_alias="ServerAlias $wwwdomain"
	apache_alias2="ServerAlias $oriwwwdomain"
	apache_rewrite="RewriteEngine On
RewriteCond %{HTTP_HOST} ^$nonwww_domain [NC]
RewriteRule ^/(.*)$     http://$wwwdomain/\$1 [R=301,L]"
	nginx_alias="$wwwdomain $oriwwwdomain"
	nginx_rewrite="if (\$host ~* ^$nonwww_domain){ rewrite ^(.*)$ http://$wwwdomain\$1 permanent;}
	if (\$request_uri ~ ^/(.*)/(index|indice).(html)) { rewrite ^/(.*)/(index|indice).(html) /\$1   permanent;}"
else
	apache_alias=""
	apache_rewrite=""
	nginx_alias=""
	nginx_rewrite="if (\$request_uri ~ ^/(.*)/(index|indice).(html)) { rewrite ^/(.*)/(index|indice).(html) /\$1   permanent;}"
fi


echo "Create domain conf."

cat > $conf_dir1/$domain.conf< $rewrite_dir/$domain.conf

cat > $conf_dir2/$domain.conf<
        ServerName   $domain
	$apache_alias
	$apache_alias2
        UseCanonicalName Off
        ServerAdmin  "[email protected]"
	DocumentRoot $web_dir/$domain/httpdocs
        DirectoryIndex index.html index.shtml index.php
	CustomLog "|/usr/local/apache2/bin/rotatelogs -l /data/www/logs/apache_log/access/${domain}_access.log.%Y-%m-%d 86400" combined
        ErrorLog "|/usr/local/apache2/bin/rotatelogs -l /data/www/logs/apache_log/error/${domain}_error.log.%Y-%m-%d 86400"
        
                SSLEngine off
        
        
        
                php_admin_flag engine on
                php_admin_flag safe_mode on
                php_admin_value open_basedir ".:$web_dir/$domain:/tmp"
        
        
                php_admin_flag engine on
                php_admin_flag safe_mode on
                php_admin_value open_basedir ".:$web_dir/$domain:/tmp"
        
         Options -ExecCGI FollowSymLinks +Includes
         AllowOverride All
        
ErrorDocument 404 /404.html
$apache_rewrite

eof

echo
echo "web site infomations:"
echo "========================================"
echo "domain list:$domain "
echo "----------------------------------------"
echo "website dir:$web_dir/$domain"
echo "----------------------------------------"
echo "nginx_conf file:$conf_dir1/$domain.conf"
echo "----------------------------------------"
echo "apache2_conf file:$conf_dir2/$domain.conf"
echo "----------------------------------------"

#if [[ $ftp_account == "y" || $access_log == "Y" ]];then
#        echo "ftp user:$ftp_user password:$ftp_pass";
#        echo "----------------------------------------"
#fi
echo "web site is OK"
echo "========================================"

  

使用方法:

  执行脚本,并传入网站域名作为参数(网站域名不能以www开头)

你可能感兴趣的:(add web server(nginx+apache))