vhost.sh 56 KB

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