vhost.sh 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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. break
  181. elif [[ "${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/v2/DV90/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. [ -e "${PATH_SSL}/${domain}.crt" ] && rm -f ${PATH_SSL}/${domain}.{crt,key}
  267. Nginx_cmd="/bin/systemctl restart nginx"
  268. Apache_cmd="${apache_install_dir}/bin/apachectl -k graceful"
  269. if [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  270. Command="${Nginx_cmd};${Apache_cmd}"
  271. elif [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" ]; then
  272. Command="${Nginx_cmd}"
  273. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  274. Command="${Apache_cmd}"
  275. fi
  276. if [ -s ~/.acme.sh/${domain}/fullchain.cer ] && [[ "${CERT_KEYLENGTH}" =~ ^2048$|^3072$|^4096$|^8192$ ]]; then
  277. ~/.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
  278. elif [ -s ~/.acme.sh/${domain}_ecc/fullchain.cer ] && [[ "${CERT_KEYLENGTH}" =~ ^ec-256$|^ec-384$|^ec-521$ ]]; then
  279. ~/.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
  280. else
  281. echo "${CFAILURE}Error: Create Let's Encrypt SSL Certificate failed! ${CEND}"
  282. [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  283. [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  284. exit 1
  285. fi
  286. fi
  287. }
  288. Print_SSL() {
  289. if [ "${Domian_Mode}" == '2' ]; then
  290. echo "$(printf "%-30s" "Self-signed SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  291. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  292. echo "$(printf "%-30s" "SSL CSR File:")${CMSG}${PATH_SSL}/${domain}.csr${CEND}"
  293. elif [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ]; then
  294. echo "$(printf "%-30s" "Let's Encrypt SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  295. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  296. fi
  297. }
  298. Input_Add_proxy() {
  299. while :; do echo
  300. read -e -p "Please input the correct proxy_pass: " Proxy_Pass
  301. if [ -z "$(echo $Proxy_Pass | grep -E '^http://|https://')" ]; then
  302. echo "${CFAILURE}input error! Please only input example http://192.168.1.1:8080${CEND}"
  303. else
  304. echo "proxy_pass=${Proxy_Pass}"
  305. break
  306. fi
  307. done
  308. }
  309. Input_Add_domain() {
  310. if [ "${sslquiet_flag}" != 'y' ]; then
  311. while :;do
  312. printf "
  313. What Are You Doing?
  314. \t${CMSG}1${CEND}. Use HTTP Only
  315. \t${CMSG}2${CEND}. Use your own SSL Certificate and Key
  316. \t${CMSG}3${CEND}. Use Let's Encrypt to Create SSL Certificate and Key
  317. \t${CMSG}q${CEND}. Exit
  318. "
  319. read -e -p "Please input the correct option: " Domian_Mode
  320. if [[ ! "${Domian_Mode}" =~ ^[1-3,q]$ ]]; then
  321. echo "${CFAILURE}input error! Please only input 1~3 and q${CEND}"
  322. else
  323. break
  324. fi
  325. done
  326. fi
  327. #Multiple_PHP
  328. if [ $(ls /dev/shm/php*-cgi.sock 2> /dev/null | wc -l) -ge 2 ]; then
  329. if [ "${mphp_flag}" != 'y' ]; then
  330. PHP_detail_ver=`${php_install_dir}/bin/php-config --version`
  331. PHP_main_ver=${PHP_detail_ver%.*}
  332. while :; do echo
  333. echo 'Please select a version of the PHP:'
  334. echo -e "\t${CMSG} 0${CEND}. PHP ${PHP_main_ver} (default)"
  335. [ -e "/dev/shm/php53-cgi.sock" ] && echo -e "\t${CMSG} 1${CEND}. PHP 5.3"
  336. [ -e "/dev/shm/php54-cgi.sock" ] && echo -e "\t${CMSG} 2${CEND}. PHP 5.4"
  337. [ -e "/dev/shm/php55-cgi.sock" ] && echo -e "\t${CMSG} 3${CEND}. PHP 5.5"
  338. [ -e "/dev/shm/php56-cgi.sock" ] && echo -e "\t${CMSG} 4${CEND}. PHP 5.6"
  339. [ -e "/dev/shm/php70-cgi.sock" ] && echo -e "\t${CMSG} 5${CEND}. PHP 7.0"
  340. [ -e "/dev/shm/php71-cgi.sock" ] && echo -e "\t${CMSG} 6${CEND}. PHP 7.1"
  341. [ -e "/dev/shm/php72-cgi.sock" ] && echo -e "\t${CMSG} 7${CEND}. PHP 7.2"
  342. [ -e "/dev/shm/php73-cgi.sock" ] && echo -e "\t${CMSG} 8${CEND}. PHP 7.3"
  343. [ -e "/dev/shm/php74-cgi.sock" ] && echo -e "\t${CMSG} 9${CEND}. PHP 7.4"
  344. [ -e "/dev/shm/php80-cgi.sock" ] && echo -e "\t${CMSG}10${CEND}. PHP 8.0"
  345. [ -e "/dev/shm/php81-cgi.sock" ] && echo -e "\t${CMSG}11${CEND}. PHP 8.1"
  346. [ -e "/dev/shm/php82-cgi.sock" ] && echo -e "\t${CMSG}12${CEND}. PHP 8.2"
  347. read -e -p "Please input a number:(Default 0 press Enter) " php_option
  348. php_option=${php_option:-0}
  349. if [[ ! ${php_option} =~ ^[0-9]$|^1[0-2]$ ]]; then
  350. echo "${CWARNING}input error! Please only input number 1~12${CEND}"
  351. else
  352. break
  353. fi
  354. done
  355. fi
  356. [ "${php_option}" == '1' ] && mphp_ver=53
  357. [ "${php_option}" == '2' ] && mphp_ver=54
  358. [ "${php_option}" == '3' ] && mphp_ver=55
  359. [ "${php_option}" == '4' ] && mphp_ver=56
  360. [ "${php_option}" == '5' ] && mphp_ver=70
  361. [ "${php_option}" == '6' ] && mphp_ver=71
  362. [ "${php_option}" == '7' ] && mphp_ver=72
  363. [ "${php_option}" == '8' ] && mphp_ver=73
  364. [ "${php_option}" == '9' ] && mphp_ver=74
  365. [ "${php_option}" == '10' ] && mphp_ver=80
  366. [ "${php_option}" == '11' ] && mphp_ver=81
  367. [ "${php_option}" == '12' ] && mphp_ver=82
  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 ${mirror_link}/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.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")
  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.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")
  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 2k;@" ${web_install_dir}/conf/vhost/${domain}.conf
  722. sed -i "s@^ server_name.*;@&\n ssl_session_cache 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_conf_command Options PrioritizeChaCha;@" ${web_install_dir}/conf/vhost/${domain}.conf
  726. 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
  727. 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
  728. sed -i "s@^ server_name.*;@&\n ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;@" ${web_install_dir}/conf/vhost/${domain}.conf
  729. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1.2 TLSv1.3;@" ${web_install_dir}/conf/vhost/${domain}.conf
  730. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  731. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  732. fi
  733. fi
  734. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  735. echo
  736. ${web_install_dir}/sbin/nginx -t
  737. if [ $? == 0 ]; then
  738. echo "Reload Nginx......"
  739. ${web_install_dir}/sbin/nginx -s reload
  740. else
  741. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  742. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  743. exit 1
  744. fi
  745. printf "
  746. #######################################################################
  747. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  748. # For more information please visit https://oneinstack.com #
  749. #######################################################################
  750. "
  751. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  752. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  753. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  754. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  755. Print_SSL
  756. }
  757. Create_nginx_proxy_conf() {
  758. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  759. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  760. server {
  761. ${Nginx_conf}
  762. server_name ${domain}${moredomainame};
  763. ${Nginx_log}
  764. index index.html index.htm index.php;
  765. ${Nginx_redirect}
  766. location / {
  767. proxy_pass ${Proxy_Pass};
  768. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  769. proxy_set_header Host \$http_host;
  770. proxy_set_header X-NginX-Proxy true;
  771. proxy_http_version 1.1;
  772. proxy_set_header Upgrade \$http_upgrade;
  773. proxy_set_header Connection "upgrade";
  774. proxy_max_temp_file_size 0;
  775. }
  776. #error_page 404 /404.html;
  777. #error_page 502 /502.html;
  778. ${anti_hotlinking}
  779. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  780. expires 30d;
  781. access_log off;
  782. }
  783. location ~ .*\.(js|css)?$ {
  784. expires 7d;
  785. access_log off;
  786. }
  787. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  788. deny all;
  789. }
  790. location /.well-known {
  791. allow all;
  792. }
  793. }
  794. EOF
  795. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  796. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  797. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  798. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  799. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  800. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  801. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  802. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  803. 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
  804. fi
  805. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  806. echo
  807. ${web_install_dir}/sbin/nginx -t
  808. if [ $? == 0 ]; then
  809. echo "Reload Nginx......"
  810. ${web_install_dir}/sbin/nginx -s reload
  811. else
  812. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  813. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  814. exit 1
  815. fi
  816. printf "
  817. #######################################################################
  818. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  819. # For more information please visit https://oneinstack.com #
  820. #######################################################################
  821. "
  822. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  823. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  824. #echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  825. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  826. Print_SSL
  827. }
  828. Apache_log() {
  829. while :; do echo
  830. read -e -p "Allow Apache access_log? [y/n]: " access_flag
  831. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  832. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  833. else
  834. break
  835. fi
  836. done
  837. if [ "${access_flag}" == 'n' ]; then
  838. Apache_log='CustomLog "/dev/null" common'
  839. else
  840. Apache_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  841. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  842. fi
  843. }
  844. Create_apache_conf() {
  845. if [ "${Apache_main_ver}" == '24' ]; then
  846. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  847. 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>")
  848. fi
  849. fi
  850. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  851. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  852. <VirtualHost *:80>
  853. ServerAdmin admin@example.com
  854. DocumentRoot "${vhostdir}"
  855. ServerName ${domain}
  856. ${Apache_Domain_alias}
  857. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  858. ${Apache_log}
  859. ${Apache_fcgi}
  860. <Directory "${vhostdir}">
  861. SetOutputFilter DEFLATE
  862. Options FollowSymLinks ExecCGI
  863. ${Apache_grant}
  864. AllowOverride All
  865. Order allow,deny
  866. Allow from all
  867. DirectoryIndex index.html index.php
  868. </Directory>
  869. </VirtualHost>
  870. EOF
  871. [ "${apache_ssl_flag}" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  872. <VirtualHost *:443>
  873. ServerAdmin admin@example.com
  874. DocumentRoot "${vhostdir}"
  875. ServerName ${domain}
  876. ${Apache_Domain_alias}
  877. ${Apache_SSL}
  878. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  879. ${Apache_log}
  880. ${Apache_fcgi}
  881. <Directory "${vhostdir}">
  882. SetOutputFilter DEFLATE
  883. Options FollowSymLinks ExecCGI
  884. ${Apache_grant}
  885. AllowOverride All
  886. Order allow,deny
  887. Allow from all
  888. DirectoryIndex index.html index.php
  889. </Directory>
  890. </VirtualHost>
  891. EOF
  892. echo
  893. ${apache_install_dir}/bin/apachectl -t
  894. if [ $? == 0 ]; then
  895. echo "Restart Apache......"
  896. ${apache_install_dir}/bin/apachectl -k graceful
  897. else
  898. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  899. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  900. exit 1
  901. fi
  902. printf "
  903. #######################################################################
  904. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  905. # For more information please visit https://oneinstack.com #
  906. #######################################################################
  907. "
  908. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  909. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  910. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  911. Print_SSL
  912. }
  913. Create_nginx_apache_modphp_conf() {
  914. # Nginx/Tengine/OpenResty
  915. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  916. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  917. server {
  918. ${Nginx_conf}
  919. server_name ${domain}${moredomainame};
  920. ${Nginx_log}
  921. index index.html index.htm index.php;
  922. root ${vhostdir};
  923. ${Nginx_redirect}
  924. ${anti_hotlinking}
  925. location / {
  926. try_files \$uri @apache;
  927. }
  928. location @apache {
  929. proxy_pass http://127.0.0.1:88;
  930. include proxy.conf;
  931. }
  932. location ~ .*\.(php|php5|cgi|pl)?$ {
  933. proxy_pass http://127.0.0.1:88;
  934. include proxy.conf;
  935. }
  936. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  937. expires 30d;
  938. access_log off;
  939. }
  940. location ~ .*\.(js|css)?$ {
  941. expires 7d;
  942. access_log off;
  943. }
  944. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  945. deny all;
  946. }
  947. location /.well-known {
  948. allow all;
  949. }
  950. }
  951. EOF
  952. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  953. echo
  954. ${web_install_dir}/sbin/nginx -t
  955. if [ $? == 0 ]; then
  956. echo "Reload Nginx......"
  957. ${web_install_dir}/sbin/nginx -s reload
  958. else
  959. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  960. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  961. fi
  962. # Apache
  963. if [ "${Apache_main_ver}" == '24' ]; then
  964. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  965. 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>")
  966. fi
  967. fi
  968. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  969. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  970. <VirtualHost *:88>
  971. ServerAdmin admin@example.com
  972. DocumentRoot "${vhostdir}"
  973. ServerName ${domain}
  974. ${Apache_Domain_alias}
  975. ${Apache_SSL}
  976. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  977. ${Apache_log}
  978. ${Apache_fcgi}
  979. <Directory "${vhostdir}">
  980. SetOutputFilter DEFLATE
  981. Options FollowSymLinks ExecCGI
  982. ${Apache_grant}
  983. AllowOverride All
  984. Order allow,deny
  985. Allow from all
  986. DirectoryIndex index.html index.php
  987. </Directory>
  988. </VirtualHost>
  989. EOF
  990. echo
  991. ${apache_install_dir}/bin/apachectl -t
  992. if [ $? == 0 ]; then
  993. echo "Restart Apache......"
  994. ${apache_install_dir}/bin/apachectl -k graceful
  995. else
  996. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  997. exit 1
  998. fi
  999. printf "
  1000. #######################################################################
  1001. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  1002. # For more information please visit https://oneinstack.com #
  1003. #######################################################################
  1004. "
  1005. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  1006. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  1007. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  1008. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  1009. Print_SSL
  1010. }
  1011. Add_Vhost() {
  1012. if [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" ]; then
  1013. Choose_ENV
  1014. Input_Add_domain
  1015. Nginx_anti_hotlinking
  1016. if [ "${proxy_flag}" == "y" ]; then
  1017. Input_Add_proxy
  1018. Create_nginx_proxy_conf
  1019. else
  1020. Nginx_rewrite
  1021. if [ "${NGX_FLAG}" == "java" ]; then
  1022. Nginx_log
  1023. Create_nginx_tomcat_conf
  1024. else
  1025. Nginx_log
  1026. Create_nginx_phpfpm_conf
  1027. fi
  1028. fi
  1029. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  1030. Choose_ENV
  1031. Input_Add_domain
  1032. Apache_log
  1033. Create_apache_conf
  1034. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  1035. Choose_ENV
  1036. Input_Add_domain
  1037. Create_tomcat_conf
  1038. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  1039. Choose_ENV
  1040. Input_Add_domain
  1041. Nginx_anti_hotlinking
  1042. if [ "${NGX_FLAG}" == "java" ]; then
  1043. Nginx_rewrite
  1044. Nginx_log
  1045. Create_nginx_tomcat_conf
  1046. elif [ "${NGX_FLAG}" == "php" ]; then
  1047. Nginx_log
  1048. Apache_log
  1049. Create_nginx_apache_modphp_conf
  1050. fi
  1051. else
  1052. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  1053. fi
  1054. }
  1055. Del_NGX_Vhost() {
  1056. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1057. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  1058. if [ -n "${Domain_List}" ]; then
  1059. echo
  1060. echo "Virtualhost list:"
  1061. echo ${CMSG}${Domain_List}${CEND}
  1062. while :; do echo
  1063. read -e -p "Please input a domain you want to delete: " domain
  1064. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1065. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1066. else
  1067. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  1068. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  1069. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  1070. [ -e "${web_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${web_install_dir}/conf/ssl/${domain}.{crt,key}
  1071. ${web_install_dir}/sbin/nginx -s reload
  1072. while :; do echo
  1073. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1074. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1075. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1076. else
  1077. break
  1078. fi
  1079. done
  1080. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1081. if [ "${quiet_flag}" != 'y' ]; then
  1082. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1083. char=$(get_char)
  1084. fi
  1085. rm -rf ${Directory}
  1086. fi
  1087. echo
  1088. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1089. [ -d ~/.acme.sh/${domain}_ecc ] && ~/.acme.sh/acme.sh --force --remove --ecc -d ${domain} > /dev/null 2>&1
  1090. echo "${CMSG}Domain: ${domain} has been deleted.${CEND}"
  1091. echo
  1092. else
  1093. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1094. fi
  1095. break
  1096. fi
  1097. done
  1098. else
  1099. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1100. fi
  1101. fi
  1102. }
  1103. Del_Apache_Vhost() {
  1104. if [ -e "${apache_install_dir}/bin/httpd" ]; then
  1105. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1106. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1107. ${apache_install_dir}/bin/apachectl -k graceful
  1108. else
  1109. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  1110. if [ -n "${Domain_List}" ]; then
  1111. echo
  1112. echo "Virtualhost list:"
  1113. echo ${CMSG}${Domain_List}${CEND}
  1114. while :; do echo
  1115. read -e -p "Please input a domain you want to delete: " domain
  1116. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1117. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1118. else
  1119. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  1120. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  1121. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1122. [ -e "${apache_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${apache_install_dir}/conf/ssl/${domain}.{crt,key}
  1123. ${apache_install_dir}/bin/apachectl -k graceful
  1124. while :; do echo
  1125. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1126. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1127. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1128. else
  1129. break
  1130. fi
  1131. done
  1132. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1133. if [ "${quiet_flag}" != 'y' ]; then
  1134. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1135. char=$(get_char)
  1136. fi
  1137. rm -rf ${Directory}
  1138. fi
  1139. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1140. [ -d ~/.acme.sh/${domain}_ecc ] && ~/.acme.sh/acme.sh --force --remove --ecc -d ${domain} > /dev/null 2>&1
  1141. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1142. else
  1143. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1144. fi
  1145. break
  1146. fi
  1147. done
  1148. else
  1149. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1150. fi
  1151. fi
  1152. fi
  1153. }
  1154. Del_Tomcat_Vhost() {
  1155. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  1156. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1157. if [ -n "$(echo ${domain} | grep '.*\..*')" ] && [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  1158. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  1159. rm -f ${tomcat_install_dir}/conf/vhost/${domain}.xml
  1160. service tomcat restart
  1161. fi
  1162. else
  1163. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  1164. if [ -n "${Domain_List}" ]; then
  1165. echo
  1166. echo "Virtualhost list:"
  1167. echo ${CMSG}${Domain_List}${CEND}
  1168. while :; do echo
  1169. read -e -p "Please input a domain you want to delete: " domain
  1170. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1171. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1172. else
  1173. if [ -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. while :; do echo
  1178. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1179. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1180. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1181. else
  1182. break
  1183. fi
  1184. done
  1185. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1186. if [ "${quiet_flag}" != 'y' ]; then
  1187. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1188. char=$(get_char)
  1189. fi
  1190. rm -rf ${Directory}
  1191. fi
  1192. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1193. else
  1194. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1195. fi
  1196. break
  1197. fi
  1198. done
  1199. else
  1200. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1201. fi
  1202. fi
  1203. fi
  1204. }
  1205. List_Vhost() {
  1206. [ -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")
  1207. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  1208. [ -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")
  1209. if [ -n "${Domain_List}" ]; then
  1210. echo
  1211. echo "Virtualhost list:"
  1212. for D in ${Domain_List}; do echo ${CMSG}${D}${CEND}; done
  1213. else
  1214. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1215. fi
  1216. }
  1217. if [ ${ARG_NUM} == 0 ]; then
  1218. Add_Vhost
  1219. else
  1220. [ "${add_flag}" == 'y' -o "${proxy_flag}" == 'y' -o "${sslquiet_flag}" == 'y' ] && Add_Vhost
  1221. [ "${list_flag}" == 'y' ] && List_Vhost
  1222. [ "${delete_flag}" == 'y' ] && { Del_NGX_Vhost; Del_Apache_Vhost; Del_Tomcat_Vhost; }
  1223. fi