1
0

vhost.sh 54 KB

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