vhost.sh 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ 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 5+ Debian 6+ 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 --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}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. if [ "${rewrite}" != 'magento2' ]; then
  414. if [ -e "config/${rewrite}.conf" ]; then
  415. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/rewrite/${rewrite}.conf
  416. else
  417. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  418. fi
  419. fi
  420. fi
  421. }
  422. Nginx_log() {
  423. while :; do echo
  424. read -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_yn
  425. if [[ ! "${access_yn}" =~ ^[y,n]$ ]]; then
  426. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  427. else
  428. break
  429. fi
  430. done
  431. if [ "${access_yn}" == 'n' ]; then
  432. N_log="access_log off;"
  433. else
  434. N_log="access_log ${wwwlogs_dir}/${domain}_nginx.log combined;"
  435. echo "You access log file=${CMSG}${wwwlogs_dir}/${domain}_nginx.log${CEND}"
  436. fi
  437. }
  438. Create_nginx_tomcat_conf() {
  439. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  440. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  441. server {
  442. ${Nginx_conf}
  443. server_name ${domain}${moredomainame};
  444. ${N_log}
  445. index index.html index.htm index.jsp;
  446. root ${vhostdir};
  447. ${Nginx_redirect}
  448. #error_page 404 = /404.html;
  449. #error_page 502 = /502.html;
  450. ${anti_hotlinking}
  451. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  452. expires 30d;
  453. access_log off;
  454. }
  455. location ~ .*\.(js|css)?$ {
  456. expires 7d;
  457. access_log off;
  458. }
  459. location ~ /\.ht {
  460. deny all;
  461. }
  462. ${NGX_CONF}
  463. }
  464. EOF
  465. [ "${https_yn}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$server_name\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  466. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  467. <Host name="${domain}" appBase="${vhostdir}" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  468. <Context path="" docBase="${vhostdir}" debug="0" reloadable="false" crossContext="true"/>
  469. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  470. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  471. </Host>
  472. EOF
  473. [ -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
  474. [ -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
  475. echo
  476. ${web_install_dir}/sbin/nginx -t
  477. if [ $? == 0 ]; then
  478. echo "Reload Nginx......"
  479. ${web_install_dir}/sbin/nginx -s reload
  480. /etc/init.d/tomcat restart
  481. else
  482. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  483. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  484. exit 1
  485. fi
  486. printf "
  487. #######################################################################
  488. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  489. # For more information please visit https://oneinstack.com #
  490. #######################################################################
  491. "
  492. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  493. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  494. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  495. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  496. [ "${nginx_ssl_yn}" == 'y' ] && Print_ssl
  497. }
  498. Create_tomcat_conf() {
  499. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  500. <Host name="${domain}" appBase="webapps" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  501. <Context path="" docBase="${vhostdir}" debug="0" reloadable="false" crossContext="true"/>
  502. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  503. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  504. </Host>
  505. EOF
  506. [ -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
  507. [ -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
  508. echo
  509. /etc/init.d/tomcat restart
  510. printf "
  511. #######################################################################
  512. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  513. # For more information please visit https://oneinstack.com #
  514. #######################################################################
  515. "
  516. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  517. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  518. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  519. echo "$(printf "%-30s" "index url:")${CMSG}http://${domain}:8080/${CEND}"
  520. }
  521. Create_nginx_php-fpm_hhvm_conf() {
  522. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  523. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  524. server {
  525. ${Nginx_conf}
  526. server_name ${domain}${moredomainame};
  527. ${N_log}
  528. index index.html index.htm index.php;
  529. root ${vhostdir};
  530. ${Nginx_redirect}
  531. include ${web_install_dir}/conf/rewrite/${rewrite}.conf;
  532. #error_page 404 = /404.html;
  533. #error_page 502 = /502.html;
  534. ${anti_hotlinking}
  535. ${NGX_CONF}
  536. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  537. expires 30d;
  538. access_log off;
  539. }
  540. location ~ .*\.(js|css)?$ {
  541. expires 7d;
  542. access_log off;
  543. }
  544. location ~ /\.ht {
  545. deny all;
  546. }
  547. }
  548. EOF
  549. if [ "${rewrite}" == 'magento2' -a -e "config/${rewrite}.conf" ]; then
  550. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/vhost/${domain}.conf
  551. sed -i "s@^ set \$MAGE_ROOT.*;@ set \$MAGE_ROOT ${vhostdir};@" ${web_install_dir}/conf/vhost/${domain}.conf
  552. sed -i "s@^ server_name.*;@ server_name ${domain}${moredomainame};@" ${web_install_dir}/conf/vhost/${domain}.conf
  553. sed -i "s@^ server_name.*;@&\n ${N_log}@" ${web_install_dir}/conf/vhost/${domain}.conf
  554. [ "${NGX_FLAG}" == 'hhvm' ] && sed -i 's@fastcgi_pass unix:.*;@fastcgi_pass unix:/var/log/hhvm/sock;@g' ${web_install_dir}/conf/vhost/${domain}.conf
  555. if [ "${anti_hotlinking_yn}" == 'y' ]; then
  556. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  557. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  558. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  559. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  560. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  561. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  562. 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
  563. fi
  564. [ "${redirect_yn}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != $domain) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  565. if [ "${nginx_ssl_yn}" == 'y' ]; then
  566. sed -i "s@^ listen 80;@&\n listen ${LISTENOPT};@" ${web_install_dir}/conf/vhost/${domain}.conf
  567. sed -i "s@^ server_name.*;@&\n ssl_stapling_verify on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  568. sed -i "s@^ server_name.*;@&\n ssl_stapling on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  569. sed -i "s@^ server_name.*;@&\n add_header Strict-Transport-Security max-age=15768000;@" ${web_install_dir}/conf/vhost/${domain}.conf
  570. sed -i "s@^ server_name.*;@&\n ssl_buffer_size 1400;@" ${web_install_dir}/conf/vhost/${domain}.conf
  571. sed -i "s@^ server_name.*;@&\n ssl_session_cache builtin:1000 shared:SSL:10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  572. sed -i "s@^ server_name.*;@&\n ssl_session_timeout 10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  573. sed -i "s@^ server_name.*;@&\n ssl_prefer_server_ciphers on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  574. 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
  575. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2;@" ${web_install_dir}/conf/vhost/${domain}.conf
  576. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  577. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  578. fi
  579. fi
  580. [ "${https_yn}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$server_name\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  581. echo
  582. ${web_install_dir}/sbin/nginx -t
  583. if [ $? == 0 ]; then
  584. echo "Reload Nginx......"
  585. ${web_install_dir}/sbin/nginx -s reload
  586. else
  587. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  588. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  589. exit 1
  590. fi
  591. printf "
  592. #######################################################################
  593. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  594. # For more information please visit https://oneinstack.com #
  595. #######################################################################
  596. "
  597. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  598. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  599. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  600. [ "${rewrite_yn}" == 'y' -a "${rewrite}" != 'magento2' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  601. [ "${nginx_ssl_yn}" == 'y' ] && Print_ssl
  602. }
  603. Apache_log() {
  604. while :; do echo
  605. read -p "Allow Apache access_log? [y/n]: " access_yn
  606. if [[ ! "${access_yn}" =~ ^[y,n]$ ]]; then
  607. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  608. else
  609. break
  610. fi
  611. done
  612. if [ "${access_yn}" == 'n' ]; then
  613. A_log='CustomLog "/dev/null" common'
  614. else
  615. A_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  616. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  617. fi
  618. }
  619. Create_apache_conf() {
  620. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP='Require all granted' || R_TMP=
  621. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  622. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  623. <VirtualHost *:80>
  624. ServerAdmin admin@example.com
  625. DocumentRoot "${vhostdir}"
  626. ServerName ${domain}
  627. ${Apache_Domain_alias}
  628. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  629. ${A_log}
  630. <Directory "${vhostdir}">
  631. SetOutputFilter DEFLATE
  632. Options FollowSymLinks ExecCGI
  633. ${R_TMP}
  634. AllowOverride All
  635. Order allow,deny
  636. Allow from all
  637. DirectoryIndex index.html index.php
  638. </Directory>
  639. </VirtualHost>
  640. EOF
  641. [ "$apache_ssl_yn" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  642. <VirtualHost *:443>
  643. ServerAdmin admin@example.com
  644. DocumentRoot "${vhostdir}"
  645. ServerName ${domain}
  646. ${Apache_Domain_alias}
  647. ${Apache_SSL}
  648. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  649. ${A_log}
  650. <Directory "${vhostdir}">
  651. SetOutputFilter DEFLATE
  652. Options FollowSymLinks ExecCGI
  653. ${R_TMP}
  654. AllowOverride All
  655. Order allow,deny
  656. Allow from all
  657. DirectoryIndex index.html index.php
  658. </Directory>
  659. </VirtualHost>
  660. EOF
  661. echo
  662. ${apache_install_dir}/bin/apachectl -t
  663. if [ $? == 0 ]; then
  664. echo "Restart Apache......"
  665. /etc/init.d/httpd restart
  666. else
  667. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  668. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  669. exit 1
  670. fi
  671. printf "
  672. #######################################################################
  673. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  674. # For more information please visit https://oneinstack.com #
  675. #######################################################################
  676. "
  677. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  678. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  679. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  680. [ "${apache_ssl_yn}" == 'y' ] && Print_ssl
  681. }
  682. Create_nginx_apache_mod-php_conf() {
  683. # Nginx/Tengine/OpenResty
  684. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  685. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  686. server {
  687. ${Nginx_conf}
  688. server_name ${domain}${moredomainame};
  689. ${N_log}
  690. index index.html index.htm index.php;
  691. root ${vhostdir};
  692. ${Nginx_redirect}
  693. ${anti_hotlinking}
  694. location / {
  695. try_files \$uri @apache;
  696. }
  697. location @apache {
  698. proxy_pass http://127.0.0.1:88;
  699. include proxy.conf;
  700. }
  701. location ~ .*\.(php|php5|cgi|pl)?$ {
  702. proxy_pass http://127.0.0.1:88;
  703. include proxy.conf;
  704. }
  705. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  706. expires 30d;
  707. access_log off;
  708. }
  709. location ~ .*\.(js|css)?$ {
  710. expires 7d;
  711. access_log off;
  712. }
  713. location ~ /\.ht {
  714. deny all;
  715. }
  716. }
  717. EOF
  718. [ "${https_yn}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$server_name\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  719. echo
  720. ${web_install_dir}/sbin/nginx -t
  721. if [ $? == 0 ]; then
  722. echo "Reload Nginx......"
  723. ${web_install_dir}/sbin/nginx -s reload
  724. else
  725. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  726. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  727. fi
  728. # Apache
  729. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && R_TMP="Require all granted" || R_TMP=
  730. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  731. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  732. <VirtualHost *:88>
  733. ServerAdmin admin@example.com
  734. DocumentRoot "${vhostdir}"
  735. ServerName ${domain}
  736. ${Apache_Domain_alias}
  737. ${Apache_SSL}
  738. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  739. ${A_log}
  740. <Directory "${vhostdir}">
  741. SetOutputFilter DEFLATE
  742. Options FollowSymLinks ExecCGI
  743. ${R_TMP}
  744. AllowOverride All
  745. Order allow,deny
  746. Allow from all
  747. DirectoryIndex index.html index.php
  748. </Directory>
  749. </VirtualHost>
  750. EOF
  751. echo
  752. ${apache_install_dir}/bin/apachectl -t
  753. if [ $? == 0 ]; then
  754. echo "Restart Apache......"
  755. /etc/init.d/httpd restart
  756. else
  757. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  758. exit 1
  759. fi
  760. printf "
  761. #######################################################################
  762. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  763. # For more information please visit https://oneinstack.com #
  764. #######################################################################
  765. "
  766. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  767. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  768. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  769. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  770. [ "${nginx_ssl_yn}" == 'y' ] && Print_ssl
  771. }
  772. Add_Vhost() {
  773. if [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
  774. Choose_env
  775. Input_Add_domain
  776. Nginx_anti_hotlinking
  777. if [ "${NGX_FLAG}" == "java" ]; then
  778. Nginx_log
  779. Create_nginx_tomcat_conf
  780. else
  781. Nginx_rewrite
  782. Nginx_log
  783. Create_nginx_php-fpm_hhvm_conf
  784. fi
  785. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
  786. Choose_env
  787. Input_Add_domain
  788. Apache_log
  789. Create_apache_conf
  790. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  791. Choose_env
  792. Input_Add_domain
  793. Create_tomcat_conf
  794. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "$(ls ${apache_install_dir}/modules/libphp?.so 2>/dev/null)" ]; then
  795. Choose_env
  796. Input_Add_domain
  797. Nginx_anti_hotlinking
  798. if [ "${NGX_FLAG}" == "java" ]; then
  799. Nginx_log
  800. Create_nginx_tomcat_conf
  801. elif [ "${NGX_FLAG}" == "hhvm" ]; then
  802. Nginx_rewrite
  803. Nginx_log
  804. Create_nginx_php-fpm_hhvm_conf
  805. elif [ "${NGX_FLAG}" == "php" ]; then
  806. #Nginx_rewrite
  807. Nginx_log
  808. Apache_log
  809. Create_nginx_apache_mod-php_conf
  810. fi
  811. else
  812. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  813. fi
  814. }
  815. Del_NGX_Vhost() {
  816. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  817. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  818. if [ -n "${Domain_List}" ]; then
  819. echo
  820. echo "Virtualhost list:"
  821. echo ${CMSG}${Domain_List}${CEND}
  822. while :; do echo
  823. read -p "Please input a domain you want to delete: " domain
  824. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  825. echo "${CWARNING}input error! ${CEND}"
  826. else
  827. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  828. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  829. rm -rf ${web_install_dir}/conf/vhost/${domain}.conf
  830. ${web_install_dir}/sbin/nginx -s reload
  831. while :; do echo
  832. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  833. if [[ ! ${Del_Vhost_wwwroot_yn} =~ ^[y,n]$ ]]; then
  834. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  835. else
  836. break
  837. fi
  838. done
  839. if [ "${Del_Vhost_wwwroot_yn}" == 'y' ]; then
  840. echo "Press Ctrl+c to cancel or Press any key to continue..."
  841. char=$(get_char)
  842. rm -rf ${Directory}
  843. fi
  844. echo
  845. echo "${CMSG}Domain: ${domain} has been deleted.${CEND}"
  846. echo
  847. else
  848. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  849. fi
  850. break
  851. fi
  852. done
  853. else
  854. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  855. fi
  856. fi
  857. }
  858. Del_Apache_Vhost() {
  859. if [ -e "${apache_install_dir}/conf/httpd.conf" ]; then
  860. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  861. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  862. /etc/init.d/httpd restart
  863. else
  864. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  865. if [ -n "${Domain_List}" ]; then
  866. echo
  867. echo "Virtualhost list:"
  868. echo ${CMSG}${Domain_List}${CEND}
  869. while :; do echo
  870. read -p "Please input a domain you want to delete: " domain
  871. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  872. echo "${CWARNING}input error! ${CEND}"
  873. else
  874. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  875. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  876. rm -rf ${apache_install_dir}/conf/vhost/${domain}.conf
  877. /etc/init.d/httpd restart
  878. while :; do echo
  879. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  880. if [[ ! ${Del_Vhost_wwwroot_yn} =~ ^[y,n]$ ]]; then
  881. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  882. else
  883. break
  884. fi
  885. done
  886. if [ "${Del_Vhost_wwwroot_yn}" == 'y' ]; then
  887. echo "Press Ctrl+c to cancel or Press any key to continue..."
  888. char=$(get_char)
  889. rm -rf ${Directory}
  890. fi
  891. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  892. else
  893. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  894. fi
  895. break
  896. fi
  897. done
  898. else
  899. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  900. fi
  901. fi
  902. fi
  903. }
  904. Del_Tomcat_Vhost() {
  905. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  906. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  907. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  908. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  909. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  910. /etc/init.d/tomcat restart
  911. fi
  912. else
  913. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  914. if [ -n "${Domain_List}" ]; then
  915. echo
  916. echo "Virtualhost list:"
  917. echo ${CMSG}${Domain_List}${CEND}
  918. while :; do echo
  919. read -p "Please input a domain you want to delete: " domain
  920. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  921. echo "${CWARNING}input error! ${CEND}"
  922. else
  923. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  924. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  925. rm -rf ${tomcat_install_dir}/conf/vhost/${domain}.xml
  926. /etc/init.d/tomcat restart
  927. while :; do echo
  928. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  929. if [[ ! ${Del_Vhost_wwwroot_yn} =~ ^[y,n]$ ]]; then
  930. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  931. else
  932. break
  933. fi
  934. done
  935. if [ "${Del_Vhost_wwwroot_yn}" == 'y' ]; then
  936. echo "Press Ctrl+c to cancel or Press any key to continue..."
  937. char=$(get_char)
  938. rm -rf ${Directory}
  939. fi
  940. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  941. else
  942. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  943. fi
  944. break
  945. fi
  946. done
  947. else
  948. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  949. fi
  950. fi
  951. fi
  952. }
  953. if [ $# == 0 ]; then
  954. Add_Vhost
  955. elif [ $# == 1 ]; then
  956. case $1 in
  957. add)
  958. Add_Vhost
  959. ;;
  960. del)
  961. Del_NGX_Vhost
  962. Del_Apache_Vhost
  963. Del_Tomcat_Vhost
  964. ;;
  965. *)
  966. Usage
  967. ;;
  968. esac
  969. else
  970. Usage
  971. fi