一、環境准備

時間同步
關閉防火牆
聯通網絡,配置yum源
軟件包鏈接:https://pan.baidu.com/s/1qYbtpnQ
二、安裝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數據源,保存后內容如下:
<?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
<?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>
訪問測試頁:

以看出來,現在tomcat可以連接到數據庫了。

