插件窝 干货文章 CentOS6.5下Tomcat7 Nginx Redis配置的方法

CentOS6.5下Tomcat7 Nginx Redis配置的方法

Redis tomcat 配置 bash 421    来源:    2025-04-26

CentOS 6.5下配置Tomcat7、Nginx和Redis

下面我将详细介绍在CentOS 6.5系统下配置Tomcat7、Nginx和Redis的方法。

1. 准备工作

首先确保系统是最新的:

yum update -y

2. 安装和配置Tomcat7

2.1 安装Java环境

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

验证安装:

java -version

2.2 安装Tomcat7

yum install tomcat7 tomcat7-webapps tomcat7-admin-webapps -y

2.3 配置Tomcat

编辑配置文件:

vi /etc/tomcat7/server.xml

可以修改连接器端口(默认8080)和其他参数。

2.4 启动Tomcat

service tomcat7 start
chkconfig tomcat7 on

验证Tomcat是否运行:

curl http://localhost:8080

3. 安装和配置Nginx

3.1 安装Nginx

首先添加EPEL仓库:

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

安装Nginx:

yum install nginx -y

3.2 配置Nginx作为反向代理

编辑Nginx配置文件:

vi /etc/nginx/nginx.conf

http块中添加以下内容:

upstream tomcat {
    server 127.0.0.1:8080;
}

server {
    listen       80;
    server_name  yourdomain.com;

    location / {
        proxy_pass http://tomcat;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

3.3 启动Nginx

service nginx start
chkconfig nginx on

验证Nginx是否运行:

curl http://localhost

4. 安装和配置Redis

4.1 安装Redis

yum install redis -y

4.2 配置Redis

编辑配置文件:

vi /etc/redis.conf

可以修改以下参数:

daemonize yes
bind 127.0.0.1
requirepass yourpassword (可选,设置密码)

4.3 启动Redis

service redis start
chkconfig redis on

验证Redis是否运行:

redis-cli ping
# 如果设置了密码
redis-cli -a yourpassword ping

5. 集成配置

5.1 配置Tomcat使用Redis作为Session存储

下载Tomcat Redis Session Manager:

wget https://github.com/jcoleman/tomcat-redis-session-manager/releases/download/v2.0.0/tomcat-redis-session-manager-2.0.0.jar
wget http://central.maven.org/maven2/redis/clients/jedis/2.9.0/jedis-2.9.0.jar
wget http://central.maven.org/maven2/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar

将下载的JAR文件放入/usr/share/tomcat7/lib/目录。

编辑Tomcat的context.xml:

vi /etc/tomcat7/context.xml

<Context>标签内添加:

<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost"
         port="6379"
         database="0"
         password="yourpassword" <!-- 如果Redis设置了密码 -->
         maxInactiveInterval="60" />

重启Tomcat:

service tomcat7 restart

6. 防火墙配置

如果启用了防火墙,需要开放相应端口:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
service iptables save
service iptables restart

7. 验证配置

  1. 访问http://your-server-ip应该显示Tomcat页面
  2. 检查Redis中是否有Tomcat session数据: bash redis-cli KEYS *

这样就完成了CentOS 6.5下Tomcat7、Nginx和Redis的配置。根据实际需求,您可能需要进一步调整配置参数。