v_add_web_domain 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/bin/bash
  2. # info: add web domain
  3. # options: user domain ip [template]
  4. #
  5. # The function adds virtual host to a server. In cases when a template is
  6. # undefined in the script, the template "default" will be used. The alias of
  7. # www.domain.tld type will be automatically assigned to the domain. If ip have
  8. # assocated dns name, this domain will also get the alias domain-tpl.$ipname.
  9. # An alias with the ip name is useful during the site testing while dns isn't
  10. # moved to a server yet.
  11. #----------------------------------------------------------#
  12. # Variable&Function #
  13. #----------------------------------------------------------#
  14. # Argument defenition
  15. user=$1
  16. domain=$(idn -t --quiet -u "$2" )
  17. domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
  18. domain_idn=$(idn -t --quiet -a "$domain")
  19. ip=$3
  20. template=${4-default}
  21. # Importing variables
  22. source $VESTA/conf/vars.conf
  23. source $V_CONF/vesta.conf
  24. source $V_FUNC/shared.func
  25. source $V_FUNC/domain.func
  26. source $V_FUNC/ip.func
  27. #----------------------------------------------------------#
  28. # Verifications #
  29. #----------------------------------------------------------#
  30. # Checking arg number
  31. check_args '3' "$#" 'user domain ip [template]'
  32. # Checking argument format
  33. format_validation 'user' 'domain' 'ip' 'template'
  34. # Checking web system is enabled
  35. is_system_enabled 'web'
  36. # Checking user
  37. is_user_valid
  38. # Checking user is active
  39. is_user_suspended
  40. # Checking domain
  41. is_domain_new 'quiet'
  42. if [ $? -ne 0 ]; then
  43. # Checking domain owner
  44. is_domain_owner
  45. # Checking domain service
  46. is_web_domain_free
  47. fi
  48. # Checking ip
  49. is_ip_avalable
  50. # Checking package
  51. is_package_full 'web_domain'
  52. # Checking template
  53. templates=$(get_user_value '$WEB_TPL')
  54. is_template_valid "web"
  55. #----------------------------------------------------------#
  56. # Action #
  57. #----------------------------------------------------------#
  58. # Defining domain aliases
  59. IP=$ip
  60. ip_name=$(get_ip_name)
  61. ip_name_idn=$(idn -t --quiet -a "$ip_name")
  62. domain_alias="www.$domain"
  63. domain_alias_idn="www.$domain_idn"
  64. if [ ! -z "$ip_name" ]; then
  65. domain_alias_dash="${domain//./-}.$ip_name"
  66. domain_alias_dash_idn="${domain_idn//./-}.$ip_name_idn"
  67. aliases="$domain_alias,$domain_alias_dash"
  68. aliases_idn="$domain_alias_idn,$domain_alias_dash_idn"
  69. alias_string="ServerAlias $domain_alias_idn $domain_alias_dash_idn"
  70. else
  71. aliases="$domain_alias"
  72. aliases_idn="$domain_alias_idn"
  73. alias_string="ServerAlias $domain_alias_idn"
  74. fi
  75. # Defining vars for add_config function
  76. group="$user"
  77. email="$user@$domain"
  78. docroot="$V_HOME/$user/web/$domain/public_html"
  79. docroot_string="DocumentRoot $docroot"
  80. conf="$V_HOME/$user/conf/web/httpd.conf"
  81. tpl_file="$V_WEBTPL/apache_$template.tpl"
  82. # Parsing template keys
  83. template_data=$(cat $V_WEBTPL/apache_$template.descr|grep -v '#')
  84. for keys in $template_data; do
  85. eval ${keys%%=*}=${keys#*=}
  86. done
  87. # Checking error log status
  88. if [ "$ELOG" = 'no' ]; then
  89. elog='#'
  90. else
  91. elog=''
  92. fi
  93. # Checking cgi
  94. if [ "$CGI" != 'yes' ]; then
  95. cgi='#'
  96. cgi_option='-ExecCGI'
  97. else
  98. cgi=''
  99. cgi_option='+ExecCGI'
  100. fi
  101. # Adding domain to the httpd.conf
  102. add_web_config
  103. # Building directory tree
  104. mkdir $V_HOME/$user/web/$domain \
  105. $V_HOME/$user/web/$domain/public_html \
  106. $V_HOME/$user/web/$domain/public_shtml \
  107. $V_HOME/$user/web/$domain/document_errors \
  108. $V_HOME/$user/web/$domain/cgi-bin \
  109. $V_HOME/$user/web/$domain/private \
  110. $V_HOME/$user/web/$domain/stats \
  111. $V_HOME/$user/web/$domain/logs
  112. # Adding domain logs
  113. touch /var/log/httpd/domains/$domain.bytes \
  114. /var/log/httpd/domains/$domain.log \
  115. /var/log/httpd/domains/$domain.error.log
  116. # Adding symlink for logs
  117. ln -s /var/log/httpd/domains/$domain.*log $V_HOME/$user/web/$domain/logs/
  118. # Adding domain skeleton
  119. if [ -e "$V_WEBTPL/skel/public_html/" ]; then
  120. cp -r $V_WEBTPL/skel/public_html/ $V_HOME/$user/web/$domain/
  121. fi
  122. if [ -e "$V_WEBTPL/skel/public_shtml/" ]; then
  123. cp -r $V_WEBTPL/skel/public_shtml/ $V_HOME/$user/web/$domain/
  124. fi
  125. if [ -e "$V_WEBTPL/skel/document_errors/" ]; then
  126. cp -r $V_WEBTPL/skel/document_errors/ $V_HOME/$user/web/$domain/
  127. fi
  128. if [ -e "$V_WEBTPL/skel/cgi-bin/" ]; then
  129. cp -r $V_WEBTPL/skel/cgi-bin/ $V_HOME/$user/web/$domain/
  130. fi
  131. # Changing tpl values
  132. for file in $(find "$V_HOME/$user/web/$domain/" -type f); do
  133. sed -i "s/%domain%/$domain/g" $file
  134. done
  135. # Changing file owner
  136. chown -R $user:$user $V_HOME/$user/web/$domain
  137. chown root:$user /var/log/httpd/domains/$domain.*
  138. chown root:apache $conf
  139. # Changing file permissions
  140. chmod 640 $conf
  141. chmod 551 $V_HOME/$user/web/$domain
  142. chmod 751 $V_HOME/$user/web/$domain/private
  143. chmod 751 $V_HOME/$user/web/$domain/cgi-bin
  144. chmod 751 $V_HOME/$user/web/$domain/public_html
  145. chmod 751 $V_HOME/$user/web/$domain/public_shtml
  146. chmod 751 $V_HOME/$user/web/$domain/document_errors
  147. chmod -f -R 775 $V_HOME/$user/web/$domain/cgi-bin/*
  148. chmod -f -R 775 $V_HOME/$user/web/$domain/public_html/*
  149. chmod -f -R 775 $V_HOME/$user/web/$domain/document_errors/*
  150. chmod 551 $V_HOME/$user/web/$domain/stats
  151. chmod 551 $V_HOME/$user/web/$domain/logs
  152. chmod 640 /var/log/httpd/domains/$domain.*
  153. # Running template trigger
  154. if [ -x $V_WEBTPL/apache_$template.sh ]; then
  155. $V_WEBTPL/apache_$template.sh $user $domain $ip $V_HOME $docroot
  156. fi
  157. # Checking main vesta httpd config
  158. main_conf='/etc/httpd/conf.d/vesta.conf'
  159. main_conf_check=$(grep "$conf" $main_conf )
  160. if [ -z "$main_conf_check" ]; then
  161. echo "Include $conf" >>$main_conf
  162. fi
  163. #----------------------------------------------------------#
  164. # Vesta #
  165. #----------------------------------------------------------#
  166. # Increasing ip value
  167. increase_ip_value "$ip"
  168. # Increasing domain value
  169. increase_user_value "$user" '$U_WEB_DOMAINS'
  170. # Defining domain variables
  171. v_str="DOMAIN='$domain'"
  172. v_str="$v_str IP='$ip' IP6=''"
  173. v_str="$v_str U_DISK='0'"
  174. v_str="$v_str U_BANDWIDTH='0'"
  175. v_str="$v_str TPL='$template'"
  176. v_str="$v_str ALIAS='$aliases'"
  177. v_str="$v_str $template_data" # Inserting PHP, CGI and ELOG keys
  178. v_str="$v_str STATS='' STATS_AUTH=''"
  179. v_str="$v_str SSL='no' SSL_HOME='single'"
  180. v_str="$v_str NGINX='' NGINX_EXT='' SUSPENDED='no' DATE='$V_DATE'"
  181. # Registering domain
  182. echo "$v_str" >> $V_USERS/$user/web.conf
  183. chmod 660 $V_USERS/$user/web.conf
  184. # Adding task to the vesta pipe
  185. restart_schedule 'web'
  186. # Logging
  187. log_history "$V_EVENT" "v_delete_web_domain $user $domain"
  188. log_event 'system' "$V_EVENT"
  189. exit