vhost.sh 56 KB

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