vhost.sh 46 KB

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