vhost.sh 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  15. # For more information please visit http://oneinstack.com #
  16. #######################################################################
  17. "
  18. . ./options.conf
  19. . ./include/color.sh
  20. . ./include/check_dir.sh
  21. . ./include/get_char.sh
  22. # Check if user is root
  23. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  24. Usage() {
  25. printf "
  26. Usage: $0 [ ${CMSG}add${CEND} | ${CMSG}del${CEND} ]
  27. ${CMSG}add${CEND} --->Add Virtualhost
  28. ${CMSG}del${CEND} --->Delete Virtualhost
  29. "
  30. }
  31. Choose_env() {
  32. if [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  33. Number=111
  34. while :; do echo
  35. echo 'Please choose to use environment:'
  36. echo -e "\t${CMSG}1${CEND}. Use php"
  37. echo -e "\t${CMSG}2${CEND}. Use java"
  38. echo -e "\t${CMSG}3${CEND}. Use hhvm"
  39. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  40. [ -z "$Choose_number" ] && Choose_number=1
  41. if [[ ! $Choose_number =~ ^[1-3]$ ]];then
  42. echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
  43. else
  44. break
  45. fi
  46. done
  47. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  48. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  49. [ "$Choose_number" == '3' ] && NGX_FLAG=hhvm
  50. elif [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  51. Number=110
  52. while :; do echo
  53. echo 'Please choose to use environment:'
  54. echo -e "\t${CMSG}1${CEND}. Use php"
  55. echo -e "\t${CMSG}2${CEND}. Use java"
  56. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  57. [ -z "$Choose_number" ] && Choose_number=1
  58. if [[ ! $Choose_number =~ ^[1-2]$ ]];then
  59. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  60. else
  61. break
  62. fi
  63. done
  64. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  65. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  66. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  67. Number=100
  68. NGX_FLAG=php
  69. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  70. Number=101
  71. while :; do echo
  72. echo 'Please choose to use environment:'
  73. echo -e "\t${CMSG}1${CEND}. Use php"
  74. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  75. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  76. [ -z "$Choose_number" ] && Choose_number=1
  77. if [[ ! $Choose_number =~ ^[1-2]$ ]];then
  78. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  79. else
  80. break
  81. fi
  82. done
  83. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  84. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  85. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  86. Number=011
  87. while :; do echo
  88. echo 'Please choose to use environment:'
  89. echo -e "\t${CMSG}1${CEND}. Use java"
  90. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  91. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  92. [ -z "$Choose_number" ] && Choose_number=1
  93. if [[ ! $Choose_number =~ ^[1-2]$ ]];then
  94. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  95. else
  96. break
  97. fi
  98. done
  99. [ "$Choose_number" == '1' ] && NGX_FLAG=java
  100. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  101. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  102. Number=010
  103. NGX_FLAG=java
  104. elif [ ! -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  105. Number=001
  106. NGX_FLAG=hhvm
  107. else
  108. Number=000
  109. NGX_FLAG=php
  110. fi
  111. if [ "$NGX_FLAG" == 'php' ];then
  112. NGX_CONF=$(echo -e "location ~ [^/]\.php(/|$) {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  113. elif [ "$NGX_FLAG" == 'java' ];then
  114. NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
  115. elif [ "$NGX_FLAG" == 'hhvm' ];then
  116. 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 }")
  117. fi
  118. }
  119. Create_SSL() {
  120. printf "
  121. You are about to be asked to enter information that will be incorporated
  122. into your certificate request.
  123. What you are about to enter is what is called a Distinguished Name or a DN.
  124. There are quite a few fields but you can leave some blank
  125. For some fields there will be a default value,
  126. If you enter '.', the field will be left blank.
  127. "
  128. echo
  129. read -p "Country Name (2 letter code) [CN]: " SELFSIGNEDSSL_C
  130. [ -z "$SELFSIGNEDSSL_C" ] && SELFSIGNEDSSL_C=CN
  131. echo
  132. read -p "State or Province Name (full name) [Shanghai]: " SELFSIGNEDSSL_ST
  133. [ -z "$SELFSIGNEDSSL_ST" ] && SELFSIGNEDSSL_ST=Shanghai
  134. echo
  135. read -p "Locality Name (eg, city) [Shanghai]: " SELFSIGNEDSSL_L
  136. [ -z "$SELFSIGNEDSSL_L" ] && SELFSIGNEDSSL_L=Shanghai
  137. echo
  138. read -p "Organization Name (eg, company) [LinuxEye Inc.]: " SELFSIGNEDSSL_O
  139. [ -z "$SELFSIGNEDSSL_O" ] && SELFSIGNEDSSL_O='LinuxEye Inc.'
  140. echo
  141. read -p "Organizational Unit Name (eg, section) [IT Dept.]: " SELFSIGNEDSSL_OU
  142. [ -z "$SELFSIGNEDSSL_OU" ] && SELFSIGNEDSSL_OU='IT Dept.'
  143. if [[ "$($web_install_dir/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  144. LISTENOPT='443 ssl http2'
  145. else
  146. LISTENOPT='443 ssl spdy'
  147. fi
  148. [ ! -d "$PATH_SSL" ] && mkdir $PATH_SSL
  149. openssl req -new -newkey rsa:2048 -sha256 -nodes -out $PATH_SSL/${domain}.csr -keyout $PATH_SSL/${domain}.key -subj "/C=${SELFSIGNEDSSL_C}/ST=${SELFSIGNEDSSL_ST}/L=${SELFSIGNEDSSL_L}/O=${SELFSIGNEDSSL_O}/OU=${SELFSIGNEDSSL_OU}/CN=${domain}" > /dev/null 2>&1
  150. /bin/cp $PATH_SSL/${domain}.csr{,_bk.`date +%Y-%m-%d_%H%M`}
  151. /bin/cp $PATH_SSL/${domain}.key{,_bk.`date +%Y-%m-%d_%H%M`}
  152. 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
  153. }
  154. Print_ssl() {
  155. echo "`printf "%-30s" "Self-signed SSL Certificate:"`${CMSG}$PATH_SSL/${domain}.crt${CEND}"
  156. echo "`printf "%-30s" "SSL Private Key:"`${CMSG}$PATH_SSL/${domain}.key${CEND}"
  157. echo "`printf "%-30s" "SSL CSR File:"`${CMSG}$PATH_SSL/${domain}.csr${CEND}"
  158. }
  159. Input_Add_domain() {
  160. if [ -e "$web_install_dir/sbin/nginx" ];then
  161. while :; do echo
  162. read -p "Do you want to setup SSL under Nginx? [y/n]: " nginx_ssl_yn
  163. if [[ ! $nginx_ssl_yn =~ ^[y,n]$ ]];then
  164. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  165. else
  166. break
  167. fi
  168. done
  169. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/bin/apachectl" ];then
  170. while :; do echo
  171. read -p "Do you want to setup SSL under Apache? [y/n]: " apache_ssl_yn
  172. if [[ ! $apache_ssl_yn =~ ^[y,n]$ ]];then
  173. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  174. else
  175. break
  176. fi
  177. done
  178. fi
  179. [ "$apache_ssl_yn" == 'y' ] && PATH_SSL=$apache_install_dir/conf/ssl
  180. [ "$nginx_ssl_yn" == 'y' ] && PATH_SSL=$web_install_dir/conf/ssl
  181. while :; do echo
  182. read -p "Please input domain(example: www.linuxeye.com): " domain
  183. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  184. echo "${CWARNING}input error! ${CEND}"
  185. else
  186. break
  187. fi
  188. done
  189. 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
  190. [ -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"
  191. [ -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"
  192. [ -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"
  193. exit
  194. else
  195. echo "domain=$domain"
  196. fi
  197. while :; do echo
  198. read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
  199. if [[ ! $moredomainame_yn =~ ^[y,n]$ ]];then
  200. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  201. else
  202. break
  203. fi
  204. done
  205. if [ "$moredomainame_yn" == 'y' ]; then
  206. while :; do echo
  207. read -p "Type domainname or IP(example: linuxeye.com 121.43.8.8): " moredomain
  208. if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
  209. echo "${CWARNING}input error! ${CEND}"
  210. else
  211. [ "$moredomain" == "$domain" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  212. echo domain list="$moredomain"
  213. moredomainame=" $moredomain"
  214. break
  215. fi
  216. done
  217. Apache_Domain_alias=ServerAlias$moredomainame
  218. Tomcat_Domain_alias=$(for D in `echo $moredomainame`; do echo "<Alias>$D</Alias>"; done)
  219. if [ -e "$web_install_dir/sbin/nginx" ];then
  220. while :; do echo
  221. read -p "Do you want to redirect from $moredomain to $domain? [y/n]: " redirect_yn
  222. if [[ ! $redirect_yn =~ ^[y,n]$ ]];then
  223. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  224. else
  225. break
  226. fi
  227. done
  228. [ "$nginx_ssl_yn" == 'y' ] && HTTP_flag=https || HTTP_flag=http
  229. [ "$redirect_yn" == 'y' ] && Nginx_redirect=$(echo -e "if (\$host != $domain) {\n rewrite ^/(.*)\$ \$scheme://$domain/\$1 permanent;\n }")
  230. fi
  231. fi
  232. if [ "$nginx_ssl_yn" == 'y' ]; then
  233. Create_SSL
  234. Nginx_conf=$(echo -e "listen $LISTENOPT;\nssl_certificate $PATH_SSL/$domain.crt;\nssl_certificate_key $PATH_SSL/$domain.key;\nssl_session_timeout 10m;\nssl_protocols TLSv1 TLSv1.1 TLSv1.2;\nssl_prefer_server_ciphers on;\nssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:RC4-SHA:\!aNULL:\!eNULL:\!EXPORT:\!DES:\!3DES:\!MD5:\!DSS:\!PKS";\nssl_session_cache builtin:1000 shared:SSL:10m;\nresolver 8.8.8.8 8.8.4.4 valid=300s;\nresolver_timeout 5s;")
  235. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"$PATH_SSL/$domain.crt\"\n SSLCertificateKeyFile \"$PATH_SSL/$domain.key\"")
  236. if [ "$moredomainame_yn" == 'y' ]; then
  237. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain$moredomainame;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  238. else
  239. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  240. fi
  241. elif [ "$apache_ssl_yn" == 'y' ]; then
  242. Create_SSL
  243. Apache_SSL=$(echo -e "SSLEngine on\n SSLCertificateFile \"$PATH_SSL/$domain.crt\"\n SSLCertificateKeyFile \"$PATH_SSL/$domain.key\"")
  244. [ -z "`grep 'Listen 443' $apache_install_dir/conf/httpd.conf`" ] && sed -i "s@Listen 80@&\nListen 443@" $apache_install_dir/conf/httpd.conf
  245. [ -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
  246. else
  247. Nginx_conf='listen 80;'
  248. fi
  249. while :; do echo
  250. echo "Please input the directory for the domain:$domain :"
  251. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  252. if [ -n "$vhostdir" -a -z "`echo $vhostdir | grep '^/'`" ];then
  253. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  254. else
  255. if [ -z "$vhostdir" ]; then
  256. vhostdir="$wwwroot_dir/$domain"
  257. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  258. fi
  259. echo
  260. echo "Create Virtul Host directory......"
  261. mkdir -p $vhostdir
  262. echo "set permissions of Virtual Host directory......"
  263. chown -R ${run_user}.$run_user $vhostdir
  264. break
  265. fi
  266. done
  267. }
  268. Nginx_anti_hotlinking() {
  269. while :; do echo
  270. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  271. if [[ ! $anti_hotlinking_yn =~ ^[y,n]$ ]];then
  272. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  273. else
  274. break
  275. fi
  276. done
  277. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  278. domain_allow="*.${domain#*.} $domain"
  279. else
  280. domain_allow="*.$domain $domain"
  281. fi
  282. if [ "$anti_hotlinking_yn" == 'y' ];then
  283. if [ "$moredomainame_yn" == 'y' ]; then
  284. domain_allow_all=$domain_allow$moredomainame
  285. else
  286. domain_allow_all=$domain_allow
  287. fi
  288. anti_hotlinking=$(echo -e "location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {\n valid_referers none blocked $domain_allow_all;\n if (\$invalid_referer) {\n #rewrite ^/ http://www.linuxeye.com/403.html;\n return 403;\n }\n }")
  289. else
  290. anti_hotlinking=
  291. fi
  292. }
  293. Nginx_rewrite() {
  294. [ ! -d "$web_install_dir/conf/rewrite" ] && mkdir $web_install_dir/conf/rewrite
  295. while :; do echo
  296. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  297. if [[ ! $rewrite_yn =~ ^[y,n]$ ]];then
  298. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  299. else
  300. break
  301. fi
  302. done
  303. if [ "$rewrite_yn" == 'n' ];then
  304. rewrite="none"
  305. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  306. else
  307. echo
  308. echo "Please input the rewrite of programme :"
  309. echo "${CMSG}wordpress${CEND},${CMSG}discuz${CEND},${CMSG}opencart${CEND},${CMSG}thinkphp${CEND},${CMSG}laravel${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND} rewrite was exist."
  310. read -p "(Default rewrite: other):" rewrite
  311. if [ "$rewrite" == "" ]; then
  312. rewrite="other"
  313. fi
  314. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  315. [ "$NGX_FLAG" == 'php' -a "$rewrite" == "thinkphp" ] && NGX_CONF=$(echo -e "location ~ \.php {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi_params;\n set \$real_script_name \$fastcgi_script_name;\n if (\$fastcgi_script_name ~ \"^(.+?\.php)(/.+)\$\") {\n set \$real_script_name \$1;\n #set \$path_info \$2;\n }\n fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;\n fastcgi_param SCRIPT_NAME \$real_script_name;\n #fastcgi_param PATH_INFO \$path_info;\n }")
  316. if [ -e "config/$rewrite.conf" ];then
  317. /bin/cp config/$rewrite.conf $web_install_dir/conf/rewrite/$rewrite.conf
  318. else
  319. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  320. fi
  321. fi
  322. }
  323. Nginx_log() {
  324. while :; do echo
  325. read -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_yn
  326. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  327. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  328. else
  329. break
  330. fi
  331. done
  332. if [ "$access_yn" == 'n' ]; then
  333. N_log="access_log off;"
  334. else
  335. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  336. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  337. fi
  338. }
  339. Create_nginx_tomcat_conf() {
  340. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  341. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  342. server {
  343. $Nginx_conf
  344. server_name $domain$moredomainame;
  345. $N_log
  346. index index.html index.htm index.jsp;
  347. root $vhostdir;
  348. $Nginx_redirect
  349. $anti_hotlinking
  350. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  351. expires 30d;
  352. access_log off;
  353. }
  354. location ~ .*\.(js|css)?$ {
  355. expires 7d;
  356. access_log off;
  357. }
  358. $NGX_CONF
  359. }
  360. $Nginx_http_to_https
  361. EOF
  362. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  363. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  364. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  365. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  366. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  367. </Host>
  368. EOF
  369. [ -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
  370. [ -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
  371. echo
  372. $web_install_dir/sbin/nginx -t
  373. if [ $? == 0 ];then
  374. echo "Reload Nginx......"
  375. $web_install_dir/sbin/nginx -s reload
  376. /etc/init.d/tomcat restart
  377. else
  378. rm -rf $web_install_dir/conf/vhost/$domain.conf
  379. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  380. exit 1
  381. fi
  382. printf "
  383. #######################################################################
  384. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  385. # For more information please visit http://oneinstack.com #
  386. #######################################################################
  387. "
  388. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  389. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  390. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  391. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  392. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  393. }
  394. Create_tomcat_conf() {
  395. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  396. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  397. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  398. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  399. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  400. </Host>
  401. EOF
  402. [ -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
  403. [ -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
  404. echo
  405. /etc/init.d/tomcat restart
  406. printf "
  407. #######################################################################
  408. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  409. # For more information please visit http://oneinstack.com #
  410. #######################################################################
  411. "
  412. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  413. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  414. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  415. echo "`printf "%-30s" "index url:"`${CMSG}http://${domain}:8080/${CEND}"
  416. }
  417. Create_nginx_php-fpm_hhvm_conf() {
  418. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  419. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  420. server {
  421. $Nginx_conf
  422. server_name $domain$moredomainame;
  423. $N_log
  424. index index.html index.htm index.php;
  425. include $web_install_dir/conf/rewrite/$rewrite.conf;
  426. root $vhostdir;
  427. $Nginx_redirect
  428. $anti_hotlinking
  429. $NGX_CONF
  430. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  431. expires 30d;
  432. access_log off;
  433. }
  434. location ~ .*\.(js|css)?$ {
  435. expires 7d;
  436. access_log off;
  437. }
  438. }
  439. $Nginx_http_to_https
  440. EOF
  441. echo
  442. $web_install_dir/sbin/nginx -t
  443. if [ $? == 0 ];then
  444. echo "Reload Nginx......"
  445. $web_install_dir/sbin/nginx -s reload
  446. else
  447. rm -rf $web_install_dir/conf/vhost/$domain.conf
  448. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  449. exit 1
  450. fi
  451. printf "
  452. #######################################################################
  453. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  454. # For more information please visit http://oneinstack.com #
  455. #######################################################################
  456. "
  457. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  458. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  459. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  460. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-30s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  461. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  462. }
  463. Apache_log() {
  464. while :; do echo
  465. read -p "Allow Apache access_log? [y/n]: " access_yn
  466. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  467. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  468. else
  469. break
  470. fi
  471. done
  472. if [ "$access_yn" == 'n' ]; then
  473. A_log='CustomLog "/dev/null" common'
  474. else
  475. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  476. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  477. fi
  478. }
  479. Create_apache_conf() {
  480. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  481. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  482. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  483. <VirtualHost *:80>
  484. ServerAdmin admin@linuxeye.com
  485. DocumentRoot "$vhostdir"
  486. ServerName $domain
  487. $Apache_Domain_alias
  488. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  489. $A_log
  490. <Directory "$vhostdir">
  491. SetOutputFilter DEFLATE
  492. Options FollowSymLinks ExecCGI
  493. $R_TMP
  494. AllowOverride All
  495. Order allow,deny
  496. Allow from all
  497. DirectoryIndex index.html index.php
  498. </Directory>
  499. </VirtualHost>
  500. EOF
  501. [ "$apache_ssl_yn" == 'y' ] && cat >> $apache_install_dir/conf/vhost/$domain.conf << EOF
  502. <VirtualHost *:443>
  503. ServerAdmin admin@linuxeye.com
  504. DocumentRoot "$vhostdir"
  505. ServerName $domain
  506. $Apache_Domain_alias
  507. $Apache_SSL
  508. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  509. $A_log
  510. <Directory "$vhostdir">
  511. SetOutputFilter DEFLATE
  512. Options FollowSymLinks ExecCGI
  513. $R_TMP
  514. AllowOverride All
  515. Order allow,deny
  516. Allow from all
  517. DirectoryIndex index.html index.php
  518. </Directory>
  519. </VirtualHost>
  520. EOF
  521. echo
  522. $apache_install_dir/bin/apachectl -t
  523. if [ $? == 0 ];then
  524. echo "Restart Apache......"
  525. /etc/init.d/httpd restart
  526. else
  527. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  528. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  529. exit 1
  530. fi
  531. printf "
  532. #######################################################################
  533. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  534. # For more information please visit http://oneinstack.com #
  535. #######################################################################
  536. "
  537. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  538. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  539. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  540. }
  541. Create_nginx_apache_mod-php_conf() {
  542. # Nginx/Tengine
  543. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  544. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  545. server {
  546. $Nginx_conf
  547. server_name $domain$moredomainame;
  548. $N_log
  549. index index.html index.htm index.php;
  550. root $vhostdir;
  551. $Nginx_redirect
  552. $anti_hotlinking
  553. location / {
  554. try_files \$uri @apache;
  555. }
  556. location @apache {
  557. proxy_pass http://127.0.0.1:88;
  558. include proxy.conf;
  559. }
  560. location ~ .*\.(php|php5|cgi|pl)?$ {
  561. proxy_pass http://127.0.0.1:88;
  562. include proxy.conf;
  563. }
  564. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  565. expires 30d;
  566. access_log off;
  567. }
  568. location ~ .*\.(js|css)?$ {
  569. expires 7d;
  570. access_log off;
  571. }
  572. }
  573. $Nginx_http_to_https
  574. EOF
  575. echo
  576. $web_install_dir/sbin/nginx -t
  577. if [ $? == 0 ];then
  578. echo "Reload Nginx......"
  579. $web_install_dir/sbin/nginx -s reload
  580. else
  581. rm -rf $web_install_dir/conf/vhost/$domain.conf
  582. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  583. fi
  584. # Apache
  585. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  586. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  587. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  588. <VirtualHost *:88>
  589. ServerAdmin admin@linuxeye.com
  590. DocumentRoot "$vhostdir"
  591. ServerName $domain
  592. $Apache_Domain_alias
  593. $Apache_SSL
  594. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  595. $A_log
  596. <Directory "$vhostdir">
  597. SetOutputFilter DEFLATE
  598. Options FollowSymLinks ExecCGI
  599. $R_TMP
  600. AllowOverride All
  601. Order allow,deny
  602. Allow from all
  603. DirectoryIndex index.html index.php
  604. </Directory>
  605. </VirtualHost>
  606. EOF
  607. echo
  608. $apache_install_dir/bin/apachectl -t
  609. if [ $? == 0 ];then
  610. echo "Restart Apache......"
  611. /etc/init.d/httpd restart
  612. else
  613. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  614. exit 1
  615. fi
  616. printf "
  617. #######################################################################
  618. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  619. # For more information please visit http://oneinstack.com #
  620. #######################################################################
  621. "
  622. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  623. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  624. echo "`printf "%-30s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  625. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  626. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  627. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  628. }
  629. Add_Vhost() {
  630. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  631. Choose_env
  632. Input_Add_domain
  633. Nginx_anti_hotlinking
  634. if [ "$NGX_FLAG" == 'java' ];then
  635. Nginx_log
  636. Create_nginx_tomcat_conf
  637. else
  638. Nginx_rewrite
  639. Nginx_log
  640. Create_nginx_php-fpm_hhvm_conf
  641. fi
  642. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  643. Choose_env
  644. Input_Add_domain
  645. Apache_log
  646. Create_apache_conf
  647. elif [ ! -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" -a -e "$tomcat_install_dir/conf/server.xml" ];then
  648. Choose_env
  649. Input_Add_domain
  650. Create_tomcat_conf
  651. elif [ -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  652. Choose_env
  653. Input_Add_domain
  654. Nginx_anti_hotlinking
  655. if [ "$NGX_FLAG" == 'java' ];then
  656. Nginx_log
  657. Create_nginx_tomcat_conf
  658. elif [ "$NGX_FLAG" == 'hhvm' ];then
  659. Nginx_rewrite
  660. Nginx_log
  661. Create_nginx_php-fpm_hhvm_conf
  662. elif [ "$NGX_FLAG" == 'php' ];then
  663. #Nginx_rewrite
  664. Nginx_log
  665. Apache_log
  666. Create_nginx_apache_mod-php_conf
  667. fi
  668. fi
  669. }
  670. Del_NGX_Vhost() {
  671. if [ -e "$web_install_dir/sbin/nginx" ];then
  672. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  673. if [ -n "$Domain_List" ];then
  674. echo
  675. echo "Virtualhost list:"
  676. echo ${CMSG}$Domain_List${CEND}
  677. while :; do echo
  678. read -p "Please input a domain you want to delete: " domain
  679. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  680. echo "${CWARNING}input error! ${CEND}"
  681. else
  682. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  683. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  684. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  685. $web_install_dir/sbin/nginx -s reload
  686. while :; do echo
  687. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  688. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  689. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  690. else
  691. break
  692. fi
  693. done
  694. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  695. echo "Press Ctrl+c to cancel or Press any key to continue..."
  696. char=`get_char`
  697. rm -rf $Directory
  698. fi
  699. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  700. else
  701. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  702. fi
  703. break
  704. fi
  705. done
  706. else
  707. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  708. fi
  709. fi
  710. }
  711. Del_Apache_Vhost() {
  712. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  713. if [ -e "$web_install_dir/sbin/nginx" ];then
  714. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  715. /etc/init.d/httpd restart
  716. else
  717. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  718. if [ -n "$Domain_List" ];then
  719. echo
  720. echo "Virtualhost list:"
  721. echo ${CMSG}$Domain_List${CEND}
  722. while :; do echo
  723. read -p "Please input a domain you want to delete: " domain
  724. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  725. echo "${CWARNING}input error! ${CEND}"
  726. else
  727. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  728. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  729. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  730. /etc/init.d/httpd restart
  731. while :; do echo
  732. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  733. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  734. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  735. else
  736. break
  737. fi
  738. done
  739. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  740. echo "Press Ctrl+c to cancel or Press any key to continue..."
  741. char=`get_char`
  742. rm -rf $Directory
  743. fi
  744. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  745. else
  746. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  747. fi
  748. break
  749. fi
  750. done
  751. else
  752. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  753. fi
  754. fi
  755. fi
  756. }
  757. Del_Tomcat_Vhost() {
  758. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  759. if [ -e "$web_install_dir/sbin/nginx" ];then
  760. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  761. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  762. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  763. /etc/init.d/tomcat restart
  764. fi
  765. else
  766. Domain_List=`ls $tomcat_install_dir/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g"`
  767. if [ -n "$Domain_List" ];then
  768. echo
  769. echo "Virtualhost list:"
  770. echo ${CMSG}$Domain_List${CEND}
  771. while :; do echo
  772. read -p "Please input a domain you want to delete: " domain
  773. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  774. echo "${CWARNING}input error! ${CEND}"
  775. else
  776. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  777. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  778. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  779. /etc/init.d/tomcat restart
  780. while :; do echo
  781. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  782. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  783. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  784. else
  785. break
  786. fi
  787. done
  788. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  789. echo "Press Ctrl+c to cancel or Press any key to continue..."
  790. char=`get_char`
  791. rm -rf $Directory
  792. fi
  793. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  794. else
  795. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  796. fi
  797. break
  798. fi
  799. done
  800. else
  801. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  802. fi
  803. fi
  804. fi
  805. }
  806. if [ $# == 0 ];then
  807. Add_Vhost
  808. elif [ $# == 1 ];then
  809. case $1 in
  810. add)
  811. Add_Vhost
  812. ;;
  813. del)
  814. Del_NGX_Vhost
  815. Del_Apache_Vhost
  816. Del_Tomcat_Vhost
  817. ;;
  818. *)
  819. Usage
  820. ;;
  821. esac
  822. else
  823. Usage
  824. fi