Linux下Nginx+Tomcat整合的安裝與配置

來源:互聯網
上載者:User

一、安裝Tomcat和JDK

1、上傳apache-tomcat-6.0.18.tar.gz和jdk-6u12-linux-i586.bin至/usr/local
2、執行如下命令安裝tomcat:

#cd /usr/local #tar zxvf apache-tomcat- 6.0 . 18 .tar.gz

 

解壓完成後將apache-tomcat-6.0.18重新命名為tomcat
3、執行如下命令安裝JDK:

#./jdk-6u12-linux-i586.bin 

4、配置環境變數:
編輯/etc下的profile檔案,加上如下內容:  

JAVA_HOME= "/usr/local/jdk1.6.0_12" CLASS_PATH= "$JAVA_HOME/lib:$JAVA_HOME/jre/lib" PATH= ".:$PATH:$JAVA_HOME/bin "    CATALINA_HOME= "/usr/local/tomcat" export JAVA_HOME CATALINA_HOME

5、啟動tomcat並輸入http://localhost:8080,如果看到貓的頁面即tomcat和jdk安裝成功
6、建立檔案目錄/home/www為網站存放目錄,設定server.xml檔案,在Host name=”localhost”處將appBase=的指向路徑改為/home/www/web
7、建立index.jsp至/home/www/web/ROOT,內容為:“My web!”  

 

二、安裝Nginx
1、上傳nginx-0.7.63.tar.gz至/usr/local

2、執行如下命令解壓nginx:

#cd /usr/local #tar zxvf  nginx- 0.7 . 63 .tar.gz

3、編譯安裝nginx

#cd nginx- 0.7 . 63 #./configure --with-http_stub_status_module --with-http_ssl_module  #啟動server狀態頁和https模組

執行完後會提示一個錯誤,說缺少PCRE library 這個是HTTP Rewrite 模組,也即是url靜態化的包
可上傳pcre-7.9.tar.gz,輸入如下命令安裝:

#tar zxvf pcre- 7.9 .tar.gz #cd pcre- 7.9 #./configure #make #make install

安裝pcre成功後,繼續安裝nginx

#cd nginx- 0.7 . 63 #./configure #make #make install

4、nginx安裝成功後的安裝目錄為/usr/local/nginx
在conf檔案夾中建立proxy.conf,用於配置一些代理參數,內容如下:

#!nginx (-)  # proxy.conf  proxy_redirect          off; proxy_set_header        Host $host; proxy_set_header        X-Real-IP $remote_addr;  #擷取真實ip #proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #擷取代理者的真實ip client_max_body_size    10m; client_body_buffer_size 128k; proxy_connect_timeout    90 ; proxy_send_timeout       90 ; proxy_read_timeout       90 ; proxy_buffer_size       4k; proxy_buffers            4  32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; 

編輯安裝目錄下conf檔案夾中的nginx.conf,輸入如下內容  

