vhost.sh 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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 "~/.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. if [ "$moredomainame_yn" == 'y' ]; then
  298. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain$moredomainame;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  299. else
  300. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  301. fi
  302. elif [ "$apache_ssl_yn" == 'y' ]; then
  303. Create_SSL
  304. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"$PATH_SSL/$domain.crt\"\n SSLCertificateKeyFile \"$PATH_SSL/$domain.key\"")
  305. [ -z "`grep 'Listen 443' $apache_install_dir/conf/httpd.conf`" ] && sed -i "s@Listen 80@&\nListen 443@" $apache_install_dir/conf/httpd.conf
  306. [ -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
  307. else
  308. Nginx_conf='listen 80;'
  309. fi
  310. while :; do echo
  311. echo "Please input the directory for the domain:$domain :"
  312. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  313. if [ -n "$vhostdir" -a -z "`echo $vhostdir | grep '^/'`" ];then
  314. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  315. else
  316. if [ -z "$vhostdir" ]; then
  317. vhostdir="$wwwroot_dir/$domain"
  318. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  319. fi
  320. echo
  321. echo "Create Virtul Host directory......"
  322. mkdir -p $vhostdir
  323. echo "set permissions of Virtual Host directory......"
  324. chown -R ${run_user}.$run_user $vhostdir
  325. break
  326. fi
  327. done
  328. }
  329. Nginx_anti_hotlinking() {
  330. while :; do echo
  331. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  332. if [[ ! $anti_hotlinking_yn =~ ^[y,n]$ ]];then
  333. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  334. else
  335. break
  336. fi
  337. done
  338. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  339. domain_allow="*.${domain#*.} $domain"
  340. else
  341. domain_allow="*.$domain $domain"
  342. fi
  343. if [ "$anti_hotlinking_yn" == 'y' ];then
  344. if [ "$moredomainame_yn" == 'y' ]; then
  345. domain_allow_all=$domain_allow$moredomainame
  346. else
  347. domain_allow_all=$domain_allow
  348. fi
  349. 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 }")
  350. else
  351. anti_hotlinking=
  352. fi
  353. }
  354. Nginx_rewrite() {
  355. [ ! -d "$web_install_dir/conf/rewrite" ] && mkdir $web_install_dir/conf/rewrite
  356. while :; do echo
  357. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  358. if [[ ! $rewrite_yn =~ ^[y,n]$ ]];then
  359. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  360. else
  361. break
  362. fi
  363. done
  364. if [ "$rewrite_yn" == 'n' ];then
  365. rewrite="none"
  366. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  367. else
  368. echo
  369. echo "Please input the rewrite of programme :"
  370. 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."
  371. read -p "(Default rewrite: other):" rewrite
  372. if [ "$rewrite" == "" ]; then
  373. rewrite="other"
  374. fi
  375. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  376. [ "$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 }")
  377. if [ -e "config/$rewrite.conf" ];then
  378. /bin/cp config/$rewrite.conf $web_install_dir/conf/rewrite/$rewrite.conf
  379. else
  380. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  381. fi
  382. fi
  383. }
  384. Nginx_log() {
  385. while :; do echo
  386. read -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_yn
  387. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  388. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  389. else
  390. break
  391. fi
  392. done
  393. if [ "$access_yn" == 'n' ]; then
  394. N_log="access_log off;"
  395. else
  396. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  397. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  398. fi
  399. }
  400. Create_nginx_tomcat_conf() {
  401. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  402. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  403. server {
  404. $Nginx_conf
  405. server_name $domain$moredomainame;
  406. $N_log
  407. index index.html index.htm index.jsp;
  408. root $vhostdir;
  409. $Nginx_redirect
  410. $anti_hotlinking
  411. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  412. expires 30d;
  413. access_log off;
  414. }
  415. location ~ .*\.(js|css)?$ {
  416. expires 7d;
  417. access_log off;
  418. }
  419. $NGX_CONF
  420. }
  421. $Nginx_http_to_https
  422. EOF
  423. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  424. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  425. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  426. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  427. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  428. </Host>
  429. EOF
  430. [ -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
  431. [ -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
  432. echo
  433. $web_install_dir/sbin/nginx -t
  434. if [ $? == 0 ];then
  435. echo "Reload Nginx......"
  436. $web_install_dir/sbin/nginx -s reload
  437. /etc/init.d/tomcat restart
  438. else
  439. rm -rf $web_install_dir/conf/vhost/$domain.conf
  440. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  441. exit 1
  442. fi
  443. printf "
  444. #######################################################################
  445. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  446. # For more information please visit https://oneinstack.com #
  447. #######################################################################
  448. "
  449. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  450. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  451. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  452. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  453. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  454. }
  455. Create_tomcat_conf() {
  456. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  457. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  458. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  459. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  460. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  461. </Host>
  462. EOF
  463. [ -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
  464. [ -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
  465. echo
  466. /etc/init.d/tomcat restart
  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" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  475. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  476. echo "`printf "%-30s" "index url:"`${CMSG}http://${domain}:8080/${CEND}"
  477. }
  478. Create_nginx_php-fpm_hhvm_conf() {
  479. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  480. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  481. server {
  482. $Nginx_conf
  483. server_name $domain$moredomainame;
  484. $N_log
  485. index index.html index.htm index.php;
  486. include $web_install_dir/conf/rewrite/$rewrite.conf;
  487. root $vhostdir;
  488. $Nginx_redirect
  489. $anti_hotlinking
  490. $NGX_CONF
  491. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  492. expires 30d;
  493. access_log off;
  494. }
  495. location ~ .*\.(js|css)?$ {
  496. expires 7d;
  497. access_log off;
  498. }
  499. }
  500. $Nginx_http_to_https
  501. EOF
  502. echo
  503. $web_install_dir/sbin/nginx -t
  504. if [ $? == 0 ];then
  505. echo "Reload Nginx......"
  506. $web_install_dir/sbin/nginx -s reload
  507. else
  508. rm -rf $web_install_dir/conf/vhost/$domain.conf
  509. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  510. exit 1
  511. fi
  512. printf "
  513. #######################################################################
  514. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  515. # For more information please visit https://oneinstack.com #
  516. #######################################################################
  517. "
  518. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  519. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  520. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  521. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-30s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  522. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  523. }
  524. Apache_log() {
  525. while :; do echo
  526. read -p "Allow Apache access_log? [y/n]: " access_yn
  527. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  528. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  529. else
  530. break
  531. fi
  532. done
  533. if [ "$access_yn" == 'n' ]; then
  534. A_log='CustomLog "/dev/null" common'
  535. else
  536. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  537. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  538. fi
  539. }
  540. Create_apache_conf() {
  541. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  542. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  543. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  544. <VirtualHost *:80>
  545. ServerAdmin admin@linuxeye.com
  546. DocumentRoot "$vhostdir"
  547. ServerName $domain
  548. $Apache_Domain_alias
  549. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  550. $A_log
  551. <Directory "$vhostdir">
  552. SetOutputFilter DEFLATE
  553. Options FollowSymLinks ExecCGI
  554. $R_TMP
  555. AllowOverride All
  556. Order allow,deny
  557. Allow from all
  558. DirectoryIndex index.html index.php
  559. </Directory>
  560. </VirtualHost>
  561. EOF
  562. [ "$apache_ssl_yn" == 'y' ] && cat >> $apache_install_dir/conf/vhost/$domain.conf << EOF
  563. <VirtualHost *:443>
  564. ServerAdmin admin@linuxeye.com
  565. DocumentRoot "$vhostdir"
  566. ServerName $domain
  567. $Apache_Domain_alias
  568. $Apache_SSL
  569. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  570. $A_log
  571. <Directory "$vhostdir">
  572. SetOutputFilter DEFLATE
  573. Options FollowSymLinks ExecCGI
  574. $R_TMP
  575. AllowOverride All
  576. Order allow,deny
  577. Allow from all
  578. DirectoryIndex index.html index.php
  579. </Directory>
  580. </VirtualHost>
  581. EOF
  582. echo
  583. $apache_install_dir/bin/apachectl -t
  584. if [ $? == 0 ];then
  585. echo "Restart Apache......"
  586. /etc/init.d/httpd restart
  587. else
  588. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  589. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  590. exit 1
  591. fi
  592. printf "
  593. #######################################################################
  594. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  595. # For more information please visit https://oneinstack.com #
  596. #######################################################################
  597. "
  598. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  599. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  600. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  601. }
  602. Create_nginx_apache_mod-php_conf() {
  603. # Nginx/Tengine/OpenResty
  604. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  605. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  606. server {
  607. $Nginx_conf
  608. server_name $domain$moredomainame;
  609. $N_log
  610. index index.html index.htm index.php;
  611. root $vhostdir;
  612. $Nginx_redirect
  613. $anti_hotlinking
  614. location / {
  615. try_files \$uri @apache;
  616. }
  617. location @apache {
  618. proxy_pass http://127.0.0.1:88;
  619. include proxy.conf;
  620. }
  621. location ~ .*\.(php|php5|cgi|pl)?$ {
  622. proxy_pass http://127.0.0.1:88;
  623. include proxy.conf;
  624. }
  625. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  626. expires 30d;
  627. access_log off;
  628. }
  629. location ~ .*\.(js|css)?$ {
  630. expires 7d;
  631. access_log off;
  632. }
  633. }
  634. $Nginx_http_to_https
  635. EOF
  636. echo
  637. $web_install_dir/sbin/nginx -t
  638. if [ $? == 0 ];then
  639. echo "Reload Nginx......"
  640. $web_install_dir/sbin/nginx -s reload
  641. else
  642. rm -rf $web_install_dir/conf/vhost/$domain.conf
  643. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  644. fi
  645. # Apache
  646. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  647. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  648. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  649. <VirtualHost *:88>
  650. ServerAdmin admin@linuxeye.com
  651. DocumentRoot "$vhostdir"
  652. ServerName $domain
  653. $Apache_Domain_alias
  654. $Apache_SSL
  655. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  656. $A_log
  657. <Directory "$vhostdir">
  658. SetOutputFilter DEFLATE
  659. Options FollowSymLinks ExecCGI
  660. $R_TMP
  661. AllowOverride All
  662. Order allow,deny
  663. Allow from all
  664. DirectoryIndex index.html index.php
  665. </Directory>
  666. </VirtualHost>
  667. EOF
  668. echo
  669. $apache_install_dir/bin/apachectl -t
  670. if [ $? == 0 ];then
  671. echo "Restart Apache......"
  672. /etc/init.d/httpd restart
  673. else
  674. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  675. exit 1
  676. fi
  677. printf "
  678. #######################################################################
  679. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  680. # For more information please visit https://oneinstack.com #
  681. #######################################################################
  682. "
  683. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  684. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  685. echo "`printf "%-30s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  686. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  687. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  688. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  689. }
  690. Add_Vhost() {
  691. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" ];then
  692. Choose_env
  693. Input_Add_domain
  694. Nginx_anti_hotlinking
  695. if [ "$NGX_FLAG" == 'java' ];then
  696. Nginx_log
  697. Create_nginx_tomcat_conf
  698. else
  699. Nginx_rewrite
  700. Nginx_log
  701. Create_nginx_php-fpm_hhvm_conf
  702. fi
  703. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/conf/httpd.conf" ];then
  704. Choose_env
  705. Input_Add_domain
  706. Apache_log
  707. Create_apache_conf
  708. elif [ ! -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" -a -e "$tomcat_install_dir/conf/server.xml" ];then
  709. Choose_env
  710. Input_Add_domain
  711. Create_tomcat_conf
  712. elif [ -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  713. Choose_env
  714. Input_Add_domain
  715. Nginx_anti_hotlinking
  716. if [ "$NGX_FLAG" == 'java' ];then
  717. Nginx_log
  718. Create_nginx_tomcat_conf
  719. elif [ "$NGX_FLAG" == 'hhvm' ];then
  720. Nginx_rewrite
  721. Nginx_log
  722. Create_nginx_php-fpm_hhvm_conf
  723. elif [ "$NGX_FLAG" == 'php' ];then
  724. #Nginx_rewrite
  725. Nginx_log
  726. Apache_log
  727. Create_nginx_apache_mod-php_conf
  728. fi
  729. fi
  730. }
  731. Del_NGX_Vhost() {
  732. if [ -e "$web_install_dir/sbin/nginx" ];then
  733. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  734. if [ -n "$Domain_List" ];then
  735. echo
  736. echo "Virtualhost list:"
  737. echo ${CMSG}$Domain_List${CEND}
  738. while :; do echo
  739. read -p "Please input a domain you want to delete: " domain
  740. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  741. echo "${CWARNING}input error! ${CEND}"
  742. else
  743. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  744. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  745. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  746. $web_install_dir/sbin/nginx -s reload
  747. while :; do echo
  748. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  749. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  750. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  751. else
  752. break
  753. fi
  754. done
  755. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  756. echo "Press Ctrl+c to cancel or Press any key to continue..."
  757. char=`get_char`
  758. rm -rf $Directory
  759. fi
  760. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  761. else
  762. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  763. fi
  764. break
  765. fi
  766. done
  767. else
  768. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  769. fi
  770. fi
  771. }
  772. Del_Apache_Vhost() {
  773. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  774. if [ -e "$web_install_dir/sbin/nginx" ];then
  775. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  776. /etc/init.d/httpd restart
  777. else
  778. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  779. if [ -n "$Domain_List" ];then
  780. echo
  781. echo "Virtualhost list:"
  782. echo ${CMSG}$Domain_List${CEND}
  783. while :; do echo
  784. read -p "Please input a domain you want to delete: " domain
  785. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  786. echo "${CWARNING}input error! ${CEND}"
  787. else
  788. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  789. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  790. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  791. /etc/init.d/httpd restart
  792. while :; do echo
  793. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  794. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  795. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  796. else
  797. break
  798. fi
  799. done
  800. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  801. echo "Press Ctrl+c to cancel or Press any key to continue..."
  802. char=`get_char`
  803. rm -rf $Directory
  804. fi
  805. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  806. else
  807. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  808. fi
  809. break
  810. fi
  811. done
  812. else
  813. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  814. fi
  815. fi
  816. fi
  817. }
  818. Del_Tomcat_Vhost() {
  819. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  820. if [ -e "$web_install_dir/sbin/nginx" ];then
  821. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  822. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  823. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  824. /etc/init.d/tomcat restart
  825. fi
  826. else
  827. Domain_List=`ls $tomcat_install_dir/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g"`
  828. if [ -n "$Domain_List" ];then
  829. echo
  830. echo "Virtualhost list:"
  831. echo ${CMSG}$Domain_List${CEND}
  832. while :; do echo
  833. read -p "Please input a domain you want to delete: " domain
  834. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  835. echo "${CWARNING}input error! ${CEND}"
  836. else
  837. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  838. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  839. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  840. /etc/init.d/tomcat restart
  841. while :; do echo
  842. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  843. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  844. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  845. else
  846. break
  847. fi
  848. done
  849. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  850. echo "Press Ctrl+c to cancel or Press any key to continue..."
  851. char=`get_char`
  852. rm -rf $Directory
  853. fi
  854. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  855. else
  856. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  857. fi
  858. break
  859. fi
  860. done
  861. else
  862. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  863. fi
  864. fi
  865. fi
  866. }
  867. if [ $# == 0 ];then
  868. Add_Vhost
  869. elif [ $# == 1 ];then
  870. case $1 in
  871. add)
  872. Add_Vhost
  873. ;;
  874. del)
  875. Del_NGX_Vhost
  876. Del_Apache_Vhost
  877. Del_Tomcat_Vhost
  878. ;;
  879. *)
  880. Usage
  881. ;;
  882. esac
  883. else
  884. Usage
  885. fi