### keepalived配置

### nginx安装培训

- 安装nginx

``` cpp
yum install nginx -y
```

- 调整nginx配置文件

``` cpp
[root@redis ~]# sed -e 's@ @@g;/^$/d;/^#/d' /etc/nginx/nginx.conf
worker_processes1;
events{
worker_connections1024;
}
http{
includemime.types;
default_typeapplication/octet-stream;
sendfileon;
keepalive_timeout65;
upstreammycluser
{
server192.168.58.30:8080;
server192.168.58.30:8081;
server192.168.58.10:8080;
server192.168.58.10:8081;
}
server{
listen80;
server_namelocalhost;
location/{
roothtml;
indexindex.htmlindex.htmindex.jsp;
proxy_passhttp://mycluser;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_redirectoff;
}
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
}
}
```

### redis主从配置

- 安装redis

``` cpp
yum install redis -y 
```

- slave redis上添加slaveof 192.168.58.30 6379这一行,这就是二者的区别

``` cpp
[root@mongo1 tmp]# sed -n '/^slaveof/p' /etc/redis.conf
slaveof 192.168.58.30 6379
[root@mongo1 tmp]# 
```

- 测试会话共享

- master上

``` cpp
[root@redis ~]# redis-cli -h 192.168.58.30
redis 192.168.58.30:6379> set name zhuima
OK
redis 192.168.58.30:6379> get name
"zhuima"
redis 192.168.58.30:6379> 
```

- slave上

``` cpp
[root@mongo1 webapps]# redis-cli -h 192.168.58.10
redis 192.168.58.10:6379> get name
"zhuima"
redis 192.168.58.10:6379> 
redis 192.168.58.10:6379> 
```

### tomcat一机多实例配置

- 配置jdk

``` cpp
[root@redis tmp]# tar xf jdk-7u60-linux-x64.gz  -C /usr/local
```

- 配置jdk环境变量

``` cpp
[root@redis local]# cat /etc/profile.d/java.sh 
export JAVA_HOME=/usr/local/jdk1.7.0_60
export PATH=$PATH:$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
[root@redis local]# source /etc/profile.d/java.sh
[root@redis local]# java -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)
[root@redis local]# 
```

- 配置多实例tomcat

``` cpp
[root@redis local]# tar xf apache-tomcat-7.0.54.tar.gz -C /usr/local/
[root@redis local]# mv apache-tomcat-7.0.54/ tomcat1
[root@redis local]# cp -Rf tomcat1 tomcat2
```

- 修改第二个tomcat的三个端口的配置信息

### 修改tomcat的content.xml文件

``` cpp
[root@www conf]# cat context.xml




    
    WEB-INF/web.xml
      
      

    
    
    

```

- 复制给其他tomcat

``` cpp
[root@redis conf]# yes | cp context.xml /usr/local/tomcat2/conf/
cp: overwrite `/usr/local/tomcat2/conf/context.xml'? [root@redis conf]# 
```
``` cpp
[root@redis conf]# for x in tomcat{1,2};do scp context.xml 192.168.58.10:/usr/local/$x/conf/;done
[email protected]'s password: 
context.xml                                                                            100% 1678     1.6KB/s   00:00    
[email protected]'s password: 
context.xml                                                                            100% 1678     1.6KB/s   00:00    
[root@redis conf]# 
```

### 客户端验证会话共享

- 测试文件192.168.58.30上面

``` cpp
[root@redis webapps]# cat index.jsp 
<%@ page language="java" %>
 
 TomcatB 
  
  

192.168.58.30:8081 Tomcat2 

               Session ID    <% session.setAttribute("abc","abc"); %>      <%= session.getId() %>                Created on      <%= session.getCreationTime() %>              ```

- 测试文件192.168.58.10上面

``` cpp
<%@ page language="java" %>
 
 TomcatB 
  
  

192.168.58.10:8080 Tomcat1 

               Session ID    <% session.setAttribute("abc","abc"); %>      <%= session.getId() %>                Created on      <%= session.getCreationTime() %>              ```

### 查看进程存在与否

``` cpp
[root@redis conf]# ps -ef | egrep "[r]edis|[j]ava|[n]ginx"
root      5814     1  1 10:34 pts/0    00:00:41 /usr/local/jdk1.7.0_60/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat1/endorsed -classpath /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat1 -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root      5830     1  1 10:35 pts/0    00:00:41 /usr/local/jdk1.7.0_60/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat2/endorsed -classpath /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat2 -Dcatalina.home=/usr/local/tomcat2 -Djava.io.tmpdir=/usr/local/tomcat2/temp org.apache.catalina.startup.Bootstrap start
redis     5921     1  0 11:07 ?        00:00:02 /usr/sbin/redis-server /etc/redis.conf
root      5989     1  0 11:19 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     5991  5989  0 11:19 ?        00:00:00 nginx: worker process                   
[root@redis conf]# 
```

### 延伸:关于tomcat部署项目的几种方式

- 直接放到webapps的ROOT下面

    - 删除ROOT下的所有文件,把你的项目包解压过去即可

- 直接放到webapps下面

    - 不用操作原来的webapps下面的任何东西,server.xml也不用调整,直接解压到该目录下即可

- 定义context来定义项目文件目录

    - 通过修改server.xml来定义虚拟目录

- 定义.conf/name/localhost定义一个xml文件

    - 通过定义xml文件来定义虚拟目录