vhost.sh 43 KB

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