nginx+tomcat+JDK 實例



ngnix 1.6.3

tomcat  7.0


1.安裝必要的編譯庫

#yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel pcre pcre-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

 

2.安裝nginx

tar zxvf nginx-1.6.3.tar.gz

  1. [root@localhost src]# cd nginx-1.6.3  
  2. [root@localhost nginx-1.6.3]#  ./configure \  
  3. --prefix=/etc/nginx \  
  4. --sbin-path=/usr/sbin/nginx  \  
  5. --conf-path=/etc/nginx/nginx.conf   \  
  6. --error-log-path=/var/log/nginx/error.log  \  
  7. --http-log-path=/var/log/nginx/access.log   \  
  8. --pid-path=/var/run/nginx.pid   \  
  9. --lock-path=/var/run/nginx.lock  \  
  10. --http-client-body-temp-path=/var/cache/nginx/client_temp  \  
  11. --http-proxy-temp-path=/var/cache/nginx/proxy_temp  \  
  12. --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp  \  
  13. --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp  \  
  14. --http-scgi-temp-path=/var/cache/nginx/scgi_temp  \  
  15. --user=nginx  \  
  16. --group=nginx   \  
  17. --with-http_ssl_module  \  
  18. --with-http_realip_module  \  
  19. --with-http_addition_module  \  
  20. --with-http_sub_module  \  
  21. --with-http_dav_module  \  
  22. --with-http_flv_module  \  
  23. --with-http_mp4_module  \  
  24. --with-http_gunzip_module  \  
  25. --with-http_gzip_static_module \  
  26. --with-http_random_index_module \  
  27. --with-http_secure_link_module \  
  28. --with-http_stub_status_module \  
  29. --with-http_auth_request_module  \  
  30. --with-file-aio  \  
  31. --with-http_spdy_module  \  
  32. --with-ipv6  \  
  33. --with-pcre  

 

    啓動# /usr/sbin/nginx

  1. [root@localhost nginx-1.6.3]# /usr/sbin/nginx -s start 


 測試,直接用curl命令讀取web信息:

  1. [root@localhost sbin]# curl -s http://localhost | grep nginx.com 
  2.  

3.安裝JDK

tar zxvf jdk-7u79-linux-x64.tar.gz

mv jdk1.7.0_79 /usr/local/jdk

配置java運行環境

vi /etc/profile

增加

JAVA_HOME=/usr/local/jdk

JRE_HOME=$JAVA_HOME/jre

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

export JAVA_HOME JRE_HOME PATH CLASSPATH

 

 

重新加載環境變量
. /etc/profile
java -version;   #這時候可以看到java版本信息

 

 

4.安裝tomcat

 

tar -zxvf apache-tomcat-7.0.47.tar.gz

mv apache-tomcat-7.0.47  /usr/local/tomcat

 

在 /etc/profile  文件末尾添加  tomcat變量

TOMCAT_HOME=/usr/local/tomcat

CATALINA_HOME=/usr/local/tomcat

CATALINA_BASE=/usr/local/tomcat

export TOMCAT_HOME CATALINA_HOME CATALINA_BASE

 

source /etc/profile;        #重新加載環境變量 

cp /usr/local/tomcat/bin/catalina.sh /etc/init.d/tomcat;     #添加自動啓動

 

在 /etc/init.d/tomcat  文件裏的第二行添加如下內容

#!/bin/sh

 CATALINA_HOME=/usr/local/tomcat

 JAVA_HOME=/usr/local/jdk

 # chkconfig: 2345 10 90

 # description:Tomcat service

 

 chkconfig tomcat on;        #設置自動啓動

service tomcat start

這時候訪問 http://localhost:8080       #可以看到tomcat的默認頁面(注意防火牆打開8080端口)

 

 

配置tomcat虛擬主機

<Host name="www.test.com"  appBase="webapps"       #指定網站域名
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/home/www/" />        #這裏是指定網站目錄

 

5.配置nginx

vi /etc/nginx/nginx.conf

 

#所有jsp的頁面均交由tomcat處理


添加如下內容到server{} 裏


  location ~ \.(jsp|jspx|do)?$ {
   proxy_set_header  Host $host;  
  proxy_set_header  X-Real-IP  $remote_addr;
   proxy_pass http://127.0.0.1:8080; #轉向tomcat處理
 }            
 location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ #設定訪問靜態文件直接讀取不經過tomcat
 {       
  expires      30d;
 }      
 location ~ .*\.(js|css)?$
 {       
  expires      1h;
 }

 

 #定義訪問日誌的寫入格式      
 log_format  wwwlog  '$remote_addr - $remote_user [$time_local] "$request" '              
  '$status $body_bytes_sent "$http_referer" '             
  '"$http_user_agent" $http_x_forwarded_for';    
 access_log  /data/logs/www_nginx.log wwwlog;#設定訪問日誌的存放路徑  
   } 
}

 

啓動nginx

 /usr/sbin/nginx

 

在網站目錄增加JSP測試代碼vi index.jsp (放在網站/目錄)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">

  <title>My JSP 'MyJsp.jsp' starting page</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->

  </head>

  <script type="text/javascript">
   function bodyLoad(){

     var dateTime=new Date();
     var hh=dateTime.getHours();//..
     var mm=dateTime.getMinutes();//..
     var ss=dateTime.getSeconds();//..

     var yy=dateTime.getFullYear();//..
     var MM=dateTime.getMonth()+1;  //..-..1........0....1
     var dd=dateTime.getDate();//..

     var week=dateTime.getDay();//.(0~6,0.....)
        var days=[ ". ", ". ", ". ", ". ", ". ", ". ", ". ",]

       document.getElementById("date").innerHTML=yy+"."+MM+"."+dd+". "+".."+days[week] ;
       document.getElementById("time").innerHTML=hh+"."+mm+"."+ss+".";

      setTimeout(bodyLoad,1000);
   }
   </script>
  </head>
    <body  οnlοad="bodyLoad()">
     <span id="date" ></span>
      <span  id="time" ></span>
   </body>
</html>


qw保存退出

nginx修改了配置文件要重啓服務纔可以生效 ,

查看主進程號#ps -ef | grep nginx

快速停止   #kill -TERM   指定主進程號 結束


或者不中斷重啓nginx

 #/usr/sbin/nginx -s reload


 

測試正常



雙tomcat.

cp -r /usr/local/tomcat/ /usr/local/tomcat2

vi /usr/local/tomcat2/conf/server.conf

修改端口和路徑和start.sh shutdown.sh

 

配置nginx虛擬主機

cd /usr/local/nginx/conf/

vi nginx.conf

 

增加一行

include /usr/local/nginx/conf/vhost/*.conf;

 

創建文件夾

mkdir vhost

 

cd vhost

建立虛擬主機配置文件:vi web1.conf

server {

    listen       80;

    server_name  web1.com www.web1.com;

 

    location / {

        root   /home/liumin;

        index  index.html index.htm index.jsp;

    }

location ~ \.(jsp|jspx|do)?$ {

        proxy_set_header  Host $host;

        proxy_set_header  X-Real-IP  $remote_addr;

        proxy_pass http://127.0.0.1:9080;

 

 location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$

 {

  expires      30d;

 }

 location ~ .*\.(js|css)?$

 {

  expires      1h;

}

}

 log_format  liumin  '$remote_addr - $remote_user [$time_local] "$request" '

  '$status $body_bytes_sent "$http_referer" '

  '"$http_user_agent" $http_x_forwarded_for';

 access_log  /var/log/nginx/liumin.com.log liumin;

}

 

nginx: [warn] the "log_format" directive may be used only on "http"

如果出現以上錯誤,nginx.conf 配置log_format

 

 

nginx配置文件參考

worker_processes 1;  

  

error_log /host/nginx/logs/error.log crit;  

  

pid /host/nginx/logs/nginx.pid;  

  

events {  

worker_connections 64;  

}  

  

http {  

include /host/nginx/conf/mime.types;  

default_type application/octet-stream;  

  

#charset gb2312;  

  

server_names_hash_bucket_size 128;  

client_header_buffer_size 32k;  

large_client_header_buffers 4 32k;  

  

keepalive_timeout 60;  

  

fastcgi_connect_timeout 300;  

fastcgi_send_timeout 300;  

fastcgi_read_timeout 300;  

fastcgi_buffer_size 128k;  

fastcgi_buffers 4 128k;  

fastcgi_busy_buffers_size 128k;  

fastcgi_temp_file_write_size 128k;  

client_body_temp_path /host/nginx/client_body_temp;  

proxy_temp_path /host/nginx/proxy_temp;  

fastcgi_temp_path /host/nginx/fastcgi_temp;  

  

gzip on;  

gzip_min_length 1k;  

gzip_buffers 4 16k;  

gzip_http_version 1.0;  

gzip_comp_level 2;  

gzip_types text/plain application/x-javascript text/css application/xml;  

gzip_vary on;  

  

client_header_timeout 3m;  

client_body_timeout 3m;  

send_timeout 3m;  

sendfile on;  

tcp_nopush on;  

tcp_nodelay on;  

#設定虛擬主機  

include /host/nginx/conf/vhost/www_test_com.conf;  

include /host/nginx/conf/vhost/www_test1_com.conf;  

include /host/nginx/conf/vhost/www_test2_com.conf;  

}  

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章