vhost.sh 46 KB

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