vhost.sh 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/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 7+ Debian 9+ and Ubuntu 16+ #
  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. oneinstack_dir=$(dirname "`readlink -f $0`")
  21. pushd ${oneinstack_dir} > /dev/null
  22. . ./options.conf
  23. . ./include/color.sh
  24. . ./include/check_dir.sh
  25. . ./include/check_os.sh
  26. . ./include/get_char.sh
  27. . ./include/openssl.sh
  28. Show_Help() {
  29. echo
  30. echo "Usage: $0 command ...[parameters]....
  31. --help, -h Show this help message
  32. --quiet, -q quiet operation
  33. --list, -l List Virtualhost
  34. --mphp_ver [53~81] Use another PHP version (PATH: /usr/local/php${mphp_ver})
  35. --proxy Use proxy
  36. --add Add Virtualhost
  37. --delete, --del Delete Virtualhost
  38. --httponly Use HTTP Only
  39. --selfsigned Use your own SSL Certificate and Key
  40. --letsencrypt Use Let's Encrypt to Create SSL Certificate and Key
  41. --dnsapi Use dns API to automatically issue Let's Encrypt Cert
  42. "
  43. }
  44. ARG_NUM=$#
  45. TEMP=`getopt -o hql --long help,quiet,list,proxy,mphp_ver:,add,delete,del,httponly,selfsigned,letsencrypt,dnsapi -- "$@" 2>/dev/null`
  46. [ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
  47. eval set -- "${TEMP}"
  48. while :; do
  49. [ -z "$1" ] && break;
  50. case "$1" in
  51. -h|--help)
  52. Show_Help; exit 0
  53. ;;
  54. -q|--quiet)
  55. quiet_flag=y; shift 1
  56. ;;
  57. -l|--list)
  58. list_flag=y; shift 1
  59. ;;
  60. --mphp_ver)
  61. mphp_ver=$2; mphp_flag=y; shift 2
  62. [[ ! "${mphp_ver}" =~ ^5[3-6]$|^7[0-4]$|^8[0-5]$ ]] && { echo "${CWARNING}mphp_ver input error! Please only input number 53~85${CEND}"; unset mphp_ver mphp_flag; }
  63. ;;
  64. --proxy)
  65. proxy_flag=y; shift 1
  66. ;;
  67. --add)
  68. add_flag=y; shift 1
  69. ;;
  70. --delete|--del)
  71. delete_flag=y; shift 1
  72. ;;
  73. --httponly)
  74. sslquiet_flag=y
  75. httponly_flag=y
  76. Domian_Mode=1
  77. shift 1
  78. ;;
  79. --selfsigned)
  80. sslquiet_flag=y
  81. selfsigned_flag=y
  82. Domian_Mode=2
  83. shift 1
  84. ;;
  85. --letsencrypt)
  86. sslquiet_flag=y
  87. letsencrypt_flag=y
  88. Domian_Mode=3
  89. shift 1
  90. ;;
  91. --dnsapi)
  92. sslquiet_flag=y
  93. dnsapi_flag=y
  94. letsencrypt_flag=y
  95. shift 1
  96. ;;
  97. --)
  98. shift
  99. ;;
  100. *)
  101. echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
  102. ;;
  103. esac
  104. done
  105. Choose_ENV() {
  106. if [ -e "${apache_install_dir}/bin/apachectl" ];then
  107. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '4' ] && { Apache_main_ver=24; Apache_grant='Require all granted'; }
  108. [ "$(${apache_install_dir}/bin/apachectl -v | awk -F'.' /version/'{print $2}')" == '2' ] && Apache_main_ver=22
  109. fi
  110. if [ -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" ] && [[ -z ${proxy_flag} || "${proxy_flag}" != 'y' ]]; then
  111. Number=11
  112. while :; do echo
  113. echo "Please choose to use environment:"
  114. echo -e "\t${CMSG}1${CEND}. Use php"
  115. echo -e "\t${CMSG}2${CEND}. Use java"
  116. read -e -p "Please input a number:(Default 1 press Enter) " ENV_FLAG
  117. ENV_FLAG=${ENV_FLAG:-1}
  118. if [[ ! ${ENV_FLAG} =~ ^[1-2]$ ]]; then
  119. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  120. else
  121. break
  122. fi
  123. done
  124. case "${ENV_FLAG}" in
  125. 1)
  126. NGX_FLAG=php
  127. ;;
  128. 2)
  129. NGX_FLAG=java
  130. ;;
  131. esac
  132. elif [ -e "${php_install_dir}/bin/phpize" -a ! -e "${tomcat_install_dir}/conf/server.xml" ]; then
  133. Number=10
  134. NGX_FLAG=php
  135. elif [ ! -e "${php_install_dir}/bin/phpize" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  136. Number=01
  137. NGX_FLAG=java
  138. else
  139. Number=00
  140. NGX_FLAG=php
  141. fi
  142. }
  143. Create_SSL() {
  144. if [ "${Domian_Mode}" == '2' ]; then
  145. printf "
  146. You are about to be asked to enter information that will be incorporated
  147. into your certificate request.
  148. What you are about to enter is what is called a Distinguished Name or a DN.
  149. There are quite a few fields but you can leave some blank
  150. For some fields there will be a default value,
  151. If you enter '.', the field will be left blank.
  152. "
  153. echo
  154. read -e -p "Country Name (2 letter code) [CN]: " SELFSIGNEDSSL_C
  155. SELFSIGNEDSSL_C=${SELFSIGNEDSSL_C:-CN}
  156. # shellcheck disable=SC2104
  157. [ ${#SELFSIGNEDSSL_C} != 2 ] && { echo "${CWARNING}input error, You must input 2 letter code country name${CEND}"; return 1; }
  158. echo
  159. read -e -p "State or Province Name (full name) [Shanghai]: " SELFSIGNEDSSL_ST
  160. SELFSIGNEDSSL_ST=${SELFSIGNEDSSL_ST:-Shanghai}
  161. echo
  162. read -e -p "Locality Name (eg, city) [Shanghai]: " SELFSIGNEDSSL_L
  163. SELFSIGNEDSSL_L=${SELFSIGNEDSSL_L:-Shanghai}
  164. echo
  165. read -e -p "Organization Name (eg, company) [Example Inc.]: " SELFSIGNEDSSL_O
  166. SELFSIGNEDSSL_O=${SELFSIGNEDSSL_O:-"Example Inc."}
  167. echo
  168. read -e -p "Organizational Unit Name (eg, section) [IT Dept.]: " SELFSIGNEDSSL_OU
  169. SELFSIGNEDSSL_OU=${SELFSIGNEDSSL_OU:-"IT Dept."}
  170. openssl req -utf8 -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
  171. 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
  172. elif [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ]; then
  173. while :; do echo
  174. echo 'Please select domain cert key length.'
  175. echo "${CMSG}Enter one of 2048, 3072, 4096, 8192 will issue a RSA cert.${CEND}"
  176. echo "${CMSG}Enter one of ec-256, ec-384, ec-521 will issue a ECC cert.${CEND}"
  177. echo
  178. read -e -p "Please enter your cert key length (default 2048): " CERT_KEYLENGTH
  179. if [ "${CERT_KEYLENGTH}" == "" ]; then
  180. CERT_KEYLENGTH="2048"
  181. break
  182. elif [[ "${CERT_KEYLENGTH}" =~ ^2048$|^3072$|^4096$|^8192$|^ec-256$|^ec-384$|^ec-521$ ]]; then
  183. break
  184. else
  185. echo "${CWARNING}input error!${CEND}"
  186. fi
  187. done
  188. if [ ! -e ~/.acme.sh/ca/acme.zerossl.com/v2/DV90/account.key ]; then
  189. while :; do echo
  190. read -e -p "Please enter your email: " EMAIL
  191. echo
  192. if [[ "${EMAIL}" =~ ^[A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,9}$ ]]; then
  193. break
  194. else
  195. echo "${CWARNING}input error!${CEND}"
  196. fi
  197. done
  198. ~/.acme.sh/acme.sh --register-account -m ${EMAIL}
  199. fi
  200. if [ "${moredomain}" == "*.${domain}" -o "${dnsapi_flag}" == 'y' ]; then
  201. while :; do echo
  202. echo 'Please select DNS provider:'
  203. 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."
  204. echo "${CMSG}More: https://oneinstack.com/faq/letsencrypt${CEND}"
  205. read -e -p "Please enter your DNS provider: " DNS_PRO
  206. if [ -e ~/.acme.sh/dnsapi/dns_${DNS_PRO}.sh ]; then
  207. break
  208. else
  209. echo "${CWARNING}You DNS api mode is not supported${CEND}"
  210. fi
  211. done
  212. while :; do echo
  213. echo "Syntax: export Key1=Value1 ; export Key2=Value1"
  214. read -e -p "Please enter your dnsapi parameters: " DNS_PAR
  215. echo
  216. eval ${DNS_PAR}
  217. if [ $? == 0 ]; then
  218. break
  219. else
  220. echo "${CWARNING}Syntax error! PS: export Ali_Key=LTq ; export Ali_Secret=0q5E${CEND}"
  221. fi
  222. done
  223. [ "${moredomainame_flag}" == 'y' ] && moredomainame_D="$(for D in ${moredomainame}; do echo -d ${D}; done)"
  224. ~/.acme.sh/acme.sh --force --issue -k ${CERT_KEYLENGTH} --dns dns_${DNS_PRO} -d ${domain} ${moredomainame_D}
  225. else
  226. if [ "${nginx_ssl_flag}" == 'y' ]; then
  227. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  228. if [ -n "`ifconfig | grep inet6`" ]; then
  229. echo "server { listen 80; listen [::]:80; server_name ${domain}${moredomainame}; root ${vhostdir}; access_log off; }" > ${web_install_dir}/conf/vhost/${domain}.conf
  230. else
  231. echo "server { listen 80; server_name ${domain}${moredomainame}; root ${vhostdir}; access_log off; }" > ${web_install_dir}/conf/vhost/${domain}.conf
  232. fi
  233. ${web_install_dir}/sbin/nginx -s reload
  234. fi
  235. if [ "${caddy_ssl_flag}" == 'y' ]; then
  236. [ ! -d ${caddy_install_dir}/conf/vhost ] && mkdir -p ${caddy_install_dir}/conf/vhost
  237. echo "${domain}${moredomainame} { root * ${vhostdir}; file_server }" > ${caddy_install_dir}/conf/vhost/${domain}.conf
  238. Reload_Caddy
  239. fi
  240. if [ "${apache_ssl_flag}" == 'y' ]; then
  241. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  242. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  243. <VirtualHost *:80>
  244. ServerAdmin admin@example.com
  245. DocumentRoot "${vhostdir}"
  246. ServerName ${domain}
  247. ${Apache_Domain_alias}
  248. <Directory "${vhostdir}">
  249. SetOutputFilter DEFLATE
  250. Options FollowSymLinks ExecCGI
  251. ${Apache_grant}
  252. AllowOverride All
  253. Order allow,deny
  254. Allow from all
  255. DirectoryIndex index.html index.php
  256. </Directory>
  257. </VirtualHost>
  258. EOF
  259. ${apache_install_dir}/bin/apachectl -k graceful
  260. fi
  261. auth_file="`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`".html
  262. auth_str='oneinstack'; echo ${auth_str} > ${vhostdir}/${auth_file}
  263. for D in ${domain} ${moredomainame}
  264. do
  265. curl_str=`curl --connect-timeout 30 -4 -s $D/${auth_file} 2>&1`
  266. [ "${curl_str}" != "${auth_str}" ] && { echo; echo "${CFAILURE}Let's Encrypt Verify error! DNS problem: NXDOMAIN looking up A for ${D}${CEND}"; }
  267. done
  268. rm -f ${vhostdir}/${auth_file}
  269. [ "${moredomainame_flag}" == 'y' ] && moredomainame_D="$(for D in ${moredomainame}; do echo -d ${D}; done)"
  270. ~/.acme.sh/acme.sh --force --issue -k ${CERT_KEYLENGTH} -w ${vhostdir} -d ${domain} ${moredomainame_D}
  271. fi
  272. [ -e "${PATH_SSL}/${domain}.crt" ] && rm -f ${PATH_SSL}/${domain}.{crt,key}
  273. Nginx_cmd="/bin/systemctl restart nginx"
  274. Apache_cmd="${apache_install_dir}/bin/apachectl -k graceful"
  275. [ -e /usr/bin/systemctl ] && Caddy_cmd="systemctl reload caddy" || Caddy_cmd="service caddy reload"
  276. if [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  277. Command="${Nginx_cmd};${Apache_cmd}"
  278. elif [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" ]; then
  279. Command="${Nginx_cmd}"
  280. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  281. Command="${Apache_cmd}"
  282. elif [ -e "${caddy_install_dir}/bin/caddy" ]; then
  283. Command="${Caddy_cmd}"
  284. fi
  285. if [ -s ~/.acme.sh/${domain}/fullchain.cer ] && [[ "${CERT_KEYLENGTH}" =~ ^2048$|^3072$|^4096$|^8192$ ]]; then
  286. ~/.acme.sh/acme.sh --force --install-cert -d ${domain} --fullchain-file ${PATH_SSL}/${domain}.crt --key-file ${PATH_SSL}/${domain}.key --reloadcmd "${Command}" > /dev/null
  287. elif [ -s ~/.acme.sh/${domain}_ecc/fullchain.cer ] && [[ "${CERT_KEYLENGTH}" =~ ^ec-256$|^ec-384$|^ec-521$ ]]; then
  288. ~/.acme.sh/acme.sh --force --install-cert --ecc -d ${domain} --fullchain-file ${PATH_SSL}/${domain}.crt --key-file ${PATH_SSL}/${domain}.key --reloadcmd "${Command}" > /dev/null
  289. else
  290. echo "${CFAILURE}Error: Create Let's Encrypt SSL Certificate failed! ${CEND}"
  291. [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  292. [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  293. [ -e "${caddy_install_dir}/conf/vhost/${domain}.conf" ] && rm -f ${caddy_install_dir}/conf/vhost/${domain}.conf
  294. exit 1
  295. fi
  296. fi
  297. }
  298. Print_SSL() {
  299. if [ "${Domian_Mode}" == '2' ]; then
  300. echo "$(printf "%-30s" "Self-signed SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  301. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  302. echo "$(printf "%-30s" "SSL CSR File:")${CMSG}${PATH_SSL}/${domain}.csr${CEND}"
  303. elif [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ]; then
  304. echo "$(printf "%-30s" "Let's Encrypt SSL Certificate:")${CMSG}${PATH_SSL}/${domain}.crt${CEND}"
  305. echo "$(printf "%-30s" "SSL Private Key:")${CMSG}${PATH_SSL}/${domain}.key${CEND}"
  306. fi
  307. }
  308. Input_Add_proxy() {
  309. while :; do echo
  310. read -e -p "Please input the correct proxy_pass: " Proxy_Pass
  311. if [ -z "$(echo $Proxy_Pass | grep -E '^http://|https://')" ]; then
  312. echo "${CFAILURE}input error! Please only input example http://192.168.1.1:8080${CEND}"
  313. else
  314. echo "proxy_pass=${Proxy_Pass}"
  315. break
  316. fi
  317. done
  318. }
  319. Input_Add_domain() {
  320. if [ "${sslquiet_flag}" != 'y' ]; then
  321. while :;do
  322. printf "
  323. What Are You Doing?
  324. \t${CMSG}1${CEND}. Use HTTP Only
  325. \t${CMSG}2${CEND}. Use your own SSL Certificate and Key
  326. \t${CMSG}3${CEND}. Use Let's Encrypt to Create SSL Certificate and Key
  327. \t${CMSG}q${CEND}. Exit
  328. "
  329. read -e -p "Please input the correct option: " Domian_Mode
  330. if [[ ! "${Domian_Mode}" =~ ^[1-3,q]$ ]]; then
  331. echo "${CFAILURE}input error! Please only input 1~3 and q${CEND}"
  332. else
  333. break
  334. fi
  335. done
  336. fi
  337. #Multiple_PHP
  338. if [ -e "${php_install_dir}/bin/php-config" ] && [ $(ls /dev/shm/php*-cgi.sock 2> /dev/shm/php_err | wc -l) -ge 2 ]; then
  339. if [ "${mphp_flag}" != 'y' ]; then
  340. PHP_detail_ver=`${php_install_dir}/bin/php-config --version`
  341. PHP_main_ver=${PHP_detail_ver%.*}
  342. while :; do echo
  343. echo 'Please select a version of the PHP:'
  344. echo -e "\t${CMSG} 0${CEND}. PHP ${PHP_main_ver} (default)"
  345. [ -e "/dev/shm/php53-cgi.sock" ] && echo -e "\t${CMSG} 1${CEND}. PHP 5.3"
  346. [ -e "/dev/shm/php54-cgi.sock" ] && echo -e "\t${CMSG} 2${CEND}. PHP 5.4"
  347. [ -e "/dev/shm/php55-cgi.sock" ] && echo -e "\t${CMSG} 3${CEND}. PHP 5.5"
  348. [ -e "/dev/shm/php56-cgi.sock" ] && echo -e "\t${CMSG} 4${CEND}. PHP 5.6"
  349. [ -e "/dev/shm/php70-cgi.sock" ] && echo -e "\t${CMSG} 5${CEND}. PHP 7.0"
  350. [ -e "/dev/shm/php71-cgi.sock" ] && echo -e "\t${CMSG} 6${CEND}. PHP 7.1"
  351. [ -e "/dev/shm/php72-cgi.sock" ] && echo -e "\t${CMSG} 7${CEND}. PHP 7.2"
  352. [ -e "/dev/shm/php73-cgi.sock" ] && echo -e "\t${CMSG} 8${CEND}. PHP 7.3"
  353. [ -e "/dev/shm/php74-cgi.sock" ] && echo -e "\t${CMSG} 9${CEND}. PHP 7.4"
  354. [ -e "/dev/shm/php80-cgi.sock" ] && echo -e "\t${CMSG}10${CEND}. PHP 8.0"
  355. [ -e "/dev/shm/php81-cgi.sock" ] && echo -e "\t${CMSG}11${CEND}. PHP 8.1"
  356. [ -e "/dev/shm/php82-cgi.sock" ] && echo -e "\t${CMSG}12${CEND}. PHP 8.2"
  357. [ -e "/dev/shm/php83-cgi.sock" ] && echo -e "\t${CMSG}13${CEND}. PHP 8.3"
  358. [ -e "/dev/shm/php84-cgi.sock" ] && echo -e "\t${CMSG}14${CEND}. PHP 8.4"
  359. [ -e "/dev/shm/php85-cgi.sock" ] && echo -e "\t${CMSG}15${CEND}. PHP 8.5"
  360. read -e -p "Please input a number:(Default 0 press Enter) " php_option
  361. php_option=${php_option:-0}
  362. if [[ ! ${php_option} =~ ^[0-9]$|^1[0-5]$ ]]; then
  363. echo "${CWARNING}input error! Please only input number 0~15${CEND}"
  364. else
  365. break
  366. fi
  367. done
  368. fi
  369. [ "${php_option}" == '1' ] && mphp_ver=53
  370. [ "${php_option}" == '2' ] && mphp_ver=54
  371. [ "${php_option}" == '3' ] && mphp_ver=55
  372. [ "${php_option}" == '4' ] && mphp_ver=56
  373. [ "${php_option}" == '5' ] && mphp_ver=70
  374. [ "${php_option}" == '6' ] && mphp_ver=71
  375. [ "${php_option}" == '7' ] && mphp_ver=72
  376. [ "${php_option}" == '8' ] && mphp_ver=73
  377. [ "${php_option}" == '9' ] && mphp_ver=74
  378. [ "${php_option}" == '10' ] && mphp_ver=80
  379. [ "${php_option}" == '11' ] && mphp_ver=81
  380. [ "${php_option}" == '12' ] && mphp_ver=82
  381. [ "${php_option}" == '13' ] && mphp_ver=83
  382. [ "${php_option}" == '14' ] && mphp_ver=84
  383. [ "${php_option}" == '15' ] && mphp_ver=85
  384. [ ! -e "/dev/shm/php${mphp_ver}-cgi.sock" ] && unset mphp_ver
  385. fi
  386. case "${NGX_FLAG}" in
  387. "php")
  388. NGX_CONF=$(echo -e "location ~ [^/]\.php(/|$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  389. ;;
  390. "java")
  391. NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
  392. ;;
  393. esac
  394. if [ "${Domian_Mode}" == '3' -o "${dnsapi_flag}" == 'y' ] && [ ! -e ~/.acme.sh/acme.sh ]; then
  395. pushd ${oneinstack_dir}/src > /dev/null
  396. [ ! -e acme.sh-master.tar.gz ] && wget -qc ${mirror_link}/oneinstack/src/acme.sh-master.tar.gz
  397. tar xzf acme.sh-master.tar.gz
  398. pushd acme.sh-master > /dev/null
  399. ./acme.sh --install > /dev/null 2>&1
  400. popd > /dev/null
  401. popd > /dev/null
  402. fi
  403. [ -e ~/.acme.sh/account.conf ] && sed -i '/^CERT_HOME=/d' ~/.acme.sh/account.conf
  404. if [[ "${Domian_Mode}" =~ ^[2-3]$ ]] || [ "${dnsapi_flag}" == 'y' ]; then
  405. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  406. nginx_ssl_flag=y
  407. PATH_SSL=${web_install_dir}/conf/ssl
  408. [ ! -d "${PATH_SSL}" ] && mkdir -p ${PATH_SSL}
  409. elif [ -e "${caddy_install_dir}/bin/caddy" ]; then
  410. caddy_ssl_flag=y
  411. PATH_SSL=${caddy_install_dir}/conf/ssl
  412. [ ! -d "${PATH_SSL}" ] && mkdir -p ${PATH_SSL}
  413. elif [ -e "${apache_install_dir}/bin/httpd" ]; then
  414. apache_ssl_flag=y
  415. PATH_SSL=${apache_install_dir}/conf/ssl
  416. [ ! -d "${PATH_SSL}" ] && mkdir -p ${PATH_SSL}
  417. fi
  418. elif [ "${Domian_Mode}" == 'q' ]; then
  419. exit 1
  420. fi
  421. while :; do echo
  422. read -e -p "Please input domain(example: www.example.com): " domain
  423. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  424. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  425. else
  426. break
  427. fi
  428. done
  429. 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
  430. [ -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"
  431. [ -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"
  432. [ -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"
  433. exit
  434. else
  435. echo "domain=${domain}"
  436. fi
  437. if [[ -z ${proxy_flag} || "${proxy_flag}" != 'y' ]]; then
  438. while :; do echo
  439. echo "Please input the directory for the domain:${domain} :"
  440. read -e -p "(Default directory: ${wwwroot_dir}/${domain}): " vhostdir
  441. if [ -n "${vhostdir}" -a -z "$(echo ${vhostdir} | grep '^/')" ]; then
  442. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  443. else
  444. if [ -z "${vhostdir}" ]; then
  445. vhostdir="${wwwroot_dir}/${domain}"
  446. echo "Virtual Host Directory=${CMSG}${vhostdir}${CEND}"
  447. fi
  448. echo
  449. echo "Create Virtul Host directory......"
  450. mkdir -p ${vhostdir}
  451. echo "set permissions of Virtual Host directory......"
  452. chown -R ${run_user}:${run_group} ${vhostdir}
  453. break
  454. fi
  455. done
  456. fi
  457. while :; do echo
  458. read -e -p "Do you want to add more domain name? [y/n]: " moredomainame_flag
  459. if [[ ! ${moredomainame_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 [ "${moredomainame_flag}" == 'y' ]; then
  466. while :; do echo
  467. read -e -p "Type domainname or IP(example: example.com other.example.com): " moredomain
  468. if [ -z "$(echo ${moredomain} | grep '.*\..*')" ]; then
  469. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  470. else
  471. [ "${moredomain}" == "${domain}" ] && echo "${CWARNING}Domain name already exists! ${CEND}" && continue
  472. echo domain list="$moredomain"
  473. moredomainame=" $moredomain"
  474. break
  475. fi
  476. done
  477. Apache_Domain_alias=ServerAlias${moredomainame}
  478. Tomcat_Domain_alias=$(for D in $(echo ${moredomainame}); do echo "<Alias>${D}</Alias>"; done)
  479. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  480. while :; do echo
  481. read -e -p "Do you want to redirect from ${moredomain} to ${domain}? [y/n]: " redirect_flag
  482. if [[ ! ${redirect_flag} =~ ^[y,n]$ ]]; then
  483. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  484. else
  485. break
  486. fi
  487. done
  488. [ "${redirect_flag}" == 'y' ] && Nginx_redirect="if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }"
  489. fi
  490. fi
  491. if [ "${nginx_ssl_flag}" == 'y' -o "${caddy_ssl_flag}" == 'y' -o "${apache_ssl_flag}" == 'y' ]; then
  492. while :; do echo
  493. read -e -p "Do you want to redirect all HTTP requests to HTTPS? [y/n]: " https_flag
  494. if [[ ! ${https_flag} =~ ^[y,n]$ ]]; then
  495. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  496. else
  497. break
  498. fi
  499. done
  500. if [[ "$(${web_install_dir}/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  501. # 获取nginx版本号
  502. nginx_version=$(${web_install_dir}/sbin/nginx -v 2>&1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
  503. # 转换版本号为可比较的数字
  504. version_number=$(echo $nginx_version | awk -F. '{ printf("%d%02d%02d\n", $1,$2,$3); }')
  505. if [ "$version_number" -lt 12501 ]; then
  506. # nginx版本小于1.25.1
  507. LISTENOPT="443 ssl http2"
  508. HTTP2OPT=""
  509. else
  510. # nginx版本大于等于1.25.1
  511. LISTENOPT="443 ssl"
  512. HTTP2OPT="http2 on"
  513. fi
  514. else
  515. LISTENOPT="443 ssl spdy"
  516. HTTP2OPT=""
  517. fi
  518. Create_SSL
  519. if [ -n "`ifconfig | grep inet6`" ]; then
  520. Nginx_conf=$(echo -e "listen 80;\n listen [::]:80;\n listen ${LISTENOPT};\n listen [::]:${LISTENOPT};\n ${HTTP2OPT};\n ssl_certificate ${PATH_SSL}/${domain}.crt;\n ssl_certificate_key ${PATH_SSL}/${domain}.key;\n ssl_protocols TLSv1.2 TLSv1.3;\n ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;\n ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256;\n ssl_conf_command Ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256;\n ssl_conf_command Options PrioritizeChaCha;\n ssl_prefer_server_ciphers on;\n ssl_session_timeout 10m;\n ssl_session_cache shared:SSL:10m;\n ssl_buffer_size 2k;\n add_header Strict-Transport-Security max-age=15768000;\n ssl_stapling on;\n ssl_stapling_verify on;\n")
  521. else
  522. Nginx_conf=$(echo -e "listen 80;\n listen ${LISTENOPT};\n ${HTTP2OPT};\n ssl_certificate ${PATH_SSL}/${domain}.crt;\n ssl_certificate_key ${PATH_SSL}/${domain}.key;\n ssl_protocols TLSv1.2 TLSv1.3;\n ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;\n ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256;\n ssl_conf_command Ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256;\n ssl_conf_command Options PrioritizeChaCha;\n ssl_prefer_server_ciphers on;\n ssl_session_timeout 10m;\n ssl_session_cache shared:SSL:10m;\n ssl_buffer_size 2k;\n add_header Strict-Transport-Security max-age=15768000;\n ssl_stapling on;\n ssl_stapling_verify on;\n")
  523. fi
  524. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  525. elif [ "${apache_ssl_flag}" == 'y' ]; then
  526. Create_SSL
  527. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"${PATH_SSL}/${domain}.crt\"\n SSLCertificateKeyFile \"${PATH_SSL}/${domain}.key\"")
  528. [ -z "$(grep 'Listen 443' ${apache_install_dir}/conf/httpd.conf)" ] && sed -i "s@Listen 80@&\nListen 443@" ${apache_install_dir}/conf/httpd.conf
  529. [ -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
  530. else
  531. if [ -n "`ifconfig | grep inet6`" ]; then
  532. Nginx_conf=$(echo -e "listen 80;\n listen [::]:80;")
  533. else
  534. Nginx_conf=$(echo -e "listen 80;")
  535. fi
  536. fi
  537. }
  538. Nginx_anti_hotlinking() {
  539. while :; do echo
  540. read -e -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_flag
  541. if [[ ! ${anti_hotlinking_flag} =~ ^[y,n]$ ]]; then
  542. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  543. else
  544. break
  545. fi
  546. done
  547. if [ -n "$(echo ${domain} | grep '.*\..*\..*')" ]; then
  548. domain_allow="*.${domain#*.} ${domain}"
  549. else
  550. domain_allow="*.${domain} ${domain}"
  551. fi
  552. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  553. if [ "${moredomainame_flag}" == 'y' -a "${moredomain}" != "*.${domain}" ]; then
  554. domain_allow_all=${domain_allow}${moredomainame}
  555. else
  556. domain_allow_all=${domain_allow}
  557. fi
  558. domain_allow_all=`echo ${domain_allow_all} | tr ' ' '\n' | awk '!a[$1]++' | xargs`
  559. 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 }")
  560. fi
  561. }
  562. Nginx_rewrite() {
  563. [ ! -d "${web_install_dir}/conf/rewrite" ] && mkdir ${web_install_dir}/conf/rewrite
  564. while :; do echo
  565. read -e -p "Allow Rewrite rule? [y/n]: " rewrite_flag
  566. if [[ ! "${rewrite_flag}" =~ ^[y,n]$ ]]; then
  567. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  568. else
  569. break
  570. fi
  571. done
  572. if [ "${rewrite_flag}" == 'n' ]; then
  573. rewrite="none"
  574. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  575. else
  576. echo
  577. echo "Please input the rewrite of programme :"
  578. echo "${CMSG}wordpress${CEND},${CMSG}opencart${CEND},${CMSG}magento2${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND},${CMSG}codeigniter${CEND},${CMSG}laravel${CEND}"
  579. echo "${CMSG}thinkphp${CEND},${CMSG}pathinfo${CEND},${CMSG}discuz${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}nextcloud${CEND},${CMSG}zblog${CEND},${CMSG}whmcs${CEND} rewrite was exist."
  580. read -e -p "(Default rewrite: other): " rewrite
  581. if [ "${rewrite}" == "" ]; then
  582. rewrite="other"
  583. fi
  584. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  585. [ "${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${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  586. [ "${NGX_FLAG}" == 'php' ] && [[ "${rewrite}" =~ ^codeigniter$|^thinkphp$|^pathinfo$ ]] && NGX_CONF=$(echo -e "location ~ [^/]\.php(/|\$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php${mphp_ver}-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n fastcgi_split_path_info ^(.+?\.php)(/.*)\$;\n set \$path_info \$fastcgi_path_info;\n fastcgi_param PATH_INFO \$path_info;\n try_files \$fastcgi_script_name =404; \n }")
  587. [ "${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${mphp_ver}-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 }")
  588. if [[ ! "${rewrite}" =~ ^magento2$|^pathinfo$ ]]; then
  589. if [ -e "config/${rewrite}.conf" ]; then
  590. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/rewrite/${rewrite}.conf
  591. else
  592. touch "${web_install_dir}/conf/rewrite/${rewrite}.conf"
  593. fi
  594. fi
  595. fi
  596. }
  597. Nginx_log() {
  598. while :; do echo
  599. read -e -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_flag
  600. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  601. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  602. else
  603. break
  604. fi
  605. done
  606. if [ "${access_flag}" == 'n' ]; then
  607. Nginx_log="access_log off;"
  608. else
  609. Nginx_log="access_log ${wwwlogs_dir}/${domain}_nginx.log combined;"
  610. echo "You access log file=${CMSG}${wwwlogs_dir}/${domain}_nginx.log${CEND}"
  611. fi
  612. }
  613. Caddy_log() {
  614. while :; do echo
  615. read -e -p "Allow Caddy access_log? [y/n]: " access_flag
  616. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  617. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  618. else
  619. break
  620. fi
  621. done
  622. if [ "${access_flag}" == 'n' ]; then
  623. Caddy_log=""
  624. else
  625. Caddy_log="log {
  626. output file ${wwwlogs_dir}/${domain}_caddy.log
  627. }"
  628. echo "You access log file=${CMSG}${wwwlogs_dir}/${domain}_caddy.log${CEND}"
  629. fi
  630. }
  631. Create_nginx_tomcat_conf() {
  632. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  633. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  634. server {
  635. ${Nginx_conf}
  636. server_name ${domain}${moredomainame};
  637. ${Nginx_log}
  638. index index.html index.htm index.jsp;
  639. root ${vhostdir};
  640. ${Nginx_redirect}
  641. #error_page 404 /404.html;
  642. #error_page 502 /502.html;
  643. ${anti_hotlinking}
  644. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  645. expires 30d;
  646. access_log off;
  647. }
  648. location ~ .*\.(js|css)?$ {
  649. expires 7d;
  650. access_log off;
  651. }
  652. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  653. deny all;
  654. }
  655. location /.well-known {
  656. allow all;
  657. }
  658. ${NGX_CONF}
  659. }
  660. EOF
  661. [ "${https_flag}" == 'y' ] && sed -i "s@^root.*;@&\nif (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  662. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  663. <Host name="${domain}" appBase="${vhostdir}" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  664. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  665. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  666. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  667. <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For"
  668. protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>
  669. </Host>
  670. EOF
  671. [ -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
  672. [ -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
  673. echo
  674. ${web_install_dir}/sbin/nginx -t
  675. if [ $? == 0 ]; then
  676. echo "Reload Nginx......"
  677. ${web_install_dir}/sbin/nginx -s reload
  678. service tomcat restart
  679. else
  680. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  681. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  682. exit 1
  683. fi
  684. printf "
  685. #######################################################################
  686. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  687. # For more information please visit https://oneinstack.com #
  688. #######################################################################
  689. "
  690. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  691. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  692. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  693. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  694. Print_SSL
  695. }
  696. Create_tomcat_conf() {
  697. cat > ${tomcat_install_dir}/conf/vhost/${domain}.xml << EOF
  698. <Host name="${domain}" appBase="webapps" unpackWARs="true" autoDeploy="true"> ${Tomcat_Domain_alias}
  699. <Context path="" docBase="${vhostdir}" reloadable="false" crossContext="true"/>
  700. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  701. prefix="${domain}_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  702. </Host>
  703. EOF
  704. [ -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
  705. [ -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
  706. echo
  707. service tomcat restart
  708. printf "
  709. #######################################################################
  710. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  711. # For more information please visit https://oneinstack.com #
  712. #######################################################################
  713. "
  714. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  715. echo "$(printf "%-30s" "Tomcat Virtualhost conf:")${CMSG}${tomcat_install_dir}/conf/vhost/${domain}.xml${CEND}"
  716. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  717. echo "$(printf "%-30s" "index url:")${CMSG}http://${domain}:8080/${CEND}"
  718. }
  719. Create_nginx_phpfpm_conf() {
  720. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  721. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  722. server {
  723. ${Nginx_conf}
  724. server_name ${domain}${moredomainame};
  725. ${Nginx_log}
  726. index index.html index.htm index.php;
  727. root ${vhostdir};
  728. ${Nginx_redirect}
  729. include ${web_install_dir}/conf/rewrite/${rewrite}.conf;
  730. #error_page 404 /404.html;
  731. #error_page 502 /502.html;
  732. ${anti_hotlinking}
  733. ${NGX_CONF}
  734. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  735. expires 30d;
  736. access_log off;
  737. }
  738. location ~ .*\.(js|css)?$ {
  739. expires 7d;
  740. access_log off;
  741. }
  742. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  743. deny all;
  744. }
  745. location /.well-known {
  746. allow all;
  747. }
  748. }
  749. EOF
  750. [ "${rewrite}" == 'pathinfo' ] && sed -i '/pathinfo.conf;$/d' ${web_install_dir}/conf/vhost/${domain}.conf
  751. if [ "${rewrite}" == 'magento2' -a -e "config/${rewrite}.conf" ]; then
  752. /bin/cp config/${rewrite}.conf ${web_install_dir}/conf/vhost/${domain}.conf
  753. sed -i "s@/dev/shm/php-cgi.sock@/dev/shm/php${mphp_ver}-cgi.sock@g" ${web_install_dir}/conf/vhost/${domain}.conf
  754. sed -i "s@^ set \$MAGE_ROOT.*;@ set \$MAGE_ROOT ${vhostdir};@" ${web_install_dir}/conf/vhost/${domain}.conf
  755. sed -i "s@^ server_name.*;@ server_name ${domain}${moredomainame};@" ${web_install_dir}/conf/vhost/${domain}.conf
  756. sed -i "s@^ server_name.*;@&\n ${Nginx_log}@" ${web_install_dir}/conf/vhost/${domain}.conf
  757. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  758. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  759. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  760. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  761. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  762. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  763. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  764. 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
  765. fi
  766. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  767. if [ "${nginx_ssl_flag}" == 'y' ]; then
  768. sed -i "s@^ listen 80;@&\n listen ${LISTENOPT};@" ${web_install_dir}/conf/vhost/${domain}.conf
  769. sed -i "s@^ server_name.*;@&\n ssl_stapling_verify on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  770. sed -i "s@^ server_name.*;@&\n ssl_stapling on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  771. sed -i "s@^ server_name.*;@&\n add_header Strict-Transport-Security max-age=15768000;@" ${web_install_dir}/conf/vhost/${domain}.conf
  772. sed -i "s@^ server_name.*;@&\n ssl_buffer_size 2k;@" ${web_install_dir}/conf/vhost/${domain}.conf
  773. sed -i "s@^ server_name.*;@&\n ssl_session_cache shared:SSL:10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  774. sed -i "s@^ server_name.*;@&\n ssl_session_timeout 10m;@" ${web_install_dir}/conf/vhost/${domain}.conf
  775. sed -i "s@^ server_name.*;@&\n ssl_prefer_server_ciphers on;@" ${web_install_dir}/conf/vhost/${domain}.conf
  776. sed -i "s@^ server_name.*;@&\n ssl_conf_command Options PrioritizeChaCha;@" ${web_install_dir}/conf/vhost/${domain}.conf
  777. sed -i "s@^ server_name.*;@&\n ssl_conf_command Ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256;@" ${web_install_dir}/conf/vhost/${domain}.conf
  778. sed -i "s@^ server_name.*;@&\n ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256;@" ${web_install_dir}/conf/vhost/${domain}.conf
  779. sed -i "s@^ server_name.*;@&\n ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;@" ${web_install_dir}/conf/vhost/${domain}.conf
  780. sed -i "s@^ server_name.*;@&\n ssl_protocols TLSv1.2 TLSv1.3;@" ${web_install_dir}/conf/vhost/${domain}.conf
  781. sed -i "s@^ server_name.*;@&\n ssl_certificate_key ${PATH_SSL}/${domain}.key;@" ${web_install_dir}/conf/vhost/${domain}.conf
  782. sed -i "s@^ server_name.*;@&\n ssl_certificate ${PATH_SSL}/${domain}.crt;@" ${web_install_dir}/conf/vhost/${domain}.conf
  783. fi
  784. fi
  785. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  786. echo
  787. ${web_install_dir}/sbin/nginx -t
  788. if [ $? == 0 ]; then
  789. echo "Reload Nginx......"
  790. ${web_install_dir}/sbin/nginx -s reload
  791. else
  792. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  793. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  794. exit 1
  795. fi
  796. printf "
  797. #######################################################################
  798. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  799. # For more information please visit https://oneinstack.com #
  800. #######################################################################
  801. "
  802. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  803. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  804. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  805. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  806. Print_SSL
  807. }
  808. Create_nginx_proxy_conf() {
  809. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  810. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  811. server {
  812. ${Nginx_conf}
  813. server_name ${domain}${moredomainame};
  814. ${Nginx_log}
  815. index index.html index.htm index.php;
  816. ${Nginx_redirect}
  817. location / {
  818. proxy_pass ${Proxy_Pass};
  819. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  820. proxy_set_header Host \$http_host;
  821. proxy_set_header X-NginX-Proxy true;
  822. proxy_http_version 1.1;
  823. proxy_set_header Upgrade \$http_upgrade;
  824. proxy_set_header Connection "upgrade";
  825. proxy_max_temp_file_size 0;
  826. }
  827. #error_page 404 /404.html;
  828. #error_page 502 /502.html;
  829. ${anti_hotlinking}
  830. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  831. expires 30d;
  832. access_log off;
  833. }
  834. location ~ .*\.(js|css)?$ {
  835. expires 7d;
  836. access_log off;
  837. }
  838. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  839. deny all;
  840. }
  841. location /.well-known {
  842. allow all;
  843. }
  844. }
  845. EOF
  846. [ "${redirect_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$host != ${domain}) { return 301 \$scheme://${domain}\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  847. if [ "${anti_hotlinking_flag}" == 'y' ]; then
  848. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  849. sed -i "s@^ root.*;@&\n }@" ${web_install_dir}/conf/vhost/${domain}.conf
  850. sed -i "s@^ root.*;@&\n return 403;@" ${web_install_dir}/conf/vhost/${domain}.conf
  851. sed -i "s@^ root.*;@&\n rewrite ^/ http://www.linuxeye.com/403.html;@" ${web_install_dir}/conf/vhost/${domain}.conf
  852. sed -i "s@^ root.*;@&\n if (\$invalid_referer) {@" ${web_install_dir}/conf/vhost/${domain}.conf
  853. sed -i "s@^ root.*;@&\n valid_referers none blocked ${domain_allow_all};@" ${web_install_dir}/conf/vhost/${domain}.conf
  854. 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
  855. fi
  856. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  857. echo
  858. ${web_install_dir}/sbin/nginx -t
  859. if [ $? == 0 ]; then
  860. echo "Reload Nginx......"
  861. ${web_install_dir}/sbin/nginx -s reload
  862. else
  863. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  864. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  865. exit 1
  866. fi
  867. printf "
  868. #######################################################################
  869. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  870. # For more information please visit https://oneinstack.com #
  871. #######################################################################
  872. "
  873. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  874. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  875. #echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  876. [ "${rewrite_flag}" == 'y' -a "${rewrite}" != 'magento2' -a "${rewrite}" != 'pathinfo' ] && echo "$(printf "%-30s" "Rewrite rule:")${CMSG}${web_install_dir}/conf/rewrite/${rewrite}.conf${CEND}"
  877. Print_SSL
  878. }
  879. Reload_Caddy() {
  880. if [ -e /usr/bin/systemctl ]; then
  881. systemctl reload caddy
  882. else
  883. service caddy reload
  884. fi
  885. }
  886. Create_caddy_php_conf() {
  887. [ ! -d ${caddy_install_dir}/conf/vhost ] && mkdir -p ${caddy_install_dir}/conf/vhost
  888. if [ "${https_flag}" == 'y' ]; then
  889. cat > ${caddy_install_dir}/conf/vhost/${domain}.conf << EOF
  890. http://${domain}, http://${moredomainame} {
  891. redir https://{host}{uri}
  892. }
  893. https://${domain}, https://${moredomainame} {
  894. root * ${vhostdir}
  895. php_fastcgi unix:/dev/shm/php${mphp_ver}-cgi.sock
  896. file_server
  897. ${Caddy_log}
  898. tls ${PATH_SSL}/${domain}.crt ${PATH_SSL}/${domain}.key
  899. handle_errors {
  900. rewrite * /{err.status_code}.html
  901. file_server
  902. }
  903. }
  904. EOF
  905. else
  906. cat > ${caddy_install_dir}/conf/vhost/${domain}.conf << EOF
  907. ${domain}${moredomainame} {
  908. root * ${vhostdir}
  909. php_fastcgi unix:/dev/shm/php${mphp_ver}-cgi.sock
  910. file_server
  911. ${Caddy_log}
  912. [ -e "${PATH_SSL}/${domain}.crt" ] && tls ${PATH_SSL}/${domain}.crt ${PATH_SSL}/${domain}.key
  913. handle_errors {
  914. rewrite * /{err.status_code}.html
  915. file_server
  916. }
  917. }
  918. EOF
  919. fi
  920. echo
  921. ${caddy_install_dir}/bin/caddy validate --config ${caddy_install_dir}/conf/Caddyfile
  922. if [ $? == 0 ]; then
  923. echo "Reload Caddy......"
  924. systemctl reload caddy
  925. else
  926. rm -f ${caddy_install_dir}/conf/vhost/${domain}.conf
  927. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  928. exit 1
  929. fi
  930. printf "
  931. #######################################################################
  932. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  933. # For more information please visit https://oneinstack.com #
  934. #######################################################################
  935. "
  936. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  937. echo "$(printf "%-30s" "Caddy Virtualhost conf:")${CMSG}${caddy_install_dir}/conf/vhost/${domain}.conf${CEND}"
  938. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  939. }
  940. Create_caddy_proxy_conf() {
  941. [ ! -d ${caddy_install_dir}/conf/vhost ] && mkdir -p ${caddy_install_dir}/conf/vhost
  942. if [ "${https_flag}" == 'y' ]; then
  943. cat > ${caddy_install_dir}/conf/vhost/${domain}.conf << EOF
  944. http://${domain}, http://${moredomainame} {
  945. redir https://{host}{uri}
  946. }
  947. https://${domain}, https://${moredomainame} {
  948. reverse_proxy ${Proxy_Pass}
  949. ${Caddy_log}
  950. tls ${PATH_SSL}/${domain}.crt ${PATH_SSL}/${domain}.key
  951. }
  952. EOF
  953. else
  954. cat > ${caddy_install_dir}/conf/vhost/${domain}.conf << EOF
  955. ${domain}${moredomainame} {
  956. reverse_proxy ${Proxy_Pass}
  957. ${Caddy_log}
  958. [ -e "${PATH_SSL}/${domain}.crt" ] && tls ${PATH_SSL}/${domain}.crt ${PATH_SSL}/${domain}.key
  959. }
  960. EOF
  961. fi
  962. echo
  963. ${caddy_install_dir}/bin/caddy validate --config ${caddy_install_dir}/conf/Caddyfile
  964. if [ $? == 0 ]; then
  965. echo "Reload Caddy......"
  966. Reload_Caddy
  967. else
  968. rm -f ${caddy_install_dir}/conf/vhost/${domain}.conf
  969. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  970. exit 1
  971. fi
  972. printf "
  973. #######################################################################
  974. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  975. # For more information please visit https://oneinstack.com #
  976. #######################################################################
  977. "
  978. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  979. echo "$(printf "%-30s" "Caddy Virtualhost conf:")${CMSG}${caddy_install_dir}/conf/vhost/${domain}.conf${CEND}"
  980. }
  981. Apache_log() {
  982. while :; do echo
  983. read -e -p "Allow Apache access_log? [y/n]: " access_flag
  984. if [[ ! "${access_flag}" =~ ^[y,n]$ ]]; then
  985. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  986. else
  987. break
  988. fi
  989. done
  990. if [ "${access_flag}" == 'n' ]; then
  991. Apache_log='CustomLog "/dev/null" common'
  992. else
  993. Apache_log="CustomLog \"${wwwlogs_dir}/${domain}_apache.log\" common"
  994. echo "You access log file=${wwwlogs_dir}/${domain}_apache.log"
  995. fi
  996. }
  997. Create_apache_conf() {
  998. if [ "${Apache_main_ver}" == '24' ]; then
  999. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  1000. 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${mphp_ver}-cgi.sock|fcgi://localhost\"\n </FilesMatch>")
  1001. fi
  1002. fi
  1003. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  1004. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  1005. <VirtualHost *:80>
  1006. ServerAdmin admin@example.com
  1007. DocumentRoot "${vhostdir}"
  1008. ServerName ${domain}
  1009. ${Apache_Domain_alias}
  1010. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  1011. ${Apache_log}
  1012. ${Apache_fcgi}
  1013. <Directory "${vhostdir}">
  1014. SetOutputFilter DEFLATE
  1015. Options FollowSymLinks ExecCGI
  1016. ${Apache_grant}
  1017. AllowOverride All
  1018. Order allow,deny
  1019. Allow from all
  1020. DirectoryIndex index.html index.php
  1021. </Directory>
  1022. </VirtualHost>
  1023. EOF
  1024. [ "${apache_ssl_flag}" == 'y' ] && cat >> ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  1025. <VirtualHost *:443>
  1026. ServerAdmin admin@example.com
  1027. DocumentRoot "${vhostdir}"
  1028. ServerName ${domain}
  1029. ${Apache_Domain_alias}
  1030. ${Apache_SSL}
  1031. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  1032. ${Apache_log}
  1033. ${Apache_fcgi}
  1034. <Directory "${vhostdir}">
  1035. SetOutputFilter DEFLATE
  1036. Options FollowSymLinks ExecCGI
  1037. ${Apache_grant}
  1038. AllowOverride All
  1039. Order allow,deny
  1040. Allow from all
  1041. DirectoryIndex index.html index.php
  1042. </Directory>
  1043. </VirtualHost>
  1044. EOF
  1045. echo
  1046. ${apache_install_dir}/bin/apachectl -t
  1047. if [ $? == 0 ]; then
  1048. echo "Restart Apache......"
  1049. ${apache_install_dir}/bin/apachectl -k graceful
  1050. else
  1051. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1052. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  1053. exit 1
  1054. fi
  1055. printf "
  1056. #######################################################################
  1057. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  1058. # For more information please visit https://oneinstack.com #
  1059. #######################################################################
  1060. "
  1061. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  1062. echo "$(printf "%-30s" "Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  1063. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  1064. Print_SSL
  1065. }
  1066. Create_nginx_apache_modphp_conf() {
  1067. # Nginx/Tengine/OpenResty
  1068. [ ! -d ${web_install_dir}/conf/vhost ] && mkdir ${web_install_dir}/conf/vhost
  1069. cat > ${web_install_dir}/conf/vhost/${domain}.conf << EOF
  1070. server {
  1071. ${Nginx_conf}
  1072. server_name ${domain}${moredomainame};
  1073. ${Nginx_log}
  1074. index index.html index.htm index.php;
  1075. root ${vhostdir};
  1076. ${Nginx_redirect}
  1077. ${anti_hotlinking}
  1078. location / {
  1079. try_files \$uri @apache;
  1080. }
  1081. location @apache {
  1082. proxy_pass http://127.0.0.1:88;
  1083. include proxy.conf;
  1084. }
  1085. location ~ .*\.(php|php5|cgi|pl)?$ {
  1086. proxy_pass http://127.0.0.1:88;
  1087. include proxy.conf;
  1088. }
  1089. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
  1090. expires 30d;
  1091. access_log off;
  1092. }
  1093. location ~ .*\.(js|css)?$ {
  1094. expires 7d;
  1095. access_log off;
  1096. }
  1097. location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  1098. deny all;
  1099. }
  1100. location /.well-known {
  1101. allow all;
  1102. }
  1103. }
  1104. EOF
  1105. [ "${https_flag}" == 'y' ] && sed -i "s@^ root.*;@&\n if (\$ssl_protocol = \"\") { return 301 https://\$host\$request_uri; }@" ${web_install_dir}/conf/vhost/${domain}.conf
  1106. echo
  1107. ${web_install_dir}/sbin/nginx -t
  1108. if [ $? == 0 ]; then
  1109. echo "Reload Nginx......"
  1110. ${web_install_dir}/sbin/nginx -s reload
  1111. else
  1112. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  1113. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  1114. fi
  1115. # Apache
  1116. if [ "${Apache_main_ver}" == '24' ]; then
  1117. if [ -e "${php_install_dir}/sbin/php-fpm" ] && [ -n "`grep -E ^LoadModule.*mod_proxy_fcgi.so ${apache_install_dir}/conf/httpd.conf`" ]; then
  1118. 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${mphp_ver}-cgi.sock|fcgi://localhost\"\n </FilesMatch>")
  1119. fi
  1120. fi
  1121. [ ! -d ${apache_install_dir}/conf/vhost ] && mkdir ${apache_install_dir}/conf/vhost
  1122. cat > ${apache_install_dir}/conf/vhost/${domain}.conf << EOF
  1123. <VirtualHost *:88>
  1124. ServerAdmin admin@example.com
  1125. DocumentRoot "${vhostdir}"
  1126. ServerName ${domain}
  1127. ${Apache_Domain_alias}
  1128. ${Apache_SSL}
  1129. ErrorLog "${wwwlogs_dir}/${domain}_error_apache.log"
  1130. ${Apache_log}
  1131. ${Apache_fcgi}
  1132. <Directory "${vhostdir}">
  1133. SetOutputFilter DEFLATE
  1134. Options FollowSymLinks ExecCGI
  1135. ${Apache_grant}
  1136. AllowOverride All
  1137. Order allow,deny
  1138. Allow from all
  1139. DirectoryIndex index.html index.php
  1140. </Directory>
  1141. </VirtualHost>
  1142. EOF
  1143. echo
  1144. ${apache_install_dir}/bin/apachectl -t
  1145. if [ $? == 0 ]; then
  1146. echo "Restart Apache......"
  1147. ${apache_install_dir}/bin/apachectl -k graceful
  1148. else
  1149. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1150. exit 1
  1151. fi
  1152. printf "
  1153. #######################################################################
  1154. # OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
  1155. # For more information please visit https://oneinstack.com #
  1156. #######################################################################
  1157. "
  1158. echo "$(printf "%-30s" "Your domain:")${CMSG}${domain}${CEND}"
  1159. echo "$(printf "%-30s" "Nginx Virtualhost conf:")${CMSG}${web_install_dir}/conf/vhost/${domain}.conf${CEND}"
  1160. echo "$(printf "%-30s" "Apache Virtualhost conf:")${CMSG}${apache_install_dir}/conf/vhost/${domain}.conf${CEND}"
  1161. echo "$(printf "%-30s" "Directory of:")${CMSG}${vhostdir}${CEND}"
  1162. Print_SSL
  1163. }
  1164. Add_Vhost() {
  1165. if [ -e "${caddy_install_dir}/bin/caddy" ] && [[ "${web_install_dir}" =~ caddy || ! -e "${web_install_dir}/sbin/nginx" ]]; then
  1166. Choose_ENV
  1167. Input_Add_domain
  1168. if [ "${proxy_flag}" == "y" ]; then
  1169. Input_Add_proxy
  1170. Caddy_log
  1171. Create_caddy_proxy_conf
  1172. else
  1173. Caddy_log
  1174. Create_caddy_php_conf
  1175. fi
  1176. elif [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" ]; then
  1177. Choose_ENV
  1178. Input_Add_domain
  1179. Nginx_anti_hotlinking
  1180. if [ "${proxy_flag}" == "y" ]; then
  1181. Input_Add_proxy
  1182. Create_nginx_proxy_conf
  1183. else
  1184. Nginx_rewrite
  1185. if [ "${NGX_FLAG}" == "java" ]; then
  1186. Nginx_log
  1187. Create_nginx_tomcat_conf
  1188. else
  1189. Nginx_log
  1190. Create_nginx_phpfpm_conf
  1191. fi
  1192. fi
  1193. elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  1194. Choose_ENV
  1195. Input_Add_domain
  1196. Apache_log
  1197. Create_apache_conf
  1198. elif [ ! -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/bin/httpd" -a -e "${tomcat_install_dir}/conf/server.xml" ]; then
  1199. Choose_ENV
  1200. Input_Add_domain
  1201. Create_tomcat_conf
  1202. elif [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/bin/httpd" ]; then
  1203. Choose_ENV
  1204. Input_Add_domain
  1205. Nginx_anti_hotlinking
  1206. if [ "${NGX_FLAG}" == "java" ]; then
  1207. Nginx_rewrite
  1208. Nginx_log
  1209. Create_nginx_tomcat_conf
  1210. elif [ "${NGX_FLAG}" == "php" ]; then
  1211. Nginx_log
  1212. Apache_log
  1213. Create_nginx_apache_modphp_conf
  1214. fi
  1215. elif [ -e "${caddy_install_dir}/bin/caddy" ]; then
  1216. Choose_ENV
  1217. Input_Add_domain
  1218. if [ "${proxy_flag}" == "y" ]; then
  1219. Input_Add_proxy
  1220. Caddy_log
  1221. Create_caddy_proxy_conf
  1222. else
  1223. Caddy_log
  1224. Create_caddy_php_conf
  1225. fi
  1226. else
  1227. echo "Error! ${CFAILURE}Web server${CEND} not found!"
  1228. fi
  1229. }
  1230. Del_NGX_Vhost() {
  1231. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1232. [ -d "${web_install_dir}/conf/vhost" ] && Domain_List=$(ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g")
  1233. if [ -n "${Domain_List}" ]; then
  1234. echo
  1235. echo "Virtualhost list:"
  1236. echo ${CMSG}${Domain_List}${CEND}
  1237. while :; do echo
  1238. read -e -p "Please input a domain you want to delete: " domain
  1239. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1240. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1241. else
  1242. if [ -e "${web_install_dir}/conf/vhost/${domain}.conf" ]; then
  1243. Directory=$(grep '^ root' ${web_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'[ ;]' '{print $(NF-1)}')
  1244. rm -f ${web_install_dir}/conf/vhost/${domain}.conf
  1245. [ -e "${web_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${web_install_dir}/conf/ssl/${domain}.{crt,key}
  1246. ${web_install_dir}/sbin/nginx -s reload
  1247. while :; do echo
  1248. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1249. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1250. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1251. else
  1252. break
  1253. fi
  1254. done
  1255. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1256. if [ "${quiet_flag}" != 'y' ]; then
  1257. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1258. char=$(get_char)
  1259. fi
  1260. rm -rf ${Directory}
  1261. fi
  1262. echo
  1263. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1264. [ -d ~/.acme.sh/${domain}_ecc ] && ~/.acme.sh/acme.sh --force --remove --ecc -d ${domain} > /dev/null 2>&1
  1265. echo "${CMSG}Domain: ${domain} has been deleted.${CEND}"
  1266. echo
  1267. else
  1268. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1269. fi
  1270. break
  1271. fi
  1272. done
  1273. else
  1274. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1275. fi
  1276. fi
  1277. }
  1278. Del_Caddy_Vhost() {
  1279. if [ -e "${caddy_install_dir}/bin/caddy" ]; then
  1280. [ -d "${caddy_install_dir}/conf/vhost" ] && Domain_List=$(ls ${caddy_install_dir}/conf/vhost | sed "s@.conf@@g")
  1281. if [ -n "${Domain_List}" ]; then
  1282. echo
  1283. echo "Virtualhost list:"
  1284. echo ${CMSG}${Domain_List}${CEND}
  1285. while :; do echo
  1286. read -e -p "Please input a domain you want to delete: " domain
  1287. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1288. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1289. else
  1290. if [ -e "${caddy_install_dir}/conf/vhost/${domain}.conf" ]; then
  1291. Directory=$(grep 'root \*' ${caddy_install_dir}/conf/vhost/${domain}.conf | head -1 | sed 's@^.*root \* @@g')
  1292. rm -f ${caddy_install_dir}/conf/vhost/${domain}.conf
  1293. Reload_Caddy
  1294. if [ -n "${Directory}" -a -d "${Directory}" ]; then
  1295. while :; do echo
  1296. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1297. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1298. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1299. else
  1300. break
  1301. fi
  1302. done
  1303. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1304. if [ "${quiet_flag}" != 'y' ]; then
  1305. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1306. char=$(get_char)
  1307. fi
  1308. rm -rf ${Directory}
  1309. fi
  1310. fi
  1311. echo
  1312. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1313. [ -d ~/.acme.sh/${domain}_ecc ] && ~/.acme.sh/acme.sh --force --remove --ecc -d ${domain} > /dev/null 2>&1
  1314. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1315. echo
  1316. else
  1317. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1318. fi
  1319. break
  1320. fi
  1321. done
  1322. fi
  1323. fi
  1324. }
  1325. Del_Apache_Vhost() {
  1326. if [ -e "${apache_install_dir}/bin/httpd" ]; then
  1327. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1328. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1329. ${apache_install_dir}/bin/apachectl -k graceful
  1330. else
  1331. Domain_List=$(ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g")
  1332. if [ -n "${Domain_List}" ]; then
  1333. echo
  1334. echo "Virtualhost list:"
  1335. echo ${CMSG}${Domain_List}${CEND}
  1336. while :; do echo
  1337. read -e -p "Please input a domain you want to delete: " domain
  1338. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1339. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1340. else
  1341. if [ -e "${apache_install_dir}/conf/vhost/${domain}.conf" ]; then
  1342. Directory=$(grep '^<Directory ' ${apache_install_dir}/conf/vhost/${domain}.conf | head -1 | awk -F'"' '{print $2}')
  1343. rm -f ${apache_install_dir}/conf/vhost/${domain}.conf
  1344. [ -e "${apache_install_dir}/conf/ssl/${domain}.crt" ] && rm -f ${apache_install_dir}/conf/ssl/${domain}.{crt,key}
  1345. ${apache_install_dir}/bin/apachectl -k graceful
  1346. while :; do echo
  1347. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1348. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1349. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1350. else
  1351. break
  1352. fi
  1353. done
  1354. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1355. if [ "${quiet_flag}" != 'y' ]; then
  1356. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1357. char=$(get_char)
  1358. fi
  1359. rm -rf ${Directory}
  1360. fi
  1361. [ -d ~/.acme.sh/${domain} ] && ~/.acme.sh/acme.sh --force --remove -d ${domain} > /dev/null 2>&1
  1362. [ -d ~/.acme.sh/${domain}_ecc ] && ~/.acme.sh/acme.sh --force --remove --ecc -d ${domain} > /dev/null 2>&1
  1363. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1364. else
  1365. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1366. fi
  1367. break
  1368. fi
  1369. done
  1370. else
  1371. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1372. fi
  1373. fi
  1374. fi
  1375. }
  1376. Del_Tomcat_Vhost() {
  1377. if [ -e "${tomcat_install_dir}/conf/server.xml" ]; then
  1378. if [ -e "${web_install_dir}/sbin/nginx" ]; then
  1379. if [ -n "$(echo ${domain} | grep '.*\..*')" ] && [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  1380. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  1381. rm -f ${tomcat_install_dir}/conf/vhost/${domain}.xml
  1382. service tomcat restart
  1383. fi
  1384. else
  1385. Domain_List=$(ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g")
  1386. if [ -n "${Domain_List}" ]; then
  1387. echo
  1388. echo "Virtualhost list:"
  1389. echo ${CMSG}${Domain_List}${CEND}
  1390. while :; do echo
  1391. read -e -p "Please input a domain you want to delete: " domain
  1392. if [ -z "$(echo ${domain} | grep '.*\..*')" ]; then
  1393. echo "${CWARNING}Your ${domain} is invalid! ${CEND}"
  1394. else
  1395. if [ -n "$(grep vhost-${domain} ${tomcat_install_dir}/conf/server.xml)" ]; then
  1396. sed -i /vhost-${domain}/d ${tomcat_install_dir}/conf/server.xml
  1397. rm -f ${tomcat_install_dir}/conf/vhost/${domain}.xml
  1398. service tomcat restart
  1399. while :; do echo
  1400. read -e -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_flag
  1401. if [[ ! ${Del_Vhost_wwwroot_flag} =~ ^[y,n]$ ]]; then
  1402. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  1403. else
  1404. break
  1405. fi
  1406. done
  1407. if [ "${Del_Vhost_wwwroot_flag}" == 'y' ]; then
  1408. if [ "${quiet_flag}" != 'y' ]; then
  1409. echo "Press Ctrl+c to cancel or Press any key to continue..."
  1410. char=$(get_char)
  1411. fi
  1412. rm -rf ${Directory}
  1413. fi
  1414. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  1415. else
  1416. echo "${CWARNING}Virtualhost: ${domain} was not exist! ${CEND}"
  1417. fi
  1418. break
  1419. fi
  1420. done
  1421. else
  1422. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1423. fi
  1424. fi
  1425. fi
  1426. }
  1427. List_Vhost() {
  1428. Domain_List_Nginx=$( [ -d "${web_install_dir}/conf/vhost" ] && ls ${web_install_dir}/conf/vhost | sed "s@.conf@@g" )
  1429. Domain_List_Caddy=$( [ -d "${caddy_install_dir}/conf/vhost" ] && ls ${caddy_install_dir}/conf/vhost | sed "s@.conf@@g" )
  1430. Domain_List_Apache=$( [ -e "${apache_install_dir}/bin/httpd" ] && ls ${apache_install_dir}/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g" )
  1431. Domain_List_Tomcat=$( [ -e "${tomcat_install_dir}/conf/server.xml" ] && ls ${tomcat_install_dir}/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g" )
  1432. Domain_List=$(echo "${Domain_List_Nginx} ${Domain_List_Caddy} ${Domain_List_Apache} ${Domain_List_Tomcat}" | tr ' ' '\n' | sort | uniq | xargs)
  1433. if [ -n "${Domain_List}" ]; then
  1434. echo
  1435. echo "Virtualhost list:"
  1436. for D in ${Domain_List}; do echo ${CMSG}${D}${CEND}; done
  1437. else
  1438. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  1439. fi
  1440. }
  1441. if [ ${ARG_NUM} == 0 ]; then
  1442. Install_openSSL | tee -a ${oneinstack_dir}/install.log
  1443. Add_Vhost
  1444. else
  1445. [ "${add_flag}" == 'y' -o "${proxy_flag}" == 'y' -o "${sslquiet_flag}" == 'y' ] && Add_Vhost
  1446. [ "${list_flag}" == 'y' ] && List_Vhost
  1447. [ "${delete_flag}" == 'y' ] && { Del_NGX_Vhost; Del_Apache_Vhost; Del_Tomcat_Vhost; Del_Caddy_Vhost; }
  1448. fi