1
0

vhost.sh 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 6+ Debian 7+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/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/RadHat 6+ Debian 7+ and Ubuntu 12+ #
  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. Usage() {
  28. printf "
  29. Usage: $0 [ ${CMSG}add${CEND} | ${CMSG}del${CEND} | ${CMSG}list${CEND} ]
  30. ${CMSG}add${CEND} --->Add Virtualhost
  31. ${CMSG}del${CEND} --->Delete Virtualhost
  32. ${CMSG}list${CEND} --->List Virtualhost
  33. "
  34. }
  35. Choose_env() {
  36. if [ -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  37. Number=111
  38. while :; do echo
  39. echo "Please choose to use environment:"
  40. echo -e "\t${CMSG}1${CEND}. Use php"
  41. echo -e "\t${CMSG}2${CEND}. Use java"
  42. echo -e "\t${CMSG}3${CEND}. Use hhvm"
  43. read -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  44. [ -z "${ENV_FLAG}" ] && ENV_FLAG=1
  45. if [[ ! ${ENV_FLAG} =~ ^[1-3]$ ]]; then
  46. echo "${CWARNING}input error! Please only input number 1~3${CEND}"
  47. else
  48. break
  49. fi
  50. done
  51. case "${ENV_FLAG}" in
  52. 1)
  53. NGX_FLAG=php
  54. ;;
  55. 2)
  56. NGX_FLAG=java
  57. ;;
  58. 3)
  59. NGX_FLAG=hhvm
  60. ;;
  61. esac
  62. elif [ -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a ! -e "/usr/bin/hhvm" ]; then
  63. Number=110
  64. while :; do echo
  65. echo "Please choose to use environment:"
  66. echo -e "\t${CMSG}1${CEND}. Use php"
  67. echo -e "\t${CMSG}2${CEND}. Use java"
  68. read -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  69. [ -z "${ENV_FLAG}" ] && ENV_FLAG=1
  70. if [[ ! ${ENV_FLAG} =~ ^[1-2]$ ]]; then
  71. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  72. else
  73. break
  74. fi
  75. done
  76. [ "${ENV_FLAG}" == '1' ] && NGX_FLAG=php
  77. [ "${ENV_FLAG}" == '2' ] && NGX_FLAG=java
  78. elif [ -e "${php_install_dir}/bin/phpize" -a ! -e "${tomcat_install_dir}/conf/server.xml" -a ! -e "/usr/bin/hhvm" ]; then
  79. Number=100
  80. NGX_FLAG=php
  81. elif [ -e "${php_install_dir}/bin/phpize" -a ! -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  82. Number=101
  83. while :; do echo
  84. echo "Please choose to use environment:"
  85. echo -e "\t${CMSG}1${CEND}. Use php"
  86. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  87. read -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  88. [ -z "${ENV_FLAG}" ] && ENV_FLAG=1
  89. if [[ ! ${ENV_FLAG} =~ ^[1-2]$ ]]; then
  90. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  91. else
  92. break
  93. fi
  94. done
  95. [ "${ENV_FLAG}" == '1' ] && NGX_FLAG=php
  96. [ "${ENV_FLAG}" == '2' ] && NGX_FLAG=hhvm
  97. elif [ ! -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  98. Number=011
  99. while :; do echo
  100. echo "Please choose to use environment:"
  101. echo -e "\t${CMSG}1${CEND}. Use java"
  102. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  103. read -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  104. [ -z "${ENV_FLAG}" ] && ENV_FLAG=1
  105. if [[ ! ${ENV_FLAG} =~ ^[1-2]$ ]]; then
  106. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  107. else
  108. break
  109. fi
  110. done
  111. [ "${ENV_FLAG}" == '1' ] && NGX_FLAG=java
  112. [ "${ENV_FLAG}" == '2' ] && NGX_FLAG=hhvm
  113. elif [ ! -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" -a ! -e "/usr/bin/hhvm" ]; then
  114. Number=010
  115. NGX_FLAG=java
  116. elif [ ! -e "${php_install_dir}/bin/phpize" -a ! -e "${tomcat_install_dir}/conf/server.xml" -a -e "/usr/bin/hhvm" ]; then
  117. Number=001
  118. NGX_FLAG=hhvm
  119. else
  120. Number=000
  121. NGX_FLAG=php
  122. fi
  123. case "${NGX_FLAG}" in
  124. "php")
  125. NGX_CONF=$(echo -e "location ~ [^/]\.php(/|$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  126. ;;
  127. "java")
  128. NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
  129. ;;
  130. "hhvm")
  131. 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 }")
  132. ;;
  133. esac
  134. }
  135. Create_SSL() {
  136. if [ "${Domian_Mode}" == '2' ]; then
  137. printf "
  138. You are about to be asked to enter information that will be incorporated
  139. into your certificate request.
  140. What you are about to enter is what is called a Distinguished Name or a DN.
  141. There are quite a few fields but you can leave some blank
  142. For some fields there will be a default value,
  143. If you enter '.', the field will be left blank.
  144. "
  145. echo
  146. read -p "Country Name (2 letter code) [CN]: " SELFSIGNEDSSL_C
  147. [ -z "${SELFSIGNEDSSL_C}" ] && SELFSIGNEDSSL_C="CN"
  148. echo
  149. read -p "State or Province Name (full name) [Shanghai]: " SELFSIGNEDSSL_ST
  150. [ -z "${SELFSIGNEDSSL_ST}" ] && SELFSIGNEDSSL_ST="Shanghai"
  151. echo
  152. read -p "Locality Name (eg, city) [Shanghai]: " SELFSIGNEDSSL_L
  153. [ -z "${SELFSIGNEDSSL_L}" ] && SELFSIGNEDSSL_L="Shanghai"
  154. echo
  155. read -p "Organization Name (eg, company) [Example Inc.]: " SELFSIGNEDSSL_O
  156. [ -z "${SELFSIGNEDSSL_O}" ] && SELFSIGNEDSSL_O="Example Inc."
  157. echo
  158. read -p "Organizational Unit Name (eg, section) [IT Dept.]: " SELFSIGNEDSSL_OU
  159. [ -z "${SELFSIGNEDSSL_OU}" ] && SELFSIGNEDSSL_OU="IT Dept."
  160. openssl req -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
  161. 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
  162. elif [ "${Domian_Mode}" == '3' ]; then
  163. echo "${CMSG}More: https://oneinstack.com/faq/letsencrypt${CEND}"
  164. if [ "${moredomain}" == "*.${domain}" ]; then
  165. while :; do echo
  166. read -p "Please enter your DNS provider: " DNS_PRO
  167. 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."
  168. if [ -e ~/.acme.sh/dnsapi/dns_${DNS_PRO}.sh ]; then
  169. break
  170. else
  171. echo "${CWARNING}You DNS api mode is not supported${CEND}"
  172. fi
  173. done
  174. while :; do echo
  175. echo "Syntax: export Key1=Value1 ; export Key2=Value1"
  176. read -p "Please enter your dnsapi parameters: " DNS_PAR
  177. echo
  178. eval $DNS_PAR
  179. if [ $? == 0 ]; then
  180. break
  181. else
  182. echo "${CWARNING}Syntax error! PS: export Ali_Key=LTq ; export Ali_Secret=0q5E${CEND}"
  183. fi
  184. done
  185. ~/.acme.sh/acme.sh --issue --dns dns_${DNS_PRO} -d ${domain} -d ${moredomain}
  186. else
  187. if [ "${nginx_ssl_flag}" == 'y' ]; then
  188. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  189. echo "server { server_name ${domain}${moredomainame}; root ${vhostdir}; access_log off; }" > ${web_install_dir}/conf/vhost/${domain}.conf
  190. ${web_install_dir}/sbin/nginx -s reload
  191. fi
  192. if [ "${apache_ssl_flag}" == 'y' ]; then
  193. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  194. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  195. <VirtualHost *:80>
  196. ServerAdmin admin@example.com
  197. DocumentRoot "${vhostdir}"
  198. ServerName ${domain}
  199. ${Apache_Domain_alias}
  200. <Directory "${vhostdir}">
  201. SetOutputFilter DEFLATE
  202. Options FollowSymLinks ExecCGI
  203. Require all granted
  204. AllowOverride All
  205. Order allow,deny
  206. Allow from all
  207. DirectoryIndex index.html index.php
  208. </Directory>
  209. </VirtualHost>
  210. EOF
  211. /etc/init.d/httpd restart > /dev/null
  212. fi
  213. auth_file="`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`".html
  214. auth_str='oneinstack'; echo ${auth_str} > ${vhostdir}/${auth_file}
  215. for D in ${domain} ${moredomainame}
  216. do
  217. curl_str=`curl --connect-timeout 30 -4 -s $D/${auth_file} 2>&1`
  218. [ "${curl_str}" != "${auth_str}" ] && { echo; echo "${CFAILURE}Let's Encrypt Verify error! DNS problem: NXDOMAIN looking up A for ${D}${CEND}"; }
  219. done
  220. rm -f ${vhostdir}/${auth_file}
  221. [ "${moredomainame_flag}" == 'y' ] && moredomainame_D="$(for D in ${moredomainame}; do echo -d ${D}; done)"
  222. ~/.acme.sh/acme.sh --issue -d ${domain} ${moredomainame_D} -w ${vhostdir}
  223. fi
  224. if [ -s ~/.acme.sh/${domain}/fullchain.cer ]; then
  225. [ -e "${PATH_SSL}/${domain}.crt" ] && rm -rf ${PATH_SSL}/${domain}.{crt,key}
  226. if [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  227. Command="/etc/init.d/nginx force-reload;/etc/init.d/httpd graceful"
  228. elif [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
  229. Command="/etc/init.d/nginx force-reload"
  230. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  231. Command="/etc/init.d/httpd graceful"
  232. fi
  233. ~/.acme.sh/acme.sh --install-cert -d ${domain} --fullchain-file ${PATH_SSL}/${domain}.crt --key-file ${PATH_SSL}/${domain}.key --reloadcmd "${Command}" > /dev/null
  234. else
  235. echo "${CFAILURE}Error: Create Let's Encrypt SSL Certificate failed! ${CEND}"
  236. exit 1
  237. fi
  238. fi
  239. }
  240. Print_ssl() {
  241. if [ "${Domian_Mode}" == '2' ]; then
  242. echo "$(printf "%-30s" "Self-signed SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  243. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  244. echo "$(printf "%-30s" "SSL CSR File:")${CMSG}${PATH_SSL}/${domain}.csr${CEND}"
  245. elif [ "${Domian_Mode}" == '3' ]; then
  246. echo "$(printf "%-30s" "Let's Encrypt SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  247. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  248. fi
  249. }
  250. Input_Add_domain() {
  251. while :;do
  252. printf "
  253. What Are You Doing?
  254. \t${CMSG}1${CEND}. Use HTTP Only
  255. \t${CMSG}2${CEND}. Use your own SSL Certificate and Key
  256. \t${CMSG}3${CEND}. Use Let's Encrypt to Create SSL Certificate and Key
  257. \t${CMSG}q${CEND}. Exit
  258. "
  259. read -p "Please input the correct option: " Domian_Mode
  260. if [[ ! "${Domian_Mode}" =~ ^[1-3,q]$ ]]; then
  261. echo "${CFAILURE}input error! Please only input 1~3 and q${CEND}"
  262. else
  263. if [ "${Domian_Mode}" == '3' ] && [ ! -e ~/.acme.sh/acme.sh ]; then
  264. pushd ${oneinstack_dir}/src > /dev/null
  265. [ ! -e acme.sh-master.tar.gz ] && wget -qc http://mirrors.linuxeye.com/oneinstack/src/acme.sh-master.tar.gz
  266. tar xzf acme.sh-master.tar.gz
  267. pushd acme.sh-master > /dev/null
  268. ./acme.sh --install > /dev/null 2>&1
  269. popd > /dev/null
  270. popd > /dev/null
  271. fi
  272. if [[ "${Domian_Mode}" =~ ^[2-3]$ ]]; then
  273. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  274. nginx_ssl_flag=y
  275. PATH_SSL=${web_install_dir}/conf/ssl
  276. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL};
  277. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/apachectl" ]; then
  278. apache_ssl_flag=y
  279. PATH_SSL=${apache_install_dir}/conf/ssl
  280. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL};
  281. fi
  282. elif [ "${Domian_Mode}" == 'q' ]; then
  283. exit 1
  284. fi
  285. break
  286. fi
  287. done
  288. while :; do echo
  289. read -p "Please input domain(example: www.example.com): " domain
  290. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  291. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  292. else
  293. break
  294. fi
  295. done
  296. 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
  297. [ -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"
  298. [ -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"
  299. [ -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"
  300. exit
  301. else
  302. echo "domain=${domain}"
  303. fi
  304. while :; do echo
  305. echo "Please input the directory for the domain:${domain} :"
  306. read -p "(Default directory: ${wwwroot_dir}/${domain}): " vhostdir
  307. if [ -n "${vhostdir}" -a -z "$(echo ${vhostdir} | grep '^/')" ]; then
  308. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  309. else
  310. if [ -z "${vhostdir}" ]; then
  311. vhostdir="${wwwroot_dir}/${domain}"
  312. echo "Virtual Host Directory=${CMSG}${vhostdir}${CEND}"
  313. fi
  314. echo
  315. echo "Create Virtul Host directory......"
  316. mkdir -p ${vhostdir}
  317. echo "set permissions of Virtual Host directory......"
  318. chown -R ${run_user}.${run_user} ${vhostdir}
  319. break
  320. fi
  321. done
  322. while :; do echo
  323. read -p "Do you want to add more domain name? [y/n]: " moredomainame_flag
  324. if [[ ! ${moredomainame_flag} =~ ^[y,n]$ ]]; then
  325. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  326. else
  327. break
  328. fi
  329. done
  330. if [ "${moredomainame_flag}" == 'y' ]; then
  331. while :; do echo
  332. read -p "Type domainname or IP(example: example.com other.example.com): " moredomain
  333. if [ -z "$(echo ${moredomain} | grep '.*\..*')" ]; then
  334. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  335. else
  336. [ "${moredomain}" == "${domain}" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  337. echo domain list="$moredomain"
  338. moredomainame=" $moredomain"
  339. break
  340. fi
  341. done
  342. Apache_Domain_alias=ServerAlias${moredomainame}
  343. Tomcat_Domain_alias=$(for D in $(echo ${moredomainame}); do echo "<Alias>${D}</Alias>"; done)
  344. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  345. while :; do echo
  346. read -p "Do you want to redirect from ${moredomain} to ${domain}? [y/n]: " redirect_flag
  347. if [[ ! ${redirect_flag} =~ ^[y,n]$ ]]; then
  348. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  349. else
  350. break
  351. fi
  352. done
  353. [ "${redirect_flag}" == 'y' ] && Nginx_redirect="if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }"
  354. fi
  355. fi
  356. if [ "${nginx_ssl_flag}" == 'y' ]; then
  357. while :; do echo
  358. read -p "Do you want to redirect all HTTP requests to HTTPS? [y/n]: " https_flag
  359. if [[ ! ${https_flag} =~ ^[y,n]$ ]]; then
  360. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  361. else
  362. break
  363. fi
  364. done
  365. if [[ "$(${web_install_dir}/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  366. LISTENOPT="443 ssl http2"
  367. else
  368. LISTENOPT="443 ssl spdy"
  369. fi
  370. Create_SSL
  371. 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;\n ssl_ciphers 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")
  372. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  373. elif [ "$apache_ssl_flag" == 'y' ]; then
  374. Create_SSL
  375. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  376. [ -z "$(grep 'Listen 443' ${apache_install_dir}/conf/httpd.conf)" ] && sed -i "s@Listen 80@&\nListen 443@" ${apache_install_dir}/conf/httpd.conf
  377. [ -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
  378. else
  379. Nginx_conf="listen 80;"
  380. fi
  381. }
  382. Nginx_anti_hotlinking() {
  383. while :; do echo
  384. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_flag
  385. if [[ ! $anti_hotlinking_flag =~ ^[y,n]$ ]]; then
  386. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  387. else
  388. break
  389. fi
  390. done
  391. if [ -n "$(echo ${domain} | grep '.*\..*\..*')" ]; then
  392. domain_allow="*.${domain#*.} ${domain}"
  393. else
  394. domain_allow="*.${domain} ${domain}"
  395. fi
  396. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  397. if [ "${moredomainame_flag}" == 'y' ]; then
  398. domain_allow_all=${domain_allow}${moredomainame}
  399. else
  400. domain_allow_all=${domain_allow}
  401. fi
  402. 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 }")
  403. else
  404. anti_hotlinking=
  405. fi
  406. }
  407. Nginx_rewrite() {
  408. [ ! -d "${web_install_dir}/conf/rewrite" ] && mkdir ${web_install_dir}/conf/rewrite
  409. while :; do echo
  410. read -p "Allow Rewrite rule? [y/n]: " rewrite_flag
  411. if [[ ! "${rewrite_flag}" =~ ^[y,n]$ ]]; then
  412. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  413. else
  414. break
  415. fi
  416. done
  417. if [ "${rewrite_flag}" == 'n' ]; then
  418. rewrite="none"
  419. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  420. else
  421. echo
  422. echo "Please input the rewrite of programme :"
  423. echo "${CMSG}wordpress${CEND},${CMSG}opencart${CEND},${CMSG}magento2${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND},${CMSG}laravel${CEND},${CMSG}thinkphp${CEND},${CMSG}pathinfo${CEND},${CMSG}discuz${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}nextcloud${CEND} rewrite was exist."
  424. read -p "(Default rewrite: other): " rewrite
  425. if [ "${rewrite}" == "" ]; then
  426. rewrite="other"
  427. fi
  428. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  429. [ "${NGX_FLAG}" == 'php' -a "${rewrite}" == "thinkphp" ] && NGX_CONF=$(echo -e "location ~ \.php {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi_params;\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 }")
  430. [ "${NGX_FLAG}" == 'php' -a "${rewrite}" == "pathinfo" ] && NGX_CONF=$(echo -e "location / {\n if (!-e \$request_filename) {\n rewrite ^(.*)\$ /index.php?s=\$1 last;\n break;\n }\n }\n\n location ~ [^/]\.php(/|$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-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 }")
  431. if [ "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ]; then
  432. if [ -e "config/${rewrite}.conf" ]; then
  433. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/rewrite/${rewrite}.conf
  434. else
  435. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  436. fi
  437. fi
  438. fi
  439. }
  440. Nginx_log() {
  441. while :; do echo
  442. read -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_flag
  443. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  444. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  445. else
  446. break
  447. fi
  448. done
  449. if [ "${access_flag}" == 'n' ]; then
  450. N_log="access_log off;"
  451. else
  452. N_log="access_log ${wwwlogs_dir}/${domain}_nginx.log combined;"
  453. echo "You access log file=${CMSG}${wwwlogs_dir}/${domain}_nginx.log${CEND}"
  454. fi
  455. }
  456. Create_nginx_tomcat_conf() {
  457. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  458. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  459. server {
  460. ${Nginx_conf}
  461. server_name ${domain}${moredomainame};
  462. ${N_log}
  463. index index.html index.htm index.jsp;
  464. root ${vhostdir};
  465. ${Nginx_redirect}
  466. #error_page 404 /404.html;
  467. #error_page 502 /502.html;
  468. ${anti_hotlinking}
  469. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  470. expires 30d;
  471. access_log off;
  472. }
  473. location ~ .*\.(js|css)?$ {
  474. expires 7d;
  475. access_log off;
  476. }
  477. location ~ /\.ht {
  478. deny all;
  479. }
  480. ${NGX_CONF}
  481. }
  482. EOF
  483. [ "${https_flag}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  484. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  485. <Host name="${domain}" appBase="${vhostdir}" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  486. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  487. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  488. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  489. <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For"
  490. protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>
  491. </Host>
  492. EOF
  493. [ -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
  494. [ -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
  495. echo
  496. ${web_install_dir}/sbin/nginx -t
  497. if [ $? == 0 ]; then
  498. echo "Reload Nginx......"
  499. ${web_install_dir}/sbin/nginx -s reload
  500. /etc/init.d/tomcat restart
  501. else
  502. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  503. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  504. exit 1
  505. fi
  506. printf "
  507. #######################################################################
  508. # OneinStack for CentOS/RadHat 6+ Debian 7+ and Ubuntu 12+ #
  509. # For more information please visit https://oneinstack.com #
  510. #######################################################################
  511. "
  512. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  513. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  514. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  515. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  516. Print_ssl
  517. }
  518. Create_tomcat_conf() {
  519. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  520. <Host name="${domain}" appBase="webapps" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  521. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  522. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  523. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  524. </Host>
  525. EOF
  526. [ -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
  527. [ -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
  528. echo
  529. /etc/init.d/tomcat restart
  530. printf "
  531. #######################################################################
  532. # OneinStack for CentOS/RadHat 6+ Debian 7+ and Ubuntu 12+ #
  533. # For more information please visit https://oneinstack.com #
  534. #######################################################################
  535. "
  536. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  537. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  538. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  539. echo "$(printf "%-30s" "index url:")${CMSG}http://${domain}:8080/${CEND}"
  540. }
  541. Create_nginx_php-fpm_hhvm_conf() {
  542. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  543. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  544. server {
  545. ${Nginx_conf}
  546. server_name ${domain}${moredomainame};
  547. ${N_log}
  548. index index.html index.htm index.php;
  549. root ${vhostdir};
  550. ${Nginx_redirect}
  551. include ${web_install_dir}/conf/rewrite/${rewrite}.conf;
  552. #error_page 404 /404.html;
  553. #error_page 502 /502.html;
  554. ${anti_hotlinking}
  555. ${NGX_CONF}
  556. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  557. expires 30d;
  558. access_log off;
  559. }
  560. location ~ .*\.(js|css)?$ {
  561. expires 7d;
  562. access_log off;
  563. }
  564. location ~ /\.ht {
  565. deny all;
  566. }
  567. }
  568. EOF
  569. [ "${rewrite}" == 'pathinfo' ] && sed -i '/pathinfo.conf;$/d' ${web_install_dir}/conf/vhost/${domain}.conf
  570. if [ "${rewrite}" == 'magento2' -a -e "config/${rewrite}.conf" ]; then
  571. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/vhost/${domain}.conf
  572. sed -i "s@^ set \$MAGE_ROOT.*;@ set \$MAGE_ROOT ${vhostdir};@" ${web_install_dir}/conf/vhost/${domain}.conf
  573. sed -i "s@^ server_name.*;@ server_name ${domain}${moredomainame};@" ${web_install_dir}/conf/vhost/${domain}.conf
  574. sed -i "s@^ server_name.*;@&\n ${N_log}@" ${web_install_dir}/conf/vhost/${domain}.conf
  575. [ "${NGX_FLAG}" == 'hhvm' ] && sed -i 's@fastcgi_pass unix:.*;@fastcgi_pass unix:/var/log/hhvm/sock;@g' ${web_install_dir}/conf/vhost/${domain}.conf
  576. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  577. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  578. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  579. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  580. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  581. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  582. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  583. 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
  584. fi
  585. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  586. if [ "${nginx_ssl_flag}" == 'y' ]; then
  587. sed -i "s@^ listen 80;@&\n listen ${LISTENOPT};@" ${web_install_dir}/conf/vhost/${domain}.conf
  588. sed -i "s@^ server_name.*;@&\n ssl_stapling_verify on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  589. sed -i "s@^ server_name.*;@&\n ssl_stapling on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  590. sed -i "s@^ server_name.*;@&\n add_header Strict-Transport-Security max-age=15768000;@" ${web_install_dir}/conf/vhost/${domain}.conf
  591. sed -i "s@^ server_name.*;@&\n ssl_buffer_size 1400;@" ${web_install_dir}/conf/vhost/${domain}.conf
  592. sed -i "s@^ server_name.*;@&\n ssl_session_cache builtin:1000 shared:SSL:10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  593. sed -i "s@^ server_name.*;@&\n ssl_session_timeout 10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  594. sed -i "s@^ server_name.*;@&\n ssl_prefer_server_ciphers on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  595. sed -i "s@^ server_name.*;@&\n ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:\!MD5;@" ${web_install_dir}/conf/vhost/${domain}.conf
  596. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2;@" ${web_install_dir}/conf/vhost/${domain}.conf
  597. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  598. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  599. fi
  600. fi
  601. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  602. echo
  603. ${web_install_dir}/sbin/nginx -t
  604. if [ $? == 0 ]; then
  605. echo "Reload Nginx......"
  606. ${web_install_dir}/sbin/nginx -s reload
  607. else
  608. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  609. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  610. exit 1
  611. fi
  612. printf "
  613. #######################################################################
  614. # OneinStack for CentOS/RadHat 6+ Debian 7+ and Ubuntu 12+ #
  615. # For more information please visit https://oneinstack.com #
  616. #######################################################################
  617. "
  618. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  619. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  620. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  621. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  622. Print_ssl
  623. }
  624. Apache_log() {
  625. while :; do echo
  626. read -p "Allow Apache access_log? [y/n]: " access_flag
  627. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  628. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  629. else
  630. break
  631. fi
  632. done
  633. if [ "${access_flag}" == 'n' ]; then
  634. A_log='CustomLog "/dev/null" common'
  635. else
  636. A_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  637. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  638. fi
  639. }
  640. Create_apache_conf() {
  641. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP='Require all granted' || R_TMP=
  642. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  643. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  644. <VirtualHost *:80>
  645. ServerAdmin admin@example.com
  646. DocumentRoot "${vhostdir}"
  647. ServerName ${domain}
  648. ${Apache_Domain_alias}
  649. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  650. ${A_log}
  651. <Directory "${vhostdir}">
  652. SetOutputFilter DEFLATE
  653. Options FollowSymLinks ExecCGI
  654. ${R_TMP}
  655. AllowOverride All
  656. Order allow,deny
  657. Allow from all
  658. DirectoryIndex index.html index.php
  659. </Directory>
  660. </VirtualHost>
  661. EOF
  662. [ "$apache_ssl_flag" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  663. <VirtualHost *:443>
  664. ServerAdmin admin@example.com
  665. DocumentRoot "${vhostdir}"
  666. ServerName ${domain}
  667. ${Apache_Domain_alias}
  668. ${Apache_SSL}
  669. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  670. ${A_log}
  671. <Directory "${vhostdir}">
  672. SetOutputFilter DEFLATE
  673. Options FollowSymLinks ExecCGI
  674. ${R_TMP}
  675. AllowOverride All
  676. Order allow,deny
  677. Allow from all
  678. DirectoryIndex index.html index.php
  679. </Directory>
  680. </VirtualHost>
  681. EOF
  682. echo
  683. ${apache_install_dir}/bin/apachectl -t
  684. if [ $? == 0 ]; then
  685. echo "Restart Apache......"
  686. /etc/init.d/httpd restart
  687. else
  688. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  689. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  690. exit 1
  691. fi
  692. printf "
  693. #######################################################################
  694. # OneinStack for CentOS/RadHat 6+ Debian 7+ and Ubuntu 12+ #
  695. # For more information please visit https://oneinstack.com #
  696. #######################################################################
  697. "
  698. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  699. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  700. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  701. Print_ssl
  702. }
  703. Create_nginx_apache_mod-php_conf() {
  704. # Nginx/Tengine/OpenResty
  705. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  706. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  707. server {
  708. ${Nginx_conf}
  709. server_name ${domain}${moredomainame};
  710. ${N_log}
  711. index index.html index.htm index.php;
  712. root ${vhostdir};
  713. ${Nginx_redirect}
  714. ${anti_hotlinking}
  715. location / {
  716. try_files \$uri @apache;
  717. }
  718. location @apache {
  719. proxy_pass http://127.0.0.1:88;
  720. include proxy.conf;
  721. }
  722. location ~ .*\.(php|php5|cgi|pl)?$ {
  723. proxy_pass http://127.0.0.1:88;
  724. include proxy.conf;
  725. }
  726. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  727. expires 30d;
  728. access_log off;
  729. }
  730. location ~ .*\.(js|css)?$ {
  731. expires 7d;
  732. access_log off;
  733. }
  734. location ~ /\.ht {
  735. deny all;
  736. }
  737. }
  738. EOF
  739. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  740. echo
  741. ${web_install_dir}/sbin/nginx -t
  742. if [ $? == 0 ]; then
  743. echo "Reload Nginx......"
  744. ${web_install_dir}/sbin/nginx -s reload
  745. else
  746. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  747. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  748. fi
  749. # Apache
  750. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP="Require all granted" || R_TMP=
  751. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  752. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  753. <VirtualHost *:88>
  754. ServerAdmin admin@example.com
  755. DocumentRoot "${vhostdir}"
  756. ServerName ${domain}
  757. ${Apache_Domain_alias}
  758. ${Apache_SSL}
  759. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  760. ${A_log}
  761. <Directory "${vhostdir}">
  762. SetOutputFilter DEFLATE
  763. Options FollowSymLinks ExecCGI
  764. ${R_TMP}
  765. AllowOverride All
  766. Order allow,deny
  767. Allow from all
  768. DirectoryIndex index.html index.php
  769. </Directory>
  770. </VirtualHost>
  771. EOF
  772. echo
  773. ${apache_install_dir}/bin/apachectl -t
  774. if [ $? == 0 ]; then
  775. echo "Restart Apache......"
  776. /etc/init.d/httpd restart
  777. else
  778. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  779. exit 1
  780. fi
  781. printf "
  782. #######################################################################
  783. # OneinStack for CentOS/RadHat 6+ Debian 7+ and Ubuntu 12+ #
  784. # For more information please visit https://oneinstack.com #
  785. #######################################################################
  786. "
  787. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  788. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  789. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  790. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  791. Print_ssl
  792. }
  793. Add_Vhost() {
  794. if [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
  795. Choose_env
  796. Input_Add_domain
  797. Nginx_anti_hotlinking
  798. if [ "${NGX_FLAG}" == "java" ]; then
  799. Nginx_log
  800. Create_nginx_tomcat_conf
  801. else
  802. Nginx_rewrite
  803. Nginx_log
  804. Create_nginx_php-fpm_hhvm_conf
  805. fi
  806. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  807. Choose_env
  808. Input_Add_domain
  809. Apache_log
  810. Create_apache_conf
  811. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  812. Choose_env
  813. Input_Add_domain
  814. Create_tomcat_conf
  815. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "$(ls ${apache_install_dir}/modules/libphp?.so 2>/dev/null)" ]; then
  816. Choose_env
  817. Input_Add_domain
  818. Nginx_anti_hotlinking
  819. if [ "${NGX_FLAG}" == "java" ]; then
  820. Nginx_log
  821. Create_nginx_tomcat_conf
  822. elif [ "${NGX_FLAG}" == "hhvm" ]; then
  823. Nginx_rewrite
  824. Nginx_log
  825. Create_nginx_php-fpm_hhvm_conf
  826. elif [ "${NGX_FLAG}" == "php" ]; then
  827. #Nginx_rewrite
  828. Nginx_log
  829. Apache_log
  830. Create_nginx_apache_mod-php_conf
  831. fi
  832. else
  833. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  834. fi
  835. }
  836. Del_NGX_Vhost() {
  837. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  838. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  839. if [ -n "${Domain_List}" ]; then
  840. echo
  841. echo "Virtualhost list:"
  842. echo ${CMSG}${Domain_List}${CEND}
  843. while :; do echo
  844. read -p "Please input a domain you want to delete: " domain
  845. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  846. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  847. else
  848. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  849. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  850. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  851. ${web_install_dir}/sbin/nginx -s reload
  852. while :; do echo
  853. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  854. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  855. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  856. else
  857. break
  858. fi
  859. done
  860. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  861. echo "Press Ctrl+c to cancel or Press any key to continue..."
  862. char=$(get_char)
  863. rm -rf ${Directory}
  864. fi
  865. echo
  866. echo "${CMSG}Domain: ${domain} has been deleted.${CEND}"
  867. echo
  868. else
  869. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  870. fi
  871. break
  872. fi
  873. done
  874. else
  875. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  876. fi
  877. fi
  878. }
  879. Del_Apache_Vhost() {
  880. if [ -e "${apache_install_dir}/conf/httpd.conf" ]; then
  881. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  882. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  883. /etc/init.d/httpd restart
  884. else
  885. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  886. if [ -n "${Domain_List}" ]; then
  887. echo
  888. echo "Virtualhost list:"
  889. echo ${CMSG}${Domain_List}${CEND}
  890. while :; do echo
  891. read -p "Please input a domain you want to delete: " domain
  892. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  893. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  894. else
  895. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  896. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  897. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  898. /etc/init.d/httpd restart
  899. while :; do echo
  900. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  901. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  902. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  903. else
  904. break
  905. fi
  906. done
  907. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  908. echo "Press Ctrl+c to cancel or Press any key to continue..."
  909. char=$(get_char)
  910. rm -rf ${Directory}
  911. fi
  912. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  913. else
  914. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  915. fi
  916. break
  917. fi
  918. done
  919. else
  920. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  921. fi
  922. fi
  923. fi
  924. }
  925. Del_Tomcat_Vhost() {
  926. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  927. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  928. if [ -n "$(echo ${domain} | grep '.*\..*')" ] && [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  929. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  930. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  931. /etc/init.d/tomcat restart
  932. fi
  933. else
  934. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  935. if [ -n "${Domain_List}" ]; then
  936. echo
  937. echo "Virtualhost list:"
  938. echo ${CMSG}${Domain_List}${CEND}
  939. while :; do echo
  940. read -p "Please input a domain you want to delete: " domain
  941. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  942. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  943. else
  944. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  945. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  946. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  947. /etc/init.d/tomcat restart
  948. while :; do echo
  949. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  950. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  951. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  952. else
  953. break
  954. fi
  955. done
  956. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  957. echo "Press Ctrl+c to cancel or Press any key to continue..."
  958. char=$(get_char)
  959. rm -rf ${Directory}
  960. fi
  961. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  962. else
  963. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  964. fi
  965. break
  966. fi
  967. done
  968. else
  969. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  970. fi
  971. fi
  972. fi
  973. }
  974. List_Vhost() {
  975. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  976. [ -e "${apache_install_dir}/conf/httpd.conf" -a ! -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  977. [ -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")
  978. if [ -n "${Domain_List}" ]; then
  979. echo
  980. echo "Virtualhost list:"
  981. for D in $Domain_List; do echo ${CMSG}$D${CEND}; done
  982. else
  983. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  984. fi
  985. }
  986. if [ $# == 0 ]; then
  987. Add_Vhost
  988. elif [ $# == 1 ]; then
  989. case $1 in
  990. add)
  991. Add_Vhost
  992. ;;
  993. del)
  994. Del_NGX_Vhost
  995. Del_Apache_Vhost
  996. Del_Tomcat_Vhost
  997. ;;
  998. list)
  999. List_Vhost
  1000. ;;
  1001. *)
  1002. Usage
  1003. ;;
  1004. esac
  1005. else
  1006. Usage
  1007. fi