vhost.sh 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+ #
  15. # For more information please visit https://oneinstack.com #
  16. #######################################################################
  17. "
  18. # Check if user is root
  19. [ $(id -u) != '0' ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  20. oneinstack_dir=$(dirname "`readlink -f $0`")
  21. pushd ${oneinstack_dir} > /dev/null
  22. . ./options.conf
  23. . ./include/color.sh
  24. . ./include/check_dir.sh
  25. . ./include/check_os.sh
  26. . ./include/get_char.sh
  27. Show_Help() {
  28. echo
  29. echo "Usage: $0 command ...[parameters]....
  30. --help, -h Show this help message
  31. --quiet, -q quiet operation
  32. --list, -l List Virtualhost
  33. --mphp_ver [53~73] Use another PHP version (PATH: /usr/local/php${mphp_ver})
  34. --add Add Virtualhost
  35. --delete, --del Delete Virtualhost
  36. --httponly Use HTTP Only
  37. --selfsigned Use your own SSL Certificate and Key
  38. --letsencrypt Use Let's Encrypt to Create SSL Certificate and Key
  39. --dnsapi Use dns API to automatically issue Let's Encrypt Cert
  40. "
  41. }
  42. ARG_NUM=$#
  43. TEMP=`getopt -o hql --long help,quiet,list,mphp_ver:,add,delete,del,httponly,selfsigned,letsencrypt,dnsapi -- "$@" 2>/dev/null`
  44. [ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
  45. eval set -- "${TEMP}"
  46. while :; do
  47. [ -z "$1" ] && break;
  48. case "$1" in
  49. -h|--help)
  50. Show_Help; exit 0
  51. ;;
  52. -q|--quiet)
  53. quiet_flag=y; shift 1
  54. ;;
  55. -l|--list)
  56. list_flag=y; shift 1
  57. ;;
  58. --mphp_ver)
  59. mphp_ver=$2; mphp_flag=y; shift 2
  60. [[ ! "${mphp_ver}" =~ ^5[3-6]$|^7[0-3]$ ]] && { echo "${CWARNING}mphp_ver input error! Please only input number 53~73${CEND}"; unset mphp_ver mphp_flag; }
  61. ;;
  62. --add)
  63. add_flag=y; shift 1
  64. ;;
  65. --delete|--del)
  66. delete_flag=y; shift 1
  67. ;;
  68. --httponly)
  69. sslquiet_flag=y
  70. httponly_flag=y
  71. Domian_Mode=1
  72. shift 1
  73. ;;
  74. --selfsigned)
  75. sslquiet_flag=y
  76. selfsigned_flag=y
  77. Domian_Mode=2
  78. shift 1
  79. ;;
  80. --letsencrypt)
  81. sslquiet_flag=y
  82. letsencrypt_flag=y
  83. Domian_Mode=3
  84. shift 1
  85. ;;
  86. --dnsapi)
  87. sslquiet_flag=y
  88. dnsapi_flag=y
  89. letsencrypt_flag=y
  90. shift 1
  91. ;;
  92. --)
  93. shift
  94. ;;
  95. *)
  96. echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
  97. ;;
  98. esac
  99. done
  100. Choose_ENV() {
  101. if [ -e "${apache_install_dir}/bin/apachectl" ];then
  102. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && { Apache_main_ver=24; Apache_grant='Require all granted'; }
  103. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '2' ] && Apache_main_ver=22
  104. fi
  105. if [ -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  106. Number=111
  107. while :; do echo
  108. echo "Please choose to use environment:"
  109. echo -e "\t${CMSG}1${CEND}. Use php"
  110. echo -e "\t${CMSG}2${CEND}. Use java"
  111. echo -e "\t${CMSG}3${CEND}. Use hhvm"
  112. read -e -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  113. ENV_FLAG=${ENV_FLAG:-1}
  114. if [[ ! ${ENV_FLAG} =~ ^[1-3]$ ]]; then
  115. echo "${CWARNING}input error! Please only input number 1~3${CEND}"
  116. else
  117. break
  118. fi
  119. done
  120. case "${ENV_FLAG}" in
  121. 1)
  122. NGX_FLAG=php
  123. ;;
  124. 2)
  125. NGX_FLAG=java
  126. ;;
  127. 3)
  128. NGX_FLAG=hhvm
  129. ;;
  130. esac
  131. elif [ -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a ! -e "/usr/bin/hhvm" ]; then
  132. Number=110
  133. while :; do echo
  134. echo "Please choose to use environment:"
  135. echo -e "\t${CMSG}1${CEND}. Use php"
  136. echo -e "\t${CMSG}2${CEND}. Use java"
  137. read -e -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  138. ENV_FLAG=${ENV_FLAG:-1}
  139. if [[ ! ${ENV_FLAG} =~ ^[1-2]$ ]]; then
  140. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  141. else
  142. break
  143. fi
  144. done
  145. [ "${ENV_FLAG}" == '1' ] && NGX_FLAG=php
  146. [ "${ENV_FLAG}" == '2' ] && NGX_FLAG=java
  147. elif [ -e "${php_install_dir}/bin/phpize" -a ! -e "${tomcat_install_dir}/conf/server.xml" -a ! -e "/usr/bin/hhvm" ]; then
  148. Number=100
  149. NGX_FLAG=php
  150. elif [ -e "${php_install_dir}/bin/phpize" -a ! -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  151. Number=101
  152. while :; do echo
  153. echo "Please choose to use environment:"
  154. echo -e "\t${CMSG}1${CEND}. Use php"
  155. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  156. read -e -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  157. ENV_FLAG=${ENV_FLAG:-1}
  158. if [[ ! ${ENV_FLAG} =~ ^[1-2]$ ]]; then
  159. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  160. else
  161. break
  162. fi
  163. done
  164. [ "${ENV_FLAG}" == '1' ] && NGX_FLAG=php
  165. [ "${ENV_FLAG}" == '2' ] && NGX_FLAG=hhvm
  166. elif [ ! -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  167. Number=011
  168. while :; do echo
  169. echo "Please choose to use environment:"
  170. echo -e "\t${CMSG}1${CEND}. Use java"
  171. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  172. read -e -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  173. ENV_FLAG=${ENV_FLAG:-1}
  174. if [[ ! ${ENV_FLAG} =~ ^[1-2]$ ]]; then
  175. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  176. else
  177. break
  178. fi
  179. done
  180. [ "${ENV_FLAG}" == '1' ] && NGX_FLAG=java
  181. [ "${ENV_FLAG}" == '2' ] && NGX_FLAG=hhvm
  182. elif [ ! -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a ! -e "/usr/bin/hhvm" ]; then
  183. Number=010
  184. NGX_FLAG=java
  185. elif [ ! -e "${php_install_dir}/bin/phpize" -a ! -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  186. Number=001
  187. NGX_FLAG=hhvm
  188. else
  189. Number=000
  190. NGX_FLAG=php
  191. fi
  192. }
  193. Create_SSL() {
  194. if [ "${Domian_Mode}" == '2' ]; then
  195. printf "
  196. You are about to be asked to enter information that will be incorporated
  197. into your certificate request.
  198. What you are about to enter is what is called a Distinguished Name or a DN.
  199. There are quite a few fields but you can leave some blank
  200. For some fields there will be a default value,
  201. If you enter '.', the field will be left blank.
  202. "
  203. echo
  204. read -e -p "Country Name (2 letter code) [CN]: " SELFSIGNEDSSL_C
  205. SELFSIGNEDSSL_C=${SELFSIGNEDSSL_C:-CN}
  206. # shellcheck disable=SC2104
  207. [ ${#SELFSIGNEDSSL_C} != 2 ] && { echo "${CWARNING}input error, You must input 2 letter code country name${CEND}"; continue; }
  208. echo
  209. read -e -p "State or Province Name (full name) [Shanghai]: " SELFSIGNEDSSL_ST
  210. SELFSIGNEDSSL_ST=${SELFSIGNEDSSL_ST:-Shanghai}
  211. echo
  212. read -e -p "Locality Name (eg, city) [Shanghai]: " SELFSIGNEDSSL_L
  213. SELFSIGNEDSSL_L=${SELFSIGNEDSSL_L:-Shanghai}
  214. echo
  215. read -e -p "Organization Name (eg, company) [Example Inc.]: " SELFSIGNEDSSL_O
  216. SELFSIGNEDSSL_O=${SELFSIGNEDSSL_O:-"Example Inc."}
  217. echo
  218. read -e -p "Organizational Unit Name (eg, section) [IT Dept.]: " SELFSIGNEDSSL_OU
  219. SELFSIGNEDSSL_OU=${SELFSIGNEDSSL_OU:-"IT Dept."}
  220. openssl req -utf8 -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
  221. 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
  222. elif [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ]; then
  223. if [ "${moredomain}" == "*.${domain}" -o "${dnsapi_flag}" == 'y' ]; then
  224. while :; do echo
  225. echo 'Please select DNS provider:'
  226. echo "${CMSG}dp${CEND},${CMSG}cx${CEND},${CMSG}ali${CEND},${CMSG}cf${CEND},${CMSG}aws${CEND},${CMSG}linode${CEND},${CMSG}he${CEND},${CMSG}namesilo${CEND},${CMSG}dgon${CEND},${CMSG}freedns${CEND},${CMSG}gd${CEND},${CMSG}namecom${CEND} and so on."
  227. echo "${CMSG}More: https://oneinstack.com/faq/letsencrypt${CEND}"
  228. read -e -p "Please enter your DNS provider: " DNS_PRO
  229. if [ -e ~/.acme.sh/dnsapi/dns_${DNS_PRO}.sh ]; then
  230. break
  231. else
  232. echo "${CWARNING}You DNS api mode is not supported${CEND}"
  233. fi
  234. done
  235. while :; do echo
  236. echo "Syntax: export Key1=Value1 ; export Key2=Value1"
  237. read -e -p "Please enter your dnsapi parameters: " DNS_PAR
  238. echo
  239. eval ${DNS_PAR}
  240. if [ $? == 0 ]; then
  241. break
  242. else
  243. echo "${CWARNING}Syntax error! PS: export Ali_Key=LTq ; export Ali_Secret=0q5E${CEND}"
  244. fi
  245. done
  246. [ "${moredomainame_flag}" == 'y' ] && moredomainame_D="$(for D in ${moredomainame}; do echo -d ${D}; done)"
  247. ~/.acme.sh/acme.sh --force --listen-v4 --issue --dns dns_${DNS_PRO} -d ${domain} ${moredomainame_D}
  248. else
  249. if [ "${nginx_ssl_flag}" == 'y' ]; then
  250. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  251. echo "server { server_name ${domain}${moredomainame}; root ${vhostdir}; access_log off; }" > ${web_install_dir}/conf/vhost/${domain}.conf
  252. ${web_install_dir}/sbin/nginx -s reload
  253. fi
  254. if [ "${apache_ssl_flag}" == 'y' ]; then
  255. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  256. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  257. <VirtualHost *:80>
  258. ServerAdmin admin@example.com
  259. DocumentRoot "${vhostdir}"
  260. ServerName ${domain}
  261. ${Apache_Domain_alias}
  262. <Directory "${vhostdir}">
  263. SetOutputFilter DEFLATE
  264. Options FollowSymLinks ExecCGI
  265. ${Apache_grant}
  266. AllowOverride All
  267. Order allow,deny
  268. Allow from all
  269. DirectoryIndex index.html index.php
  270. </Directory>
  271. </VirtualHost>
  272. EOF
  273. ${apache_install_dir}/bin/apachectl -k graceful
  274. fi
  275. auth_file="`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`".html
  276. auth_str='oneinstack'; echo ${auth_str} > ${vhostdir}/${auth_file}
  277. for D in ${domain} ${moredomainame}
  278. do
  279. curl_str=`curl --connect-timeout 30 -4 -s $D/${auth_file} 2>&1`
  280. [ "${curl_str}" != "${auth_str}" ] && { echo; echo "${CFAILURE}Let's Encrypt Verify error! DNS problem: NXDOMAIN looking up A for ${D}${CEND}"; }
  281. done
  282. rm -f ${vhostdir}/${auth_file}
  283. [ "${moredomainame_flag}" == 'y' ] && moredomainame_D="$(for D in ${moredomainame}; do echo -d ${D}; done)"
  284. ~/.acme.sh/acme.sh --force --listen-v4 --issue -d ${domain} ${moredomainame_D} -w ${vhostdir}
  285. fi
  286. if [ -s ~/.acme.sh/${domain}/fullchain.cer ]; then
  287. [ -e "${PATH_SSL}/${domain}.crt" ] && rm -f ${PATH_SSL}/${domain}.{crt,key}
  288. [ -e /bin/systemctl -a -e /lib/systemd/system/nginx.service ] && Nginx_cmd='/bin/systemctl restart nginx' || Nginx_cmd='/etc/init.d/nginx force-reload'
  289. Apache_cmd="${apache_install_dir}/bin/apachectl -k graceful"
  290. if [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  291. Command="${Nginx_cmd};${Apache_cmd}"
  292. elif [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" ]; then
  293. Command="${Nginx_cmd}"
  294. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  295. Command="${Apache_cmd}"
  296. fi
  297. ~/.acme.sh/acme.sh --force --install-cert -d ${domain} --fullchain-file ${PATH_SSL}/${domain}.crt --key-file ${PATH_SSL}/${domain}.key --reloadcmd "${Command}" > /dev/null
  298. else
  299. echo "${CFAILURE}Error: Create Let's Encrypt SSL Certificate failed! ${CEND}"
  300. [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  301. [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  302. exit 1
  303. fi
  304. fi
  305. }
  306. Print_SSL() {
  307. if [ "${Domian_Mode}" == '2' ]; then
  308. echo "$(printf "%-30s" "Self-signed SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  309. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  310. echo "$(printf "%-30s" "SSL CSR File:")${CMSG}${PATH_SSL}/${domain}.csr${CEND}"
  311. elif [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ]; then
  312. echo "$(printf "%-30s" "Let's Encrypt SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  313. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  314. fi
  315. }
  316. Input_Add_domain() {
  317. if [ "${sslquiet_flag}" != 'y' ]; then
  318. while :;do
  319. printf "
  320. What Are You Doing?
  321. \t${CMSG}1${CEND}. Use HTTP Only
  322. \t${CMSG}2${CEND}. Use your own SSL Certificate and Key
  323. \t${CMSG}3${CEND}. Use Let's Encrypt to Create SSL Certificate and Key
  324. \t${CMSG}q${CEND}. Exit
  325. "
  326. read -e -p "Please input the correct option: " Domian_Mode
  327. if [[ ! "${Domian_Mode}" =~ ^[1-3,q]$ ]]; then
  328. echo "${CFAILURE}input error! Please only input 1~3 and q${CEND}"
  329. else
  330. break
  331. fi
  332. done
  333. fi
  334. #Multiple_PHP
  335. if [ $(ls /dev/shm/php*-cgi.sock 2> /dev/null | wc -l) -ge 2 ]; then
  336. if [ "${mphp_flag}" != 'y' ]; then
  337. PHP_detail_ver=`${php_install_dir}/bin/php-config --version`
  338. PHP_main_ver=${PHP_detail_ver%.*}
  339. while :; do echo
  340. echo 'Please select a version of the PHP:'
  341. echo -e "\t${CMSG} 1${CEND}. PHP ${PHP_main_ver} (default)"
  342. [ -e "/dev/shm/php53-cgi.sock" ] && echo -e "\t${CMSG} 2${CEND}. PHP 5.3"
  343. [ -e "/dev/shm/php54-cgi.sock" ] && echo -e "\t${CMSG} 3${CEND}. PHP 5.4"
  344. [ -e "/dev/shm/php55-cgi.sock" ] && echo -e "\t${CMSG} 4${CEND}. PHP 5.5"
  345. [ -e "/dev/shm/php56-cgi.sock" ] && echo -e "\t${CMSG} 5${CEND}. PHP 5.6"
  346. [ -e "/dev/shm/php70-cgi.sock" ] && echo -e "\t${CMSG} 6${CEND}. PHP 7.0"
  347. [ -e "/dev/shm/php71-cgi.sock" ] && echo -e "\t${CMSG} 7${CEND}. PHP 7.1"
  348. [ -e "/dev/shm/php72-cgi.sock" ] && echo -e "\t${CMSG} 8${CEND}. PHP 7.2"
  349. [ -e "/dev/shm/php73-cgi.sock" ] && echo -e "\t${CMSG} 9${CEND}. PHP 7.3"
  350. [ -e "/dev/shm/php74-cgi.sock" ] && echo -e "\t${CMSG}10${CEND}. PHP 7.4"
  351. read -e -p "Please input a number:(Default 1 press Enter) " php_option
  352. php_option=${php_option:-1}
  353. if [[ ! ${php_option} =~ ^[1-9]$|^10$ ]]; then
  354. echo "${CWARNING}input error! Please only input number 1~10${CEND}"
  355. else
  356. break
  357. fi
  358. done
  359. fi
  360. [ "${php_option}" == '2' ] && mphp_ver=53
  361. [ "${php_option}" == '3' ] && mphp_ver=54
  362. [ "${php_option}" == '4' ] && mphp_ver=55
  363. [ "${php_option}" == '5' ] && mphp_ver=56
  364. [ "${php_option}" == '6' ] && mphp_ver=70
  365. [ "${php_option}" == '7' ] && mphp_ver=71
  366. [ "${php_option}" == '8' ] && mphp_ver=72
  367. [ "${php_option}" == '9' ] && mphp_ver=73
  368. [ "${php_option}" == '10' ] && mphp_ver=74
  369. [ ! -e "/dev/shm/php${mphp_ver}-cgi.sock" ] && unset mphp_ver
  370. fi
  371. case "${NGX_FLAG}" in
  372. "php")
  373. NGX_CONF=$(echo -e "location ~ [^/]\.php(/|$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  374. ;;
  375. "java")
  376. NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
  377. ;;
  378. "hhvm")
  379. 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 }")
  380. ;;
  381. esac
  382. if [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ] && [ ! -e ~/.acme.sh/acme.sh ]; then
  383. pushd ${oneinstack_dir}/src > /dev/null
  384. [ ! -e acme.sh-master.tar.gz ] && wget -qc http://mirrors.linuxeye.com/oneinstack/src/acme.sh-master.tar.gz
  385. tar xzf acme.sh-master.tar.gz
  386. pushd acme.sh-master > /dev/null
  387. ./acme.sh --install > /dev/null 2>&1
  388. popd > /dev/null
  389. popd > /dev/null
  390. fi
  391. [ -e ~/.acme.sh/account.conf ] && sed -i '/^CERT_HOME=/d' ~/.acme.sh/account.conf
  392. if [[ "${Domian_Mode}" =~ ^[2-3]$ ]] || [ "${dnsapi_flag}" == 'y' ]; then
  393. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  394. nginx_ssl_flag=y
  395. PATH_SSL=${web_install_dir}/conf/ssl
  396. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL}
  397. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  398. apache_ssl_flag=y
  399. PATH_SSL=${apache_install_dir}/conf/ssl
  400. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL}
  401. fi
  402. elif [ "${Domian_Mode}" == 'q' ]; then
  403. exit 1
  404. fi
  405. while :; do echo
  406. read -e -p "Please input domain(example: www.example.com): " domain
  407. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  408. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  409. else
  410. break
  411. fi
  412. done
  413. 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
  414. [ -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"
  415. [ -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"
  416. [ -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"
  417. exit
  418. else
  419. echo "domain=${domain}"
  420. fi
  421. while :; do echo
  422. echo "Please input the directory for the domain:${domain} :"
  423. read -e -p "(Default directory: ${wwwroot_dir}/${domain}): " vhostdir
  424. if [ -n "${vhostdir}" -a -z "$(echo ${vhostdir} | grep '^/')" ]; then
  425. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  426. else
  427. if [ -z "${vhostdir}" ]; then
  428. vhostdir="${wwwroot_dir}/${domain}"
  429. echo "Virtual Host Directory=${CMSG}${vhostdir}${CEND}"
  430. fi
  431. echo
  432. echo "Create Virtul Host directory......"
  433. mkdir -p ${vhostdir}
  434. echo "set permissions of Virtual Host directory......"
  435. chown -R ${run_user}.${run_group} ${vhostdir}
  436. break
  437. fi
  438. done
  439. while :; do echo
  440. read -e -p "Do you want to add more domain name? [y/n]: " moredomainame_flag
  441. if [[ ! ${moredomainame_flag} =~ ^[y,n]$ ]]; then
  442. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  443. else
  444. break
  445. fi
  446. done
  447. if [ "${moredomainame_flag}" == 'y' ]; then
  448. while :; do echo
  449. read -e -p "Type domainname or IP(example: example.com other.example.com): " moredomain
  450. if [ -z "$(echo ${moredomain} | grep '.*\..*')" ]; then
  451. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  452. else
  453. [ "${moredomain}" == "${domain}" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  454. echo domain list="$moredomain"
  455. moredomainame=" $moredomain"
  456. break
  457. fi
  458. done
  459. Apache_Domain_alias=ServerAlias${moredomainame}
  460. Tomcat_Domain_alias=$(for D in $(echo ${moredomainame}); do echo "<Alias>${D}</Alias>"; done)
  461. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  462. while :; do echo
  463. read -e -p "Do you want to redirect from ${moredomain} to ${domain}? [y/n]: " redirect_flag
  464. if [[ ! ${redirect_flag} =~ ^[y,n]$ ]]; then
  465. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  466. else
  467. break
  468. fi
  469. done
  470. [ "${redirect_flag}" == 'y' ] && Nginx_redirect="if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }"
  471. fi
  472. fi
  473. if [ "${nginx_ssl_flag}" == 'y' ]; then
  474. while :; do echo
  475. read -e -p "Do you want to redirect all HTTP requests to HTTPS? [y/n]: " https_flag
  476. if [[ ! ${https_flag} =~ ^[y,n]$ ]]; then
  477. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  478. else
  479. break
  480. fi
  481. done
  482. if [[ "$(${web_install_dir}/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  483. LISTENOPT="443 ssl http2"
  484. else
  485. LISTENOPT="443 ssl spdy"
  486. fi
  487. Create_SSL
  488. if [ -n "`ifconfig | grep inet6`" ]; then
  489. Nginx_conf=$(echo -e "listen 80;\n listen [::]:80;\n listen ${LISTENOPT};\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 TLSv1.3;\n ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256: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")
  490. else
  491. 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 TLSv1.3;\n ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256: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")
  492. fi
  493. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  494. elif [ "$apache_ssl_flag" == 'y' ]; then
  495. Create_SSL
  496. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  497. [ -z "$(grep 'Listen 443' ${apache_install_dir}/conf/httpd.conf)" ] && sed -i "s@Listen 80@&\nListen 443@" ${apache_install_dir}/conf/httpd.conf
  498. [ -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
  499. else
  500. if [ -n "`ifconfig | grep inet6`" ]; then
  501. Nginx_conf=$(echo -e "listen 80;\n listen [::]:80;")
  502. else
  503. Nginx_conf=$(echo -e "listen 80;")
  504. fi
  505. fi
  506. }
  507. Nginx_anti_hotlinking() {
  508. while :; do echo
  509. read -e -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_flag
  510. if [[ ! ${anti_hotlinking_flag} =~ ^[y,n]$ ]]; then
  511. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  512. else
  513. break
  514. fi
  515. done
  516. if [ -n "$(echo ${domain} | grep '.*\..*\..*')" ]; then
  517. domain_allow="*.${domain#*.} ${domain}"
  518. else
  519. domain_allow="*.${domain} ${domain}"
  520. fi
  521. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  522. if [ "${moredomainame_flag}" == 'y' -a "${moredomain}" != "*.${domain}" ]; then
  523. domain_allow_all=${domain_allow}${moredomainame}
  524. else
  525. domain_allow_all=${domain_allow}
  526. fi
  527. domain_allow_all=`echo ${domain_allow_all} | tr ' ' '\n' | awk '!a[$1]++' | xargs`
  528. 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 return 403;\n }\n }")
  529. fi
  530. }
  531. Nginx_rewrite() {
  532. [ ! -d "${web_install_dir}/conf/rewrite" ] && mkdir ${web_install_dir}/conf/rewrite
  533. while :; do echo
  534. read -e -p "Allow Rewrite rule? [y/n]: " rewrite_flag
  535. if [[ ! "${rewrite_flag}" =~ ^[y,n]$ ]]; then
  536. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  537. else
  538. break
  539. fi
  540. done
  541. if [ "${rewrite_flag}" == 'n' ]; then
  542. rewrite="none"
  543. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  544. else
  545. echo
  546. echo "Please input the rewrite of programme :"
  547. echo "${CMSG}wordpress${CEND},${CMSG}opencart${CEND},${CMSG}magento2${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND},${CMSG}codeigniter${CEND},${CMSG}laravel${CEND}"
  548. echo "${CMSG}thinkphp${CEND},${CMSG}pathinfo${CEND},${CMSG}discuz${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}nextcloud${CEND},${CMSG}zblog${CEND},${CMSG}whmcs${CEND} rewrite was exist."
  549. read -e -p "(Default rewrite: other): " rewrite
  550. if [ "${rewrite}" == "" ]; then
  551. rewrite="other"
  552. fi
  553. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  554. [ "${NGX_FLAG}" == 'php' -a "${rewrite}" == "joomla" ] && NGX_CONF=$(echo -e "location ~ \\.php\$ {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  555. #[ "${NGX_FLAG}" == 'php' ] && [[ "${rewrite}" =~ ^codeigniter$|^thinkphp$|^pathinfo$ ]] && NGX_CONF=$(echo -e "location ~ [^/]\.php(/|\$) {\n try_files \$uri =404;\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\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 }")
  556. [ "${NGX_FLAG}" == 'php' ] && [[ "${rewrite}" =~ ^codeigniter$|^thinkphp$|^pathinfo$ ]] && NGX_CONF=$(echo -e "location ~ [^/]\.php(/|\$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n fastcgi_split_path_info ^(.+?\.php)(/.*)\$;\n set \$path_info \$fastcgi_path_info;\n fastcgi_param PATH_INFO \$path_info;\n try_files \$fastcgi_script_name =404; \n }")
  557. [ "${NGX_FLAG}" == 'php' -a "${rewrite}" == "typecho" ] && NGX_CONF=$(echo -e "location ~ .*\.php(\/.*)*\$ {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n set \$path_info \"\";\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 }")
  558. if [[ ! "${rewrite}" =~ ^magento2$|^pathinfo$ ]]; then
  559. if [ -e "config/${rewrite}.conf" ]; then
  560. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/rewrite/${rewrite}.conf
  561. else
  562. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  563. fi
  564. fi
  565. fi
  566. }
  567. Nginx_log() {
  568. while :; do echo
  569. read -e -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_flag
  570. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  571. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  572. else
  573. break
  574. fi
  575. done
  576. if [ "${access_flag}" == 'n' ]; then
  577. Nginx_log="access_log off;"
  578. else
  579. Nginx_log="access_log ${wwwlogs_dir}/${domain}_nginx.log combined;"
  580. echo "You access log file=${CMSG}${wwwlogs_dir}/${domain}_nginx.log${CEND}"
  581. fi
  582. }
  583. Create_nginx_tomcat_conf() {
  584. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  585. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  586. server {
  587. ${Nginx_conf}
  588. server_name ${domain}${moredomainame};
  589. ${Nginx_log}
  590. index index.html index.htm index.jsp;
  591. root ${vhostdir};
  592. ${Nginx_redirect}
  593. #error_page 404 /404.html;
  594. #error_page 502 /502.html;
  595. ${anti_hotlinking}
  596. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  597. expires 30d;
  598. access_log off;
  599. }
  600. location ~ .*\.(js|css)?$ {
  601. expires 7d;
  602. access_log off;
  603. }
  604. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  605. deny all;
  606. }
  607. ${NGX_CONF}
  608. }
  609. EOF
  610. [ "${https_flag}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  611. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  612. <Host name="${domain}" appBase="${vhostdir}" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  613. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  614. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  615. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  616. <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For"
  617. protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>
  618. </Host>
  619. EOF
  620. [ -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
  621. [ -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
  622. echo
  623. ${web_install_dir}/sbin/nginx -t
  624. if [ $? == 0 ]; then
  625. echo "Reload Nginx......"
  626. ${web_install_dir}/sbin/nginx -s reload
  627. service tomcat restart
  628. else
  629. rm -f ${web_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/RedHat 6+ Debian 8+ and Ubuntu 14+ #
  636. # For more information please visit https://oneinstack.com #
  637. #######################################################################
  638. "
  639. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  640. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  641. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  642. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  643. Print_SSL
  644. }
  645. Create_tomcat_conf() {
  646. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  647. <Host name="${domain}" appBase="webapps" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  648. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  649. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  650. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  651. </Host>
  652. EOF
  653. [ -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
  654. [ -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
  655. echo
  656. service tomcat restart
  657. printf "
  658. #######################################################################
  659. # OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+ #
  660. # For more information please visit https://oneinstack.com #
  661. #######################################################################
  662. "
  663. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  664. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  665. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  666. echo "$(printf "%-30s" "index url:")${CMSG}http://${domain}:8080/${CEND}"
  667. }
  668. Create_nginx_phpfpm_hhvm_conf() {
  669. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  670. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  671. server {
  672. ${Nginx_conf}
  673. server_name ${domain}${moredomainame};
  674. ${Nginx_log}
  675. index index.html index.htm index.php;
  676. root ${vhostdir};
  677. ${Nginx_redirect}
  678. include ${web_install_dir}/conf/rewrite/${rewrite}.conf;
  679. #error_page 404 /404.html;
  680. #error_page 502 /502.html;
  681. ${anti_hotlinking}
  682. ${NGX_CONF}
  683. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  684. expires 30d;
  685. access_log off;
  686. }
  687. location ~ .*\.(js|css)?$ {
  688. expires 7d;
  689. access_log off;
  690. }
  691. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  692. deny all;
  693. }
  694. }
  695. EOF
  696. [ "${rewrite}" == 'pathinfo' ] && sed -i '/pathinfo.conf;$/d' ${web_install_dir}/conf/vhost/${domain}.conf
  697. if [ "${rewrite}" == 'magento2' -a -e "config/${rewrite}.conf" ]; then
  698. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/vhost/${domain}.conf
  699. sed -i "s@/dev/shm/php-cgi.sock@/dev/shm/php${mphp_ver}-cgi.sock@g" ${web_install_dir}/conf/vhost/${domain}.conf
  700. sed -i "s@^ set \$MAGE_ROOT.*;@ set \$MAGE_ROOT ${vhostdir};@" ${web_install_dir}/conf/vhost/${domain}.conf
  701. sed -i "s@^ server_name.*;@ server_name ${domain}${moredomainame};@" ${web_install_dir}/conf/vhost/${domain}.conf
  702. sed -i "s@^ server_name.*;@&\n ${Nginx_log}@" ${web_install_dir}/conf/vhost/${domain}.conf
  703. [ "${NGX_FLAG}" == 'hhvm' ] && sed -i 's@fastcgi_pass unix:.*;@fastcgi_pass unix:/var/log/hhvm/sock;@g' ${web_install_dir}/conf/vhost/${domain}.conf
  704. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  705. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  706. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  707. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  708. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  709. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  710. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  711. sed -i "s@^ root.*;@&\n location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv|mp4)\$ {@" ${web_install_dir}/conf/vhost/${domain}.conf
  712. fi
  713. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  714. if [ "${nginx_ssl_flag}" == 'y' ]; then
  715. sed -i "s@^ listen 80;@&\n listen ${LISTENOPT};@" ${web_install_dir}/conf/vhost/${domain}.conf
  716. sed -i "s@^ server_name.*;@&\n ssl_stapling_verify on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  717. sed -i "s@^ server_name.*;@&\n ssl_stapling on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  718. sed -i "s@^ server_name.*;@&\n add_header Strict-Transport-Security max-age=15768000;@" ${web_install_dir}/conf/vhost/${domain}.conf
  719. sed -i "s@^ server_name.*;@&\n ssl_buffer_size 1400;@" ${web_install_dir}/conf/vhost/${domain}.conf
  720. sed -i "s@^ server_name.*;@&\n ssl_session_cache builtin:1000 shared:SSL:10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  721. sed -i "s@^ server_name.*;@&\n ssl_session_timeout 10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  722. sed -i "s@^ server_name.*;@&\n ssl_prefer_server_ciphers on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  723. sed -i "s@^ server_name.*;@&\n ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:\!MD5;@" ${web_install_dir}/conf/vhost/${domain}.conf
  724. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;@" ${web_install_dir}/conf/vhost/${domain}.conf
  725. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  726. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  727. fi
  728. fi
  729. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  730. echo
  731. ${web_install_dir}/sbin/nginx -t
  732. if [ $? == 0 ]; then
  733. echo "Reload Nginx......"
  734. ${web_install_dir}/sbin/nginx -s reload
  735. else
  736. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  737. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  738. exit 1
  739. fi
  740. printf "
  741. #######################################################################
  742. # OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+ #
  743. # For more information please visit https://oneinstack.com #
  744. #######################################################################
  745. "
  746. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  747. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  748. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  749. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  750. Print_SSL
  751. }
  752. Apache_log() {
  753. while :; do echo
  754. read -e -p "Allow Apache access_log? [y/n]: " access_flag
  755. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  756. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  757. else
  758. break
  759. fi
  760. done
  761. if [ "${access_flag}" == 'n' ]; then
  762. Apache_log='CustomLog "/dev/null" common'
  763. else
  764. Apache_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  765. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  766. fi
  767. }
  768. Create_apache_conf() {
  769. if [ "${Apache_main_ver}" == '24' ]; then
  770. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  771. Apache_fcgi=$(echo -e "<Files ~ (\\.user.ini|\\.htaccess|\\.git|\\.svn|\\.project|LICENSE|README.md)\$>\n Order allow,deny\n Deny from all\n </Files>\n <FilesMatch \\.php\$>\n SetHandler \"proxy:unix:/dev/shm/php${mphp_ver}-cgi.sock|fcgi://localhost\"\n </FilesMatch>")
  772. fi
  773. fi
  774. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  775. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  776. <VirtualHost *:80>
  777. ServerAdmin admin@example.com
  778. DocumentRoot "${vhostdir}"
  779. ServerName ${domain}
  780. ${Apache_Domain_alias}
  781. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  782. ${Apache_log}
  783. ${Apache_fcgi}
  784. <Directory "${vhostdir}">
  785. SetOutputFilter DEFLATE
  786. Options FollowSymLinks ExecCGI
  787. ${Apache_grant}
  788. AllowOverride All
  789. Order allow,deny
  790. Allow from all
  791. DirectoryIndex index.html index.php
  792. </Directory>
  793. </VirtualHost>
  794. EOF
  795. [ "$apache_ssl_flag" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  796. <VirtualHost *:443>
  797. ServerAdmin admin@example.com
  798. DocumentRoot "${vhostdir}"
  799. ServerName ${domain}
  800. ${Apache_Domain_alias}
  801. ${Apache_SSL}
  802. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  803. ${Apache_log}
  804. ${Apache_fcgi}
  805. <Directory "${vhostdir}">
  806. SetOutputFilter DEFLATE
  807. Options FollowSymLinks ExecCGI
  808. ${Apache_grant}
  809. AllowOverride All
  810. Order allow,deny
  811. Allow from all
  812. DirectoryIndex index.html index.php
  813. </Directory>
  814. </VirtualHost>
  815. EOF
  816. echo
  817. ${apache_install_dir}/bin/apachectl -t
  818. if [ $? == 0 ]; then
  819. echo "Restart Apache......"
  820. ${apache_install_dir}/bin/apachectl -k graceful
  821. else
  822. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  823. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  824. exit 1
  825. fi
  826. printf "
  827. #######################################################################
  828. # OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+ #
  829. # For more information please visit https://oneinstack.com #
  830. #######################################################################
  831. "
  832. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  833. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  834. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  835. Print_SSL
  836. }
  837. Create_nginx_apache_modphp_conf() {
  838. # Nginx/Tengine/OpenResty
  839. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  840. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  841. server {
  842. ${Nginx_conf}
  843. server_name ${domain}${moredomainame};
  844. ${Nginx_log}
  845. index index.html index.htm index.php;
  846. root ${vhostdir};
  847. ${Nginx_redirect}
  848. ${anti_hotlinking}
  849. location / {
  850. try_files \$uri @apache;
  851. }
  852. location @apache {
  853. proxy_pass http://127.0.0.1:88;
  854. include proxy.conf;
  855. }
  856. location ~ .*\.(php|php5|cgi|pl)?$ {
  857. proxy_pass http://127.0.0.1:88;
  858. include proxy.conf;
  859. }
  860. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  861. expires 30d;
  862. access_log off;
  863. }
  864. location ~ .*\.(js|css)?$ {
  865. expires 7d;
  866. access_log off;
  867. }
  868. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  869. deny all;
  870. }
  871. }
  872. EOF
  873. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  874. echo
  875. ${web_install_dir}/sbin/nginx -t
  876. if [ $? == 0 ]; then
  877. echo "Reload Nginx......"
  878. ${web_install_dir}/sbin/nginx -s reload
  879. else
  880. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  881. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  882. fi
  883. # Apache
  884. if [ "${Apache_main_ver}" == '24' ]; then
  885. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  886. Apache_fcgi=$(echo -e "<Files ~ (\\.user.ini|\\.htaccess|\\.git|\\.svn|\\.project|LICENSE|README.md)\$>\n Order allow,deny\n Deny from all\n </Files>\n <FilesMatch \\.php\$>\n SetHandler \"proxy:unix:/dev/shm/php${mphp_ver}-cgi.sock|fcgi://localhost\"\n </FilesMatch>")
  887. fi
  888. fi
  889. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  890. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  891. <VirtualHost *:88>
  892. ServerAdmin admin@example.com
  893. DocumentRoot "${vhostdir}"
  894. ServerName ${domain}
  895. ${Apache_Domain_alias}
  896. ${Apache_SSL}
  897. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  898. ${Apache_log}
  899. ${Apache_fcgi}
  900. <Directory "${vhostdir}">
  901. SetOutputFilter DEFLATE
  902. Options FollowSymLinks ExecCGI
  903. ${Apache_grant}
  904. AllowOverride All
  905. Order allow,deny
  906. Allow from all
  907. DirectoryIndex index.html index.php
  908. </Directory>
  909. </VirtualHost>
  910. EOF
  911. echo
  912. ${apache_install_dir}/bin/apachectl -t
  913. if [ $? == 0 ]; then
  914. echo "Restart Apache......"
  915. ${apache_install_dir}/bin/apachectl -k graceful
  916. else
  917. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  918. exit 1
  919. fi
  920. printf "
  921. #######################################################################
  922. # OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+ #
  923. # For more information please visit https://oneinstack.com #
  924. #######################################################################
  925. "
  926. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  927. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  928. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  929. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  930. Print_SSL
  931. }
  932. Add_Vhost() {
  933. if [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" ]; then
  934. Choose_ENV
  935. Input_Add_domain
  936. Nginx_anti_hotlinking
  937. Nginx_rewrite
  938. if [ "${NGX_FLAG}" == "java" ]; then
  939. Nginx_log
  940. Create_nginx_tomcat_conf
  941. else
  942. Nginx_log
  943. Create_nginx_phpfpm_hhvm_conf
  944. fi
  945. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  946. Choose_ENV
  947. Input_Add_domain
  948. Apache_log
  949. Create_apache_conf
  950. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  951. Choose_ENV
  952. Input_Add_domain
  953. Create_tomcat_conf
  954. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  955. Choose_ENV
  956. Input_Add_domain
  957. Nginx_anti_hotlinking
  958. if [ "${NGX_FLAG}" == "java" ]; then
  959. Nginx_rewrite
  960. Nginx_log
  961. Create_nginx_tomcat_conf
  962. elif [ "${NGX_FLAG}" == "hhvm" ]; then
  963. Nginx_rewrite
  964. Nginx_log
  965. Create_nginx_phpfpm_hhvm_conf
  966. elif [ "${NGX_FLAG}" == "php" ]; then
  967. Nginx_log
  968. Apache_log
  969. Create_nginx_apache_modphp_conf
  970. fi
  971. else
  972. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  973. fi
  974. }
  975. Del_NGX_Vhost() {
  976. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  977. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  978. if [ -n "${Domain_List}" ]; then
  979. echo
  980. echo "Virtualhost list:"
  981. echo ${CMSG}${Domain_List}${CEND}
  982. while :; do echo
  983. read -e -p "Please input a domain you want to delete: " domain
  984. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  985. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  986. else
  987. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  988. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  989. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  990. [ -e "${web_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${web_install_dir}/conf/ssl/${domain}.{crt,key}
  991. ${web_install_dir}/sbin/nginx -s reload
  992. while :; do echo
  993. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  994. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  995. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  996. else
  997. break
  998. fi
  999. done
  1000. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1001. if [ "${quiet_flag}" != 'y' ]; then
  1002. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1003. char=$(get_char)
  1004. fi
  1005. rm -rf ${Directory}
  1006. fi
  1007. echo
  1008. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1009. echo "${CMSG}Domain: ${domain} has been deleted.${CEND}"
  1010. echo
  1011. else
  1012. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1013. fi
  1014. break
  1015. fi
  1016. done
  1017. else
  1018. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1019. fi
  1020. fi
  1021. }
  1022. Del_Apache_Vhost() {
  1023. if [ -e "${apache_install_dir}/bin/httpd" ]; then
  1024. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1025. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1026. ${apache_install_dir}/bin/apachectl -k graceful
  1027. else
  1028. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  1029. if [ -n "${Domain_List}" ]; then
  1030. echo
  1031. echo "Virtualhost list:"
  1032. echo ${CMSG}${Domain_List}${CEND}
  1033. while :; do echo
  1034. read -e -p "Please input a domain you want to delete: " domain
  1035. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1036. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1037. else
  1038. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  1039. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  1040. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1041. [ -e "${apache_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${apache_install_dir}/conf/ssl/${domain}.{crt,key}
  1042. ${apache_install_dir}/bin/apachectl -k graceful
  1043. while :; do echo
  1044. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1045. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1046. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1047. else
  1048. break
  1049. fi
  1050. done
  1051. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1052. if [ "${quiet_flag}" != 'y' ]; then
  1053. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1054. char=$(get_char)
  1055. fi
  1056. rm -rf ${Directory}
  1057. fi
  1058. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1059. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1060. else
  1061. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1062. fi
  1063. break
  1064. fi
  1065. done
  1066. else
  1067. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1068. fi
  1069. fi
  1070. fi
  1071. }
  1072. Del_Tomcat_Vhost() {
  1073. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  1074. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1075. if [ -n "$(echo ${domain} | grep '.*\..*')" ] && [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  1076. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  1077. rm -f ${tomcat_install_dir}/conf/vhost/${domain}.xml
  1078. service tomcat restart
  1079. fi
  1080. else
  1081. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  1082. if [ -n "${Domain_List}" ]; then
  1083. echo
  1084. echo "Virtualhost list:"
  1085. echo ${CMSG}${Domain_List}${CEND}
  1086. while :; do echo
  1087. read -e -p "Please input a domain you want to delete: " domain
  1088. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1089. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1090. else
  1091. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  1092. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  1093. rm -f ${tomcat_install_dir}/conf/vhost/${domain}.xml
  1094. service tomcat restart
  1095. while :; do echo
  1096. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1097. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1098. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1099. else
  1100. break
  1101. fi
  1102. done
  1103. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1104. if [ "${quiet_flag}" != 'y' ]; then
  1105. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1106. char=$(get_char)
  1107. fi
  1108. rm -rf ${Directory}
  1109. fi
  1110. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1111. else
  1112. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1113. fi
  1114. break
  1115. fi
  1116. done
  1117. else
  1118. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1119. fi
  1120. fi
  1121. fi
  1122. }
  1123. List_Vhost() {
  1124. [ -e "${tomcat_install_dir}/conf/server.xml" -a ! -d "${web_install_dir}/sbin/nginx" ] && Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  1125. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  1126. [ -e "${apache_install_dir}/bin/httpd" -a ! -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  1127. if [ -n "${Domain_List}" ]; then
  1128. echo
  1129. echo "Virtualhost list:"
  1130. for D in ${Domain_List}; do echo ${CMSG}${D}${CEND}; done
  1131. else
  1132. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1133. fi
  1134. }
  1135. if [ ${ARG_NUM} == 0 ]; then
  1136. Add_Vhost
  1137. else
  1138. [ "${add_flag}" == 'y' -o "${sslquiet_flag}" == 'y' ] && Add_Vhost
  1139. [ "${list_flag}" == 'y' ] && List_Vhost
  1140. [ "${delete_flag}" == 'y' ] && { Del_NGX_Vhost; Del_Apache_Vhost; Del_Tomcat_Vhost; }
  1141. fi