Tomcat
Tomcat的目录结构:
- bin:脚本及启动时用到的类
- 1ib:类库
- conf:配置文件
- webapps:应用程序默认部署目录
- work:工作目录
- temp:临时文件目录
配置文件
- server.xml:主配置文件
- context.xml:每个webapp都可以有专用的配置文件,这些配置文件通常位于webapp应用程序目录下的WEB-INF目录中,用于定义会话管理器、JDBC等;conf/context.xml是为各webapp提供默认配置;
- web.xml:每个webapp“部署”之后才能被访问:此文件则用于为所有的webapp提供默认部署相关的配置:
- tomcat-users.xml:用户认证的账号和密码配置文件:
- catalina.policy:当使用-security选项启动tomcat实例时会读取此配置文件来实现其安全运行策略;
- catalina.properties:Java属性的定义文件,用于设定类加戴器路径等,以及一些]Vm性能相关的调优参数;
- logging.properties:日志相关的配置信息:
Java WebAPP组织结构:
有特定的组织形式、层次型的目录结构;主要包含了servlet代码文件、JSP页面文件、类文件、部署描述符文件等;
/usr/1ocal/tomcat/webapps/app1/
- /:webapp的根目录:
- WEB-INF/:当前webapp的私有资源目录,通常存放当前webapp自用的web.xml;
- META-INF/:当前webapp的私有资源目录,通常存放当前webapp自用的context.xml;
- classes/:此webapp的私有类
- 1ib/:此webapp的私有类,被打包为jar格式类:
- index.jsp:webapp的主页
webapp归档格式:
- .war:webapp
- .jar:EJB的类
- .rar:资源适配器
- .ear:企业级应用程序:
安装JDK
mkdir -p /usr/local/java
tar xf jdk-8u191-linux-x64.tar.gz -C /usr/local/java
ln -sv /usr/local/java/jdk1.8.0_191 /usr/local/java/latest
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java/latest
export PATH=$JAVA_HOME/bin:$PATH
. /etc/profile.d/java.sh
java
安装Tomcat
cd /usr/local/
tar xf apache-tomcat-8.5.37.tar.gz -C /usr/local/
ln -sv apache-tomcat-8.5.37/ tomcat
cat /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
. /etc/profile.d/tomcat.sh
catalina.sh conftest
catalina.sh start
ss -tnlp
ps aux|grep java
客户端测试
firefox http://192.168.100.101:8080
部署(deployment)webapp相关的操作:
-
deploy:部署,将webapp的源文件旋转于目标目录、配置tomcat服务器能够基于context.xml文件中定义的路径来访问此webapp;将其特有类通过class loader装载至tomcat;
有两种方式:
- 自动部署:auto deploy
- 手动部署:
1、冷部署:把webapp复制到指定位置,而后才启动tomcat;
2、热部署:在不停止tomcat的前提下进行的部分;部署工具:manager、ant脚本、tcd(tomcat client deployer)等;
-
undeploy:反部署,停止webapp,并从tomcat实例拆除其部分文件和部署名:
-
stop:停止,不再向用户提供服务;
-
start:启动处于“停止”状态的webapp:
-
redeploy:重新部署;
示例一
手动添加一个测试应用程序
创建webapp特有的目录结构
mkdir myapp/{lib,classed,WEB-INF,META-INF} -pv
tree myapp/
提供webapp各文件
vim myapp/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<% out.println("This is JSP test Page."); %>
</body>
</html>
tree /usr/local/tomcat/work/
less work/Catalina/localhost/myapp/org/apache/jsp/index_jsp.java
客户端测试
firefox http://192.168.100.101:8080/myapp/
tomcat自带的应用程序:
manager app:webapp管理工具
host manager:Virtual Hosts管理工具
vim /usr/local/tomcat/conf/tomcat-users.xml
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>
</tomcat-users>
catalina.sh stop
catalina.sh start
客户端访问
firefox http://127.0.0.1:8080/
firefox http://127.0.0.1:8080/host-manager/
firefox http://127.0.0.1:8080/manager/
定义一个虚拟主机
cp server.xml{,.backup}
vim server.xml
<Host name="web1.test.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="false">
<Context path="" docBase="/data/webapps/" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="web1_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
mkdir -pv /data/{webapps,logs}
cp -r /usr/local/tomcat/webapps/myapp/* /data/webapps/
catalina.sh configtest
catalina.sh stop
catalina.sh start
ss -tnl
客户端测试
echo web1.test.com 192.168.100.101 >> /etc/hosts
firefox http://web1.test.com:8080/
部署一个shopxx虚拟主机
vim server.xml
<Host name="web1.test.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="false">
<Context path="" docBase="ROOT" reloadable="true" />
<Context path="/shop" docBase="shopxx" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="web1_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
mv /root/shopxx-3.0Beta/ /data/webapps
ln -sv shopxx-3.0Beta/ shopxx
catalina.sh conftest
catalina.sh stop
catalina.sh start
ss -tnl
yum install mariadb-server mariadb -y
systemctl start mariadb
ss -tnl|grep 3306
mysql
grant ALL on shopxx.* TO shopuser@'127.0.0.1' IDENTIFIED BY 'shoppass';
FLUSH PRIVILEGES;
客户端测试
firefox http://web1.test.com:8080/shop
做访问控制
vim server.xml
<Host name="web1.test.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="ROOT" reloadable="true">
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
deny="192\.168\.100\.101" />
</Context>
<Context path="/shop" docBase="shopxx" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="web1_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
catalina.sh conftest
catalina.sh stop
catalina.sh start
ss -tnl
客户端测试
curl web1.test.com:8080 -I|grep 403
LNMT
动静分离
client >> http >>nginx >> reverse_proxy >> http >> tomcat(http connector)
yum install -y nginx
vim nginx.conf
location ~* \.(jsp|do)$ {
proxy_pass http://web1.test.com:8080;
}
nginx -t
nginx -s reload
firefox http://192.168.100.101/index.html
firefox http://192.168.100.101/index.jsp
LAMT
client >> http>> httpd>> reverse_proxy>> {http|ajp} >>tomcat {http connector|ajp connector}
反代模块
主 proxy_module
子 proxy_module_http proxy_module_ajp
yum install httpd -y
查看httpd有哪些模块
httpd -M
less /etc/httpd/conf.modules.d/00-proxy.conf
#proxy_module_http
vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
ServerName web1.test.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://192.168.100.101:8080/
ProxyPassReverse / http://192.168.100.101:8080/
<Location />
Require all granted
</Location>
</VirtualHost>
httpd -t
systemctl restart httpd
#proxy_module_ajp
vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
ServerName web1.test.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
# ProxyPass /status !
ProxyPass / ajp://192.168.100.101:8009/
ProxyPassReverse / ajp://192.168.100.101:8009/
<Location />
Require all granted
</Location>
</VirtualHost>
httpd -t
systemctl restart httpd
firefox http://web1.test.com
firefox http://192.168.100.101
LNAMT
nginx >> apache> tomcat
nginx做负载均衡
apache管理tomcat
apache只做连接请求