vhost.sh 43 KB

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