1
0

vhost.sh 56 KB

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