1
0

vhost.sh 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  15. # For more information please visit https://oneinstack.com #
  16. #######################################################################
  17. "
  18. . ./options.conf
  19. . ./include/color.sh
  20. . ./include/check_dir.sh
  21. . ./include/check_os.sh
  22. . ./include/get_char.sh
  23. # Check if user is root
  24. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  25. Usage() {
  26. printf "
  27. Usage: $0 [ ${CMSG}add${CEND} | ${CMSG}del${CEND} ]
  28. ${CMSG}add${CEND} --->Add Virtualhost
  29. ${CMSG}del${CEND} --->Delete Virtualhost
  30. "
  31. }
  32. Choose_env() {
  33. if [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  34. Number=111
  35. while :; do echo
  36. echo 'Please choose to use environment:'
  37. echo -e "\t${CMSG}1${CEND}. Use php"
  38. echo -e "\t${CMSG}2${CEND}. Use java"
  39. echo -e "\t${CMSG}3${CEND}. Use hhvm"
  40. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  41. [ -z "$Choose_number" ] && Choose_number=1
  42. if [[ ! $Choose_number =~ ^[1-3]$ ]];then
  43. echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
  44. else
  45. break
  46. fi
  47. done
  48. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  49. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  50. [ "$Choose_number" == '3' ] && NGX_FLAG=hhvm
  51. elif [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  52. Number=110
  53. while :; do echo
  54. echo 'Please choose to use environment:'
  55. echo -e "\t${CMSG}1${CEND}. Use php"
  56. echo -e "\t${CMSG}2${CEND}. Use java"
  57. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  58. [ -z "$Choose_number" ] && Choose_number=1
  59. if [[ ! $Choose_number =~ ^[1-2]$ ]];then
  60. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  61. else
  62. break
  63. fi
  64. done
  65. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  66. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  67. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  68. Number=100
  69. NGX_FLAG=php
  70. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  71. Number=101
  72. while :; do echo
  73. echo 'Please choose to use environment:'
  74. echo -e "\t${CMSG}1${CEND}. Use php"
  75. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  76. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  77. [ -z "$Choose_number" ] && Choose_number=1
  78. if [[ ! $Choose_number =~ ^[1-2]$ ]];then
  79. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  80. else
  81. break
  82. fi
  83. done
  84. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  85. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  86. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  87. Number=011
  88. while :; do echo
  89. echo 'Please choose to use environment:'
  90. echo -e "\t${CMSG}1${CEND}. Use java"
  91. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  92. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  93. [ -z "$Choose_number" ] && Choose_number=1
  94. if [[ ! $Choose_number =~ ^[1-2]$ ]];then
  95. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  96. else
  97. break
  98. fi
  99. done
  100. [ "$Choose_number" == '1' ] && NGX_FLAG=java
  101. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  102. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  103. Number=010
  104. NGX_FLAG=java
  105. elif [ ! -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  106. Number=001
  107. NGX_FLAG=hhvm
  108. else
  109. Number=000
  110. NGX_FLAG=php
  111. fi
  112. if [ "$NGX_FLAG" == 'php' ];then
  113. NGX_CONF=$(echo -e "location ~ [^/]\.php(/|$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  114. elif [ "$NGX_FLAG" == 'java' ];then
  115. NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
  116. elif [ "$NGX_FLAG" == 'hhvm' ];then
  117. NGX_CONF=$(echo -e "location ~ .*\.(php|php5)?$ {\n fastcgi_pass unix:/var/log/hhvm/sock;\n fastcgi_index index.php;\n fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n include fastcgi_params;\n }")
  118. fi
  119. }
  120. Create_self_SSL() {
  121. printf "
  122. You are about to be asked to enter information that will be incorporated
  123. into your certificate request.
  124. What you are about to enter is what is called a Distinguished Name or a DN.
  125. There are quite a few fields but you can leave some blank
  126. For some fields there will be a default value,
  127. If you enter '.', the field will be left blank.
  128. "
  129. echo
  130. read -p "Country Name (2 letter code) [CN]: " SELFSIGNEDSSL_C
  131. [ -z "$SELFSIGNEDSSL_C" ] && SELFSIGNEDSSL_C=CN
  132. echo
  133. read -p "State or Province Name (full name) [Shanghai]: " SELFSIGNEDSSL_ST
  134. [ -z "$SELFSIGNEDSSL_ST" ] && SELFSIGNEDSSL_ST=Shanghai
  135. echo
  136. read -p "Locality Name (eg, city) [Shanghai]: " SELFSIGNEDSSL_L
  137. [ -z "$SELFSIGNEDSSL_L" ] && SELFSIGNEDSSL_L=Shanghai
  138. echo
  139. read -p "Organization Name (eg, company) [LinuxEye Inc.]: " SELFSIGNEDSSL_O
  140. [ -z "$SELFSIGNEDSSL_O" ] && SELFSIGNEDSSL_O='LinuxEye Inc.'
  141. echo
  142. read -p "Organizational Unit Name (eg, section) [IT Dept.]: " SELFSIGNEDSSL_OU
  143. [ -z "$SELFSIGNEDSSL_OU" ] && SELFSIGNEDSSL_OU='IT Dept.'
  144. openssl req -new -newkey rsa:2048 -sha256 -nodes -out $PATH_SSL/${domain}.csr -keyout $PATH_SSL/${domain}.key -subj "/C=${SELFSIGNEDSSL_C}/ST=${SELFSIGNEDSSL_ST}/L=${SELFSIGNEDSSL_L}/O=${SELFSIGNEDSSL_O}/OU=${SELFSIGNEDSSL_OU}/CN=${domain}" > /dev/null 2>&1
  145. /bin/cp $PATH_SSL/${domain}.csr{,_bk.`date +%Y-%m-%d_%H%M`}
  146. /bin/cp $PATH_SSL/${domain}.key{,_bk.`date +%Y-%m-%d_%H%M`}
  147. openssl x509 -req -days 36500 -sha256 -in $PATH_SSL/${domain}.csr -signkey $PATH_SSL/${domain}.key -out $PATH_SSL/${domain}.crt > /dev/null 2>&1
  148. }
  149. Create_SSL() {
  150. if [ -e "/usr/local/bin/certbot-auto" -a -e "/root/.local/share/letsencrypt/bin/letsencrypt" ];then
  151. while :; do echo
  152. read -p "Do you want to use a Let's Encrypt certificate? [y/n]: " letsencrypt_yn
  153. if [[ ! $letsencrypt_yn =~ ^[y,n]$ ]];then
  154. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  155. else
  156. break
  157. fi
  158. done
  159. if [ "$letsencrypt_yn" == 'y' ];then
  160. PUBLIC_IPADDR=`./include/get_public_ipaddr.py`
  161. for D in $domain $moredomainame
  162. do
  163. Domain_IPADDR=`ping $D -c1 | sed '1{s/[^(]*(//;s/).*//;q}'`
  164. [ "$PUBLIC_IPADDR" != "$Domain_IPADDR" ] && { echo; echo "${CFAILURE}DNS problem: NXDOMAIN looking up A for $D${CEND}"; echo; exit 1; }
  165. done
  166. #add Email
  167. while :
  168. do
  169. echo
  170. read -p "Please enter Administrator Email(example: admin@linuxeye.com): " Admin_Email
  171. if [ -z "`echo $Admin_Email | grep '.*@.*\..*'`" ]; then
  172. echo "${CWARNING}input error! ${CEND}"
  173. else
  174. break
  175. fi
  176. done
  177. [ "$moredomainame_yn" == 'y' ] && moredomainame_D="`for D in $moredomainame; do echo -d $D; done`"
  178. [ "$nginx_ssl_yn" == 'y' ] && S=nginx
  179. [ "$apache_ssl_yn" == 'y' ] && S=httpd
  180. [ ! -d "$wwwroot_dir/$domain/.well-known" ] && mkdir -p $wwwroot_dir/$domain/.well-known;chown -R $run_user.$run_user $wwwroot_dir/$domain/.well-known
  181. certbot-auto certonly --standalone --agree-tos --email $Admin_Email -d $domain $moredomainame_D --pre-hook "service $S stop" --post-hook "service $S start"
  182. if [ -s "/etc/letsencrypt/live/$domain/cert.pem" ];then
  183. [ -e "$PATH_SSL/$domain.crt" ] && rm -rf $PATH_SSL/$domain.{crt,key}
  184. ln -s /etc/letsencrypt/live/$domain/fullchain.pem $PATH_SSL/$domain.crt
  185. ln -s /etc/letsencrypt/live/$domain/privkey.pem $PATH_SSL/$domain.key
  186. if [ -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/conf/httpd.conf" ];then
  187. Cron_Command='/etc/init.d/nginx reload;/etc/init.d/httpd graceful'
  188. elif [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" ];then
  189. Cron_Command='/etc/init.d/nginx reload'
  190. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/conf/httpd.conf" ];then
  191. Cron_Command='/etc/init.d/httpd graceful'
  192. fi
  193. [ "$OS" == 'CentOS' ] && Cron_file=/var/spool/cron/root || Cron_file=/var/spool/cron/crontabs/root
  194. [ -z "`grep "$domain $moredomainame_D" $Cron_file`" ] && echo "0 10 * * 1 /usr/local/bin/certbot-auto certonly -a webroot --agree-tos --renew-by-default --webroot-path=$wwwroot_dir/$domain -d $domain $moredomainame_D;$Cron_Command" >> $Cron_file
  195. else
  196. echo "${CFAILURE}Error: Let's Encrypt SSL certificate installation failed${CEND}"
  197. exit 1
  198. fi
  199. else
  200. Create_self_SSL
  201. fi
  202. else
  203. Create_self_SSL
  204. fi
  205. }
  206. Print_ssl() {
  207. if [ "$letsencrypt_yn" == 'y' ];then
  208. echo "`printf "%-30s" "Let's Encrypt SSL Certificate:"`${CMSG}/etc/letsencrypt/live/$domain/fullchain.pem${CEND}"
  209. echo "`printf "%-30s" "SSL Private Key:"`${CMSG}/etc/letsencrypt/live/$domain/privkey.pem${CEND}"
  210. else
  211. echo "`printf "%-30s" "Self-signed SSL Certificate:"`${CMSG}$PATH_SSL/${domain}.crt${CEND}"
  212. echo "`printf "%-30s" "SSL Private Key:"`${CMSG}$PATH_SSL/${domain}.key${CEND}"
  213. echo "`printf "%-30s" "SSL CSR File:"`${CMSG}$PATH_SSL/${domain}.csr${CEND}"
  214. fi
  215. }
  216. Input_Add_domain() {
  217. if [ -e "$web_install_dir/sbin/nginx" ];then
  218. while :; do echo
  219. read -p "Do you want to setup SSL under Nginx? [y/n]: " nginx_ssl_yn
  220. if [[ ! $nginx_ssl_yn =~ ^[y,n]$ ]];then
  221. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  222. else
  223. break
  224. fi
  225. done
  226. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/bin/apachectl" ];then
  227. while :; do echo
  228. read -p "Do you want to setup SSL under Apache? [y/n]: " apache_ssl_yn
  229. if [[ ! $apache_ssl_yn =~ ^[y,n]$ ]];then
  230. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  231. else
  232. break
  233. fi
  234. done
  235. fi
  236. [ "$apache_ssl_yn" == 'y' ] && { PATH_SSL=$apache_install_dir/conf/ssl; [ ! -d "$PATH_SSL" ] && mkdir $PATH_SSL; }
  237. [ "$nginx_ssl_yn" == 'y' ] && { PATH_SSL=$web_install_dir/conf/ssl; [ ! -d "$PATH_SSL" ] && mkdir $PATH_SSL; }
  238. while :; do echo
  239. read -p "Please input domain(example: www.linuxeye.com): " domain
  240. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  241. echo "${CWARNING}input error! ${CEND}"
  242. else
  243. break
  244. fi
  245. done
  246. if [ -e "$web_install_dir/conf/vhost/$domain.conf" -o -e "$apache_install_dir/conf/vhost/$domain.conf" -o -e "$tomcat_install_dir/conf/vhost/$domain.xml" ]; then
  247. [ -e "$web_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Nginx/Tengine/OpenResty already exist! \nYou can delete ${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND} and re-create"
  248. [ -e "$apache_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Apache already exist! \nYou can delete ${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND} and re-create"
  249. [ -e "$tomcat_install_dir/conf/vhost/$domain.xml" ] && echo -e "$domain in the Tomcat already exist! \nYou can delete ${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND} and re-create"
  250. exit
  251. else
  252. echo "domain=$domain"
  253. fi
  254. while :; do echo
  255. read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
  256. if [[ ! $moredomainame_yn =~ ^[y,n]$ ]];then
  257. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  258. else
  259. break
  260. fi
  261. done
  262. if [ "$moredomainame_yn" == 'y' ]; then
  263. while :; do echo
  264. read -p "Type domainname or IP(example: linuxeye.com 121.43.8.8): " moredomain
  265. if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
  266. echo "${CWARNING}input error! ${CEND}"
  267. else
  268. [ "$moredomain" == "$domain" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  269. echo domain list="$moredomain"
  270. moredomainame=" $moredomain"
  271. break
  272. fi
  273. done
  274. Apache_Domain_alias=ServerAlias$moredomainame
  275. Tomcat_Domain_alias=$(for D in `echo $moredomainame`; do echo "<Alias>$D</Alias>"; done)
  276. if [ -e "$web_install_dir/sbin/nginx" ];then
  277. while :; do echo
  278. read -p "Do you want to redirect from $moredomain to $domain? [y/n]: " redirect_yn
  279. if [[ ! $redirect_yn =~ ^[y,n]$ ]];then
  280. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  281. else
  282. break
  283. fi
  284. done
  285. [ "$redirect_yn" == 'y' ] && Nginx_redirect=$(echo -e "if (\$host != $domain) {\n rewrite ^/(.*)\$ \$scheme://$domain/\$1 permanent;\n }")
  286. fi
  287. fi
  288. if [ "$nginx_ssl_yn" == 'y' ]; then
  289. if [[ "$($web_install_dir/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  290. LISTENOPT='443 ssl http2'
  291. else
  292. LISTENOPT='443 ssl spdy'
  293. fi
  294. Create_SSL
  295. Nginx_conf=$(echo -e "listen $LISTENOPT;\nssl_certificate $PATH_SSL/$domain.crt;\nssl_certificate_key $PATH_SSL/$domain.key;\nssl_session_timeout 10m;\nssl_protocols TLSv1 TLSv1.1 TLSv1.2;\nssl_prefer_server_ciphers on;\nssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:RC4-SHA:\!aNULL:\!eNULL:\!EXPORT:\!DES:\!3DES:\!MD5:\!DSS:\!PKS";\nssl_session_cache builtin:1000 shared:SSL:10m;\nresolver 8.8.8.8 8.8.4.4 valid=300s;\nresolver_timeout 5s;")
  296. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"$PATH_SSL/$domain.crt\"\n SSLCertificateKeyFile \"$PATH_SSL/$domain.key\"")
  297. elif [ "$apache_ssl_yn" == 'y' ]; then
  298. Create_SSL
  299. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"$PATH_SSL/$domain.crt\"\n SSLCertificateKeyFile \"$PATH_SSL/$domain.key\"")
  300. [ -z "`grep 'Listen 443' $apache_install_dir/conf/httpd.conf`" ] && sed -i "s@Listen 80@&\nListen 443@" $apache_install_dir/conf/httpd.conf
  301. [ -z "`grep 'ServerName 0.0.0.0:443' $apache_install_dir/conf/httpd.conf`" ] && sed -i "s@ServerName 0.0.0.0:80@&\nServerName 0.0.0.0:443@" $apache_install_dir/conf/httpd.conf
  302. else
  303. Nginx_conf='listen 80;'
  304. fi
  305. while :; do echo
  306. echo "Please input the directory for the domain:$domain :"
  307. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  308. if [ -n "$vhostdir" -a -z "`echo $vhostdir | grep '^/'`" ];then
  309. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  310. else
  311. if [ -z "$vhostdir" ]; then
  312. vhostdir="$wwwroot_dir/$domain"
  313. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  314. fi
  315. echo
  316. echo "Create Virtul Host directory......"
  317. mkdir -p $vhostdir
  318. echo "set permissions of Virtual Host directory......"
  319. chown -R ${run_user}.$run_user $vhostdir
  320. break
  321. fi
  322. done
  323. }
  324. Nginx_anti_hotlinking() {
  325. while :; do echo
  326. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  327. if [[ ! $anti_hotlinking_yn =~ ^[y,n]$ ]];then
  328. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  329. else
  330. break
  331. fi
  332. done
  333. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  334. domain_allow="*.${domain#*.} $domain"
  335. else
  336. domain_allow="*.$domain $domain"
  337. fi
  338. if [ "$anti_hotlinking_yn" == 'y' ];then
  339. if [ "$moredomainame_yn" == 'y' ]; then
  340. domain_allow_all=$domain_allow$moredomainame
  341. else
  342. domain_allow_all=$domain_allow
  343. fi
  344. anti_hotlinking=$(echo -e "location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {\n valid_referers none blocked $domain_allow_all;\n if (\$invalid_referer) {\n #rewrite ^/ http://www.linuxeye.com/403.html;\n return 403;\n }\n }")
  345. else
  346. anti_hotlinking=
  347. fi
  348. }
  349. Nginx_rewrite() {
  350. [ ! -d "$web_install_dir/conf/rewrite" ] && mkdir $web_install_dir/conf/rewrite
  351. while :; do echo
  352. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  353. if [[ ! $rewrite_yn =~ ^[y,n]$ ]];then
  354. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  355. else
  356. break
  357. fi
  358. done
  359. if [ "$rewrite_yn" == 'n' ];then
  360. rewrite="none"
  361. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  362. else
  363. echo
  364. echo "Please input the rewrite of programme :"
  365. echo "${CMSG}wordpress${CEND},${CMSG}discuz${CEND},${CMSG}opencart${CEND},${CMSG}thinkphp${CEND},${CMSG}laravel${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND} rewrite was exist."
  366. read -p "(Default rewrite: other):" rewrite
  367. if [ "$rewrite" == "" ]; then
  368. rewrite="other"
  369. fi
  370. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  371. [ "$NGX_FLAG" == 'php' -a "$rewrite" == "thinkphp" ] && NGX_CONF=$(echo -e "location ~ \.php {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi_params;\n set \$real_script_name \$fastcgi_script_name;\n if (\$fastcgi_script_name ~ \"^(.+?\.php)(/.+)\$\") {\n set \$real_script_name \$1;\n #set \$path_info \$2;\n }\n fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;\n fastcgi_param SCRIPT_NAME \$real_script_name;\n #fastcgi_param PATH_INFO \$path_info;\n }")
  372. if [ -e "config/$rewrite.conf" ];then
  373. /bin/cp config/$rewrite.conf $web_install_dir/conf/rewrite/$rewrite.conf
  374. else
  375. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  376. fi
  377. fi
  378. }
  379. Nginx_log() {
  380. while :; do echo
  381. read -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_yn
  382. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  383. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  384. else
  385. break
  386. fi
  387. done
  388. if [ "$access_yn" == 'n' ]; then
  389. N_log="access_log off;"
  390. else
  391. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  392. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  393. fi
  394. }
  395. Create_nginx_tomcat_conf() {
  396. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  397. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  398. server {
  399. $Nginx_conf
  400. server_name $domain$moredomainame;
  401. $N_log
  402. index index.html index.htm index.jsp;
  403. root $vhostdir;
  404. $Nginx_redirect
  405. $anti_hotlinking
  406. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  407. expires 30d;
  408. access_log off;
  409. }
  410. location ~ .*\.(js|css)?$ {
  411. expires 7d;
  412. access_log off;
  413. }
  414. $NGX_CONF
  415. }
  416. EOF
  417. if [ "$nginx_ssl_yn" == 'y' -a "$redirect_yn" == 'y' ];then
  418. cat >> $web_install_dir/conf/vhost/$domain.conf << EOF
  419. server {
  420. listen 80;
  421. server_name $domain$moredomainame;
  422. rewrite ^/(.*) https://$domain/\$1 permanent;
  423. }
  424. EOF
  425. elif [ "$nginx_ssl_yn" == 'y' ];then
  426. cat >> $web_install_dir/conf/vhost/$domain.conf << EOF
  427. server {
  428. listen 80;
  429. server_name $domain$moredomainame;
  430. $N_log
  431. index index.html index.htm index.jsp;
  432. root $vhostdir;
  433. $Nginx_redirect
  434. $anti_hotlinking
  435. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  436. expires 30d;
  437. access_log off;
  438. }
  439. location ~ .*\.(js|css)?$ {
  440. expires 7d;
  441. access_log off;
  442. }
  443. $NGX_CONF
  444. }
  445. EOF
  446. fi
  447. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  448. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  449. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  450. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  451. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  452. </Host>
  453. EOF
  454. [ -z "`grep -o "vhost-${domain} SYSTEM" $tomcat_install_dir/conf/server.xml`" ] && sed -i "/vhost-localhost SYSTEM/a<\!ENTITY vhost-${domain} SYSTEM \"file://$tomcat_install_dir/conf/vhost/$domain.xml\">" $tomcat_install_dir/conf/server.xml
  455. [ -z "`grep -o "vhost-${domain};" $tomcat_install_dir/conf/server.xml`" ] && sed -i "s@vhost-localhost;@&\n \&vhost-${domain};@" $tomcat_install_dir/conf/server.xml
  456. echo
  457. $web_install_dir/sbin/nginx -t
  458. if [ $? == 0 ];then
  459. echo "Reload Nginx......"
  460. $web_install_dir/sbin/nginx -s reload
  461. /etc/init.d/tomcat restart
  462. else
  463. rm -rf $web_install_dir/conf/vhost/$domain.conf
  464. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  465. exit 1
  466. fi
  467. printf "
  468. #######################################################################
  469. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  470. # For more information please visit https://oneinstack.com #
  471. #######################################################################
  472. "
  473. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  474. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  475. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  476. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  477. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  478. }
  479. Create_tomcat_conf() {
  480. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  481. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  482. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  483. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  484. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  485. </Host>
  486. EOF
  487. [ -z "`grep -o "vhost-${domain} SYSTEM" $tomcat_install_dir/conf/server.xml`" ] && sed -i "/vhost-localhost SYSTEM/a<\!ENTITY vhost-${domain} SYSTEM \"file://$tomcat_install_dir/conf/vhost/$domain.xml\">" $tomcat_install_dir/conf/server.xml
  488. [ -z "`grep -o "vhost-${domain};" $tomcat_install_dir/conf/server.xml`" ] && sed -i "s@vhost-localhost;@&\n \&vhost-${domain};@" $tomcat_install_dir/conf/server.xml
  489. echo
  490. /etc/init.d/tomcat restart
  491. printf "
  492. #######################################################################
  493. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  494. # For more information please visit https://oneinstack.com #
  495. #######################################################################
  496. "
  497. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  498. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  499. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  500. echo "`printf "%-30s" "index url:"`${CMSG}http://${domain}:8080/${CEND}"
  501. }
  502. Create_nginx_php-fpm_hhvm_conf() {
  503. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  504. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  505. server {
  506. $Nginx_conf
  507. server_name $domain$moredomainame;
  508. $N_log
  509. index index.html index.htm index.php;
  510. include $web_install_dir/conf/rewrite/$rewrite.conf;
  511. root $vhostdir;
  512. $Nginx_redirect
  513. $anti_hotlinking
  514. $NGX_CONF
  515. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  516. expires 30d;
  517. access_log off;
  518. }
  519. location ~ .*\.(js|css)?$ {
  520. expires 7d;
  521. access_log off;
  522. }
  523. }
  524. EOF
  525. if [ "$nginx_ssl_yn" == 'y' -a "$redirect_yn" == 'y' ];then
  526. cat >> $web_install_dir/conf/vhost/$domain.conf << EOF
  527. server {
  528. listen 80;
  529. server_name $domain$moredomainame;
  530. rewrite ^/(.*) https://$domain/\$1 permanent;
  531. }
  532. EOF
  533. elif [ "$nginx_ssl_yn" == 'y' ];then
  534. cat >> $web_install_dir/conf/vhost/$domain.conf << EOF
  535. server {
  536. listen 80;
  537. server_name $domain$moredomainame;
  538. $N_log
  539. index index.html index.htm index.php;
  540. include $web_install_dir/conf/rewrite/$rewrite.conf;
  541. root $vhostdir;
  542. $Nginx_redirect
  543. $anti_hotlinking
  544. $NGX_CONF
  545. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  546. expires 30d;
  547. access_log off;
  548. }
  549. location ~ .*\.(js|css)?$ {
  550. expires 7d;
  551. access_log off;
  552. }
  553. }
  554. EOF
  555. fi
  556. echo
  557. $web_install_dir/sbin/nginx -t
  558. if [ $? == 0 ];then
  559. echo "Reload Nginx......"
  560. $web_install_dir/sbin/nginx -s reload
  561. else
  562. rm -rf $web_install_dir/conf/vhost/$domain.conf
  563. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  564. exit 1
  565. fi
  566. printf "
  567. #######################################################################
  568. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  569. # For more information please visit https://oneinstack.com #
  570. #######################################################################
  571. "
  572. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  573. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  574. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  575. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-30s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  576. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  577. }
  578. Apache_log() {
  579. while :; do echo
  580. read -p "Allow Apache access_log? [y/n]: " access_yn
  581. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  582. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  583. else
  584. break
  585. fi
  586. done
  587. if [ "$access_yn" == 'n' ]; then
  588. A_log='CustomLog "/dev/null" common'
  589. else
  590. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  591. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  592. fi
  593. }
  594. Create_apache_conf() {
  595. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  596. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  597. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  598. <VirtualHost *:80>
  599. ServerAdmin admin@linuxeye.com
  600. DocumentRoot "$vhostdir"
  601. ServerName $domain
  602. $Apache_Domain_alias
  603. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  604. $A_log
  605. <Directory "$vhostdir">
  606. SetOutputFilter DEFLATE
  607. Options FollowSymLinks ExecCGI
  608. $R_TMP
  609. AllowOverride All
  610. Order allow,deny
  611. Allow from all
  612. DirectoryIndex index.html index.php
  613. </Directory>
  614. </VirtualHost>
  615. EOF
  616. [ "$apache_ssl_yn" == 'y' ] && cat >> $apache_install_dir/conf/vhost/$domain.conf << EOF
  617. <VirtualHost *:443>
  618. ServerAdmin admin@linuxeye.com
  619. DocumentRoot "$vhostdir"
  620. ServerName $domain
  621. $Apache_Domain_alias
  622. $Apache_SSL
  623. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  624. $A_log
  625. <Directory "$vhostdir">
  626. SetOutputFilter DEFLATE
  627. Options FollowSymLinks ExecCGI
  628. $R_TMP
  629. AllowOverride All
  630. Order allow,deny
  631. Allow from all
  632. DirectoryIndex index.html index.php
  633. </Directory>
  634. </VirtualHost>
  635. EOF
  636. echo
  637. $apache_install_dir/bin/apachectl -t
  638. if [ $? == 0 ];then
  639. echo "Restart Apache......"
  640. /etc/init.d/httpd restart
  641. else
  642. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  643. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  644. exit 1
  645. fi
  646. printf "
  647. #######################################################################
  648. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  649. # For more information please visit https://oneinstack.com #
  650. #######################################################################
  651. "
  652. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  653. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  654. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  655. }
  656. Create_nginx_apache_mod-php_conf() {
  657. # Nginx/Tengine/OpenResty
  658. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  659. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  660. server {
  661. $Nginx_conf
  662. server_name $domain$moredomainame;
  663. $N_log
  664. index index.html index.htm index.php;
  665. root $vhostdir;
  666. $Nginx_redirect
  667. $anti_hotlinking
  668. location / {
  669. try_files \$uri @apache;
  670. }
  671. location @apache {
  672. proxy_pass http://127.0.0.1:88;
  673. include proxy.conf;
  674. }
  675. location ~ .*\.(php|php5|cgi|pl)?$ {
  676. proxy_pass http://127.0.0.1:88;
  677. include proxy.conf;
  678. }
  679. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  680. expires 30d;
  681. access_log off;
  682. }
  683. location ~ .*\.(js|css)?$ {
  684. expires 7d;
  685. access_log off;
  686. }
  687. }
  688. EOF
  689. if [ "$nginx_ssl_yn" == 'y' -a "$redirect_yn" == 'y' ];then
  690. cat >> $web_install_dir/conf/vhost/$domain.conf << EOF
  691. server {
  692. listen 80;
  693. server_name $domain$moredomainame;
  694. rewrite ^/(.*) https://$domain/\$1 permanent;
  695. }
  696. EOF
  697. elif [ "$nginx_ssl_yn" == 'y' ];then
  698. cat >> $web_install_dir/conf/vhost/$domain.conf << EOF
  699. server {
  700. listen 80;
  701. server_name $domain$moredomainame;
  702. $N_log
  703. index index.html index.htm index.php;
  704. root $vhostdir;
  705. $Nginx_redirect
  706. $anti_hotlinking
  707. location / {
  708. try_files \$uri @apache;
  709. }
  710. location @apache {
  711. proxy_pass http://127.0.0.1:88;
  712. include proxy.conf;
  713. }
  714. location ~ .*\.(php|php5|cgi|pl)?$ {
  715. proxy_pass http://127.0.0.1:88;
  716. include proxy.conf;
  717. }
  718. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  719. expires 30d;
  720. access_log off;
  721. }
  722. location ~ .*\.(js|css)?$ {
  723. expires 7d;
  724. access_log off;
  725. }
  726. }
  727. EOF
  728. fi
  729. echo
  730. $web_install_dir/sbin/nginx -t
  731. if [ $? == 0 ];then
  732. echo "Reload Nginx......"
  733. $web_install_dir/sbin/nginx -s reload
  734. else
  735. rm -rf $web_install_dir/conf/vhost/$domain.conf
  736. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  737. fi
  738. # Apache
  739. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  740. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  741. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  742. <VirtualHost *:88>
  743. ServerAdmin admin@linuxeye.com
  744. DocumentRoot "$vhostdir"
  745. ServerName $domain
  746. $Apache_Domain_alias
  747. $Apache_SSL
  748. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  749. $A_log
  750. <Directory "$vhostdir">
  751. SetOutputFilter DEFLATE
  752. Options FollowSymLinks ExecCGI
  753. $R_TMP
  754. AllowOverride All
  755. Order allow,deny
  756. Allow from all
  757. DirectoryIndex index.html index.php
  758. </Directory>
  759. </VirtualHost>
  760. EOF
  761. echo
  762. $apache_install_dir/bin/apachectl -t
  763. if [ $? == 0 ];then
  764. echo "Restart Apache......"
  765. /etc/init.d/httpd restart
  766. else
  767. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  768. exit 1
  769. fi
  770. printf "
  771. #######################################################################
  772. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  773. # For more information please visit https://oneinstack.com #
  774. #######################################################################
  775. "
  776. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  777. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  778. echo "`printf "%-30s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  779. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  780. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  781. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  782. }
  783. Add_Vhost() {
  784. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" ];then
  785. Choose_env
  786. Input_Add_domain
  787. Nginx_anti_hotlinking
  788. if [ "$NGX_FLAG" == 'java' ];then
  789. Nginx_log
  790. Create_nginx_tomcat_conf
  791. else
  792. Nginx_rewrite
  793. Nginx_log
  794. Create_nginx_php-fpm_hhvm_conf
  795. fi
  796. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/conf/httpd.conf" ];then
  797. Choose_env
  798. Input_Add_domain
  799. Apache_log
  800. Create_apache_conf
  801. elif [ ! -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" -a -e "$tomcat_install_dir/conf/server.xml" ];then
  802. Choose_env
  803. Input_Add_domain
  804. Create_tomcat_conf
  805. elif [ -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  806. Choose_env
  807. Input_Add_domain
  808. Nginx_anti_hotlinking
  809. if [ "$NGX_FLAG" == 'java' ];then
  810. Nginx_log
  811. Create_nginx_tomcat_conf
  812. elif [ "$NGX_FLAG" == 'hhvm' ];then
  813. Nginx_rewrite
  814. Nginx_log
  815. Create_nginx_php-fpm_hhvm_conf
  816. elif [ "$NGX_FLAG" == 'php' ];then
  817. #Nginx_rewrite
  818. Nginx_log
  819. Apache_log
  820. Create_nginx_apache_mod-php_conf
  821. fi
  822. fi
  823. }
  824. Del_NGX_Vhost() {
  825. if [ -e "$web_install_dir/sbin/nginx" ];then
  826. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  827. if [ -n "$Domain_List" ];then
  828. echo
  829. echo "Virtualhost list:"
  830. echo ${CMSG}$Domain_List${CEND}
  831. while :; do echo
  832. read -p "Please input a domain you want to delete: " domain
  833. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  834. echo "${CWARNING}input error! ${CEND}"
  835. else
  836. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  837. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  838. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  839. $web_install_dir/sbin/nginx -s reload
  840. while :; do echo
  841. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  842. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  843. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  844. else
  845. break
  846. fi
  847. done
  848. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  849. echo "Press Ctrl+c to cancel or Press any key to continue..."
  850. char=`get_char`
  851. rm -rf $Directory
  852. fi
  853. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  854. else
  855. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  856. fi
  857. break
  858. fi
  859. done
  860. else
  861. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  862. fi
  863. fi
  864. }
  865. Del_Apache_Vhost() {
  866. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  867. if [ -e "$web_install_dir/sbin/nginx" ];then
  868. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  869. /etc/init.d/httpd restart
  870. else
  871. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  872. if [ -n "$Domain_List" ];then
  873. echo
  874. echo "Virtualhost list:"
  875. echo ${CMSG}$Domain_List${CEND}
  876. while :; do echo
  877. read -p "Please input a domain you want to delete: " domain
  878. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  879. echo "${CWARNING}input error! ${CEND}"
  880. else
  881. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  882. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  883. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  884. /etc/init.d/httpd restart
  885. while :; do echo
  886. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  887. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  888. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  889. else
  890. break
  891. fi
  892. done
  893. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  894. echo "Press Ctrl+c to cancel or Press any key to continue..."
  895. char=`get_char`
  896. rm -rf $Directory
  897. fi
  898. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  899. else
  900. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  901. fi
  902. break
  903. fi
  904. done
  905. else
  906. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  907. fi
  908. fi
  909. fi
  910. }
  911. Del_Tomcat_Vhost() {
  912. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  913. if [ -e "$web_install_dir/sbin/nginx" ];then
  914. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  915. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  916. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  917. /etc/init.d/tomcat restart
  918. fi
  919. else
  920. Domain_List=`ls $tomcat_install_dir/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g"`
  921. if [ -n "$Domain_List" ];then
  922. echo
  923. echo "Virtualhost list:"
  924. echo ${CMSG}$Domain_List${CEND}
  925. while :; do echo
  926. read -p "Please input a domain you want to delete: " domain
  927. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  928. echo "${CWARNING}input error! ${CEND}"
  929. else
  930. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  931. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  932. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  933. /etc/init.d/tomcat restart
  934. while :; do echo
  935. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  936. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  937. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  938. else
  939. break
  940. fi
  941. done
  942. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  943. echo "Press Ctrl+c to cancel or Press any key to continue..."
  944. char=`get_char`
  945. rm -rf $Directory
  946. fi
  947. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  948. else
  949. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  950. fi
  951. break
  952. fi
  953. done
  954. else
  955. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  956. fi
  957. fi
  958. fi
  959. }
  960. if [ $# == 0 ];then
  961. Add_Vhost
  962. elif [ $# == 1 ];then
  963. case $1 in
  964. add)
  965. Add_Vhost
  966. ;;
  967. del)
  968. Del_NGX_Vhost
  969. Del_Apache_Vhost
  970. Del_Tomcat_Vhost
  971. ;;
  972. *)
  973. Usage
  974. ;;
  975. esac
  976. else
  977. Usage
  978. fi