vhost.sh 44 KB

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