首先确保您的CentOS 6.5系统已更新到最新状态:
yum update -y
yum install -y wget gcc make openssl-devel
yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel
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
创建系统服务文件/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
编辑/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"/>
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
创建系统服务文件/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
编辑/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;
}
}
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
创建配置文件目录并复制配置文件:
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
mkdir -p /var/lib/redis/6379
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
/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" />
service redis_6379 start
service tomcat7 start
service nginx start
redis-cli ping
# 应该返回 "PONG"
curl -I http://localhost:8080
curl -I http://localhost
Java版本问题:
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
Nginx启动失败:
netstat -tulnp | grep :80
/usr/local/nginx/sbin/nginx -t
Redis连接问题:
service redis_6379 status
iptables -L -n
Tomcat会话共享失败:
tail -f /opt/tomcat7/logs/catalina.out
通过以上配置,您已经在CentOS 6.5上成功搭建了由Nginx作为反向代理、Tomcat作为应用服务器、Redis作为会话存储的完整环境。