实验
通常,一个tomcat站点由于可能出现单点故障以及无法应付过多的客户复杂多样的请求等问题,不能单独应用于生产环境下,所以我们需要一套更可靠的解决方案来完善web站点架构
Nginx是一款性能优越的http服务器软件,它能够支持高达50000个并发连接数的响应,拥有强大的静态资源处理能力,运行稳定,并且内存、CPU等系统资源消耗非常低。目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理以及负载均衡器,来提升整个站点的负载并发能力
- 实验环境说明:
类型 | IP地址 | 系统 | 软件包 |
Nginx服务器 | 192.168.179.226 | centos7 | nginx-1.12.2.tar.gz |
Tomcat服务器1 | 192.168.179.148 | centos7 | apache-tomcat-8.5.50.tar.gz jdk-8u231-linux-x64.tar.gz |
Tomcat服务器2 | 192.168.179.132 | centos7 | apache-tomcat-8.5.50.tar.gz jdk-8u231-linux-x64.tar.gz |
客户机 | centos7 |
安装Tomcat1服务
tar zxvf jdk-8u91-linux-x64.tar.gz -C /opt/
cd /opt/
//更改环境变量
mv /opt/jdk1.8.0_91/ /usr/local/
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
//解压缩包
tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
//重命名
cd /usr/local/
mv apache-tomcat-8.5.16/ tomcat/
//便于系统识别命令
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
//创建两台服务的站点
mkdir -pv /web/webapp1
cd /usr/local/tomcat/web/webapp1/
vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("Welcome KGC Web1");%>
</body>
</html>
//修改主配置文件
vim /usr/local/tomcat/conf/server.xml
查找Host
插入
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
//启动服务
startup.sh
安装Tomcat服务器2
tar zxvf jdk-8u91-linux-x64.tar.gz -C /opt/
cd /opt/
//更改环境变量
mv /opt/jdk1.8.0_91/ /usr/local/
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
//解压缩包
tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
//重命名
cd /usr/local/
mv apache-tomcat-8.5.16/ tomcat/
//便于系统识别命令
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
//创建两台服务的站点
mkdir -pv /web/webapp1
cd /usr/local/tomcat/web/webapp1/
vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("Welcome KGC Web2");%>
</body>
</html>
//修改主配置文件
vim /usr/local/tomcat/conf/server.xml
查找Host
插入
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
//启动服务
startup.sh
访问测试:
安装Nginx服务作为代理服务器
//解压缩包
tar zxvf nginx-1.12.2.tar.gz -C /opt
//创建用户
useradd -M -s /sbin/nologin nginx
//安装环境依赖包
yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
//配置
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
//创建软连接,便于管理服务命令
ln -s /usr/local/nginx/sbin/* /usr/local/sbin
//便于service管理服务
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG;;
stop)
kill -s QUIT $(cat $PIDF);;
restart)
$0 stop
$0 start;;
reload)
kill -s HUP $(cat $PIDF);;
*)
exit 1
esac
exit 0
//添加执行权限
chmod +x /etc/init.d/nginx
//添加为系统服务
chkconfig --add nginx
vim /usr/local/nginx/conf/nginx.conf
#gzip on;
upstream tomcat_server {
server 192.168.179.148:8080 weight=1;
server 192.168.179.132:8080 weight=1;
}
server {
listen 80;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server;
}
}
//检查是否成功
/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
//启动服务
访问Nginx服务测试: