1
0

vhost.sh 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. Nginx_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 "$web_install_dir/conf/ssl" ] && mkdir $web_install_dir/conf/ssl
  149. openssl req -new -newkey rsa:2048 -sha256 -nodes -out $web_install_dir/conf/ssl/${domain}.csr -keyout $web_install_dir/conf/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 $web_install_dir/conf/ssl/${domain}.csr{,_bk.`date +%Y-%m-%d_%H%M`}
  151. /bin/cp $web_install_dir/conf/ssl/${domain}.key{,_bk.`date +%Y-%m-%d_%H%M`}
  152. openssl x509 -req -days 36500 -sha256 -in $web_install_dir/conf/ssl/${domain}.csr -signkey $web_install_dir/conf/ssl/${domain}.key -out $web_install_dir/conf/ssl/${domain}.crt > /dev/null 2>&1
  153. }
  154. Print_ssl() {
  155. echo "`printf "%-30s" "Self-signed SSL Certificate:"`${CMSG}$web_install_dir/conf/ssl/${domain}.crt${CEND}"
  156. echo "`printf "%-30s" "SSL Private Key:"`${CMSG}$web_install_dir/conf/ssl/${domain}.key${CEND}"
  157. echo "`printf "%-30s" "SSL CSR File:"`${CMSG}$web_install_dir/conf/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. fi
  170. while :; do echo
  171. read -p "Please input domain(example: www.linuxeye.com): " domain
  172. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  173. echo "${CWARNING}input error! ${CEND}"
  174. else
  175. break
  176. fi
  177. done
  178. 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
  179. [ -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"
  180. [ -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"
  181. [ -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"
  182. exit
  183. else
  184. echo "domain=$domain"
  185. fi
  186. while :; do echo
  187. read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
  188. if [[ ! $moredomainame_yn =~ ^[y,n]$ ]];then
  189. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  190. else
  191. break
  192. fi
  193. done
  194. if [ "$moredomainame_yn" == 'y' ]; then
  195. while :; do echo
  196. read -p "Type domainname or IP(example: linuxeye.com 121.43.8.8): " moredomain
  197. if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
  198. echo "${CWARNING}input error! ${CEND}"
  199. else
  200. [ "$moredomain" == "$domain" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  201. echo domain list="$moredomain"
  202. moredomainame=" $moredomain"
  203. break
  204. fi
  205. done
  206. Apache_Domain_alias=ServerAlias$moredomainame
  207. Tomcat_Domain_alias=$(for D in `echo $moredomainame`; do echo "<Alias>$D</Alias>"; done)
  208. if [ -e "$web_install_dir/sbin/nginx" ];then
  209. while :; do echo
  210. read -p "Do you want to redirect from $moredomain to $domain? [y/n]: " redirect_yn
  211. if [[ ! $redirect_yn =~ ^[y,n]$ ]];then
  212. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  213. else
  214. break
  215. fi
  216. done
  217. [ "$nginx_ssl_yn" == 'y' ] && HTTP_flag=https || HTTP_flag=http
  218. [ "$redirect_yn" == 'y' ] && Nginx_redirect=$(echo -e "if (\$host != $domain) {\n rewrite ^/(.*)\$ \$scheme://$domain/\$1 permanent;\n }")
  219. fi
  220. fi
  221. if [ "$nginx_ssl_yn" == 'y' ]; then
  222. Nginx_ssl
  223. Nginx_conf=$(echo -e "listen $LISTENOPT;\nssl_certificate $web_install_dir/conf/ssl/$domain.crt;\nssl_certificate_key $web_install_dir/conf/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;")
  224. if [ "$moredomainame_yn" == 'y' ]; then
  225. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain$moredomainame;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  226. else
  227. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  228. fi
  229. else
  230. Nginx_conf='listen 80;'
  231. fi
  232. while :; do echo
  233. echo "Please input the directory for the domain:$domain :"
  234. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  235. if [ -n "$vhostdir" -a -z "`echo $vhostdir | grep '^/'`" ];then
  236. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  237. else
  238. if [ -z "$vhostdir" ]; then
  239. vhostdir="$wwwroot_dir/$domain"
  240. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  241. fi
  242. echo
  243. echo "Create Virtul Host directory......"
  244. mkdir -p $vhostdir
  245. echo "set permissions of Virtual Host directory......"
  246. chown -R ${run_user}.$run_user $vhostdir
  247. break
  248. fi
  249. done
  250. }
  251. Nginx_anti_hotlinking() {
  252. while :; do echo
  253. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  254. if [[ ! $anti_hotlinking_yn =~ ^[y,n]$ ]];then
  255. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  256. else
  257. break
  258. fi
  259. done
  260. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  261. domain_allow="*.${domain#*.} $domain"
  262. else
  263. domain_allow="*.$domain $domain"
  264. fi
  265. if [ "$anti_hotlinking_yn" == 'y' ];then
  266. if [ "$moredomainame_yn" == 'y' ]; then
  267. domain_allow_all=$domain_allow$moredomainame
  268. else
  269. domain_allow_all=$domain_allow
  270. fi
  271. 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 }")
  272. else
  273. anti_hotlinking=
  274. fi
  275. }
  276. Nginx_rewrite() {
  277. [ ! -d "$web_install_dir/conf/rewrite" ] && mkdir $web_install_dir/conf/rewrite
  278. while :; do echo
  279. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  280. if [[ ! $rewrite_yn =~ ^[y,n]$ ]];then
  281. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  282. else
  283. break
  284. fi
  285. done
  286. if [ "$rewrite_yn" == 'n' ];then
  287. rewrite="none"
  288. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  289. else
  290. echo
  291. echo "Please input the rewrite of programme :"
  292. 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."
  293. read -p "(Default rewrite: other):" rewrite
  294. if [ "$rewrite" == "" ]; then
  295. rewrite="other"
  296. fi
  297. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  298. [ "$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 }")
  299. if [ -e "config/$rewrite.conf" ];then
  300. /bin/cp config/$rewrite.conf $web_install_dir/conf/rewrite/$rewrite.conf
  301. else
  302. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  303. fi
  304. fi
  305. }
  306. Nginx_log() {
  307. while :; do echo
  308. read -p "Allow Nginx/Tengine/OpenResty access_log? [y/n]: " access_yn
  309. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  310. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  311. else
  312. break
  313. fi
  314. done
  315. if [ "$access_yn" == 'n' ]; then
  316. N_log="access_log off;"
  317. else
  318. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  319. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  320. fi
  321. }
  322. Create_nginx_tomcat_conf() {
  323. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  324. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  325. server {
  326. $Nginx_conf
  327. server_name $domain$moredomainame;
  328. $N_log
  329. index index.html index.htm index.jsp;
  330. root $vhostdir;
  331. $Nginx_redirect
  332. $anti_hotlinking
  333. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  334. expires 30d;
  335. access_log off;
  336. }
  337. location ~ .*\.(js|css)?$ {
  338. expires 7d;
  339. access_log off;
  340. }
  341. $NGX_CONF
  342. }
  343. $Nginx_http_to_https
  344. EOF
  345. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  346. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  347. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  348. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  349. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  350. </Host>
  351. EOF
  352. [ -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
  353. [ -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
  354. echo
  355. $web_install_dir/sbin/nginx -t
  356. if [ $? == 0 ];then
  357. echo "Reload Nginx......"
  358. $web_install_dir/sbin/nginx -s reload
  359. /etc/init.d/tomcat restart
  360. else
  361. rm -rf $web_install_dir/conf/vhost/$domain.conf
  362. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  363. exit 1
  364. fi
  365. printf "
  366. #######################################################################
  367. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  368. # For more information please visit http://oneinstack.com #
  369. #######################################################################
  370. "
  371. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  372. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  373. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  374. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  375. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  376. }
  377. Create_tomcat_conf() {
  378. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  379. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  380. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  381. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  382. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  383. </Host>
  384. EOF
  385. [ -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
  386. [ -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
  387. echo
  388. /etc/init.d/tomcat restart
  389. printf "
  390. #######################################################################
  391. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  392. # For more information please visit http://oneinstack.com #
  393. #######################################################################
  394. "
  395. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  396. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  397. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  398. echo "`printf "%-30s" "index url:"`${CMSG}http://${domain}:8080/${CEND}"
  399. }
  400. Create_nginx_php-fpm_hhvm_conf() {
  401. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  402. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  403. server {
  404. $Nginx_conf
  405. server_name $domain$moredomainame;
  406. $N_log
  407. index index.html index.htm index.php;
  408. include $web_install_dir/conf/rewrite/$rewrite.conf;
  409. root $vhostdir;
  410. $Nginx_redirect
  411. $anti_hotlinking
  412. $NGX_CONF
  413. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  414. expires 30d;
  415. access_log off;
  416. }
  417. location ~ .*\.(js|css)?$ {
  418. expires 7d;
  419. access_log off;
  420. }
  421. }
  422. $Nginx_http_to_https
  423. EOF
  424. echo
  425. $web_install_dir/sbin/nginx -t
  426. if [ $? == 0 ];then
  427. echo "Reload Nginx......"
  428. $web_install_dir/sbin/nginx -s reload
  429. else
  430. rm -rf $web_install_dir/conf/vhost/$domain.conf
  431. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  432. exit 1
  433. fi
  434. printf "
  435. #######################################################################
  436. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  437. # For more information please visit http://oneinstack.com #
  438. #######################################################################
  439. "
  440. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  441. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  442. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  443. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-30s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  444. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  445. }
  446. Apache_log() {
  447. while :; do echo
  448. read -p "Allow Apache access_log? [y/n]: " access_yn
  449. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  450. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  451. else
  452. break
  453. fi
  454. done
  455. if [ "$access_yn" == 'n' ]; then
  456. A_log='CustomLog "/dev/null" common'
  457. else
  458. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  459. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  460. fi
  461. }
  462. Create_apache_conf() {
  463. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  464. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  465. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  466. <VirtualHost *:80>
  467. ServerAdmin admin@linuxeye.com
  468. DocumentRoot "$vhostdir"
  469. ServerName $domain
  470. $Apache_Domain_alias
  471. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  472. $A_log
  473. <Directory "$vhostdir">
  474. SetOutputFilter DEFLATE
  475. Options FollowSymLinks ExecCGI
  476. $R_TMP
  477. AllowOverride All
  478. Order allow,deny
  479. Allow from all
  480. DirectoryIndex index.html index.php
  481. </Directory>
  482. </VirtualHost>
  483. EOF
  484. echo
  485. $apache_install_dir/bin/apachectl -t
  486. if [ $? == 0 ];then
  487. echo "Restart Apache......"
  488. /etc/init.d/httpd restart
  489. else
  490. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  491. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  492. exit 1
  493. fi
  494. printf "
  495. #######################################################################
  496. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  497. # For more information please visit http://oneinstack.com #
  498. #######################################################################
  499. "
  500. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  501. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  502. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  503. }
  504. Create_nginx_apache_mod-php_conf() {
  505. # Nginx/Tengine
  506. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  507. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  508. server {
  509. $Nginx_conf
  510. server_name $domain$moredomainame;
  511. $N_log
  512. index index.html index.htm index.php;
  513. root $vhostdir;
  514. $Nginx_redirect
  515. $anti_hotlinking
  516. location / {
  517. try_files \$uri @apache;
  518. }
  519. location @apache {
  520. proxy_pass http://127.0.0.1:88;
  521. include proxy.conf;
  522. }
  523. location ~ .*\.(php|php5|cgi|pl)?$ {
  524. proxy_pass http://127.0.0.1:88;
  525. include proxy.conf;
  526. }
  527. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  528. expires 30d;
  529. access_log off;
  530. }
  531. location ~ .*\.(js|css)?$ {
  532. expires 7d;
  533. access_log off;
  534. }
  535. }
  536. $Nginx_http_to_https
  537. EOF
  538. echo
  539. $web_install_dir/sbin/nginx -t
  540. if [ $? == 0 ];then
  541. echo "Reload Nginx......"
  542. $web_install_dir/sbin/nginx -s reload
  543. else
  544. rm -rf $web_install_dir/conf/vhost/$domain.conf
  545. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  546. fi
  547. # Apache
  548. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  549. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  550. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  551. <VirtualHost *:88>
  552. ServerAdmin admin@linuxeye.com
  553. DocumentRoot "$vhostdir"
  554. ServerName $domain
  555. $Apache_Domain_alias
  556. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  557. $A_log
  558. <Directory "$vhostdir">
  559. SetOutputFilter DEFLATE
  560. Options FollowSymLinks ExecCGI
  561. $R_TMP
  562. AllowOverride All
  563. Order allow,deny
  564. Allow from all
  565. DirectoryIndex index.html index.php
  566. </Directory>
  567. </VirtualHost>
  568. EOF
  569. echo
  570. $apache_install_dir/bin/apachectl -t
  571. if [ $? == 0 ];then
  572. echo "Restart Apache......"
  573. /etc/init.d/httpd restart
  574. else
  575. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  576. exit 1
  577. fi
  578. printf "
  579. #######################################################################
  580. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  581. # For more information please visit http://oneinstack.com #
  582. #######################################################################
  583. "
  584. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  585. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  586. echo "`printf "%-30s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  587. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  588. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  589. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  590. }
  591. Add_Vhost() {
  592. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  593. Choose_env
  594. Input_Add_domain
  595. Nginx_anti_hotlinking
  596. if [ "$NGX_FLAG" == 'java' ];then
  597. Nginx_log
  598. Create_nginx_tomcat_conf
  599. else
  600. Nginx_rewrite
  601. Nginx_log
  602. Create_nginx_php-fpm_hhvm_conf
  603. fi
  604. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  605. Choose_env
  606. Input_Add_domain
  607. Apache_log
  608. Create_apache_conf
  609. elif [ ! -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" -a -e "$tomcat_install_dir/conf/server.xml" ];then
  610. Choose_env
  611. Input_Add_domain
  612. Create_tomcat_conf
  613. elif [ -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  614. Choose_env
  615. Input_Add_domain
  616. Nginx_anti_hotlinking
  617. if [ "$NGX_FLAG" == 'java' ];then
  618. Nginx_log
  619. Create_nginx_tomcat_conf
  620. elif [ "$NGX_FLAG" == 'hhvm' ];then
  621. Nginx_rewrite
  622. Nginx_log
  623. Create_nginx_php-fpm_hhvm_conf
  624. elif [ "$NGX_FLAG" == 'php' ];then
  625. #Nginx_rewrite
  626. Nginx_log
  627. Apache_log
  628. Create_nginx_apache_mod-php_conf
  629. fi
  630. fi
  631. }
  632. Del_NGX_Vhost() {
  633. if [ -e "$web_install_dir/sbin/nginx" ];then
  634. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  635. if [ -n "$Domain_List" ];then
  636. echo
  637. echo "Virtualhost list:"
  638. echo ${CMSG}$Domain_List${CEND}
  639. while :; do echo
  640. read -p "Please input a domain you want to delete: " domain
  641. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  642. echo "${CWARNING}input error! ${CEND}"
  643. else
  644. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  645. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  646. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  647. $web_install_dir/sbin/nginx -s reload
  648. while :; do echo
  649. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  650. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  651. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  652. else
  653. break
  654. fi
  655. done
  656. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  657. echo "Press Ctrl+c to cancel or Press any key to continue..."
  658. char=`get_char`
  659. rm -rf $Directory
  660. fi
  661. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  662. else
  663. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  664. fi
  665. break
  666. fi
  667. done
  668. else
  669. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  670. fi
  671. fi
  672. }
  673. Del_Apache_Vhost() {
  674. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  675. if [ -e "$web_install_dir/sbin/nginx" ];then
  676. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  677. /etc/init.d/httpd restart
  678. else
  679. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  680. if [ -n "$Domain_List" ];then
  681. echo
  682. echo "Virtualhost list:"
  683. echo ${CMSG}$Domain_List${CEND}
  684. while :; do echo
  685. read -p "Please input a domain you want to delete: " domain
  686. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  687. echo "${CWARNING}input error! ${CEND}"
  688. else
  689. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  690. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  691. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  692. /etc/init.d/httpd restart
  693. while :; do echo
  694. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  695. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  696. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  697. else
  698. break
  699. fi
  700. done
  701. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  702. echo "Press Ctrl+c to cancel or Press any key to continue..."
  703. char=`get_char`
  704. rm -rf $Directory
  705. fi
  706. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  707. else
  708. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  709. fi
  710. break
  711. fi
  712. done
  713. else
  714. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  715. fi
  716. fi
  717. fi
  718. }
  719. Del_Tomcat_Vhost() {
  720. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  721. if [ -e "$web_install_dir/sbin/nginx" ];then
  722. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  723. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  724. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  725. /etc/init.d/tomcat restart
  726. fi
  727. else
  728. Domain_List=`ls $tomcat_install_dir/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g"`
  729. if [ -n "$Domain_List" ];then
  730. echo
  731. echo "Virtualhost list:"
  732. echo ${CMSG}$Domain_List${CEND}
  733. while :; do echo
  734. read -p "Please input a domain you want to delete: " domain
  735. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  736. echo "${CWARNING}input error! ${CEND}"
  737. else
  738. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  739. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  740. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  741. /etc/init.d/tomcat restart
  742. while :; do echo
  743. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  744. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  745. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  746. else
  747. break
  748. fi
  749. done
  750. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  751. echo "Press Ctrl+c to cancel or Press any key to continue..."
  752. char=`get_char`
  753. rm -rf $Directory
  754. fi
  755. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  756. else
  757. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  758. fi
  759. break
  760. fi
  761. done
  762. else
  763. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  764. fi
  765. fi
  766. fi
  767. }
  768. if [ $# == 0 ];then
  769. Add_Vhost
  770. elif [ $# == 1 ];then
  771. case $1 in
  772. add)
  773. Add_Vhost
  774. ;;
  775. del)
  776. Del_NGX_Vhost
  777. Del_Apache_Vhost
  778. Del_Tomcat_Vhost
  779. ;;
  780. *)
  781. Usage
  782. ;;
  783. esac
  784. else
  785. Usage
  786. fi