vhost.sh 38 KB

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