vhost.sh 37 KB

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