vhost.sh 44 KB

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