vhost.sh 44 KB

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