vhost.sh 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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~80] 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]$|^80$ ]] && { echo "${CWARNING}mphp_ver input error! Please only input number 53~80${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" ]; 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. read -e -p "Please input a number:(Default 0 press Enter) " php_option
  325. php_option=${php_option:-0}
  326. if [[ ! ${php_option} =~ ^[0-9]$|^10$ ]]; then
  327. echo "${CWARNING}input error! Please only input number 0~10${CEND}"
  328. else
  329. break
  330. fi
  331. done
  332. fi
  333. [ "${php_option}" == '1' ] && mphp_ver=53
  334. [ "${php_option}" == '2' ] && mphp_ver=54
  335. [ "${php_option}" == '3' ] && mphp_ver=55
  336. [ "${php_option}" == '4' ] && mphp_ver=56
  337. [ "${php_option}" == '5' ] && mphp_ver=70
  338. [ "${php_option}" == '6' ] && mphp_ver=71
  339. [ "${php_option}" == '7' ] && mphp_ver=72
  340. [ "${php_option}" == '8' ] && mphp_ver=73
  341. [ "${php_option}" == '9' ] && mphp_ver=74
  342. [ "${php_option}" == '10' ] && mphp_ver=80
  343. [ ! -e "/dev/shm/php${mphp_ver}-cgi.sock" ] && unset mphp_ver
  344. fi
  345. case "${NGX_FLAG}" in
  346. "php")
  347. 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 }")
  348. ;;
  349. "java")
  350. NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
  351. ;;
  352. esac
  353. if [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ] && [ ! -e ~/.acme.sh/acme.sh ]; then
  354. pushd ${oneinstack_dir}/src > /dev/null
  355. [ ! -e acme.sh-master.tar.gz ] && wget -qc http://mirrors.linuxeye.com/oneinstack/src/acme.sh-master.tar.gz
  356. tar xzf acme.sh-master.tar.gz
  357. pushd acme.sh-master > /dev/null
  358. ./acme.sh --install > /dev/null 2>&1
  359. popd > /dev/null
  360. popd > /dev/null
  361. fi
  362. [ -e ~/.acme.sh/account.conf ] && sed -i '/^CERT_HOME=/d' ~/.acme.sh/account.conf
  363. if [[ "${Domian_Mode}" =~ ^[2-3]$ ]] || [ "${dnsapi_flag}" == 'y' ]; then
  364. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  365. nginx_ssl_flag=y
  366. PATH_SSL=${web_install_dir}/conf/ssl
  367. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL}
  368. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  369. apache_ssl_flag=y
  370. PATH_SSL=${apache_install_dir}/conf/ssl
  371. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL}
  372. fi
  373. elif [ "${Domian_Mode}" == 'q' ]; then
  374. exit 1
  375. fi
  376. while :; do echo
  377. read -e -p "Please input domain(example: www.example.com): " domain
  378. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  379. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  380. else
  381. break
  382. fi
  383. done
  384. 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
  385. [ -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"
  386. [ -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"
  387. [ -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"
  388. exit
  389. else
  390. echo "domain=${domain}"
  391. fi
  392. if [[ -z ${proxy_flag} || "${proxy_flag}" != 'y' ]]; then
  393. while :; do echo
  394. echo "Please input the directory for the domain:${domain} :"
  395. read -e -p "(Default directory: ${wwwroot_dir}/${domain}): " vhostdir
  396. if [ -n "${vhostdir}" -a -z "$(echo ${vhostdir} | grep '^/')" ]; then
  397. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  398. else
  399. if [ -z "${vhostdir}" ]; then
  400. vhostdir="${wwwroot_dir}/${domain}"
  401. echo "Virtual Host Directory=${CMSG}${vhostdir}${CEND}"
  402. fi
  403. echo
  404. echo "Create Virtul Host directory......"
  405. mkdir -p ${vhostdir}
  406. echo "set permissions of Virtual Host directory......"
  407. chown -R ${run_user}:${run_group} ${vhostdir}
  408. break
  409. fi
  410. done
  411. fi
  412. while :; do echo
  413. read -e -p "Do you want to add more domain name? [y/n]: " moredomainame_flag
  414. if [[ ! ${moredomainame_flag} =~ ^[y,n]$ ]]; then
  415. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  416. else
  417. break
  418. fi
  419. done
  420. if [ "${moredomainame_flag}" == 'y' ]; then
  421. while :; do echo
  422. read -e -p "Type domainname or IP(example: example.com other.example.com): " moredomain
  423. if [ -z "$(echo ${moredomain} | grep '.*\..*')" ]; then
  424. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  425. else
  426. [ "${moredomain}" == "${domain}" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  427. echo domain list="$moredomain"
  428. moredomainame=" $moredomain"
  429. break
  430. fi
  431. done
  432. Apache_Domain_alias=ServerAlias${moredomainame}
  433. Tomcat_Domain_alias=$(for D in $(echo ${moredomainame}); do echo "<Alias>${D}</Alias>"; done)
  434. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  435. while :; do echo
  436. read -e -p "Do you want to redirect from ${moredomain} to ${domain}? [y/n]: " redirect_flag
  437. if [[ ! ${redirect_flag} =~ ^[y,n]$ ]]; then
  438. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  439. else
  440. break
  441. fi
  442. done
  443. [ "${redirect_flag}" == 'y' ] && Nginx_redirect="if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }"
  444. fi
  445. fi
  446. if [ "${nginx_ssl_flag}" == 'y' ]; then
  447. while :; do echo
  448. read -e -p "Do you want to redirect all HTTP requests to HTTPS? [y/n]: " https_flag
  449. if [[ ! ${https_flag} =~ ^[y,n]$ ]]; then
  450. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  451. else
  452. break
  453. fi
  454. done
  455. if [[ "$(${web_install_dir}/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  456. LISTENOPT="443 ssl http2"
  457. else
  458. LISTENOPT="443 ssl spdy"
  459. fi
  460. Create_SSL
  461. if [ -n "`ifconfig | grep inet6`" ]; then
  462. 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")
  463. else
  464. 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")
  465. fi
  466. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  467. elif [ "$apache_ssl_flag" == 'y' ]; then
  468. Create_SSL
  469. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  470. [ -z "$(grep 'Listen 443' ${apache_install_dir}/conf/httpd.conf)" ] && sed -i "s@Listen 80@&\nListen 443@" ${apache_install_dir}/conf/httpd.conf
  471. [ -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
  472. else
  473. if [ -n "`ifconfig | grep inet6`" ]; then
  474. Nginx_conf=$(echo -e "listen 80;\n listen [::]:80;")
  475. else
  476. Nginx_conf=$(echo -e "listen 80;")
  477. fi
  478. fi
  479. }
  480. Nginx_anti_hotlinking() {
  481. while :; do echo
  482. read -e -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_flag
  483. if [[ ! ${anti_hotlinking_flag} =~ ^[y,n]$ ]]; then
  484. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  485. else
  486. break
  487. fi
  488. done
  489. if [ -n "$(echo ${domain} | grep '.*\..*\..*')" ]; then
  490. domain_allow="*.${domain#*.} ${domain}"
  491. else
  492. domain_allow="*.${domain} ${domain}"
  493. fi
  494. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  495. if [ "${moredomainame_flag}" == 'y' -a "${moredomain}" != "*.${domain}" ]; then
  496. domain_allow_all=${domain_allow}${moredomainame}
  497. else
  498. domain_allow_all=${domain_allow}
  499. fi
  500. domain_allow_all=`echo ${domain_allow_all} | tr ' ' '\n' | awk '!a[$1]++' | xargs`
  501. 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 }")
  502. fi
  503. }
  504. Nginx_rewrite() {
  505. [ ! -d "${web_install_dir}/conf/rewrite" ] && mkdir ${web_install_dir}/conf/rewrite
  506. while :; do echo
  507. read -e -p "Allow Rewrite rule? [y/n]: " rewrite_flag
  508. if [[ ! "${rewrite_flag}" =~ ^[y,n]$ ]]; then
  509. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  510. else
  511. break
  512. fi
  513. done
  514. if [ "${rewrite_flag}" == 'n' ]; then
  515. rewrite="none"
  516. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  517. else
  518. echo
  519. echo "Please input the rewrite of programme :"
  520. echo "${CMSG}wordpress${CEND},${CMSG}opencart${CEND},${CMSG}magento2${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND},${CMSG}codeigniter${CEND},${CMSG}laravel${CEND}"
  521. 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."
  522. read -e -p "(Default rewrite: other): " rewrite
  523. if [ "${rewrite}" == "" ]; then
  524. rewrite="other"
  525. fi
  526. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  527. [ "${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 }")
  528. [ "${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 }")
  529. [ "${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 }")
  530. if [[ ! "${rewrite}" =~ ^magento2$|^pathinfo$ ]]; then
  531. if [ -e "config/${rewrite}.conf" ]; then
  532. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/rewrite/${rewrite}.conf
  533. else
  534. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  535. fi
  536. fi
  537. fi
  538. }
  539. Nginx_log() {
  540. while :; do echo
  541. read -e -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_flag
  542. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  543. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  544. else
  545. break
  546. fi
  547. done
  548. if [ "${access_flag}" == 'n' ]; then
  549. Nginx_log="access_log off;"
  550. else
  551. Nginx_log="access_log ${wwwlogs_dir}/${domain}_nginx.log combined;"
  552. echo "You access log file=${CMSG}${wwwlogs_dir}/${domain}_nginx.log${CEND}"
  553. fi
  554. }
  555. Create_nginx_tomcat_conf() {
  556. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  557. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  558. server {
  559. ${Nginx_conf}
  560. server_name ${domain}${moredomainame};
  561. ${Nginx_log}
  562. index index.html index.htm index.jsp;
  563. root ${vhostdir};
  564. ${Nginx_redirect}
  565. #error_page 404 /404.html;
  566. #error_page 502 /502.html;
  567. ${anti_hotlinking}
  568. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  569. expires 30d;
  570. access_log off;
  571. }
  572. location ~ .*\.(js|css)?$ {
  573. expires 7d;
  574. access_log off;
  575. }
  576. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  577. deny all;
  578. }
  579. ${NGX_CONF}
  580. }
  581. EOF
  582. [ "${https_flag}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  583. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  584. <Host name="${domain}" appBase="${vhostdir}" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  585. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  586. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  587. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  588. <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For"
  589. protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>
  590. </Host>
  591. EOF
  592. [ -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
  593. [ -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
  594. echo
  595. ${web_install_dir}/sbin/nginx -t
  596. if [ $? == 0 ]; then
  597. echo "Reload Nginx......"
  598. ${web_install_dir}/sbin/nginx -s reload
  599. service tomcat restart
  600. else
  601. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  602. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  603. exit 1
  604. fi
  605. printf "
  606. #######################################################################
  607. # OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+ #
  608. # For more information please visit https://oneinstack.com #
  609. #######################################################################
  610. "
  611. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  612. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  613. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  614. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  615. Print_SSL
  616. }
  617. Create_tomcat_conf() {
  618. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  619. <Host name="${domain}" appBase="webapps" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  620. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  621. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  622. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  623. </Host>
  624. EOF
  625. [ -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
  626. [ -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
  627. echo
  628. service tomcat restart
  629. printf "
  630. #######################################################################
  631. # OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+ #
  632. # For more information please visit https://oneinstack.com #
  633. #######################################################################
  634. "
  635. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  636. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  637. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  638. echo "$(printf "%-30s" "index url:")${CMSG}http://${domain}:8080/${CEND}"
  639. }
  640. Create_nginx_phpfpm_conf() {
  641. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  642. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  643. server {
  644. ${Nginx_conf}
  645. server_name ${domain}${moredomainame};
  646. ${Nginx_log}
  647. index index.html index.htm index.php;
  648. root ${vhostdir};
  649. ${Nginx_redirect}
  650. include ${web_install_dir}/conf/rewrite/${rewrite}.conf;
  651. #error_page 404 /404.html;
  652. #error_page 502 /502.html;
  653. ${anti_hotlinking}
  654. ${NGX_CONF}
  655. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  656. expires 30d;
  657. access_log off;
  658. }
  659. location ~ .*\.(js|css)?$ {
  660. expires 7d;
  661. access_log off;
  662. }
  663. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  664. deny all;
  665. }
  666. }
  667. EOF
  668. [ "${rewrite}" == 'pathinfo' ] && sed -i '/pathinfo.conf;$/d' ${web_install_dir}/conf/vhost/${domain}.conf
  669. if [ "${rewrite}" == 'magento2' -a -e "config/${rewrite}.conf" ]; then
  670. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/vhost/${domain}.conf
  671. sed -i "s@/dev/shm/php-cgi.sock@/dev/shm/php${mphp_ver}-cgi.sock@g" ${web_install_dir}/conf/vhost/${domain}.conf
  672. sed -i "s@^ set \$MAGE_ROOT.*;@ set \$MAGE_ROOT ${vhostdir};@" ${web_install_dir}/conf/vhost/${domain}.conf
  673. sed -i "s@^ server_name.*;@ server_name ${domain}${moredomainame};@" ${web_install_dir}/conf/vhost/${domain}.conf
  674. sed -i "s@^ server_name.*;@&\n ${Nginx_log}@" ${web_install_dir}/conf/vhost/${domain}.conf
  675. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  676. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  677. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  678. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  679. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  680. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  681. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  682. 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
  683. fi
  684. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  685. if [ "${nginx_ssl_flag}" == 'y' ]; then
  686. sed -i "s@^ listen 80;@&\n listen ${LISTENOPT};@" ${web_install_dir}/conf/vhost/${domain}.conf
  687. sed -i "s@^ server_name.*;@&\n ssl_stapling_verify on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  688. sed -i "s@^ server_name.*;@&\n ssl_stapling on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  689. sed -i "s@^ server_name.*;@&\n add_header Strict-Transport-Security max-age=15768000;@" ${web_install_dir}/conf/vhost/${domain}.conf
  690. sed -i "s@^ server_name.*;@&\n ssl_buffer_size 1400;@" ${web_install_dir}/conf/vhost/${domain}.conf
  691. sed -i "s@^ server_name.*;@&\n ssl_session_cache builtin:1000 shared:SSL:10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  692. sed -i "s@^ server_name.*;@&\n ssl_session_timeout 10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  693. sed -i "s@^ server_name.*;@&\n ssl_prefer_server_ciphers on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  694. 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
  695. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;@" ${web_install_dir}/conf/vhost/${domain}.conf
  696. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  697. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  698. fi
  699. fi
  700. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  701. echo
  702. ${web_install_dir}/sbin/nginx -t
  703. if [ $? == 0 ]; then
  704. echo "Reload Nginx......"
  705. ${web_install_dir}/sbin/nginx -s reload
  706. else
  707. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  708. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  709. exit 1
  710. fi
  711. printf "
  712. #######################################################################
  713. # OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+ #
  714. # For more information please visit https://oneinstack.com #
  715. #######################################################################
  716. "
  717. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  718. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  719. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  720. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  721. Print_SSL
  722. }
  723. Create_nginx_proxy_conf() {
  724. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  725. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  726. server {
  727. ${Nginx_conf}
  728. server_name ${domain}${moredomainame};
  729. ${Nginx_log}
  730. index index.html index.htm index.php;
  731. ${Nginx_redirect}
  732. location / {
  733. proxy_pass ${Proxy_Pass};
  734. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  735. proxy_set_header Host \$http_host;
  736. proxy_set_header X-NginX-Proxy true;
  737. proxy_http_version 1.1;
  738. proxy_set_header Upgrade \$http_upgrade;
  739. proxy_set_header Connection "upgrade";
  740. proxy_max_temp_file_size 0;
  741. }
  742. #error_page 404 /404.html;
  743. #error_page 502 /502.html;
  744. ${anti_hotlinking}
  745. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  746. expires 30d;
  747. access_log off;
  748. }
  749. location ~ .*\.(js|css)?$ {
  750. expires 7d;
  751. access_log off;
  752. }
  753. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  754. deny all;
  755. }
  756. }
  757. EOF
  758. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  759. if [ "${nginx_ssl_flag}" == 'y' ]; then
  760. sed -i "s@^ listen 80;@&\n listen ${LISTENOPT};@" ${web_install_dir}/conf/vhost/${domain}.conf
  761. sed -i "s@^ server_name.*;@&\n ssl_stapling_verify on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  762. sed -i "s@^ server_name.*;@&\n ssl_stapling on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  763. sed -i "s@^ server_name.*;@&\n add_header Strict-Transport-Security max-age=15768000;@" ${web_install_dir}/conf/vhost/${domain}.conf
  764. sed -i "s@^ server_name.*;@&\n ssl_buffer_size 1400;@" ${web_install_dir}/conf/vhost/${domain}.conf
  765. sed -i "s@^ server_name.*;@&\n ssl_session_cache builtin:1000 shared:SSL:10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  766. sed -i "s@^ server_name.*;@&\n ssl_session_timeout 10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  767. sed -i "s@^ server_name.*;@&\n ssl_prefer_server_ciphers on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  768. 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
  769. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;@" ${web_install_dir}/conf/vhost/${domain}.conf
  770. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  771. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  772. fi
  773. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  774. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  775. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  776. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  777. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  778. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  779. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  780. 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
  781. fi
  782. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  783. echo
  784. ${web_install_dir}/sbin/nginx -t
  785. if [ $? == 0 ]; then
  786. echo "Reload Nginx......"
  787. ${web_install_dir}/sbin/nginx -s reload
  788. else
  789. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  790. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  791. exit 1
  792. fi
  793. printf "
  794. #######################################################################
  795. # OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+ #
  796. # For more information please visit https://oneinstack.com #
  797. #######################################################################
  798. "
  799. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  800. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  801. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  802. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  803. Print_SSL
  804. }
  805. Apache_log() {
  806. while :; do echo
  807. read -e -p "Allow Apache access_log? [y/n]: " access_flag
  808. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  809. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  810. else
  811. break
  812. fi
  813. done
  814. if [ "${access_flag}" == 'n' ]; then
  815. Apache_log='CustomLog "/dev/null" common'
  816. else
  817. Apache_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  818. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  819. fi
  820. }
  821. Create_apache_conf() {
  822. if [ "${Apache_main_ver}" == '24' ]; then
  823. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  824. 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>")
  825. fi
  826. fi
  827. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  828. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  829. <VirtualHost *:80>
  830. ServerAdmin admin@example.com
  831. DocumentRoot "${vhostdir}"
  832. ServerName ${domain}
  833. ${Apache_Domain_alias}
  834. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  835. ${Apache_log}
  836. ${Apache_fcgi}
  837. <Directory "${vhostdir}">
  838. SetOutputFilter DEFLATE
  839. Options FollowSymLinks ExecCGI
  840. ${Apache_grant}
  841. AllowOverride All
  842. Order allow,deny
  843. Allow from all
  844. DirectoryIndex index.html index.php
  845. </Directory>
  846. </VirtualHost>
  847. EOF
  848. [ "$apache_ssl_flag" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  849. <VirtualHost *:443>
  850. ServerAdmin admin@example.com
  851. DocumentRoot "${vhostdir}"
  852. ServerName ${domain}
  853. ${Apache_Domain_alias}
  854. ${Apache_SSL}
  855. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  856. ${Apache_log}
  857. ${Apache_fcgi}
  858. <Directory "${vhostdir}">
  859. SetOutputFilter DEFLATE
  860. Options FollowSymLinks ExecCGI
  861. ${Apache_grant}
  862. AllowOverride All
  863. Order allow,deny
  864. Allow from all
  865. DirectoryIndex index.html index.php
  866. </Directory>
  867. </VirtualHost>
  868. EOF
  869. echo
  870. ${apache_install_dir}/bin/apachectl -t
  871. if [ $? == 0 ]; then
  872. echo "Restart Apache......"
  873. ${apache_install_dir}/bin/apachectl -k graceful
  874. else
  875. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  876. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  877. exit 1
  878. fi
  879. printf "
  880. #######################################################################
  881. # OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+ #
  882. # For more information please visit https://oneinstack.com #
  883. #######################################################################
  884. "
  885. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  886. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  887. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  888. Print_SSL
  889. }
  890. Create_nginx_apache_modphp_conf() {
  891. # Nginx/Tengine/OpenResty
  892. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  893. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  894. server {
  895. ${Nginx_conf}
  896. server_name ${domain}${moredomainame};
  897. ${Nginx_log}
  898. index index.html index.htm index.php;
  899. root ${vhostdir};
  900. ${Nginx_redirect}
  901. ${anti_hotlinking}
  902. location / {
  903. try_files \$uri @apache;
  904. }
  905. location @apache {
  906. proxy_pass http://127.0.0.1:88;
  907. include proxy.conf;
  908. }
  909. location ~ .*\.(php|php5|cgi|pl)?$ {
  910. proxy_pass http://127.0.0.1:88;
  911. include proxy.conf;
  912. }
  913. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  914. expires 30d;
  915. access_log off;
  916. }
  917. location ~ .*\.(js|css)?$ {
  918. expires 7d;
  919. access_log off;
  920. }
  921. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  922. deny 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