vhost.sh 46 KB

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