1. 程式人生 > >ubuntu 12.04下編譯安裝nginx-1.9.3之後 tomcat叢集

ubuntu 12.04下編譯安裝nginx-1.9.3之後 tomcat叢集

   一、tomcat安裝

  1.安裝nginx 請檢視http://blog.csdn.net/liangzi4454/article/details/47023447

   2. jdk安裝不在贅述,不會的同學,可以網上查詢教程;

   3. tomcat下載

       本人tomcat版本為 tomcat-6.0.44,請自行到官網下載對應的版本或是在終端輸入 wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-6/v6.0.44/bin/apache-tomcat-6.0.44.tar.gz 下載。

   4.tomcat安裝

      建立/usr/local/tomcat-6.0.44_1和/usr/local/tomcat-6.0.44_2目錄,並將tomcat解壓兩份到這兩個目錄中。

   5.修改/usr/local/tomcat-6.0.44_2/conf/server.xml 中的三個埠8005為8015,8080為8090,8009為8019。

   6.在以上兩個tomcat目錄的webapps下建立ROOT/a/index.jsp,啟動tomcat ,瀏覽器輸入http://localhost:8080/a/index.jsp和http://localhost:8090/a/index.jsp 見到index頁面,說明安裝成功。

  6.增加tomcat記憶體

     開啟/bin/catalina.sh 頂部加入JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true"

   二、nginx與單個tomcat叢集

   1. 在/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;

    2.在根目錄下建立/home/www/root1/ROOT和/home/www/root1/ROOT目錄,並將剛才tomcat中建立的index.jsp放到該目錄下

    3.修改兩個tomcat的server.xml如下,將第2不建立的兩個目錄加入到appBase中:

      <Host name="localhost"  appBase="/home/www/root1"
            xmlValidation="false" xmlNamespaceAware="false">

    4.開啟/usr/local/nginx/nginx.conf 檔案,修改內容並退出:

     # 執行nginx所在的使用者名稱和使用者組
    user nginx nginx; #新增
    #user  nobody;
    worker_processes  1;
# 全域性錯誤日誌及PID檔案
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log   /usr/local/nginx/logs/error.log; #新增


# pid        logs/nginx.pid;
pid         /usr/local/nginx/nginx.pid; #新增


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    # 全域性訪問日誌
    #access_log  logs/access.log  main;
    access_log  /usr/local/nginx/logs/access.log; #新增
    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    server {
        listen       80;
        server_name  localhost;


        root   /home/www/root1/ROOT; #新增 網站根目錄        


        #charset koi8-r;
        access_log  /usr/local/nginx/logs/host.access.log; #新增
        #access_log  logs/host.access.log  main;


        location / {
           # 下邊兩行暫時注掉
           # root   html;
           # index  index.html index.htm;
           # 新增 將index.jsp 加入到預設的訪問首頁地址
           index  index.html index.htm index.jsp;
           #轉向tomcat處理
           proxy_pass http://localhost:8080;
        }
        #新增 所有jsp的頁面均交由tomcat處理
        location ~ .*.jsp$ {
            index index.jsp;
            proxy_pass http://localhost:8080;
        }
        #新增 設定訪問靜態檔案直接讀取不經過tomcat
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires 30d;
        }
        #新增
        location ~ .*\.(js|css)?$ {
            expires 1h;
        }


        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;


    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}

   5.測試nginx配置

終端執行如下命令:

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

出現如下資訊表示失敗:

nginx: [emerg] getpwnam("nginx") failed in /usr/local/nginx/conf/nginx.conf:2
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

解決辦法: 新增使用者組

  終端執行如下命令:

sudo adduser --system --no-create-home --disabled-password --group nginx

再次測試nginx 出現如下資訊表示成功:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6.啟動nginx後啟動tomcat

終端執行如下兩條命令:

/etc/init.d/nginx start

/usr/local/tomcat-6.0.44_1/bin/catalina.sh start

7. 測試

瀏覽器中輸入http://localhost:8080看到index.jsp資訊表示nginx和單個tomcat叢集建立成功

三、建立nginx和多個tomcat叢集

未完待續。。。