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