vhost.sh 36 KB

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