vhost.sh 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. Require all granted
  208. AllowOverride All
  209. Order allow,deny
  210. Allow from all
  211. DirectoryIndex index.html index.php
  212. </Directory>
  213. </VirtualHost>
  214. EOF
  215. /etc/init.d/httpd restart > /dev/null
  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 -rf ${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. if [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  232. Command="${Nginx_cmd};/etc/init.d/httpd graceful"
  233. elif [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
  234. Command="${Nginx_cmd}"
  235. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  236. Command="/etc/init.d/httpd graceful"
  237. fi
  238. ~/.acme.sh/acme.sh --install-cert -d ${domain} --fullchain-file ${PATH_SSL}/${domain}.crt --key-file ${PATH_SSL}/${domain}.key --reloadcmd "${Command}" > /dev/null
  239. else
  240. echo "${CFAILURE}Error: Create Let's Encrypt SSL Certificate failed! ${CEND}"
  241. [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  242. [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  243. exit 1
  244. fi
  245. fi
  246. }
  247. Print_ssl() {
  248. if [ "${Domian_Mode}" == '2' ]; then
  249. echo "$(printf "%-30s" "Self-signed SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  250. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  251. echo "$(printf "%-30s" "SSL CSR File:")${CMSG}${PATH_SSL}/${domain}.csr${CEND}"
  252. elif [ "${Domian_Mode}" == '3' -o "${ARG1}" == 'dnsapi' ]; then
  253. echo "$(printf "%-30s" "Let's Encrypt SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  254. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  255. fi
  256. }
  257. Input_Add_domain() {
  258. if [ "${ARG1}" != 'dnsapi' ]; then
  259. while :;do
  260. printf "
  261. What Are You Doing?
  262. \t${CMSG}1${CEND}. Use HTTP Only
  263. \t${CMSG}2${CEND}. Use your own SSL Certificate and Key
  264. \t${CMSG}3${CEND}. Use Let's Encrypt to Create SSL Certificate and Key
  265. \t${CMSG}q${CEND}. Exit
  266. "
  267. read -e -p "Please input the correct option: " Domian_Mode
  268. if [[ ! "${Domian_Mode}" =~ ^[1-3,q]$ ]]; then
  269. echo "${CFAILURE}input error! Please only input 1~3 and q${CEND}"
  270. else
  271. break
  272. fi
  273. done
  274. fi
  275. if [ "${Domian_Mode}" == '3' -o "${ARG1}" == 'dnsapi' ] && [ ! -e ~/.acme.sh/acme.sh ]; then
  276. pushd ${oneinstack_dir}/src > /dev/null
  277. [ ! -e acme.sh-master.tar.gz ] && wget -qc http://mirrors.linuxeye.com/oneinstack/src/acme.sh-master.tar.gz
  278. tar xzf acme.sh-master.tar.gz
  279. pushd acme.sh-master > /dev/null
  280. ./acme.sh --install > /dev/null 2>&1
  281. popd > /dev/null
  282. popd > /dev/null
  283. fi
  284. if [[ "${Domian_Mode}" =~ ^[2-3]$ ]] || [ "${ARG1}" == 'dnsapi' ]; then
  285. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  286. nginx_ssl_flag=y
  287. PATH_SSL=${web_install_dir}/conf/ssl
  288. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL};
  289. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/apachectl" ]; then
  290. apache_ssl_flag=y
  291. PATH_SSL=${apache_install_dir}/conf/ssl
  292. [ ! -d "${PATH_SSL}" ] && mkdir ${PATH_SSL};
  293. fi
  294. elif [ "${Domian_Mode}" == 'q' ]; then
  295. exit 1
  296. fi
  297. while :; do echo
  298. read -e -p "Please input domain(example: www.example.com): " domain
  299. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  300. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  301. else
  302. break
  303. fi
  304. done
  305. 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
  306. [ -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"
  307. [ -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"
  308. [ -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"
  309. exit
  310. else
  311. echo "domain=${domain}"
  312. fi
  313. while :; do echo
  314. echo "Please input the directory for the domain:${domain} :"
  315. read -e -p "(Default directory: ${wwwroot_dir}/${domain}): " vhostdir
  316. if [ -n "${vhostdir}" -a -z "$(echo ${vhostdir} | grep '^/')" ]; then
  317. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  318. else
  319. if [ -z "${vhostdir}" ]; then
  320. vhostdir="${wwwroot_dir}/${domain}"
  321. echo "Virtual Host Directory=${CMSG}${vhostdir}${CEND}"
  322. fi
  323. echo
  324. echo "Create Virtul Host directory......"
  325. mkdir -p ${vhostdir}
  326. echo "set permissions of Virtual Host directory......"
  327. chown -R ${run_user}.${run_user} ${vhostdir}
  328. break
  329. fi
  330. done
  331. while :; do echo
  332. read -e -p "Do you want to add more domain name? [y/n]: " moredomainame_flag
  333. if [[ ! ${moredomainame_flag} =~ ^[y,n]$ ]]; then
  334. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  335. else
  336. break
  337. fi
  338. done
  339. if [ "${moredomainame_flag}" == 'y' ]; then
  340. while :; do echo
  341. read -e -p "Type domainname or IP(example: example.com other.example.com): " moredomain
  342. if [ -z "$(echo ${moredomain} | grep '.*\..*')" ]; then
  343. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  344. else
  345. [ "${moredomain}" == "${domain}" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  346. echo domain list="$moredomain"
  347. moredomainame=" $moredomain"
  348. break
  349. fi
  350. done
  351. Apache_Domain_alias=ServerAlias${moredomainame}
  352. Tomcat_Domain_alias=$(for D in $(echo ${moredomainame}); do echo "<Alias>${D}</Alias>"; done)
  353. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  354. while :; do echo
  355. read -e -p "Do you want to redirect from ${moredomain} to ${domain}? [y/n]: " redirect_flag
  356. if [[ ! ${redirect_flag} =~ ^[y,n]$ ]]; then
  357. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  358. else
  359. break
  360. fi
  361. done
  362. [ "${redirect_flag}" == 'y' ] && Nginx_redirect="if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }"
  363. fi
  364. fi
  365. if [ "${nginx_ssl_flag}" == 'y' ]; then
  366. while :; do echo
  367. read -e -p "Do you want to redirect all HTTP requests to HTTPS? [y/n]: " https_flag
  368. if [[ ! ${https_flag} =~ ^[y,n]$ ]]; then
  369. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  370. else
  371. break
  372. fi
  373. done
  374. if [[ "$(${web_install_dir}/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  375. LISTENOPT="443 ssl http2"
  376. else
  377. LISTENOPT="443 ssl spdy"
  378. fi
  379. Create_SSL
  380. 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")
  381. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  382. elif [ "$apache_ssl_flag" == 'y' ]; then
  383. Create_SSL
  384. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  385. [ -z "$(grep 'Listen 443' ${apache_install_dir}/conf/httpd.conf)" ] && sed -i "s@Listen 80@&\nListen 443@" ${apache_install_dir}/conf/httpd.conf
  386. [ -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
  387. else
  388. Nginx_conf="listen 80;"
  389. fi
  390. }
  391. Nginx_anti_hotlinking() {
  392. while :; do echo
  393. read -e -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_flag
  394. if [[ ! ${anti_hotlinking_flag} =~ ^[y,n]$ ]]; then
  395. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  396. else
  397. break
  398. fi
  399. done
  400. if [ -n "$(echo ${domain} | grep '.*\..*\..*')" ]; then
  401. domain_allow="*.${domain#*.} ${domain}"
  402. else
  403. domain_allow="*.${domain} ${domain}"
  404. fi
  405. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  406. if [ "${moredomainame_flag}" == 'y' -a "${moredomain}" != "*.${domain}" ]; then
  407. domain_allow_all=${domain_allow}${moredomainame}
  408. else
  409. domain_allow_all=${domain_allow}
  410. fi
  411. domain_allow_all=`echo ${domain_allow_all} | tr ' ' '\n' | awk '!a[$1]++' | xargs`
  412. 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 }")
  413. else
  414. anti_hotlinking=
  415. fi
  416. }
  417. Nginx_rewrite() {
  418. [ ! -d "${web_install_dir}/conf/rewrite" ] && mkdir ${web_install_dir}/conf/rewrite
  419. while :; do echo
  420. read -e -p "Allow Rewrite rule? [y/n]: " rewrite_flag
  421. if [[ ! "${rewrite_flag}" =~ ^[y,n]$ ]]; then
  422. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  423. else
  424. break
  425. fi
  426. done
  427. if [ "${rewrite_flag}" == 'n' ]; then
  428. rewrite="none"
  429. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  430. else
  431. echo
  432. echo "Please input the rewrite of programme :"
  433. 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."
  434. read -e -p "(Default rewrite: other): " rewrite
  435. if [ "${rewrite}" == "" ]; then
  436. rewrite="other"
  437. fi
  438. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  439. [ "${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 }")
  440. [ "${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 }")
  441. [ "${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 }")
  442. if [ "${rewrite}" != 'magento2' -a "${rewrite}" != '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. N_log="access_log off;"
  462. else
  463. N_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. ${N_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 -rf ${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. ${N_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 ${N_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 -rf ${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. A_log='CustomLog "/dev/null" common'
  646. else
  647. A_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. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP='Require all granted' || R_TMP=
  653. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  654. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  655. <VirtualHost *:80>
  656. ServerAdmin admin@example.com
  657. DocumentRoot "${vhostdir}"
  658. ServerName ${domain}
  659. ${Apache_Domain_alias}
  660. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  661. ${A_log}
  662. <Directory "${vhostdir}">
  663. SetOutputFilter DEFLATE
  664. Options FollowSymLinks ExecCGI
  665. ${R_TMP}
  666. AllowOverride All
  667. Order allow,deny
  668. Allow from all
  669. DirectoryIndex index.html index.php
  670. </Directory>
  671. </VirtualHost>
  672. EOF
  673. [ "$apache_ssl_flag" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  674. <VirtualHost *:443>
  675. ServerAdmin admin@example.com
  676. DocumentRoot "${vhostdir}"
  677. ServerName ${domain}
  678. ${Apache_Domain_alias}
  679. ${Apache_SSL}
  680. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  681. ${A_log}
  682. <Directory "${vhostdir}">
  683. SetOutputFilter DEFLATE
  684. Options FollowSymLinks ExecCGI
  685. ${R_TMP}
  686. AllowOverride All
  687. Order allow,deny
  688. Allow from all
  689. DirectoryIndex index.html index.php
  690. </Directory>
  691. </VirtualHost>
  692. EOF
  693. echo
  694. ${apache_install_dir}/bin/apachectl -t
  695. if [ $? == 0 ]; then
  696. echo "Restart Apache......"
  697. /etc/init.d/httpd restart
  698. else
  699. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  700. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  701. exit 1
  702. fi
  703. printf "
  704. #######################################################################
  705. # OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+ #
  706. # For more information please visit https://oneinstack.com #
  707. #######################################################################
  708. "
  709. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  710. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  711. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  712. Print_ssl
  713. }
  714. Create_nginx_apache_mod-php_conf() {
  715. # Nginx/Tengine/OpenResty
  716. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  717. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  718. server {
  719. ${Nginx_conf}
  720. server_name ${domain}${moredomainame};
  721. ${N_log}
  722. index index.html index.htm index.php;
  723. root ${vhostdir};
  724. ${Nginx_redirect}
  725. ${anti_hotlinking}
  726. location / {
  727. try_files \$uri @apache;
  728. }
  729. location @apache {
  730. proxy_pass http://127.0.0.1:88;
  731. include proxy.conf;
  732. }
  733. location ~ .*\.(php|php5|cgi|pl)?$ {
  734. proxy_pass http://127.0.0.1:88;
  735. include proxy.conf;
  736. }
  737. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  738. expires 30d;
  739. access_log off;
  740. }
  741. location ~ .*\.(js|css)?$ {
  742. expires 7d;
  743. access_log off;
  744. }
  745. location ~ /\.ht {
  746. deny all;
  747. }
  748. }
  749. EOF
  750. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  751. echo
  752. ${web_install_dir}/sbin/nginx -t
  753. if [ $? == 0 ]; then
  754. echo "Reload Nginx......"
  755. ${web_install_dir}/sbin/nginx -s reload
  756. else
  757. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  758. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  759. fi
  760. # Apache
  761. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP="Require all granted" || R_TMP=
  762. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  763. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  764. <VirtualHost *:88>
  765. ServerAdmin admin@example.com
  766. DocumentRoot "${vhostdir}"
  767. ServerName ${domain}
  768. ${Apache_Domain_alias}
  769. ${Apache_SSL}
  770. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  771. ${A_log}
  772. <Directory "${vhostdir}">
  773. SetOutputFilter DEFLATE
  774. Options FollowSymLinks ExecCGI
  775. ${R_TMP}
  776. AllowOverride All
  777. Order allow,deny
  778. Allow from all
  779. DirectoryIndex index.html index.php
  780. </Directory>
  781. </VirtualHost>
  782. EOF
  783. echo
  784. ${apache_install_dir}/bin/apachectl -t
  785. if [ $? == 0 ]; then
  786. echo "Restart Apache......"
  787. /etc/init.d/httpd restart
  788. else
  789. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  790. exit 1
  791. fi
  792. printf "
  793. #######################################################################
  794. # OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+ #
  795. # For more information please visit https://oneinstack.com #
  796. #######################################################################
  797. "
  798. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  799. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  800. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  801. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  802. Print_ssl
  803. }
  804. Add_Vhost() {
  805. if [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
  806. Choose_env
  807. Input_Add_domain
  808. Nginx_anti_hotlinking
  809. if [ "${NGX_FLAG}" == "java" ]; then
  810. Nginx_log
  811. Create_nginx_tomcat_conf
  812. else
  813. Nginx_rewrite
  814. Nginx_log
  815. Create_nginx_php-fpm_hhvm_conf
  816. fi
  817. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  818. Choose_env
  819. Input_Add_domain
  820. Apache_log
  821. Create_apache_conf
  822. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  823. Choose_env
  824. Input_Add_domain
  825. Create_tomcat_conf
  826. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "$(ls ${apache_install_dir}/modules/libphp?.so 2>/dev/null)" ]; then
  827. Choose_env
  828. Input_Add_domain
  829. Nginx_anti_hotlinking
  830. if [ "${NGX_FLAG}" == "java" ]; then
  831. Nginx_log
  832. Create_nginx_tomcat_conf
  833. elif [ "${NGX_FLAG}" == "hhvm" ]; then
  834. Nginx_rewrite
  835. Nginx_log
  836. Create_nginx_php-fpm_hhvm_conf
  837. elif [ "${NGX_FLAG}" == "php" ]; then
  838. #Nginx_rewrite
  839. Nginx_log
  840. Apache_log
  841. Create_nginx_apache_mod-php_conf
  842. fi
  843. else
  844. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  845. fi
  846. }
  847. Del_NGX_Vhost() {
  848. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  849. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  850. if [ -n "${Domain_List}" ]; then
  851. echo
  852. echo "Virtualhost list:"
  853. echo ${CMSG}${Domain_List}${CEND}
  854. while :; do echo
  855. read -e -p "Please input a domain you want to delete: " domain
  856. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  857. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  858. else
  859. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  860. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  861. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  862. ${web_install_dir}/sbin/nginx -s reload
  863. while :; do echo
  864. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  865. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  866. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  867. else
  868. break
  869. fi
  870. done
  871. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  872. echo "Press Ctrl+c to cancel or Press any key to continue..."
  873. char=$(get_char)
  874. rm -rf ${Directory}
  875. fi
  876. echo
  877. echo "${CMSG}Domain: ${domain} has been deleted.${CEND}"
  878. echo
  879. else
  880. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  881. fi
  882. break
  883. fi
  884. done
  885. else
  886. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  887. fi
  888. fi
  889. }
  890. Del_Apache_Vhost() {
  891. if [ -e "${apache_install_dir}/conf/httpd.conf" ]; then
  892. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  893. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  894. /etc/init.d/httpd restart
  895. else
  896. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  897. if [ -n "${Domain_List}" ]; then
  898. echo
  899. echo "Virtualhost list:"
  900. echo ${CMSG}${Domain_List}${CEND}
  901. while :; do echo
  902. read -e -p "Please input a domain you want to delete: " domain
  903. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  904. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  905. else
  906. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  907. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  908. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  909. /etc/init.d/httpd restart
  910. while :; do echo
  911. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  912. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  913. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  914. else
  915. break
  916. fi
  917. done
  918. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  919. echo "Press Ctrl+c to cancel or Press any key to continue..."
  920. char=$(get_char)
  921. rm -rf ${Directory}
  922. fi
  923. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  924. else
  925. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  926. fi
  927. break
  928. fi
  929. done
  930. else
  931. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  932. fi
  933. fi
  934. fi
  935. }
  936. Del_Tomcat_Vhost() {
  937. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  938. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  939. if [ -n "$(echo ${domain} | grep '.*\..*')" ] && [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  940. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  941. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  942. /etc/init.d/tomcat restart
  943. fi
  944. else
  945. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  946. if [ -n "${Domain_List}" ]; then
  947. echo
  948. echo "Virtualhost list:"
  949. echo ${CMSG}${Domain_List}${CEND}
  950. while :; do echo
  951. read -e -p "Please input a domain you want to delete: " domain
  952. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  953. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  954. else
  955. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  956. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  957. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  958. /etc/init.d/tomcat restart
  959. while :; do echo
  960. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  961. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  962. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  963. else
  964. break
  965. fi
  966. done
  967. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  968. echo "Press Ctrl+c to cancel or Press any key to continue..."
  969. char=$(get_char)
  970. rm -rf ${Directory}
  971. fi
  972. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  973. else
  974. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  975. fi
  976. break
  977. fi
  978. done
  979. else
  980. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  981. fi
  982. fi
  983. fi
  984. }
  985. List_Vhost() {
  986. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  987. [ -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")
  988. [ -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")
  989. if [ -n "${Domain_List}" ]; then
  990. echo
  991. echo "Virtualhost list:"
  992. for D in $Domain_List; do echo ${CMSG}$D${CEND}; done
  993. else
  994. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  995. fi
  996. }
  997. if [ $# == 0 ]; then
  998. Add_Vhost
  999. elif [ $# == 1 ]; then
  1000. case ${ARG1} in
  1001. add|dnsapi)
  1002. Add_Vhost
  1003. ;;
  1004. del)
  1005. Del_NGX_Vhost
  1006. Del_Apache_Vhost
  1007. Del_Tomcat_Vhost
  1008. ;;
  1009. list)
  1010. List_Vhost
  1011. ;;
  1012. *)
  1013. Usage
  1014. ;;
  1015. esac
  1016. else
  1017. Usage
  1018. fi