vhost.sh 43 KB

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