vhost.sh 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 --agree-tos --renew-by-default --webroot -w ${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. #error_page 404 = /404.html;
  425. #error_page 502 = /502.html;
  426. ${Nginx_redirect}
  427. ${anti_hotlinking}
  428. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  429. expires 30d;
  430. access_log off;
  431. }
  432. location ~ .*\.(js|css)?$ {
  433. expires 7d;
  434. access_log off;
  435. }
  436. location ~ /\.ht {
  437. deny all;
  438. }
  439. ${NGX_CONF}
  440. }
  441. EOF
  442. [ "${https_yn}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  443. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  444. <Host name="${domain}" appBase="webapps" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  445. <Context path="" docBase="${vhostdir}" debug="0" reloadable="false" crossContext="true"/>
  446. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  447. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  448. </Host>
  449. EOF
  450. [ -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
  451. [ -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
  452. echo
  453. ${web_install_dir}/sbin/nginx -t
  454. if [ $? == 0 ]; then
  455. echo "Reload Nginx......"
  456. ${web_install_dir}/sbin/nginx -s reload
  457. /etc/init.d/tomcat restart
  458. else
  459. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  460. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  461. exit 1
  462. fi
  463. printf "
  464. #######################################################################
  465. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  466. # For more information please visit https://oneinstack.com #
  467. #######################################################################
  468. "
  469. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  470. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  471. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  472. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  473. [ "${nginx_ssl_yn}" == 'y' ] && Print_ssl
  474. }
  475. Create_tomcat_conf() {
  476. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  477. <Host name="${domain}" appBase="webapps" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  478. <Context path="" docBase="${vhostdir}" debug="0" reloadable="false" crossContext="true"/>
  479. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  480. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  481. </Host>
  482. EOF
  483. [ -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
  484. [ -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
  485. echo
  486. /etc/init.d/tomcat restart
  487. printf "
  488. #######################################################################
  489. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  490. # For more information please visit https://oneinstack.com #
  491. #######################################################################
  492. "
  493. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  494. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  495. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  496. echo "$(printf "%-30s" "index url:")${CMSG}http://${domain}:8080/${CEND}"
  497. }
  498. Create_nginx_php-fpm_hhvm_conf() {
  499. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  500. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  501. server {
  502. ${Nginx_conf}
  503. server_name ${domain}${moredomainame};
  504. ${N_log}
  505. index index.html index.htm index.php;
  506. include ${web_install_dir}/conf/rewrite/${rewrite}.conf;
  507. root ${vhostdir};
  508. #error_page 404 = /404.html;
  509. #error_page 502 = /502.html;
  510. ${Nginx_redirect}
  511. ${anti_hotlinking}
  512. ${NGX_CONF}
  513. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  514. expires 30d;
  515. access_log off;
  516. }
  517. location ~ .*\.(js|css)?$ {
  518. expires 7d;
  519. access_log off;
  520. }
  521. location ~ /\.ht {
  522. deny all;
  523. }
  524. }
  525. EOF
  526. [ "${https_yn}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  527. echo
  528. ${web_install_dir}/sbin/nginx -t
  529. if [ $? == 0 ]; then
  530. echo "Reload Nginx......"
  531. ${web_install_dir}/sbin/nginx -s reload
  532. else
  533. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  534. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  535. exit 1
  536. fi
  537. printf "
  538. #######################################################################
  539. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  540. # For more information please visit https://oneinstack.com #
  541. #######################################################################
  542. "
  543. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  544. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  545. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  546. [ "${rewrite_yn}" == 'y' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  547. [ "${nginx_ssl_yn}" == 'y' ] && Print_ssl
  548. }
  549. Apache_log() {
  550. while :; do echo
  551. read -p "Allow Apache access_log? [y/n]: " access_yn
  552. if [[ ! "${access_yn}" =~ ^[y,n]$ ]]; then
  553. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  554. else
  555. break
  556. fi
  557. done
  558. if [ "${access_yn}" == 'n' ]; then
  559. A_log='CustomLog "/dev/null" common'
  560. else
  561. A_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  562. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  563. fi
  564. }
  565. Create_apache_conf() {
  566. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP='Require all granted' || R_TMP=
  567. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  568. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  569. <VirtualHost *:80>
  570. ServerAdmin admin@example.com
  571. DocumentRoot "${vhostdir}"
  572. ServerName ${domain}
  573. ${Apache_Domain_alias}
  574. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  575. ${A_log}
  576. <Directory "${vhostdir}">
  577. SetOutputFilter DEFLATE
  578. Options FollowSymLinks ExecCGI
  579. ${R_TMP}
  580. AllowOverride All
  581. Order allow,deny
  582. Allow from all
  583. DirectoryIndex index.html index.php
  584. </Directory>
  585. </VirtualHost>
  586. EOF
  587. [ "$apache_ssl_yn" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  588. <VirtualHost *:443>
  589. ServerAdmin admin@example.com
  590. DocumentRoot "${vhostdir}"
  591. ServerName ${domain}
  592. ${Apache_Domain_alias}
  593. ${Apache_SSL}
  594. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  595. ${A_log}
  596. <Directory "${vhostdir}">
  597. SetOutputFilter DEFLATE
  598. Options FollowSymLinks ExecCGI
  599. ${R_TMP}
  600. AllowOverride All
  601. Order allow,deny
  602. Allow from all
  603. DirectoryIndex index.html index.php
  604. </Directory>
  605. </VirtualHost>
  606. EOF
  607. echo
  608. ${apache_install_dir}/bin/apachectl -t
  609. if [ $? == 0 ]; then
  610. echo "Restart Apache......"
  611. /etc/init.d/httpd restart
  612. else
  613. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  614. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  615. exit 1
  616. fi
  617. printf "
  618. #######################################################################
  619. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  620. # For more information please visit https://oneinstack.com #
  621. #######################################################################
  622. "
  623. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  624. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  625. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  626. [ "${apache_ssl_yn}" == 'y' ] && Print_ssl
  627. }
  628. Create_nginx_apache_mod-php_conf() {
  629. # Nginx/Tengine/OpenResty
  630. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  631. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  632. server {
  633. ${Nginx_conf}
  634. server_name ${domain}${moredomainame};
  635. ${N_log}
  636. index index.html index.htm index.php;
  637. root ${vhostdir};
  638. ${Nginx_redirect}
  639. ${anti_hotlinking}
  640. location / {
  641. try_files \$uri @apache;
  642. }
  643. location @apache {
  644. proxy_pass http://127.0.0.1:88;
  645. include proxy.conf;
  646. }
  647. location ~ .*\.(php|php5|cgi|pl)?$ {
  648. proxy_pass http://127.0.0.1:88;
  649. include proxy.conf;
  650. }
  651. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  652. expires 30d;
  653. access_log off;
  654. }
  655. location ~ .*\.(js|css)?$ {
  656. expires 7d;
  657. access_log off;
  658. }
  659. location ~ /\.ht {
  660. deny all;
  661. }
  662. }
  663. EOF
  664. [ "${https_yn}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  665. echo
  666. ${web_install_dir}/sbin/nginx -t
  667. if [ $? == 0 ]; then
  668. echo "Reload Nginx......"
  669. ${web_install_dir}/sbin/nginx -s reload
  670. else
  671. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  672. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  673. fi
  674. # Apache
  675. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP="Require all granted" || R_TMP=
  676. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  677. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  678. <VirtualHost *:88>
  679. ServerAdmin admin@example.com
  680. DocumentRoot "${vhostdir}"
  681. ServerName ${domain}
  682. ${Apache_Domain_alias}
  683. ${Apache_SSL}
  684. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  685. ${A_log}
  686. <Directory "${vhostdir}">
  687. SetOutputFilter DEFLATE
  688. Options FollowSymLinks ExecCGI
  689. ${R_TMP}
  690. AllowOverride All
  691. Order allow,deny
  692. Allow from all
  693. DirectoryIndex index.html index.php
  694. </Directory>
  695. </VirtualHost>
  696. EOF
  697. echo
  698. ${apache_install_dir}/bin/apachectl -t
  699. if [ $? == 0 ]; then
  700. echo "Restart Apache......"
  701. /etc/init.d/httpd restart
  702. else
  703. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  704. exit 1
  705. fi
  706. printf "
  707. #######################################################################
  708. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  709. # For more information please visit https://oneinstack.com #
  710. #######################################################################
  711. "
  712. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  713. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  714. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  715. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  716. [ "${rewrite_yn}" == 'y' ] && echo "$(printf "%-28s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  717. [ "${nginx_ssl_yn}" == 'y' ] && Print_ssl
  718. }
  719. Add_Vhost() {
  720. if [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
  721. Choose_env
  722. Input_Add_domain
  723. Nginx_anti_hotlinking
  724. if [ "${NGX_FLAG}" == "java" ]; then
  725. Nginx_log
  726. Create_nginx_tomcat_conf
  727. else
  728. Nginx_rewrite
  729. Nginx_log
  730. Create_nginx_php-fpm_hhvm_conf
  731. fi
  732. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  733. Choose_env
  734. Input_Add_domain
  735. Apache_log
  736. Create_apache_conf
  737. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  738. Choose_env
  739. Input_Add_domain
  740. Create_tomcat_conf
  741. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "$(ls ${apache_install_dir}/modules/libphp?.so 2>/dev/null)" ]; then
  742. Choose_env
  743. Input_Add_domain
  744. Nginx_anti_hotlinking
  745. if [ "${NGX_FLAG}" == "java" ]; then
  746. Nginx_log
  747. Create_nginx_tomcat_conf
  748. elif [ "${NGX_FLAG}" == "hhvm" ]; then
  749. Nginx_rewrite
  750. Nginx_log
  751. Create_nginx_php-fpm_hhvm_conf
  752. elif [ "${NGX_FLAG}" == "php" ]; then
  753. #Nginx_rewrite
  754. Nginx_log
  755. Apache_log
  756. Create_nginx_apache_mod-php_conf
  757. fi
  758. else
  759. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  760. fi
  761. }
  762. Del_NGX_Vhost() {
  763. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  764. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  765. if [ -n "${Domain_List}" ]; then
  766. echo
  767. echo "Virtualhost list:"
  768. echo ${CMSG}${Domain_List}${CEND}
  769. while :; do echo
  770. read -p "Please input a domain you want to delete: " domain
  771. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  772. echo "${CWARNING}input error! ${CEND}"
  773. else
  774. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  775. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  776. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  777. ${web_install_dir}/sbin/nginx -s reload
  778. while :; do echo
  779. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  780. if [[ ! ${Del_Vhost_wwwroot_yn} =~ ^[y,n]$ ]]; then
  781. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  782. else
  783. break
  784. fi
  785. done
  786. if [ "${Del_Vhost_wwwroot_yn}" == 'y' ]; then
  787. echo "Press Ctrl+c to cancel or Press any key to continue..."
  788. char=$(get_char)
  789. rm -rf ${Directory}
  790. fi
  791. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  792. else
  793. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  794. fi
  795. break
  796. fi
  797. done
  798. else
  799. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  800. fi
  801. fi
  802. }
  803. Del_Apache_Vhost() {
  804. if [ -e "${apache_install_dir}/conf/httpd.conf" ]; then
  805. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  806. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  807. /etc/init.d/httpd restart
  808. else
  809. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  810. if [ -n "${Domain_List}" ]; then
  811. echo
  812. echo "Virtualhost list:"
  813. echo ${CMSG}${Domain_List}${CEND}
  814. while :; do echo
  815. read -p "Please input a domain you want to delete: " domain
  816. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  817. echo "${CWARNING}input error! ${CEND}"
  818. else
  819. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  820. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  821. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  822. /etc/init.d/httpd restart
  823. while :; do echo
  824. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  825. if [[ ! ${Del_Vhost_wwwroot_yn} =~ ^[y,n]$ ]]; then
  826. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  827. else
  828. break
  829. fi
  830. done
  831. if [ "${Del_Vhost_wwwroot_yn}" == 'y' ]; then
  832. echo "Press Ctrl+c to cancel or Press any key to continue..."
  833. char=$(get_char)
  834. rm -rf ${Directory}
  835. fi
  836. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  837. else
  838. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  839. fi
  840. break
  841. fi
  842. done
  843. else
  844. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  845. fi
  846. fi
  847. fi
  848. }
  849. Del_Tomcat_Vhost() {
  850. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  851. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  852. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  853. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  854. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  855. /etc/init.d/tomcat restart
  856. fi
  857. else
  858. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  859. if [ -n "${Domain_List}" ]; then
  860. echo
  861. echo "Virtualhost list:"
  862. echo ${CMSG}${Domain_List}${CEND}
  863. while :; do echo
  864. read -p "Please input a domain you want to delete: " domain
  865. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  866. echo "${CWARNING}input error! ${CEND}"
  867. else
  868. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  869. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  870. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  871. /etc/init.d/tomcat restart
  872. while :; do echo
  873. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  874. if [[ ! ${Del_Vhost_wwwroot_yn} =~ ^[y,n]$ ]]; then
  875. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  876. else
  877. break
  878. fi
  879. done
  880. if [ "${Del_Vhost_wwwroot_yn}" == 'y' ]; then
  881. echo "Press Ctrl+c to cancel or Press any key to continue..."
  882. char=$(get_char)
  883. rm -rf ${Directory}
  884. fi
  885. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  886. else
  887. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  888. fi
  889. break
  890. fi
  891. done
  892. else
  893. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  894. fi
  895. fi
  896. fi
  897. }
  898. if [ $# == 0 ]; then
  899. Add_Vhost
  900. elif [ $# == 1 ]; then
  901. case $1 in
  902. add)
  903. Add_Vhost
  904. ;;
  905. del)
  906. Del_NGX_Vhost
  907. Del_Apache_Vhost
  908. Del_Tomcat_Vhost
  909. ;;
  910. *)
  911. Usage
  912. ;;
  913. esac
  914. else
  915. Usage
  916. fi