vhost.sh 48 KB

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