vhost.sh 57 KB

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