vhost.sh 46 KB

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