vhost.sh 44 KB

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