vhost.sh 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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 TLSv1.1 TLSv1.2 TLSv1.3;\n ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;\n ssl_prefer_server_ciphers on;\n ssl_session_timeout 10m;\n ssl_session_cache builtin:1000 shared:SSL:10m;\n ssl_buffer_size 1400;\n add_header Strict-Transport-Security max-age=15768000;\n ssl_stapling on;\n ssl_stapling_verify on;\n")
  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 TLSv1.1 TLSv1.2 TLSv1.3;\n ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;\n ssl_prefer_server_ciphers on;\n ssl_session_timeout 10m;\n ssl_session_cache builtin:1000 shared:SSL:10m;\n ssl_buffer_size 1400;\n add_header Strict-Transport-Security max-age=15768000;\n ssl_stapling on;\n ssl_stapling_verify on;\n")
  488. 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 1400;@" ${web_install_dir}/conf/vhost/${domain}.conf
  720. sed -i "s@^ server_name.*;@&\n ssl_session_cache builtin:1000 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_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:\!MD5;@" ${web_install_dir}/conf/vhost/${domain}.conf
  724. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;@" ${web_install_dir}/conf/vhost/${domain}.conf
  725. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  726. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  727. fi
  728. fi
  729. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  730. echo
  731. ${web_install_dir}/sbin/nginx -t
  732. if [ $? == 0 ]; then
  733. echo "Reload Nginx......"
  734. ${web_install_dir}/sbin/nginx -s reload
  735. else
  736. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  737. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  738. exit 1
  739. fi
  740. printf "
  741. #######################################################################
  742. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  743. # For more information please visit https://oneinstack.com #
  744. #######################################################################
  745. "
  746. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  747. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  748. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  749. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  750. Print_SSL
  751. }
  752. Create_nginx_proxy_conf() {
  753. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  754. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  755. server {
  756. ${Nginx_conf}
  757. server_name ${domain}${moredomainame};
  758. ${Nginx_log}
  759. index index.html index.htm index.php;
  760. ${Nginx_redirect}
  761. location / {
  762. proxy_pass ${Proxy_Pass};
  763. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  764. proxy_set_header Host \$http_host;
  765. proxy_set_header X-NginX-Proxy true;
  766. proxy_http_version 1.1;
  767. proxy_set_header Upgrade \$http_upgrade;
  768. proxy_set_header Connection "upgrade";
  769. proxy_max_temp_file_size 0;
  770. }
  771. #error_page 404 /404.html;
  772. #error_page 502 /502.html;
  773. ${anti_hotlinking}
  774. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  775. expires 30d;
  776. access_log off;
  777. }
  778. location ~ .*\.(js|css)?$ {
  779. expires 7d;
  780. access_log off;
  781. }
  782. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  783. deny all;
  784. }
  785. location /.well-known {
  786. allow all;
  787. }
  788. }
  789. EOF
  790. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  791. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  792. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  793. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  794. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  795. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  796. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  797. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  798. 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
  799. fi
  800. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  801. echo
  802. ${web_install_dir}/sbin/nginx -t
  803. if [ $? == 0 ]; then
  804. echo "Reload Nginx......"
  805. ${web_install_dir}/sbin/nginx -s reload
  806. else
  807. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  808. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  809. exit 1
  810. fi
  811. printf "
  812. #######################################################################
  813. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  814. # For more information please visit https://oneinstack.com #
  815. #######################################################################
  816. "
  817. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  818. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  819. #echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  820. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  821. Print_SSL
  822. }
  823. Apache_log() {
  824. while :; do echo
  825. read -e -p "Allow Apache access_log? [y/n]: " access_flag
  826. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  827. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  828. else
  829. break
  830. fi
  831. done
  832. if [ "${access_flag}" == 'n' ]; then
  833. Apache_log='CustomLog "/dev/null" common'
  834. else
  835. Apache_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  836. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  837. fi
  838. }
  839. Create_apache_conf() {
  840. if [ "${Apache_main_ver}" == '24' ]; then
  841. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  842. 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>")
  843. fi
  844. fi
  845. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  846. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  847. <VirtualHost *:80>
  848. ServerAdmin admin@example.com
  849. DocumentRoot "${vhostdir}"
  850. ServerName ${domain}
  851. ${Apache_Domain_alias}
  852. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  853. ${Apache_log}
  854. ${Apache_fcgi}
  855. <Directory "${vhostdir}">
  856. SetOutputFilter DEFLATE
  857. Options FollowSymLinks ExecCGI
  858. ${Apache_grant}
  859. AllowOverride All
  860. Order allow,deny
  861. Allow from all
  862. DirectoryIndex index.html index.php
  863. </Directory>
  864. </VirtualHost>
  865. EOF
  866. [ "${apache_ssl_flag}" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  867. <VirtualHost *:443>
  868. ServerAdmin admin@example.com
  869. DocumentRoot "${vhostdir}"
  870. ServerName ${domain}
  871. ${Apache_Domain_alias}
  872. ${Apache_SSL}
  873. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  874. ${Apache_log}
  875. ${Apache_fcgi}
  876. <Directory "${vhostdir}">
  877. SetOutputFilter DEFLATE
  878. Options FollowSymLinks ExecCGI
  879. ${Apache_grant}
  880. AllowOverride All
  881. Order allow,deny
  882. Allow from all
  883. DirectoryIndex index.html index.php
  884. </Directory>
  885. </VirtualHost>
  886. EOF
  887. echo
  888. ${apache_install_dir}/bin/apachectl -t
  889. if [ $? == 0 ]; then
  890. echo "Restart Apache......"
  891. ${apache_install_dir}/bin/apachectl -k graceful
  892. else
  893. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  894. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  895. exit 1
  896. fi
  897. printf "
  898. #######################################################################
  899. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  900. # For more information please visit https://oneinstack.com #
  901. #######################################################################
  902. "
  903. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  904. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  905. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  906. Print_SSL
  907. }
  908. Create_nginx_apache_modphp_conf() {
  909. # Nginx/Tengine/OpenResty
  910. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  911. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  912. server {
  913. ${Nginx_conf}
  914. server_name ${domain}${moredomainame};
  915. ${Nginx_log}
  916. index index.html index.htm index.php;
  917. root ${vhostdir};
  918. ${Nginx_redirect}
  919. ${anti_hotlinking}
  920. location / {
  921. try_files \$uri @apache;
  922. }
  923. location @apache {
  924. proxy_pass http://127.0.0.1:88;
  925. include proxy.conf;
  926. }
  927. location ~ .*\.(php|php5|cgi|pl)?$ {
  928. proxy_pass http://127.0.0.1:88;
  929. include proxy.conf;
  930. }
  931. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  932. expires 30d;
  933. access_log off;
  934. }
  935. location ~ .*\.(js|css)?$ {
  936. expires 7d;
  937. access_log off;
  938. }
  939. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  940. deny all;
  941. }
  942. location /.well-known {
  943. allow all;
  944. }
  945. }
  946. EOF
  947. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  948. echo
  949. ${web_install_dir}/sbin/nginx -t
  950. if [ $? == 0 ]; then
  951. echo "Reload Nginx......"
  952. ${web_install_dir}/sbin/nginx -s reload
  953. else
  954. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  955. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  956. fi
  957. # Apache
  958. if [ "${Apache_main_ver}" == '24' ]; then
  959. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  960. 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>")
  961. fi
  962. fi
  963. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  964. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  965. <VirtualHost *:88>
  966. ServerAdmin admin@example.com
  967. DocumentRoot "${vhostdir}"
  968. ServerName ${domain}
  969. ${Apache_Domain_alias}
  970. ${Apache_SSL}
  971. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  972. ${Apache_log}
  973. ${Apache_fcgi}
  974. <Directory "${vhostdir}">
  975. SetOutputFilter DEFLATE
  976. Options FollowSymLinks ExecCGI
  977. ${Apache_grant}
  978. AllowOverride All
  979. Order allow,deny
  980. Allow from all
  981. DirectoryIndex index.html index.php
  982. </Directory>
  983. </VirtualHost>
  984. EOF
  985. echo
  986. ${apache_install_dir}/bin/apachectl -t
  987. if [ $? == 0 ]; then
  988. echo "Restart Apache......"
  989. ${apache_install_dir}/bin/apachectl -k graceful
  990. else
  991. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  992. exit 1
  993. fi
  994. printf "
  995. #######################################################################
  996. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  997. # For more information please visit https://oneinstack.com #
  998. #######################################################################
  999. "
  1000. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  1001. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  1002. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  1003. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  1004. Print_SSL
  1005. }
  1006. Add_Vhost() {
  1007. if [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" ]; then
  1008. Choose_ENV
  1009. Input_Add_domain
  1010. Nginx_anti_hotlinking
  1011. if [ "${proxy_flag}" == "y" ]; then
  1012. Input_Add_proxy
  1013. Create_nginx_proxy_conf
  1014. else
  1015. Nginx_rewrite
  1016. if [ "${NGX_FLAG}" == "java" ]; then
  1017. Nginx_log
  1018. Create_nginx_tomcat_conf
  1019. else
  1020. Nginx_log
  1021. Create_nginx_phpfpm_conf
  1022. fi
  1023. fi
  1024. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  1025. Choose_ENV
  1026. Input_Add_domain
  1027. Apache_log
  1028. Create_apache_conf
  1029. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  1030. Choose_ENV
  1031. Input_Add_domain
  1032. Create_tomcat_conf
  1033. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  1034. Choose_ENV
  1035. Input_Add_domain
  1036. Nginx_anti_hotlinking
  1037. if [ "${NGX_FLAG}" == "java" ]; then
  1038. Nginx_rewrite
  1039. Nginx_log
  1040. Create_nginx_tomcat_conf
  1041. elif [ "${NGX_FLAG}" == "php" ]; then
  1042. Nginx_log
  1043. Apache_log
  1044. Create_nginx_apache_modphp_conf
  1045. fi
  1046. else
  1047. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  1048. fi
  1049. }
  1050. Del_NGX_Vhost() {
  1051. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1052. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  1053. if [ -n "${Domain_List}" ]; then
  1054. echo
  1055. echo "Virtualhost list:"
  1056. echo ${CMSG}${Domain_List}${CEND}
  1057. while :; do echo
  1058. read -e -p "Please input a domain you want to delete: " domain
  1059. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1060. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1061. else
  1062. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  1063. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  1064. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  1065. [ -e "${web_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${web_install_dir}/conf/ssl/${domain}.{crt,key}
  1066. ${web_install_dir}/sbin/nginx -s reload
  1067. while :; do echo
  1068. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1069. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1070. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1071. else
  1072. break
  1073. fi
  1074. done
  1075. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1076. if [ "${quiet_flag}" != 'y' ]; then
  1077. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1078. char=$(get_char)
  1079. fi
  1080. rm -rf ${Directory}
  1081. fi
  1082. echo
  1083. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1084. [ -d ~/.acme.sh/${domain}_ecc ] && ~/.acme.sh/acme.sh --force --remove --ecc -d ${domain} > /dev/null 2>&1
  1085. echo "${CMSG}Domain: ${domain} has been deleted.${CEND}"
  1086. echo
  1087. else
  1088. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1089. fi
  1090. break
  1091. fi
  1092. done
  1093. else
  1094. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1095. fi
  1096. fi
  1097. }
  1098. Del_Apache_Vhost() {
  1099. if [ -e "${apache_install_dir}/bin/httpd" ]; then
  1100. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1101. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1102. ${apache_install_dir}/bin/apachectl -k graceful
  1103. else
  1104. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  1105. if [ -n "${Domain_List}" ]; then
  1106. echo
  1107. echo "Virtualhost list:"
  1108. echo ${CMSG}${Domain_List}${CEND}
  1109. while :; do echo
  1110. read -e -p "Please input a domain you want to delete: " domain
  1111. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1112. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1113. else
  1114. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  1115. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  1116. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1117. [ -e "${apache_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${apache_install_dir}/conf/ssl/${domain}.{crt,key}
  1118. ${apache_install_dir}/bin/apachectl -k graceful
  1119. while :; do echo
  1120. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1121. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1122. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1123. else
  1124. break
  1125. fi
  1126. done
  1127. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1128. if [ "${quiet_flag}" != 'y' ]; then
  1129. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1130. char=$(get_char)
  1131. fi
  1132. rm -rf ${Directory}
  1133. fi
  1134. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1135. [ -d ~/.acme.sh/${domain}_ecc ] && ~/.acme.sh/acme.sh --force --remove --ecc -d ${domain} > /dev/null 2>&1
  1136. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1137. else
  1138. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1139. fi
  1140. break
  1141. fi
  1142. done
  1143. else
  1144. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1145. fi
  1146. fi
  1147. fi
  1148. }
  1149. Del_Tomcat_Vhost() {
  1150. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  1151. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1152. if [ -n "$(echo ${domain} | grep '.*\..*')" ] && [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  1153. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  1154. rm -f ${tomcat_install_dir}/conf/vhost/${domain}.xml
  1155. service tomcat restart
  1156. fi
  1157. else
  1158. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  1159. if [ -n "${Domain_List}" ]; then
  1160. echo
  1161. echo "Virtualhost list:"
  1162. echo ${CMSG}${Domain_List}${CEND}
  1163. while :; do echo
  1164. read -e -p "Please input a domain you want to delete: " domain
  1165. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1166. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1167. else
  1168. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  1169. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  1170. rm -f ${tomcat_install_dir}/conf/vhost/${domain}.xml
  1171. service tomcat restart
  1172. while :; do echo
  1173. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1174. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1175. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1176. else
  1177. break
  1178. fi
  1179. done
  1180. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1181. if [ "${quiet_flag}" != 'y' ]; then
  1182. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1183. char=$(get_char)
  1184. fi
  1185. rm -rf ${Directory}
  1186. fi
  1187. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1188. else
  1189. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1190. fi
  1191. break
  1192. fi
  1193. done
  1194. else
  1195. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1196. fi
  1197. fi
  1198. fi
  1199. }
  1200. List_Vhost() {
  1201. [ -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")
  1202. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  1203. [ -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")
  1204. if [ -n "${Domain_List}" ]; then
  1205. echo
  1206. echo "Virtualhost list:"
  1207. for D in ${Domain_List}; do echo ${CMSG}${D}${CEND}; done
  1208. else
  1209. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1210. fi
  1211. }
  1212. if [ ${ARG_NUM} == 0 ]; then
  1213. Add_Vhost
  1214. else
  1215. [ "${add_flag}" == 'y' -o "${proxy_flag}" == 'y' -o "${sslquiet_flag}" == 'y' ] && Add_Vhost
  1216. [ "${list_flag}" == 'y' ] && List_Vhost
  1217. [ "${delete_flag}" == 'y' ] && { Del_NGX_Vhost; Del_Apache_Vhost; Del_Tomcat_Vhost; }
  1218. fi