一、准备三台centos,ip分别规划为:192.168.107.136 192.168.107.137 192.168.107.138
二、安装tomcat
1.拷贝安装包
2.解压安装包
tar zvxf apache-tomcat-8.5.16.tar.gz
3.移动解压后文件夹到/usr/local下
mv apache-tomcat-8.5.16/ /usr/local/tomcat8
4.创建javaweb站点
mkdir -pv /web/webapp1
vim /web/webapp1/index.jsp
<%@ page language='java' import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page </title>
</head>
<body>
<% out.println("Welcom to test site,http://www.test1.com");%>
</body>
</html>
5.修改tomcat主配置文件
在host段增加context
vim /usr/local/tomcat8/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
6.启动tomcat
/usr/local/tomcat8/bin/startup.sh
三、安装nginx
1.搭建本地yum
cd /etc/yum.repo.d
mkdir /backup
mv * /backup
vim local.repo
[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0
mount /dev/cdrom /mnt
yum clean all
yum repolist
2.安装相关软件包
yum install -y pcre-devel zlib-devel openssl-devel
3.解压并安装nginx
useradd -M -s /sbin/nologin nginx
tar zvxf nginx-1.12.0.tar.gz -C /usr/src
cd /usr/src/nginx-1.12.0
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
make && make install
4.配置nginx.conf
vim /usr/local/nginx/conf/nginx.conf
upstream tomcat {
server 192.168.107.137:8080 weight=1;
server 192.168.107.138:8080 weight=1;
}
server {
listen 80;
server_name localhost;
#静态页面请求
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#动态页面请求
location ~ .*.jsp$ {
proxy_pass http://tomcat;
}
#静态请求直接读取
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
expires 30d;
}
5.启动服务
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
nginx
四、访问测试
http://192.168.107.136 访问静态页面
http://192.168.107.136/index.jsp 访问动态页面