vhost.sh 46 KB

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