插件窝 干货文章 CentOS6.5下Tomcat7 Nginx Redis配置实例分析

CentOS6.5下Tomcat7 Nginx Redis配置实例分析

Redis Nginx Tomcat 配置 681    来源:    2025-04-22

CentOS 6.5下Tomcat 7、Nginx与Redis配置实例分析

环境准备

首先确保您的CentOS 6.5系统已更新到最新状态:

yum update -y
yum install -y wget gcc make openssl-devel

1. Tomcat 7安装与配置

安装Java环境

yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel

下载并安装Tomcat 7

cd /opt
wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.76/bin/apache-tomcat-7.0.76.tar.gz
tar -xzf apache-tomcat-7.0.76.tar.gz
mv apache-tomcat-7.0.76 tomcat7

配置Tomcat服务

创建系统服务文件/etc/init.d/tomcat7

#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat7
# chkconfig: 234 20 80
JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
CATALINA_HOME=/opt/tomcat7

case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
esac
exit 0

设置权限并添加服务:

chmod 755 /etc/init.d/tomcat7
chkconfig --add tomcat7
chkconfig tomcat7 on

配置Tomcat连接器

编辑/opt/tomcat7/conf/server.xml,修改连接器配置:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
           URIEncoding="UTF-8"
           compression="on"
           compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript"/>

2. Nginx安装与配置

安装Nginx

yum install -y pcre-devel zlib-devel
cd /opt
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xzf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make && make install

配置Nginx服务

创建系统服务文件/etc/init.d/nginx

#!/bin/bash
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx

nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf

case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done"
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done"
;;
test)
$nginx -t -c $conf
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done"
;;
*)
echo "Usage: $0 {start|stop|restart|reload|test}"
exit 1
;;
esac

设置权限并添加服务:

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

配置Nginx反向代理Tomcat

编辑/usr/local/nginx/conf/nginx.conf,添加以下配置:

upstream tomcat_server {
    server 127.0.0.1:8080 weight=1;
}

server {
    listen       80;
    server_name  yourdomain.com;

    location / {
        proxy_pass http://tomcat_server;
        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_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;
    }

    # 静态文件由Nginx直接处理
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|ttf)$ {
        root /opt/tomcat7/webapps/ROOT;
        expires 30d;
    }
}

3. Redis安装与配置

安装Redis

cd /opt
wget http://download.redis.io/releases/redis-3.2.11.tar.gz
tar -xzf redis-3.2.11.tar.gz
cd redis-3.2.11
make && make install

配置Redis服务

创建配置文件目录并复制配置文件:

mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf

编辑/etc/redis/6379.conf,修改以下参数:

daemonize yes
pidfile /var/run/redis_6379.pid
logfile /var/log/redis_6379.log
dir /var/lib/redis/6379

创建系统服务文件/etc/init.d/redis_6379

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# chkconfig: 2345 80 90
# description: Redis is a persistent key-value database

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

设置权限并添加服务:

chmod 755 /etc/init.d/redis_6379
chkconfig --add redis_6379
chkconfig redis_6379 on

创建Redis数据目录

mkdir -p /var/lib/redis/6379

4. 集成配置

Tomcat与Redis会话共享

  1. 下载Tomcat Redis会话管理器:
cd /opt/tomcat7/lib
wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/2.0.1/tomcat-cluster-redis-session-manager-2.0.1.jar
wget https://repo1.maven.org/maven2/redis/clients/jedis/2.9.0/jedis-2.9.0.jar
wget https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar
  1. 编辑/opt/tomcat7/conf/context.xml,添加以下内容:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost"
         port="6379"
         database="0"
         maxInactiveInterval="60" />

5. 启动服务

service redis_6379 start
service tomcat7 start
service nginx start

验证配置

  1. 检查Redis是否运行:
redis-cli ping
# 应该返回 "PONG"
  1. 检查Tomcat是否运行:
curl -I http://localhost:8080
  1. 检查Nginx反向代理是否工作:
curl -I http://localhost

常见问题解决

  1. Java版本问题

    • 如果遇到Java版本不兼容,可以安装其他版本:
    yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
    
  2. Nginx启动失败

    • 检查端口是否被占用:netstat -tulnp | grep :80
    • 检查配置文件语法:/usr/local/nginx/sbin/nginx -t
  3. Redis连接问题

    • 确保Redis服务已启动:service redis_6379 status
    • 检查防火墙设置:iptables -L -n
  4. Tomcat会话共享失败

    • 检查Redis连接信息是否正确
    • 确保所有必要的JAR文件已放置在Tomcat的lib目录
    • 检查Tomcat日志:tail -f /opt/tomcat7/logs/catalina.out

通过以上配置,您已经在CentOS 6.5上成功搭建了由Nginx作为反向代理、Tomcat作为应用服务器、Redis作为会话存储的完整环境。