vhost.sh 53 KB

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