目录
引言
1.实验环境搭建
2.部署Nginx服务器及配置静态页面Web服务
3.部署Tomcat服务及配置动态页面Web服务
4.实验验收
动态页面
静态页面
引言
tomcat服务既可以处理动态页面,也可以处理静态页面;但其处理静态页面的速度远远不如nginx和apache服务,但ngingx和apache服务无法直接处理动态页面,下文就讲述了nginx和tomcat组合使用,实现动静分离和负载均衡;目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理及负载均衡器,提升整个站点的负载并发能力。
-
standalone模式,Tomcat单独运行,直接接受用户的请求,不推荐。
反向代理,单机运行,提供了一个Nginx作为反向代理,可以做到静态由nginx提供响应,动态jsp代理给Tomcat
-
LNMT:Linux + Nginx + MySQL + Tomcat
LAMT:Linux + Apache(Httpd)+ MySQL + Tomcat
前置一台Nginx,给多台Tomcat实例做反向代理和负载均衡调度,Tomcat上部署的纯动态页面更适合
LNMT:Linux + Nginx + MySQL + Tomcat
-
多级代理
LNNMT:Linux + Nginx + Nginx + MySQL + Tomcat
动态服务器的问题,往往就是并发能力太弱,往往需要多台动态服务器一起提供服务。如何把并发的压力分摊,这就需要调度,采用一定的调度策略,将请求分发给不同的服务器,这就是Load Balance负载均衡。当单机Tomcat,演化出多机多级部署的时候,一个问题便凸显出来,这就是Session。而这个问题的由来,都是由于HTTP协议在设计之初没有想到未来的发展。
在部署Tomcat之前,要部署好环境(JDK),因为JDK是Tomcat运行的必要环境
1.实验环境搭建
根据实验环境,客户端可以用真机模拟,此实验需要五台虚拟机,Centos7-1Nginx代理服务器,Centos7-2和Centos7-3作为负载均衡第一分支的后端服务器,Centos7-4和Centos7-6作为负载均衡第二分支的后端服务器;Centos7-2和Centos7-4作为处理静态资源的服务器,需要搭建Nginx服务;Centos7-3和Centos7-6作为处理动态资源的服务器,需要搭建Tomcat服务
五台虚拟机统一关闭防火墙
2.部署Nginx服务器及配置静态页面Web服务
Centos7-1、Centos7-2、Centos7-4都要部署Nginx服务
[root@7-1 opt]#yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
#安装依赖关系包
[root@7-1 opt]#useradd -M -s /sbin/nologin nginx
#新建Nginx用户 方便管理
[root@7-1 opt]#yum install nginx -y
[root@7-1 opt]#systemctl start nginx
[root@7-1 opt]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since 六 2024-03-02 13:40:43 CST; 3s ago
Process: 2436 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2433 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2429 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 2438 (nginx)
CGroup: /system.slice/nginx.service
├─2438 nginx: master process /usr/sbin/nginx
├─2439 nginx: worker process
└─2440 nginx: worker process
3月 02 13:40:42 7-1 systemd[1]: Starting The nginx HTTP and reverse prox.....
3月 02 13:40:43 7-1 nginx[2433]: nginx: the configuration file /etc/ngin...ok
3月 02 13:40:43 7-1 nginx[2433]: nginx: configuration file /etc/nginx/ng...ul
3月 02 13:40:43 7-1 systemd[1]: Started The nginx HTTP and reverse proxy...r.
Hint: Some lines were ellipsized, use -l to show in full.
[root@7-2 ~]#yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
#安装依赖关系包
[root@7-2 ~]#useradd -M -s /sbin/nologin nginx
#新建Nginx用户 方便管理
[root@7-2 ~]#systemctl start nginx
[root@7-2 ~]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; static; vendor preset: disabled)
Active: active (running) since Sat 2024-03-02 00:32:49 EST; 5s ago
Process: 1986 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1983 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 1988 (nginx)
CGroup: /system.slice/nginx.service
├─1988 nginx: master process /usr/local/nginx/sbin/nginx
└─1989 nginx: worker process
Mar 02 00:32:49 node2.localdomain systemd[1]: Starting The nginx HTTP and ....
Mar 02 00:32:49 node2.localdomain nginx[1983]: nginx: the configuration fi...k
Mar 02 00:32:49 node2.localdomain nginx[1983]: nginx: configuration file /...l
Mar 02 00:32:49 node2.localdomain systemd[1]: Failed to read PID from file...t
Mar 02 00:32:49 node2.localdomain systemd[1]: Started The nginx HTTP and r....
Hint: Some lines were ellipsized, use -l to show in full.
[root@7-2 ~]#cd /usr/local/nginx/
[root@7-2 nginx]#ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@7-2 nginx]#cd html/
[root@7-2 html]#ls
50x.html index.html
[root@7-2 html]#mkdir /usr/local/nginx/html/static
[root@7-2 html]#ls
50x.html index.html static
[root@7-2 html]#cd static/
[root@7-2 static]#rz -E
rz waiting to receive.
[root@7-2 static]#ls
f32b966ddaf1002dfa4b2f6c556e57f0.jpg
[root@7-2 static]#mv f32b966ddaf1002dfa4b2f6c556e57f0.jpg nginx.jpg
[root@7-2 static]#ls
nginx.jpg
[root@7-2 static]#vim /usr/local/nginx/conf/nginx.conf
静态要加jpg,路径要修改为/usr/local/nginx/html/static
[root@7-4 ~]#yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
#安装依赖关系包
[root@7-4 ~]#useradd -M -s /sbin/nologin nginx
#新建Nginx用户 方便管理
[root@7-4 ~]#systemctl start nginx
[root@7-4 ~]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; static; vendor preset: disabled)
Active: active (running) since 六 2024-03-02 13:33:00 CST; 12s ago
Process: 1962 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1960 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 1964 (nginx)
CGroup: /system.slice/nginx.service
├─1964 nginx: master process /usr/local/nginx/sbin/nginx
└─1965 nginx: worker process
3月 02 13:32:59 G.D systemd[1]: Starting The nginx HTTP and reverse prox.....
3月 02 13:33:00 G.D nginx[1960]: nginx: the configuration file /usr/loca...ok
3月 02 13:33:00 G.D nginx[1960]: nginx: configuration file /usr/local/ng...ul
3月 02 13:33:00 G.D systemd[1]: Started The nginx HTTP and reverse proxy...r.
Hint: Some lines were ellipsized, use -l to show in full.
[root@7-4 ~]#cd /usr/local/nginx/html/
[root@7-4 html]#ls
50x.html index.html
[root@7-4 html]#mkdir static
[root@7-4 html]#cd static/
[root@7-4 static]#ls
[root@7-4 static]#rz -E
rz waiting to receive.
[root@7-4 static]#ls
f32b966ddaf1002dfa4b2f6c556e57f0.jpg
[root@7-4 static]#mv f32b966ddaf1002dfa4b2f6c556e57f0.jpg nginx.jpg
[root@7-4 static]#ls
nginx.jpg
3.部署Tomcat服务及配置动态页面Web服务
Centos7-3和Centos7-6需要部署Tomcat服务
[root@7-3 ~]#cd /opt
[root@7-3 opt]#rz -E
rz waiting to receive.
[root@7-3 opt]#ls
jdk-8u291-linux-x64.tar.gz
[root@7-3 opt]#tar xf jdk-8u291-linux-x64.tar.gz
[root@7-3 opt]#ls
jdk1.8.0_291 jdk-8u291-linux-x64.tar.gz
[root@7-3 opt]#java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@7-3 opt]#mv jdk1.8.0_291/ /usr/local/
[root@7-3 opt]#cd /usr/local/
[root@7-3 local]#ls
bin games jdk1.8.0_291 lib64 nginx share
etc include lib libexec sbin src
[root@7-3 local]#vim /etc/profile.d/jdk.sh
[root@7-3 local]#cat /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
[root@7-3 local]#source /etc/profile.d/jdk.sh
[root@7-3 local]#echo $PATH
/usr/local/jdk1.8.0_91/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@7-3 local]#ln -s jdk1.8.0_291/ jdk
[root@7-3 ~]#java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@7-3 opt]#cd /opt
[root@7-3 opt]#ls
jdk-8u291-linux-x64.tar.gz
[root@7-3 opt]#rz -E
rz waiting to receive.
[root@7-3 opt]#ls
apache-tomcat-9.0.16.tar.gz jdk-8u291-linux-x64.tar.gz
[root@7-3 opt]#tar xf apache-tomcat-9.0.16.tar.gz
[root@7-3 opt]#ls
apache-tomcat-9.0.16 apache-tomcat-9.0.16.tar.gz jdk-8u291-linux-x64.tar.gz
[root@7-3 opt]#cp -r apache-tomcat-9.0.16 /usr/local/
[root@7-3 opt]#cd /usr/local/
[root@7-3 local]#ls
apache-tomcat-9.0.16 etc include lib libexec sbin src
bin games jdk1.8.0_291 lib64 nginx share
[root@7-3 local]#ln -s apache-tomcat-9.0.16/ tomcat
[root@7-3 local]#ls
apache-tomcat-9.0.16 etc include lib libexec sbin src
bin games jdk1.8.0_291 lib64 nginx share tomcat
[root@7-3 ~]#ss -natp |grep 8080
LISTEN 0 100 :::8080 :::* users:(("java",pid=3237,fd=57))
[root@7-3 ~]#cd /usr/local/tomcat/webapps/
[root@7-3 webapps]#ls
docs examples host-manager manager ROOT
[root@7-3 webapps]#mkdir test
[root@7-3 webapps]#ls
docs examples host-manager manager ROOT test
[root@7-3 webapps]#cd test/
[root@7-3 test]#ls
[root@7-3 test]#vim index.jsp
[root@7-3 test]#cat index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<tile>JSP test1 page </title>
</head>
<boby>
<% out.println("动态页面1,http://www.test1.com");%>
</boby>
</html>
[root@7-3 test]#vim /usr/local/tomcat/conf/server.xml
[root@7-3 test]#/usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@7-4 static]#vim /usr/local/nginx/conf/nginx.conf
[root@7-6 ~]#cd /opt
[root@7-6 opt]#rz -E
rz waiting to receive.
[root@7-6 opt]#ls
jdk-8u291-linux-x64.tar.gz
[root@7-6 opt]#tar xf jdk-8u291-linux-x64.tar.gz
[root@7-6 opt]#ls
jdk1.8.0_291 jdk-8u291-linux-x64.tar.gz
[root@7-6 opt]#java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@7-6 opt]#mv jdk1.8.0_291/ /usr/local/
[root@7-6 opt]#cd /usr/local/
[root@7-6 local]#ls
bin games jdk1.8.0_291 lib64 nginx share
etc include lib libexec sbin src
[root@7-6 local]#ln -s jdk1.8.0_291/ jdk
[root@7-6 local]#vim /etc/profile.d/jdk.sh
[root@7-6 local]#cat /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
[root@7-6 local]#source /etc/profile.d/jdk.sh
[root@7-6 ~]#java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@7-6 local]#cd /opt/
[root@7-6 opt]#ls
jdk-8u291-linux-x64.tar.gz
[root@7-6 opt]#rz -E
rz waiting to receive.
[root@7-6 opt]#ls
apache-tomcat-9.0.16.tar.gz jdk-8u291-linux-x64.tar.gz
[root@7-6 opt]#tar xf apache-tomcat-9.0.16.tar.gz
[root@7-6 opt]#cp -r apache-tomcat-9.0.16 /usr/local/
[root@7-6 opt]#cd /usr/local/
[root@7-6 local]#ls
apache-tomcat-9.0.16 etc include lib libexec sbin src
bin games jdk1.8.0_291 lib64 nginx share
[root@7-6 local]#ln -s apache-tomcat-9.0.16/ tomcat
[root@7-6 local]#ls
apache-tomcat-9.0.16 etc include lib libexec sbin src
bin games jdk1.8.0_291 lib64 nginx share tomcat
[root@7-6 ~]#/usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@7-6 ~]#ss -natp|grep 8080
LISTEN 0 100 :::8080 :::* users:(("java",pid=2811,fd=57))
[root@7-6 ~]#cd /usr/local/tomcat/webapps/
[root@7-6 webapps]#ls
docs examples host-manager manager ROOT
[root@7-6 webapps]#mkdir test
[root@7-6 webapps]#ls
docs examples host-manager manager ROOT test
[root@7-6 webapps]#cd test/
[root@7-6 test]#ls
[root@7-6 test]#vim index.jsp
[root@7-6 test]#cat index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<tile>JSP test2 page </title>
</head>
<boby>
<% out.println("动态页面2,http://www.test2.com");%>
</boby>
</html>
[root@7-6 test]#vim /usr/local/tomcat/conf/server.xml
[root@7-6 test]#/usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
4.实验验收
动态页面
[root@7-1 opt]#tail -f /var/log/nginx/access.log
静态页面
[root@7-1 opt]#tail -f /var/log/nginx/access.log