1
0

vhost.sh 57 KB

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