1
0

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