vhost.sh 38 KB

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