tomcat搭建及nginx实现负载均衡

部署tomcat
实验案列:
在这里插入图片描述
案列实施:
1、关闭防火墙:[root@localhost yum.repos.d]# systemctl stop firewalld.service
1) 在安装tomcat之前先安装jdk。(Java开发程序包,默认情况下自带安装)
[root@localhost yum.repos.d]# java -version
2、安装配置tomcat
1)解压apache-tomcat
[root@localhost yum.repos.d]# cd /mnt/
[root@localhost mnt]# tar zxf apache-tomcat-8.5.16.tar.gz -C /usr/src/
[root@localhost mnt]# cd /usr/src/
[root@localhost src]# ls
apache-tomcat-8.5.16 cmake-2.8.12 debug kernels mysql-5.6.36
[root@localhost src]# mv apache-tomcat-8.5.16/ /usr/local/tomcat8
[root@localhost src]# /usr/local/tomcat8/bin/startup.sh #启动tomcat
查询tomcat端口(端口:8080)
[root@localhost src]# netstat -anpt | grep 8080
tcp6 0 0 :::8080 ::😗 LISTEN 30645/java
2) 打开浏览器测试http://192.168.1.1:8080/
在这里插入图片描述
3、建立Java的web站点:
1)在根目录下建立一个web目录,并在里面建立一个webapp1目录,用于存放网站文件。
[root@localhost src]# mkdir -pv /web/webapp1
mkdir: 已创建目录 “/web”
mkdir: 已创建目录 “/web/webapp1”
2)在目录下创建一个index.jsp的测试页面。
[root@localhost src]# vim /web/webapp1/index.jsp
[root@localhost src]# more /web/webapp1/index.jsp
192.168.1.1
3)修改tomcat的server.xml文件。在host段增加context段!
[root@localhost src]# vim /usr/local/tomcat8/conf/server.xml



3) 关闭tomcat,在重新启动。
[root@localhost src]# /usr/local/tomcat8/bin/shutdown.sh
[root@localhost src]# /usr/local/tomcat8/bin/startup.sh
测试一下:
在这里插入图片描述
案列:nginx反向代理+tomcat负载均衡群集
在这里插入图片描述
在这里插入图片描述
实施准备:
1、 关闭防火墙
2、 确认是否按照jdk、Java版本是否一致。
3、 配置tomcat,版本一致。
4、 创建/web/webapp1目录,修改tomcat配置文件server.xml,将网站目录更改为/web/webapp1路径下。
5、 /web/webapp1/下建立index.jsp。为了区别测试页面写上各自ip。
6、 启动tomcat,访问tomcat2是否成功。

Nginx服务器配置
关闭防火墙,selinux
安装相关软件包
[root@localhost ~]# yum -y install pcre-devel.x86_64 zlib-devel.x86_64 openssl-devel
解压并安装nginx
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -g www www -s /bin/false
[root@localhost ~]# tar zxf /mnt/nginx-1.12.0.tar.gz -C /usr/src/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module
[root@localhost nginx-1.12.0]# make && make install
在安装好nginx时,配置【http{……}加入代码】
nginx.conf【完整配置如下】(黑体字是增加内容)
#gzip on;
upstream tomcat_server {
server 172.16.16.172:8080 weight=1;
server 172.16.16.173:8080 weight=1;
}

weight代表维权,权重越高,被分配的几率越大。

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass    http://tomcat_server;
    }

通过命令检查nginx配置语法是否出错。
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# service nginx start 加入系统服务启动nginx的方法
Starting Nginx done
[root@localhostnginx-1.12.0]#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#没加入系统的方法
[root@localhost ~]# ps aux | grep nginx 查看进程服务
root 3848 0.0 0.0 20484 516 ? Ss 03:13 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 3849 0.0 0.1 23012 1428 ? S 03:13 0:00 nginx: worker process
root 4245 0.0 0.0 112664 972 pts/0 R+ 03:30 0:00 grep --color=auto nginx
[root@localhost ~]# netstat -anpt | grep nginx 查看端口号及PID进程号
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4317/nginx: master

测试复制均衡效果:
1、 访问http://172.16.16.170/
2、 不断刷新浏览器测试,页面会反复切换。
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值