#運行nginx所在的使用者名稱和使用者組 #user  www www;     #啟動進程數 worker_processes  8 ; #全域錯誤記錄檔及PID檔案 error_log  /usr/local/nginx/logs/nginx_error.log  crit;    pid        /usr/local/nginx/nginx.pid;    #Specifies the value  for  maximum file descriptors that can be opened by  this  process.    worker_rlimit_nofile  65535 ; #工作模式及串連數上限 events {    use epoll;    worker_connections  65535 ; } #設定http伺服器,利用它的反向 Proxy功能提供負載平衡支援 http {    #設定mime類型    include       mime.types;    default_type  application/octet-stream;    include /usr/local/nginx/conf/proxy.conf;    #charset  gb2312;    #設定請求緩衝        server_names_hash_bucket_size  128 ;    client_header_buffer_size 32k;    large_client_header_buffers  4  32k;    client_max_body_size 8m;             sendfile on;    tcp_nopush     on;       keepalive_timeout  60 ;       tcp_nodelay on;    #  fastcgi_connect_timeout  300 ; #  fastcgi_send_timeout  300 ; #  fastcgi_read_timeout  300 ; #  fastcgi_buffer_size 64k; #  fastcgi_buffers  4  64k; #  fastcgi_busy_buffers_size 128k; #  fastcgi_temp_file_write_size 128k;    #  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;       #limit_zone  crawler  $binary_remote_addr  10m;   ###禁止通過ip訪問網站    server{          server_name _;          return  404 ;          }          server    {      listen        80 ;      server_name  localhost;      index index.html index.htm index.jsp;#設定訪問的預設首頁地址      root  /home/www/web/ROOT;#設定網站的資源存放路徑         #limit_conn   crawler   20 ;                 location ~ .*.jsp$ #所有jsp的頁面均交由tomcat處理      {        index index.jsp;        proxy_pass http: //localhost:8080;#轉向tomcat處理        }                      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #設定訪問靜態檔案直接讀取不經過tomcat      {        expires      30d;      }         location ~ .*\.(js|css)?$      {        expires      1h;      }        #定義訪問日誌的寫入格式       log_format  access   '$remote_addr - $remote_user [$time_local] "$request" '                '$status $body_bytes_sent "$http_referer" '                '"$http_user_agent" $http_x_forwarded_for' ;      access_log  /usr/local/nginx/logs/localhost.log access;#設定訪問日誌的存放路徑              }

5、修改/usr/local/nginx/conf/nginx.conf設定檔後,請執行以下命令檢查設定檔是否正確:

#/usr/local/nginx/sbin/nginx -t

如果螢幕顯示以下兩行資訊,說明設定檔正確:  

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok   the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully

如果提示unknown host,則可在伺服器上執行:ping www.baidu.com如果也是同樣提示unknown host則有兩種可能:
    a、伺服器沒有設定DNS伺服器位址,查看/etc/resolv.conf下是否設定,若無則加上
    b、防火牆攔截

 6、啟動nginx的命令

#/usr/local/nginx/sbin/nginx

這時,輸入以下命令查看Nginx主進程號:

ps -ef | grep  "nginx: master process"  | grep -v  "grep"  | awk -F  ' '  '{print $2}'

7、停止nginx的命令

#/usr/local/nginx/sbin/nginx -s stop

8、在不停止Nginx服務的情況下平滑變更Nginx配置
a、修改/usr/local/nginx/conf/nginx.conf設定檔後,請執行以下命令檢查設定檔是否正確:

/usr/local/nginx/sbin/nginx -t

  如果螢幕顯示以下兩行資訊,說明設定檔正確:

  the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok   the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully

b、這時,輸入以下命令查看Nginx主進程號:

ps -ef | grep  "nginx: master process"  | grep -v  "grep"  | awk -F  ' '  '{print $2}'

螢幕顯示的即為Nginx主進程號,例如:
  6302
  這時,執行以下命令即可使修改過的Nginx設定檔生效:

kill -HUP  6302

  
或者無需這麼麻煩,找到Nginx的Pid檔案:

kill -HUP `cat /usr/local/nginx/nginx.pid`

9、nginx啟動好後啟動tomcat,此時輸入http://主機ip地址即可看到“My web!” 

 
三、其他
stub_status
文法: stub_status on

預設值: None

範圍: location

建立一個 location 地區啟用 stub_status

“stub status” 模組返回的狀態資訊跟 mathopd’s 的狀態資訊很相似. 返回的狀態資訊如下:

Active connections:  291 server accepts handled requests 16630948  16630948  31070465 Reading:  6  Writing:  179  Waiting:  106

active connections — 對後端發起的活動串連數

server accepts handled requests — nginx 總共處理了 16630948 個串連, 成功建立 16630948 次握手 (證明中間沒有失敗的), 總共處理了 31070465 個請求 (平均每次握手處理了 1.8個資料請求)

reading — nginx 讀取到用戶端的Header資訊數

writing — nginx 返回給用戶端的Header資訊數

waiting — 開啟 keep-alive 的情況下,這個值等於 active – (reading + writing),意思就是Nginx說已經處理完正在等候下一次請求指令的駐留串連

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.