系统环境:

centos5.4 X86_64
zh002 192.168.1.12
zh003 192.168.1.13
软件环境:
glassfish-3.1.2-web-unix-ml.sh
nginx-1.2.0._linux.tar.gz
jdk-6u32-linux-x64-rpm.bin
 
1.安装nginx
# useradd nginx -s /sbin/nologin 
# tar xf nginx-1.2.0._linux.tar.gz
# cd nginx-1.2.0
注释掉 nginx-1.2.0/auto/cc/gcc里的以下两行,编译出来的nginx会比较小
# debug
#CFLAGS="$CFLAGS -g"
 
#vim src/core/nginx.h
#define NGINX_VERSION "1.0.2”
#define NGINX_VER "nginx" (修改此行,去掉后面的“NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号)
 
#  yum -y install pcre-devel gcc gcc-c++ openssl-devel zlib-devel make 
# ./configure --user=nginx --group=nginx  --prefix=/etc/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --
 
error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --http-client-
 
body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --with-http_ssl_module --with-
 
http_gzip_static_module --with-http_stub_status_module
# make && make install
# egrep "^$|#" -v /etc/nginx/nginx.conf 
user  nginx nginx;
worker_processes  1;
error_log /var/log/nginx/error.log  warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile    51200;
events {
    use epoll;
    worker_connections  51200;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    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  /var/log/nginx/access.log  access;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  60;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types   text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    server {
        listen       80;
        server_name  192.168.1.12;
        root /opt/nginx/static/ ;
        index  index.html index.htm;
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
        expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
        expires      1h;
        }
 
        location /nds {
        proxy_pass http://192.168.1.12:8080/nds;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_body_buffer_size 128k;
        client_body_temp_path   /tmp/nginx_temp;
        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;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
 
# mkdir -p /var/cache/nginx/client_temp
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
 
 
2. 安装jdk 
# chmod +x jdk-6u32-linux-x64-rpm.bin
# ./jdk-6u32-linux-x64-rpm.bin
 
# vim /etc/profile
添加三行
export JAVA_HOME=/usr/java/jdk1.6.0_27/
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=.:$PATH:$JAVA_HOME/bin:/usr/local/glassfish3/glassfish/bin/   
# source /etc/profile
 
# java -version
java version "1.6.0_32"
Java(TM) SE Runtime Environment (build 1.6.0_32-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.7-b02, mixed mode)
 
 
3. 安装glassfish(需要图形界面,先配置vnc)
# rpm -q vnc-server
vnc-server-4.1.2-14.el5_3.1
# vncserver
设置密码
# vncserver -kill :1
# vim ~/.vnc/xstartup
 
去掉这两行前的# 注释
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
 
# vncserver
 
# ./glassfish-3.1.2-web-unix-ml.sh
 
安装过程共出现http Port: Host name not foud 需要在hosts里添加一条记录
127.0.0.1 zh002 (另外一台机子也是一样,改成zh003即可)
 
装完之后修改/etc/profile里的
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/glassfish3/glassfish/bin/   
 
# source /etc/profile
 
装完后默认的管理端口是4848,web端口是8080
启动服务
# asadmin start-domain domain1
关闭服务:
# asadmin stop-domain domain1
如果需要远程web访问的话,需要打开Server(Admin Server)-General-Secure Administration-Enable Secure Admin (启用远程管理必须是要密码
 
的)
设置密码:Domain-Administrator Password
 
 
可以远程访问管理了
http://192.168.1.12:4848/
http://192.168.1.13:4848/
 
 
4. 配置keepalived 来实现机器的集群功能
 
# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
# yum -y install gcc gcc-c++ openssl-devel
# tar xf keepalived-1.2.2.tar.gz
# cd keepalived-1.2.2
# ./configure --prefix=/usr/local/keepalived
# make && make install
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# mkdir /etc/keepalived
 
192.168.1.12
Keepalived配置:
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
# written by :Dennis
global_defs {
router_id Nginx_Id_2
}
vrrp_script chk_nginx {
script "/opt/check_http.sh"
interval 2
weight 2
}
vrrp_instance Nginx1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 110
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #检查脚本
        chk_nginx
        }
virtual_ipaddress {
192.168.1.14
}
}
 
 
# cat /opt/check_http.sh
#!/bin/bash
url="http://192.168.1.12/index.html"
status=$(/usr/bin/curl -s --head "$url" | awk '/HTTP/ {print $2}')
if [ "$status" != "200" ]; then
/etc/init.d/keepalived stop
fi
 
# chmod +x /opt/check_http.sh
 
启动服务及设置开机自启动:
# nginx
# /etc/init.d/keepalived start
# echo "/usr/sbin/nginx" >> /etc/rc.local
# chkconfig keepalived on
 
192.168.1.13
Keepalived配置:
! Configuration File for keepalived
# written by :Dennis
global_defs {
        router_id Nginx_Id_1
        }
vrrp_script chk_nginx {
        script "/opt/check_http.sh" #定义检测脚本
        interval 2 #检测间隔
        weight 2
}
vrrp_instance Nginx1 { #定义一个实例
        state MASTER #定义为master
        interface eth0
        virtual_router_id 51 # 0-255 在同一个instance 中一致在整个vrrp 中唯一
        priority 150 #优先级,优先级最大的会成为master
        authentication {
        auth_type PASS
        auth_pass 1111
}
track_script { #检查脚本
        chk_nginx
        }
virtual_ipaddress { #此实例的浮动IP
        192.168.1.14
        }
}
 
# cat /opt/check_http.sh
url="http://192.168.1.13/index.html"
status=$(/usr/bin/curl -s --head "$url" | awk '/HTTP/ {print $2}')
if [ "$status" != "200" ]; then
/etc/init.d/keepalived stop
fi
 
# chmod +x /opt/check_http.sh
 
 
启动服务及设置开机自启动:
# nginx
# /etc/init.d/keepalived start
# echo "/usr/sbin/nginx" >> /etc/rc.local
# chkconfig keepalived on