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