0. 环境参数:
服务器:CentOS 7.5
Nginx:1.12.2
Tomcat:9.0.21
Redis:4.0.14
MySQL:5.7
1. 服务器创建目录结构
项目名称(可以替换自己的项目名称):fhsdv2
资源存放:
mkdir -p /root/fhsdv2/
项目日志:
mkdir -p /root/logs/api/
mkdir -p /root/logs/server/
Redis日志:
mkdir -p /usr/local/redis/var/
Redis配置文件:
mkdir -p /root/fhsdv2/redis-cluster/
cd /root/fhsdv2/redis-cluster/
mkdir 70017002 7003 7004 7005 7006
2. Tomcat
cd /root/fhsdv2/
下载源码解压安装:
curl -O http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.42/bin/apache-tomcat-8.5.42.zip
unzip -o -d ./ apache-tomcat-8.5.42.zip
cd /root/fhsdv2/apache-tomcat-8.5.42/bin
chmod +x startup.sh
chmod +x shutdown.sh
chmod +x catalina.sh
chmod +x setclasspath.sh
chmod +x bootstrap.jar
chmod +x tomcat-jni.jar
chmod +x tomcat-jni.jar
./startup.sh
查看是否启动(默认兼听8080):
netstat -tln | grep 8080
强制关闭Tomcat进程:
lsof -i :8080|grep -v "PID"|awk '{print "kill -9",$2}'|sh
3. Ruby环境和Redis支持包
注:如果选用Redis5,集群命令已经集成到了RedisCli中,可以跳过该步骤。参考官方文档https://redis.io/topics/cluster-tutorial
安装RVM:
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
find / -name rvm -print
source /usr/local/rvm/scripts/rvm
安装Ruby2.4.6:
rvm list known
rvm install 2.4
rvm use 2.4.6
ruby -version
安装redis支持:
gem install redis
修改连接密码:
vi /usr/local/rvm/gems/ruby-2.4.6/gems/redis-4.1.2/lib/redis/client.rb
修改 :password => '*******',
接下来创建集群,创建集群之前要先启动各个节点,因此下面的命令要在4.Redis节点完成后才能正常运行:
查找redis-trib.rb文件
find / -name client.rb -print
创建集群:
/path to your redis src folder/redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
4.Redis节点
下载安装redis4:
curl -O http://download.redis.io/releases/redis-4.0.14.tar.gz
tar xzf redis-4.0.14.tar.gz
cd redis-4.0.14
make
修改配置文件:
vi /root/fhsdv2/redis-4.0.14/redis.conf
关闭保护模式:protected-mode no
vi /root/fhsdv2/redis-cluster/7001/redis.conf
IP绑定(修改为自己的Redis集群IP):
bind 127.0.0.1
端口绑定:
port 7001
指定PID文件:
pidfile /var/run/redis_7001.pid
追加模式:
appendonly yes
开启集群:
cluster-enabled yes
指定配置文件:
cluster-config-file nodes-7001.conf
注释掉:
#always-show-logo yes
修改密码,这里与3. Ruby环境和Redis支持包中修改的密码一致:
requirepass *****
其他节点雷同,只要修改端口号:7002/7003/7004/7005/7006
分别启动各个节点:
/root/fhsdv2/redis-4.0.14/src/redis-server /root/fhsdv2/redis-cluster/7001/redis.conf
/root/fhsdv2/redis-4.0.14/src/redis-server /root/fhsdv2/redis-cluster/7002/redis.conf
...
查看节点日志:
vi /usr/local/redis/var/redis_7001.log
查看节点状态:
netstat -tnlp | grep redis
然后可以创建集群:
/path to your redis src folder/redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
关闭节点:
ps -ef | grep redis
kill -9 2964 2969 2974 2979 2984 2989
清空节点:
redis-cli -p 7001
auth *****
flushall
cluster reset
exit
4. Nginx反向代理
安装:
yum install nginx
修改配置文件:
vi /etc/nginx/nginx.conf
upstream tomcat {
server localhost:8080;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.shdaklbdt.cn www.fengfenghuohuo.top;
charset utf8;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://tomcat;
#Proxy Settings
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;
proxy_max_temp_file_size 0;
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;
}
}
5. 阿里云RDS
购买云数据库RDS服务,创建实例,创建数据库:
账号:
IP访问白名单:
查看链接,写入项目代码中:
6. 最终服务器状态
Nginx监听80端口,外部请求代理到Tomcat监听的8080端口,数据库连接阿里云数据库,缓存写入Redis集群。