vhost.sh 44 KB

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