vhost.sh 55 KB

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