安装配置JDK:
将jdk-7u65-linux-x64.tar.gz解压
[root@tomcat-1 ~]# rm -rf /usr/bin/java
[root@tomcat-1 ~]# tar xf jdk-7u65-linux-x64.tar.gz
解压后会生成jdk1.7.0_65文件夹,将文件夹移动到/usr/local下并重命名为java
[root@tomcat-1 ~]# mv jdk1.7.0_65/ /usr/local/java
在/etc/profile.d/ 下建立java.sh脚本
[root@tomcat-1 ~]# vim /etc/profile #末尾出追加
export JAVA_HOME=/usr/local/java #设置java根目录
export PATH=
P
A
T
H
:
PATH:
PATH:JAVA_HOME/bin #在PATH环境变量中添加java跟目录的bin子目录
将java.sh 脚本导入到环境变量,使其生效
[root@tomcat-1 ~]# source /etc/profile
运行 java -version 或者 javac -version 命令查看java版本
[root@tomcat-1 ~]# java -version
java version “1.7.0_65”
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)
[root@tomcat-1 ~]# javac -version
javac 1.7.0_65
安装配置Tomcat:
解压软件包
[root@tomcat-1 ~]# tar xf apache-tomcat-7.0.54.tar.gz
解压后生成apache-tomcat-7.0.54文件夹,将该文件夹移动到/usr/local下,并改名为tomcat7
[root@tomcat-1 ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat7
启动Tomcat
[root@tomcat-1 ~]# /usr/local/tomcat7/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.
Tomcat 默认运行在8080端口
[root@tomcat-1 ~]# netstat -anpt |grep :8080
tcp 0 0 :::8080 ::? LISTE
N 55349/java
关闭Tomcat
[root@tomcat-1 ~]# /usr/local/tomcat7/bin/shutdown.sh
浏览器访问测试 http://192.168.200.103:8080
建立java的web站点:
首先在跟目录下建立一个webapp目录,用于存放网站文件
[root@tomcat-1 ~]# mkdir /webapp
在webapp1目录下建立一个index.jsp的测试页面
[root@tomcat-1 ~]# vim /webapp/index.jsp
Server Info:
SessionID:<%=session.getId()%>
SessionIP:<%=request.getServerName()%>
SessionPort:<%=request.getServerPort()%>
<%
out.println(“server one”);
%>
Tomcat 配置相关说明
/usr/local/tomcat7 #主目录
bin #存放windows或linux平台上启动或关闭的Tomcat的脚本文件
conf #存放Tomcat的各种全局配置文件,其中最主要的是server.xml和web.xml
lib #存放Tomcat运行需要的库文件(JARS)
logs #存放Tomcat执行时的LOG文件
webapps #Tomcat的主要Web发布目录(包括应用程序事例)
work #存放jsp编译后产生的class文件
[root@tomcat-1 ~]# ls /usr/local/tomcat7/conf/
catalina.policy #权限控制配置文件
catalina.properties #Tomcat属性配置文件
context.xml #上下文配置文件(selinux)
logging.properties #日志log相关配置文件
server.xml #主配置文件
tomcat-users.xml #manager-gui管理用户配置文件(Tomcat安装后生成的管理界面,该文件可开启访问)
web.xml #Tomcat的servlet,servlet-mapping,filter,MIME等相关配置
server.xml 主要配置文件,可修改启动端口,设置网站根目录,虚拟主机,开启https等功能。
server.xml的结构构成
[root@nginx-2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
crushlinux@163.com
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.200.254
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.200.253
}
}
[root@nginx-1 ~]# service keepalived start
[root@nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.254/32 scope global eth0
inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
valid_lft forever preferred_lft forever
[root@nginx-2 ~]# service keepalived start
[root@nginx-2 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:6f:7d:87 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.102/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.253/32 scope global eth0
inet6 fe80::20c:29ff:fe6f:7d87/64 scope link
valid_lft forever preferred_lft forever
客户端测试:
打开浏览器访问: http://192.168.200.253 #不断刷新可看到由于权重相同,页面会反复切换
客户端测试:
打开浏览器访问: http://192.168.200.254 #不断刷新可看到由于权重相同,页面会反复切换
nginx-1/2 二台机器都执行监控Nginx进程的脚本
[root@nginx-1 ~]# cat nginx_pidcheck
#!/bin/bash
while :
do
nginxpid=ps -C nginx --no-header | wc -l
if [
n
g
i
n
x
p
i
d
−
e
q
0
]
t
h
e
n
/
u
s
r
/
l
o
c
a
l
/
n
g
i
n
x
/
s
b
i
n
/
n
g
i
n
x
k
e
e
p
p
i
d
=
nginxpid -eq 0 ] then /usr/local/nginx/sbin/nginx keeppid=
nginxpid−eq0]then/usr/local/nginx/sbin/nginxkeeppid=(ps -C keepalived --no-header | wc -l)
if [ $keeppid -eq 0 ]
then
/etc/init.d/keepalived start
fi
sleep 5
nginxpid=ps -C nginx --no-header | wc -l
if [ $nginxpid -eq 0 ]
then
/etc/init.d/keepalived stop
fi
fi
sleep 5
done
[root@nginx-1 ~]# sh nginx_pidcheck &
[root@nginx-1 ~]# vim /etc/rc.local
sh nginx_pidcheck &
这是执行无限循环的脚本,两台Nginx机器上都有执行此脚本,每隔5秒执行一次,用ps -C是命令来收集nginx的PID值到底是否为0,如果是0的话,即Nginx已经进程死掉,尝试启动nginx进程;如果继续为0,即Nginx启动失败,则关闭本机的Keeplaived服务,VIP地址则会由备机接管,当然了,整个网站就会全部由备机的Nginx来提供服务了,这样保证Nginx服务的高可用。
脚本测试:
[root@nginx-1 ~]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4321/nginx
[root@nginx-1 ~]# killall -s QUIT nginx
[root@nginx-1 ~]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 59418/nginx
VIP转移测试:
[root@nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.101/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.254/32 scope global eth0
inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
valid_lft forever preferred_lft forever
[root@tomcat-1 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel
[root@tomcat-1 ~]# tar xf libevent-2.0.15-stable.tar.gz
[root@tomcat-1 ~]# cd libevent-2.0.15-stable
[root@tomcat-1 libevent-2.0.15-stable]# ./configure --prefix=/usr/local/libevent && make && make install
[root@tomcat-1 ~]# tar xf memcached-1.4.5.tar.gz
[root@tomcat-1 ~]# cd memcached-1.4.5
[root@tomcat-1 memcached-1.4.5]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/ && make && make install
[root@tomcat-1 memcached-1.4.5]# ldconfig -v |grep libevent
libevent_pthreads-2.0.so.5 -> libevent_pthreads.so
libevent-2.0.so.5 -> libevent.so
libevent_extra-2.0.so.5 -> libevent_extra.so
libevent_core-2.0.so.5 -> libevent_core.so
libevent_openssl-2.0.so.5 -> libevent_openssl.so
libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
[root@tomcat-1 memcached-1.4.5]# /usr/local/memcached/bin/memcached -u root -m 512M -n 10 -f 2 -d -vvv -c 512
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
[root@localhost memcached-1.4.5]# vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/libevent/lib/
[root@localhost memcached-1.4.5]# ldconfig
[root@localhost memcached-1.4.5]# /usr/local/memcached/bin/memcached -u root -m 512M -n 10 -f 2 -d -vvv -c 512
选项:
-h #查看帮助信息
-p #指定memcached监听的端口号默认11211
-l #memcached服务器的ip地址
-u #memcached程序运行时使用的用户身份必须是root用户
-m #指定使用本机的多少物理内存存数据默认64M
-c #memcached服务的最大链接数
-vvv #显示详细信息
-n #chunk size 的最小空间是多少单位字节
-f #chunk size大小增长的倍数默认 1.25倍
-d #在后台启动
[root@tomcat-1 ~]# netstat -antp| grep :11211 #(检测memecached是否存活,memcacehd 端口为11211)
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 71559/memcached
tcp 0 0 :::11211 ::? LISTEN 71559/memcached
测试memcached 能否存取数据
[root@tomcat-1 ~]# yum -y install telnet
[root@localhost ~]# telnet 192.168.200.103 11211
set username 0 0 8
zhangsan
STORED
get username
VALUE username 0 8
zhangsan
END
quit
Connection closed by foreign host.
最后执行让Tomcat-1 Tomcat-2 通过(msm)连接到Memcached
将session包中的“.jar复制到/usr/local/tomcat7/lib/ 下面
[root@tomcat-1 ~]# cp session/ /usr/local/tomcat7/lib/
编辑tomcat 配置文件连接指定的 memcached服务器
tomcat-1 和 tomcat-2 配置文件一模一样,都按照一下样例写
[root@tomcat-1 ~]# vim /usr/local/tomcat7/conf/context.xml
[root@tomcat-2 ~]# vim /usr/local/tomcat7/conf/context.xml
[root@tomcat-1 ~]# /usr/local/tomcat7/bin/shutdown.sh
[root@tomcat-1 ~]# /usr/local/tomcat7/bin/startup.sh
如果成功,tomcat与Memcached 端口会连在一起,前后有变化
Tomcat-1与Tomcat-2如下图
[root@tomcat-1 ~]# netstat -antp|grep java
tcp 0 0 ::ffff:127.0.0.1:8005 ::? LISTEN 62496/java
tcp 0 0 :::8009 ::? LISTEN 62496/java
tcp 0 0 :::8080 ::? LISTEN 62496/java
tcp 0 0 ::ffff:192.168.200.10:28232 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28231 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28230 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28228 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28229 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
[root@tomcat-1 ~]# netstat -antp|grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28230 ESTABLISHED 62402/memcached
tcp 45 0 192.168.200.103:11211 192.168.200.103:28228 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28232 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28229 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28231 ESTABLISHED 62402/memcached
tcp 0 0 :::11211 ::? LISTEN 62402/memcached****