欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

centos65 负载均衡nginx session共享redis

程序员文章站 2022-05-12 14:46:03
...
一、安装JDK7

1.查询已经安装的jdk
rpm -qa|grep jdk
2.卸载已经安装的jdk
rpm -e --nodeps *****(查询出来的东西...)
3.将安装文件jdk-7u79-linux-x64.rpm上传到/usr/java
cd /usr/java
4.安装
rpm -ivh jdk-7u79-linux-x64.rpm
5.编辑profile 添加以下三行
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.7.0_79
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
6.使配置立即生效
source /etc/profile

二、安装nginx

1.创建一个新的文件
touch /etc/yum.repos.d/nginx.repo
2.编辑新增文件
vim /etc/yum.repos.d/nginx.repo
3.将以下内容拷贝进去
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

4.进行yum安装
yum -y install nginx
5.启动nginx
service nginx start
6.测试nginx
curl 127.0.0.1

三、安装redis

1.安装tcl支持
yum install tcl
2.安装gcc支持
yum install -y gcc g++ gcc-c++ make
3.下载redis
wget http://download.redis.io/releases/redis-2.8.16.tar.gz
4.解压
tar xzf redis-2.8.16.tar.gz
5.进入目录
cd redis-2.8.16
6.安装
make
make test
make install

7.编辑redis.conf文件并拷贝到etc目录下
vim redis.conf
修改daemonize no改成yes
cp redis.conf /etc

8.自启动
进入目录
cd /etc/init.d
创建文件
touch redis
编辑文件
vim redis
将以下内容拷贝到文件里面保存退出
##########################
#chkconfig: 2345 10 90
#description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin

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

PIDFILE=/var/run/redis.pid
C/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
##############################

9.修改权限
chmod +x /etc/init.d/redis
10.启动redis服务
service redis start

11.进入redis-cli进行测试

set redis hello,redis
+OK
get redis
$11
hello,redis

四、nginx配置

1.忘记nginx装在哪里
whereis nginx
2.切入到目录
cd /etc/nginx
3.编辑配置文件
upstream www.test.com{
ip_hash;
server localhost:8080;
server localhost:9080;
}
server {
listen 80;
server_name www.test.com;
charset utf-8;
location / {
proxy_pass http://www.test.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
proxy_connect_timeout 16;
proxy_read_timeout 16;
proxy_send_timeout 16;
}
}

4.重新加载一下
nginx -s reload
5.修改hosts文件
vim /etc/hosts
新增
127.0.0.1 www.test.com

五、同一台服务器上跑两个tomcat配置

1. 修改profile
vim /etc/profile
新增以下内容:
CATALINA_BASE=/mnt/apache-tomcat-01 【tomcat1路径】
CATALINA_HOME=/mnt/apache-tomcat-01
export CATALINA_BASE CATALINA_HOME
CATALINA_2_BASE=/mnt/apache-tomcat-02 【tomcat2路径】
CATALINA_2_HOME=/mnt/apache-tomcat-02
export CATALINA_2_BASE CATALINA_2_HOME
TOMCAT_HOME=/mnt/apache-tomcat-01
export TOMCAT_HOME
TOMCAT_2_HOME=/mnt/apache-tomcat-02
export TOMCAT_2_HOME
2.使配置生效
source /etc/profile
3.把tomcat复制两份,其中一份无需修改,另外一份需要做以下修改
修改server.xml
端口:8005->9005
端口:8080->9080

8009->9009