一、环境准备
时间同步
关闭防火墙
二、安装nginx
1、解决依赖关系
[root@nginx-server ~]# yum install gcc openssl-devel pcre-devel zlib-devel -y
2、添加用户nginx,实现以之运行nginx服务进程
[root@nginx-server ~]# groupadd -r nginx
[root@nginx-server ~]# useradd -r -g nginx -s /bin/false -M nginx
3.、下载nginx软件,并编译安装
[root@nginx-server ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz
[root@nginx-server ~]# tar xf nginx-1.6.3.tar.gz
[root@nginx-server ~]# cd nginx-1.6.3
[root@nginx-server nginx-1.6.3]#./configure \
–prefix=/usr/local/nginx \
–sbin-path=/usr/local/nginx/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/nginx.pid \
–lock-path=/var/lock/nginx.lock \
–user=nginx \
–group=nginx \
–with-http_ssl_module \
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–http-client-body-temp-path=/var/tmp/nginx/client/ \
–http-proxy-temp-path=/var/tmp/nginx/proxy/ \
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
–http-scgi-temp-path=/var/tmp/nginx/scgi \
–with-pcre
[root@nginx-server nginx-1.6.3]# make && make install
4.为nginx提供SysV init脚本
[root@nginx-server ~]# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: – 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0
nginx=”/usr/sbin/nginx”
prog=$(basename $nginx)
NGINX_CONF_FILE=”/etc/nginx/nginx.conf”
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=\([^ ]*\).*/\1/g’ -`
options=`$nginx -V 2>&1 | grep ‘configure arguments:’`
for opt in $options; do
if [ `echo $opt | grep ‘.*-temp-path’` ]; then
value=`echo $opt | cut -d “=” -f 2`
if [ ! -d “$value” ]; then
# echo “creating” $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $”Starting $prog: ”
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $”Stopping $prog: ”
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $”Reloading $prog: ”
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case “$1″ in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
exit 2
esac
而后为此脚本赋予执行权限:
[root@nginx-server ~]# chmod +x /etc/rc.d/init.d/nginx
添加至服务管理列表,并让其开机自动启动:
[root@nginx-server ~]# chkconfig –add nginx
[root@nginx-server ~]# chkconfig nginx on
而后就可以启动服务并测试了:
[root@nginx-server ~]# service nginx start
正在启动 nginx: [确定]
[root@nginx-server ~]# netstat -tnlp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 35524/nginx
[root@nginx-server ~]# curl -I http://192.168.0.11/
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 29 Dec 2017 13:36:19 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 29 Dec 2017 13:20:55 GMT
Connection: keep-alive
ETag: “5a464137-264”
Accept-Ranges: bytes
三、安装tomcat服务器
1.安装JDK,配置Java环境
[root@tomcat-server-1 ~]# rpm -vih jdk-8u25-linux-x64.rpm
Preparing… ########################################### [100%]
1:jdk1.8.0_25 ########################################### [100%]
Unpacking JAR files…
rt.jar…
jsse.jar…
charsets.jar…
tools.jar…
localedata.jar…
jfxrt.jar…
[root@tomcat-server ~]# cat /etc/profile.d/java.sh #设置java环境变量
export JAVA_HOME=/usr/java/latest
export CLASSPATH=$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@tomcat-server-1 ~]# . /etc/profile.d/java.sh
[root@tomcat-server-1 ~]# java -version #查看java变量是否配置成功
java version “1.8.0_25”
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
2.安装tomcat服务
[root@tomcat-server-1 ~]# wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz
[root@tomcat-server-1 ~]# tar xf apache-tomcat-8.0.48.tar.gz -C /usr/local/
[root@tomcat-server-1 ~]# cd /usr/local/
[root@tomcat-server-1 local]# ln -sv apache-tomcat-8.0.48 tomcat
“tomcat” -> “apache-tomcat-8.0.48”
[root@tomcat-server-1 local]# cat /etc/profile.d/tomcat.sh #配tomcat环境变量
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
[root@tomcat-server-1 local]# . /etc/profile.d/tomcat.sh
[root@tomcat1-server-1 local]# catalina.sh start #启动服务
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat-server-1 local]# netstat -tnlp #查看端口是否启动
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 947/sshd
tcp 0 0 :::22 :::* LISTEN 947/sshd
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 8014/java
tcp 0 0 :::8009 :::* LISTEN 8014/java
tcp 0 0 :::8080 :::* LISTEN 8014/java
[root@tomcat-server-1 local]# curl -I http://192.168.0.12:8080 #测试是否可以打开
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 29 Dec 2017 13:49:39 GMT
安装完成。tomcat-server-2和tomcat-server-1相同,在此忽略
设置默认虚拟主机,并增加jvmRoute
[root@tomcat1-server-1 local]# vim /usr/local/tomcat/conf/server.xml
<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”tomcat-1″> #jvmRoute是jvm标识,就是页面最顶部的标签,在实际生产环境中,所有的后台tomcat标识都要一样,这里为了实验的说明性,两台tomcat的标识改成不一样的,分别为tomcat-1h和tomcat-2
<Host name=”localhost” appBase=”webapps”
unpackWARs=”true” autoDeploy=”true”>
<Context docBase=”/Data/webapps1″ path=”” reloadable=”true” /> #修改默认虚拟主机,并将网站文件路径指向/Data/webapps1
创建context目录和测试页面
[root@tomcat1-server-1 local]# mkdir -pv /Data/webapps1
mkdir: 已创建目录 “/Data”
mkdir: 已创建目录 “/Data/webapps1″
[root@tomcat1-server-1 local]# cat /Data/webapps1/index.jsp #创建测试页面,server-2中将tomcat-1改为tomcat-1即可
<%@ page language=”java” %>
<html>
<head><title>Tomcat-1</title></head>
<body>
<h1><font color=”red”>www.tomcat-1.com</font></h1>
<table align=”centre” border=”1″>
<tr>
<td>Session ID</td>
<% session.setAttribute(“tomcat-1.com”,”tomcat-1.com”); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
测试配置,并启动
[root@tomcat1-server-1 local]# catalina.sh stop
[root@tomcat1-server-1 local]# catalina.sh configtest
[root@tomcat1-server-1 local]# catalina.sh start
四、配置nginx负载均衡tomcat
[root@nginx-server ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
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”‘;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
upstream tomcat-web {
server 192.168.0.12:8080;
server 192.168.0.13:8080;
}
server {
listen 80;
server_name www.tomcat.com;
location / {
root html;
index index.html index.htm index.jsp;
}
location ~* \.(jsp|do)$ {
proxy_pass http://tomcat-web;
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;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.0.0/24;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重新载入
[root@nginx-server ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx-server ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx: [确定]
测试(测试机配置hosts文件解析),访问http://www.tomcat.com/index.jsp URL
多刷新几次,从结果能看出,nginx把访问请求分别分发给了后端的tomcat-1和tomcat-2,客户端的访问请求实现了负载均衡,但session id不一样(即:没有实现session保持)
五、安装redis服务
下载源码,并编译
[root@redis-server ~]# wget http://download.redis.io/releases/redis-3.2.3.tar.gz
[root@redis-server ~]# tar xf redis-3.2.3.tar.gz
[root@redis-server ~]# cd redis-3.2.3
[root@redis-server redis-3.2.3]# make
[root@redis-server redis-3.2.3]# make PREFIX=/usr/local/redis install
配置redis
[root@redis-server redis-3.2.3]# mkdir /usr/local/redis/etc/
[root@redis-server redis-3.2.3]# cp redis.conf /usr/local/redis/etc/
[root@redis-server redis-3.2.3]# cd /usr/local/redis/bin/
[root@redis-server bin]# cp redis-benchmark redis-cli redis-server /usr/bin/
调整下内存分配使用方式并使其生效
#此参数可用的值为0,1,2
#0表示当用户空间请求更多的内存时,内核尝试估算出可用的内存
#1表示内核允许超量使用内存直到内存用完为止
#2表示整个内存地址空间不能超过swap+(vm.overcommit_ratio)%的RAM值
[root@redis-server bin]# echo “vm.overcommit_memory=1”>>/etc/sysctl.conf
[root@redis-server bin]# sysctl -p
修改redis配置
[root@redis-server bin]#vim /usr/local/redis/etc/redis.conf
# 修改一下配置
#设置redis监听的地址
bind 0.0.0.0
# redis以守护进程的方式运行
# no表示不以守护进程的方式运行(会占用一个终端)
daemonize yes
# 客户端闲置多长时间后断开连接,默认为0关闭此功能
timeout 300
# 设置redis日志级别,默认级别:notice
loglevel verbose
# 设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:””
# 并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了
logfile “/usr/local/redis/log/redis-access.log”
#redis默认是空密码访问,这样很不安全。需要启用redis的密码验证功能
requirepass pwd@123
redis环境变量配置
[root@redis-server bin]# echo “export PATH=/usr/local/redis/bin:$PATH” > /etc/profile.d/redis.sh
[root@redis-server bin]# . /etc/profile.d/redis.sh
创建Redis 系统启动脚本
[root@redis-server bin]# cat /etc/init.d/redis
#!/bin/bash
#chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF=”/usr/local/redis/etc/redis.conf”
case “$1” in
start)
if [ -f $PIDFILE ]
then
echo “$PIDFILE exists, process is already running or crashed”
else
echo “Starting Redis server…”
$EXEC $CONF
fi
if [ “$?”=”0” ]
then
echo “Redis is running…”
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo “$PIDFILE does not exist, process is not running”
else
PID=$(cat $PIDFILE)
echo “Stopping …”
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo “Waiting for Redis to shutdown …”
sleep 1
done
echo “Redis stopped”
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo “Usage: /etc/init.d/redis {start|stop|restart|force-reload}” >&2
exit 1
esac
[root@redis-server bin]# chmod +x /etc/init.d/redis
[root@redis-server bin]# service redis start #启动
测试:
[root@redis-server bin]# redis-cli -h 192.168.0.14 -p 6379 -a pwd@123
192.168.0.14:6379> keys *
(empty list or set)
192.168.0.14:6379> set name pwb
OK
192.168.0.14:6379> get name
“pwb”
redis源码安装完毕
六、配置tomcatsession redis同步(tomcat-server上)
Tomcat8连接Reids需要以下3个软件包:
1 commons-pool2-2.2.jar
2 jedis-2.5.2.jar
3 tomcat-redis-session-manager-2.0.0.jar #tomcat7这需要将这个包替换为tomcat-redis-session-manage-tomcat7.jar
将所需要的jar包复制到$CATALINA_HOME/lib/下,即tomcat安装目录的lib目录下
[root@tomcat-server-1 ~]# cp commons-pool2-2.2.jar jedis-2.5.2.jar tomcat-redis-session-manager-2.0.0.jar /usr/local/tomcat/lib
在Tomcat的conf/context.xml文件中加入使用redis-session的配置
<Valve className=”com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve” />
<Manager className=”com.orangefunction.tomcat.redissessions.RedisSessionManager”
host=”192.168.0.14″
password=”pwd@123″
port=”6379″
database=”0″
maxInactiveInterval=”60″
/>
注意Valve必须配置在Manager之前
通过浏览器访问测试,结果如下:
可以看出,分别访问了不同的tomcat,但是得到的session却是相同的,说明达到了集群的目的。
注:从Tomcat6开始默认开启了Session持久化设置,测试时可以关闭本地Session持久化,在Tomcat的conf目录下的context.xml文件中,取消 <Manager pathname=”” /> 注释即可
七、配置tomcat连接数据库
安装mysql,创建认证用户(mysql-server上)
[root@mysql-server yum]# yum install mysql-server -y
[root@mysql-server yum]# service mysqld start
[root@mysql-server yum]# mysql
mysql> grant all on *.* to tomcat_user@’192.168.0.%’ identified by ‘123456’;
Query OK, 0 rows affected (0.00 sec)
下载mysql-connector-java-5.1.22-bin.jar并复制到$CATALINA_HOME/lib目录下(tomcat-serve上)
[root@tomcat-server-2 ]# cd /usr/local/tomcat/
[root@tomcat-server-2 tomcat]#wget https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.22.tar.gz
[root@tomcat-server-2 tomcat]#unzip mysql-connector-java-5.1.22-bin.jar.zip
[root@tomcat-server-2 tomcat]#cp mysql-connector-java-5.1.22-bin.jar lib/
配置JNDI数据源,保存后内容如下:
conf/context.xml
<?xml version=’1.0′ encoding=’utf-8′?>
<!–
The ASF licenses this file to You under the Apache License, Version 2.0
(the “License”); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
–>
<!– The contents of this file will be loaded for each web application –>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!–配置mysql数据库的连接池–>
<!–配置mysql数据库的连接池,
需要做的额外步骤是将mysql的Java驱动类放到tomcat的lib目录下
maxIdle 连接池中最多可空闲maxIdle个连接
minIdle 连接池中最少空闲maxIdle个连接
initialSize 初始化连接数目
maxWait 连接池中连接用完时,新的请求等待时间,毫秒
username 数据库用户名
password 数据库密码
–>
<!– Default set of monitored resources. If one of these changes, the –>
<!– web application will be reloaded. –>
<!– Uncomment this to disable session persistence across Tomcat restarts –>
<!–
<Manager pathname=”” />
–>
<!– Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) –>
<!–
<Valve className=”org.apache.catalina.valves.CometConnectionManagerValve” />
–>
<Valve className=”com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve” />
<Manager className=”com.orangefunction.tomcat.redissessions.RedisSessionManager”
host=”192.168.0.14″
password=”pwd@123″
port=”6379″
database=”0″
maxInactiveInterval=”60″
/>
</Context>
在项目的目录下新建WEB-INF目录,用于存放网站xml配置文件,用于tomcat连接mysql数据库
[root@tomcat-server-2 tomcat]mkdir /Data/webapps1/WEB-INF
[root@tomcat-server-2 tomcat]vim /Data/webapps1/WEB-INF/web.xml
WEB-INF/web.xml
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!–
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the “License”); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
–>
<web-app xmlns=”http://xmlns.jcp.org/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”
version=”3.1″>
<!– 数据源 –>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
重启服务
[root@tomcat1-server-2 WEB-INF]# catalina.sh stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat1-server-2 WEB-INF]# catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
现在创建一个简单的测试.jsp页面,测试tomcat和mysql的连通性
[root@tomcat-server-2 tomcat]# vim /Data/webapps1/test.jsp
<%@page import=”javax.naming.InitialContext”%>
<%@page import=”javax.sql.DataSource”%>
<%@page import=”java.sql.Connection”%>
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Insert title here</title>
</head>
<body>
<%
out.print(“MySQL 数据源测试开始…” + “<br/>”);
DataSource ds = null;
try {
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup(“java:comp/env/jdbc/TestDB”);
Connection conn = ds.getConnection();
conn.close();
out.print(“MySQL 数据源测试成功!”);
} catch (Exception ex) {
out.print(“出现意外,信息是:” + ex.getMessage());
ex.printStackTrace();
}
%>
%</body>
%</html>
访问测试页: