vhost.sh 57 KB

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