vhost.sh 43 KB

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