vhost.sh 44 KB

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