vhost.sh 43 KB

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