系統環境:javascript
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