myvesta/bin/v-add-web-domain
2012-11-09 18:26:32 +02:00

190 lines
5.9 KiB
Bash
Executable file

#!/bin/bash
# info: add web domain
# options: user domain ip [template] [restart]
#
# The function adds virtual host to a server. In cases when a template is
# undefined in the script, the template "default" will be used. The alias of
# www.domain.tld type will be automatically assigned to the domain. If ip have
# assocated dns name, this domain will also get the alias domain-tpl.$ipname.
# An alias with the ip name is useful during the site testing while dns isn't
# moved to a server yet.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | sed -e 's/\.*$//g' -e 's/^\.*//g')
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
ip=$3
template=$4
restart=$5
# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/ip.sh
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '3' "$#" 'user domain ip [template] [restart]'
validate_format 'user' 'domain' 'ip'
is_system_enabled "$WEB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_domain_new 'web'
is_ip_valid
is_ip_avalable
is_package_full 'WEB_DOMAINS'
if [ ! -z "$template" ]; then
validate_format 'template'
is_apache_template_valid
else
template=$(get_user_value '$TEMPLATE')
is_apache_template_valid
fi
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining domain aliases
IP=$ip
ip_name=$(get_ip_name)
ip_name_idn=$(idn -t --quiet -a "$ip_name")
domain_alias="www.$domain"
domain_alias_idn="www.$domain_idn"
if [ ! -z "$ip_name" ]; then
domain_alias_dash="${domain//./-}.$ip_name"
domain_alias_dash_idn="${domain_idn//./-}.$ip_name_idn"
aliases="$domain_alias,$domain_alias_dash"
aliases_idn="$domain_alias_idn,$domain_alias_dash_idn"
alias_string="ServerAlias $domain_alias_idn $domain_alias_dash_idn"
else
aliases="$domain_alias"
aliases_idn="$domain_alias_idn"
alias_string="ServerAlias $domain_alias_idn"
fi
# Defining vars for add_config function
group="$user"
email="$user@$domain"
docroot="$HOMEDIR/$user/web/$domain/public_html"
conf="$HOMEDIR/$user/conf/web/httpd.conf"
tpl_file="$WEBTPL/apache_$template.tpl"
elog=''
cgi=''
cgi_option='+ExecCGI'
# Adding domain to the httpd.conf
add_web_config
# Building directory tree
mkdir $HOMEDIR/$user/web/$domain \
$HOMEDIR/$user/web/$domain/public_html \
$HOMEDIR/$user/web/$domain/public_shtml \
$HOMEDIR/$user/web/$domain/document_errors \
$HOMEDIR/$user/web/$domain/cgi-bin \
$HOMEDIR/$user/web/$domain/private \
$HOMEDIR/$user/web/$domain/stats \
$HOMEDIR/$user/web/$domain/logs
# Adding domain logs
touch /var/log/httpd/domains/$domain.bytes \
/var/log/httpd/domains/$domain.log \
/var/log/httpd/domains/$domain.error.log
# Adding symlink for logs
ln -s /var/log/httpd/domains/$domain.*log $HOMEDIR/$user/web/$domain/logs/
# Adding domain skeleton
if [ -e "$WEBTPL/skel/public_html/" ]; then
cp -r $WEBTPL/skel/public_html/ $HOMEDIR/$user/web/$domain/
fi
if [ -e "$WEBTPL/skel/public_shtml/" ]; then
cp -r $WEBTPL/skel/public_shtml/ $HOMEDIR/$user/web/$domain/
fi
if [ -e "$WEBTPL/skel/document_errors/" ]; then
cp -r $WEBTPL/skel/document_errors/ $HOMEDIR/$user/web/$domain/
fi
if [ -e "$WEBTPL/skel/cgi-bin/" ]; then
cp -r $WEBTPL/skel/cgi-bin/ $HOMEDIR/$user/web/$domain/
fi
# Changing tpl values
for file in $(find "$HOMEDIR/$user/web/$domain/" -type f); do
sed -i "s/%domain%/$domain/g" $file
done
# Changing file owner
chown -R $user:$user $HOMEDIR/$user/web/$domain
chown root:$user /var/log/httpd/domains/$domain.*
chown root:apache $conf
# Changing file permissions
chmod 660 $conf
chmod 551 $HOMEDIR/$user/web/$domain
chmod 771 $HOMEDIR/$user/web/$domain/private
chmod 771 $HOMEDIR/$user/web/$domain/cgi-bin
chmod 771 $HOMEDIR/$user/web/$domain/public_html
chmod 771 $HOMEDIR/$user/web/$domain/public_shtml
chmod 771 $HOMEDIR/$user/web/$domain/document_errors
chmod -f -R 665 $HOMEDIR/$user/web/$domain/cgi-bin/*
chmod -f -R 665 $HOMEDIR/$user/web/$domain/public_html/*
chmod -f -R 665 $HOMEDIR/$user/web/$domain/document_errors/*
chmod 551 $HOMEDIR/$user/web/$domain/stats
chmod 551 $HOMEDIR/$user/web/$domain/logs
chmod 640 /var/log/httpd/domains/$domain.*
# Running template trigger
if [ -x $WEBTPL/apache_$template.sh ]; then
$WEBTPL/apache_$template.sh $user $domain $ip $HOMEDIR $docroot
fi
# Checking main vesta httpd config
main_conf='/etc/httpd/conf.d/vesta.conf'
main_conf_check=$(grep "$conf" $main_conf )
if [ -z "$main_conf_check" ]; then
echo "Include $conf" >>$main_conf
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Increasing counters
increase_ip_value "$ip"
increase_user_value "$user" '$U_WEB_DOMAINS'
increase_user_value "$user" '$U_WEB_ALIASES'
# Defining domain variables
str="DOMAIN='$domain' IP='$ip' IP6='' ALIAS='$aliases' TPL='$template'"
str="$str CGI='yes' ELOG='yes' SSL='no' SSL_HOME='same' FTP_USER=''"
str="$str FTP_MD5='' NGINX='' NGINX_EXT='' STATS='' STATS_USER=''"
str="$str STATS_CRYPT='' U_DISK='0' U_BANDWIDTH='0' SUSPENDED='no'"
str="$str TIME='$TIME' DATE='$DATE'"
# Registering domain
echo "$str" >> $USER_DATA/web.conf
chmod 660 $USER_DATA/web.conf
# Restart web server
if [ "$restart" != 'no' ]; then
$BIN/v-restart-web "$EVENT"
fi
# Logging
log_history "added web domain $domain"
log_event "$OK" "$EVENT"
exit