v_add_web_domain 6.3 KB

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