vhost.sh 46 KB

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