1. Tomcat 简介
Tomcat 是由Apache 软件基金会下的项目, Tomcat 实现了 Servlet 和 JavaServer Page(JSP) 的支持, 提供了作为Web 服务器的功能, 由Tomcat 本身也内含HTTP服务器, 也可以单独视为Web服务器.
不可以把 Tomcat 和 HTTP服务器 混淆, Apache HTTP 服务器使用C 余元实现的 HTTP Web服务器, Apache Tomcat 包含了配置管理工具, 也可以通过XML 格式的配置文件来进行配置.
Tomcat 提供了一个Jasper 编译器, 用以将JSP 编译成对应的Servlet, Tomcat 的Sevelet 引擎通常用于引擎和 Apache 或者其他Web 服务器, 一起工作, 纯用于开发过程调用以及那些对速度和事务处理只有很小要求的用户, 很少会将 Tomcat 作为单独的Web 服务器.
2. 配置 JAVA 环境
- 准备安装包
[root@node10009 src]# ls ./jdk-8u131-linux-x64.tar.gz
./jdk-8u131-linux-x64.tar.gz
- 解压并移动
[root@node10009 src]# tar zxf jdk-8u131-linux-x64.tar.gz
[root@node10009 src]# cp -r jdk1.8.0_131/ /opt/app/jdk1.8
- 修改环境变量
vim /etc/profile
# 底部增加
JAVA_HOME=/opt/app/jdk1.8
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$PATH:$JAVA_HOME/bin
- 测试
[root@node10009 app]# java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
[root@node10009 app]#
3. Tomcat 安装
- 下载
[root@node10009 src]# ls ./apache-tomcat-9.0.4.tar.gz
./apache-tomcat-9.0.4.tar.gz
[root@node10009 src]#
- 解压
[root@node10009 src]# tar zxf apache-tomcat-9.0.4.tar.gz
[root@node10009 src]# ls -ld apache-tomcat-9.0.4
drwxr-xr-x 9 root root 160 Dec 25 22:00 apache-tomcat-9.0.4
- 将tomcat 移动并拷贝
[root@node10009 src]# cp -r apache-tomcat-9.0.4 /opt/app/tomcat
[root@node10009 src]# cd /opt/app/tomcat/
[root@node10009 tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
- 启动
[root@node10009 app]# startup.sh
Using CATALINA_BASE: /opt/app/tomcat
Using CATALINA_HOME: /opt/app/tomcat
Using CATALINA_TMPDIR: /opt/app/tomcat/temp
Using JRE_HOME: /opt/app/jdk1.8
Using CLASSPATH: /opt/app/tomcat/bin/bootstrap.jar:/opt/app/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@node10009 app]# ss -tan | grep 80
LISTEN 0 100 :::8080 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 100 :::8009 :::*
TIME-WAIT 0 0 ::1:42232 ::1:8009
TIME-WAIT 0 0 ::1:52006 ::1:8080
[root@node10009 app]#
- 测试
4. 设置虚拟主机
- 修改配置文件
...
...
<!-- 虚拟主机 -->
<Service name="testVhost">
<Connector port="800" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="809" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="testVhost" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="myapp"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="testVhost_access_log" suffix=".txt"
pattern="%{X-Real-IP}i %h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
...
- 添加测试项目
[root@node10009 tomcat]#
[root@node10009 tomcat]# ls myapp/
docs examples host-manager manager ROOT
[root@node10009 tomcat]# cat myapp/ROOT/index.jsp
<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<h1>Hello JSP</h1>
<%
out.print("test jsppage");
%>
[root@node10009 tomcat]#
- 重启服务
[root@node10009 tomcat]# ./bin/catalina.sh stop
Using CATALINA_BASE: /opt/app/tomcat
Using CATALINA_HOME: /opt/app/tomcat
Using CATALINA_TMPDIR: /opt/app/tomcat/temp
Using JRE_HOME: /opt/app/jdk1.8
Using CLASSPATH: /opt/app/tomcat/bin/bootstrap.jar:/opt/app/tomcat/bin/tomcat-juli.jar
^[[A[root@node10009 tomcat]# ./bin/catalina.sh start
Using CATALINA_BASE: /opt/app/tomcat
Using CATALINA_HOME: /opt/app/tomcat
Using CATALINA_TMPDIR: /opt/app/tomcat/temp
Using JRE_HOME: /opt/app/jdk1.8
Using CLASSPATH: /opt/app/tomcat/bin/bootstrap.jar:/opt/app/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@node10009 tomcat]#
- 测试
5. 测试前端使用 Nginx 代理
- 编辑nginx 配置文件
- 添加
proxy_pass
将指定端口的请求转发到后端的tomcat
- 添加
upstream localtomcat {
server 127.0.0.1:8080;
}
...
server {
listen 80;
server_name localhost;
location ~ .*$ {
proxy_pass http://localtomcat;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_temp_file_write_size 64k;
}
}
...
- 修改tomcat 配置文件
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%{X-Real-IP}i %h %l %u %t "%r" %s %b" />
- 测试
END