vhost.sh 37 KB

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