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