1.安装Tomcat并配置启动
1.yum安装maven和java:
[root@web01 ~]# yum install java maven -y
2.二进制安装tomcat
[root@web01 ~]# mkdir /soft && cd /soft
[root@web01 soft]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.26/bin/apache-tomcat-9.0.26.tar.gz
[root@web01 soft]# tar xf apache-tomcat-9.0.26.tar.gz
[root@web01 soft]# ln -s /soft/apache-tomcat-9.0.26 /soft/tomcat
[root@web01 soft]# /soft/tomcat/bin/startup.sh
[root@web01 soft]# netstat -lntp|grep java
tcp6 0 0 :::8009 :::* LISTEN 8500/java
tcp6 0 0 :::8080 :::* LISTEN 8500/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 8500/java
2.Tomcat部署项目--->>网市场
1.配置server.xml文件 ,新增在 engline内
<Host name="cms.cheng.com" appBase="/code/cms"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="cms_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
2.创建站点目录,上传源码包
[root@web01 ~]# mkdir /code/cms
[root@web01 ~]# cd /code/cms/
[root@web01 cms]# rz mail_osc-wangmarket-master.zip
[root@web01 cms]# unzip mail_osc-wangmarket-master.zip
3.使用mvn编译
[root@web01 cms]# cd wangmarket/
[root@web01 wangmarket]# mvn clean package -Dmaven.skip.test=true
[root@web01 wangmarket]# cp target/wangmarket.war /code/cms/
[root@web01 cms]# mv wangmarket.war ROOT.war
4.Nginx配置反向代理
[root@web01 conf.d]# cat cms.cheng.com.conf
server {
listen 80;
server_name cms.cheng.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_host;
}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
5.重启Tomcat服务
[root@web01 cms]# /soft/tomcat/bin/shutdown.sh && /soft/tomcat/bin/startup.sh && tail -f /soft/tomcat/logs/catalina.out
6.配置域名解析
cms.cheng.com 10.0.0.7
7.安装图解:
8.部署多节点组建集群
[root@web01 cms]# scp -rp /code root@172.16.1.8:/
[root@web01 cms]# scp -rp /soft root@172.16.1.8:/
[root@web02 soft]# rm -rf tomcat/
[root@web02 soft]# ln -s /soft/apache-tomcat-9.0.26 /soft/tomcat
[root@web01 cms]# scp -rp /etc/nginx/conf.d/cms.cheng.com.conf root@172.16.1.8:/etc/nginx/conf.d/
[root@web02 soft]# nginx -t
[root@web02 soft]# systemctl restart nginx
[root@web02 soft]# yum install java -y
[root@web02 soft]# /soft/tomcat/bin/startup.sh
测试:
cms.cheng.com 10.0.0.8
9.接入负载均衡
[root@lb01 conf.d]# cat proxy_cms.cheng.com.conf
upstream cms {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name cms.cheng.com;
location / {
proxy_pass http://cms;
include proxy_params;
}
}
[root@lb01 conf.d]# nginx -t
[root@lb01 conf.d]# systemctl restart nginx
10.通过浏览器访问发现无法登陆
11.Nginx+Tomcat集群会话共享 redis
1.下载TomcatClusterRedisSessionManager (所有web集群都需要操作)
[root@web01 ~]# unzip tomcat-cluster-redis-session-manager.zip
[root@web01 ~]# cd tomcat-cluster-redis-session-manager/
1.拷贝jar包
[root@web01 tomcat-cluster-redis-session-manager]# cp lib/* /soft/tomcat/lib/
2拷贝tomcat连接redis配置文件
[root@web01 tomcat-cluster-redis-session-manager]# cp conf/redis-data-cache.properties /soft/tomcat/conf/
3修改redis-data-cache.properties
[root@web01 tomcat-cluster-redis-session-manager]# vim /soft/tomcat/conf/redis-data-
redis.hosts=172.16.1.52:6379
4.添加如下两行至tomcat/conf/context.xml
[root@web01 ~]# vim /soft/tomcat/conf/context.xml
<Context>
.....
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />
....
</Context>
5.修改tomcat/conf/web.xml 配置文件session的超时时间 ,单位是分钟
<session-config>
<session-timeout>60</session-timeout> #根据情况调整
</session-config>
12.安装redis,当然也可以自行搭建redis集群,anyway
[root@redis ~]# yum install redis -y
[root@redis ~]# cat /etc/redis.conf
...
bind 172.16.1.51 172.16.1.52
requirepass 123456 #如果不需要密码,则不要配置
...
[root@redis ~]# systemctl start redis
[root@redis ~]# systemctl enable redis
13.重启多台机器的Tomcat
[root@web01 ~]# /soft/tomcat/bin/shutdown.sh
[root@web01 ~]# /soft/tomcat/bin/startup.sh
14.在172.16.1.51的数据库上,创建一个wangmarket的库,配置授权访问用户
[root@db01 ~]# mysql -uroot -p000000
MariaDB [(none)]> create database wangmarket charset utf8;
MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by '000000';
MariaDB [(none)]> flush privileges;