centos Nginx+Tomcat+Session共享+SSL证书+mysql读写分离

前端之家收集整理的这篇文章主要介绍了centos Nginx+Tomcat+Session共享+SSL证书+mysql读写分离前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

现在阿里云服务器上申请SSL证书。然后绑定服务器ip。

服务器配置:

192.168.0.1(42.42.42.42(公网ip))

192.168.0.2

192.168.0.3

192.168.0.4

192.168.0.5

服务器提供一个对外的弹性ip(公网ip),各服务器之间通过子网ip连接。

192.168.0.1配置redis+Nginx

192.168.0.2,192.168.0.3 配置tomcat

192.168.0.4,192.168.0.5 配置MysqL

每台服务器上来直接

yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison pcre pcre-devel zlib zlib-devel openssl openssl-devel libaio

关闭防火墙:

我的是华为服务器在安全组配置就行 防火墙如果再开就会访问不了。

chkconfig iptables off 永久关闭防火墙

service iptables stop 这种关闭防火墙下次开机还是会打开

修改配置文件
# vi /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled
需要重启机器生效!!一般购买的服务这个都关闭了。

Nginx安装:

cd /usr/local/src

官网下载。上传服务器或直接wget

tar -zxvf Nginx-1.13.6

cd Nginx-1.13.6

mkdir/usr/local/Nginx

./configure --prefix=/usr/local/Nginx

make && make install

cd /usr/local/Nginx

mkdir -p /usr/local/Nginx/conf/cert

将证书Nginx SSL证书放到 /usr/local/Nginx/conf/cert目录下

vim /conf/Nginx.conf

user  root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    #配置被代理的服务器
    upstream my_servers{
             #ip_hash
         server 192.168.0.6:8080;
         server 192.168.0.4:8080;            
    }
    server {
        listen       81;
        server_name  game.xxxxx.com;

        location / {
            root   html;
            index  index.html index.htm;
	        proxy_pass http://my_servers;
            proxy_http_version 1.1;
	        proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    # HTTPS server
    server { 
    	listen 443;
       	server_name game.xxxxx.com;
       	ssl on;
       	ssl_certificate   cert/214292271630849.pem;
       	ssl_certificate_key  cert/214292271630849.key;
       	ssl_session_timeout 5m;
       	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
       	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      	ssl_prefer_server_ciphers on;
      	location / {
    		proxy_pass http://my_servers;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       	}
    }

}

/usr/local/Nginx/sbin/Nginx -t

测试如果successful

/usr/local/Nginx/sbin/Nginx

启动Nginx

/usr/local/Nginx/sbin/Nginx -s reload 重新加载Nginx 相当于重启

Redis安装:

mkdir /usr/local/redis

cd /usr/local/src

上传或直接wget redis。

tar -zxvf redis-4.0.1

cd redis-4.0.1

安装

make PREFIX=/usr/local/redis install

复制配置文件

cp /usr/local/src/redis-4.0.1/redis.conf /usr/local/redis/

复制启动脚本,并重命名

cp /usr/local/src/redis-4.0.1/utils/redis_init_script /etc/rc.d/init.d/redis

#!/bin/sh
#chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/${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

$EXEC $CONF & 用于后台启动,不要忘记&

#chkconfig: 2345 80 90 这个注释也要加上,如果不添加上面的内容,在注册服务时会提示: service redis does not support chkconfig。

EXEC=/usr/local/bin/redis-server 改为 EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli 改为 CLIEXEC=/usr/local/redis/bin/redis-cli

CONF="/etc/redis/${REDISPORT}.conf" 改为 CONF="/usr/local/redis/conf/${REDISPORT}.conf"

mkdir /usr/local/redis/conf
cp /usr/local/src/redis3.0/redis.conf /usr/local/redis/conf/6379.conf
以上配置操作完成后, 便可将 Redis 注册成为服务:
# chkconfig --add redis

修改 redis 配置文件设置:
# vi /usr/local/redis/conf/6379.conf
修改如下配置
daemonize no 改为> daemonize yes
pidfile /var/run/redis.pid 改为> pidfile /var/run/redis_6379.pid

protected-mode yes 如果不设置密码这个要改成no

port 6379 是端口号 如果修改 文件名和上面的启动脚本也要修改一下

requirepass 123456 这里是密码设置


启动 Redis 服务
# service redis start
将 Redis 添加到环境变量中:
# vi /etc/profile
在最后添加以下内容
## Redis env
export PATH=$PATH:/usr/local/redis/bin
使配置生效:
# source /etc/profile

启动服务

service redis start

Tomcat 安装:192.168.0.2-192.168.0.3

安装前先安装jdk

mkdir /usr/local/java

cd /usr/local/src

tar -zxvf jdk-8u152-linux-x64.tar.gz

mv /usr/local/src/jdk1.8.0_152/usr/local/java/

添加环境变量

vim /etc/profile

加入如下内容

JAVA_HOME=/usr/local/java/jdk1.8.0_152/
JRE_HOME=/usr/local/java/jdk1.8.0_152/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

配置生效

source /etc/profile

测试

java –version

mkdir /usr/local/tomcat

cd /usr/local/src

上传或直接wget安装包

tar -zxvf apache-tomcat-8.5.23

mvapache-tomcat-8.5.23/* /usr/local/tomcat

POM.XML

<!-- Jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<!-- Spring Data Redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.3.RELEASE</version>
</dependency>
<!-- Spring Session -->
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>
<!-- Apache Commons Pool -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>



WEB.XML

<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
</filter-mapping>

redis.xml

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="localhost" />
    <property name="password" value="your-password" />
    <property name="port" value="6379" />
    <property name="database" value="10" />
</bean>

时间问题先整理这些。。。 过几天在好好整理整理

原文链接:https://www.f2er.com/centos/375393.html

猜你在找的CentOS相关文章