vhost.sh 54 KB

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