一、 jdk 安装配置
# yum install java-1.8.0-openjdk-devel (依赖的java-1.8.0-openjdk,java-1.8.0-openjdk,headless也会被安装 ) # alternatives -h # vim /etc/profile.d/java.sh 加入 export JAVA_HOME=/usr
# . /etc/profile.d/java.sh # printenv 查看环境变量
能过rpm包安装
# wget ftp://172.16.0.1/pub/Sources/7.x86_64/jdk/jdk-8u25-linux-x64.rpm # rpm -ivh jdk-8u25-linux-x64.rpm # rpm -ql jdk1.8.0_25-1.8.0_25-fcs # cd /usr/java/default # vim /etc/profile.d/javad.sh JAVA_HOME=/usr/java/latest PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME PATH # . /etc/profile.d/javad.sh # java -version
tomcat:运行于JDK之上,表现为一个独立而完整的java进程,可与用户交互的web服务器
使用Java语言编写
Tomcat的核心组件:server.xml,其配置的格式为
<Server>
<Serivce>
<connector/>
<connecotr/>
…
<Engine>
<Host>
<Context/>
<Context/>
</Host>
<Host>
…
</Host>
…
</Engine>
</service>
</Server>
说明:顶级组件:Server
服务类组件:Service
连接类组件:http,https,ajp
容器类:Engine,Host,Context
被嵌套类:value,logger,realm,loader,manager
集群类组件:listener,cluster,…
二、tomcat的配置
1、安装tomcat:
a、二进制格式安装
# tar xvf apache-tomcat-VERSION.tar.gz -C /usr/local/ # cd /usr/local/ # ln -sv apache-tomcat-VERSION tomcat #vim /etc/profile.d/tomcat.sh export CATALINA_BASE=/usr/local/tomcat export PATH=$CATALINA_BASE/bin:$PATH # . /etc/profile.d/tomcat.sh
b、yum源安装
# yum -y install tomcat-webapps tomcat-docs-webapp tomcat-admin-webapps tomcat tomcat-lib
安装完后可以使用命令
# systemctl start tomcat && ss -tnl
启动tomcat服务并且查看8080端口是否启用
在浏览器中进行测试 http://172.16.254.248:8080/
2、tomcat程序目录结构
bin:脚本及启动时用到的类
conf:配置文件目录
lib:库文件,java类库
logs:日志文件目录
temp:临时文件目录
webapps:webapp的默认目录
work:工作目录
3、tomcat的配置文件说明
server.xml:主配置文件
web.xml:每个webapp只有部署后才能被访问,它的部署方式通过由web.xml进行定义,其存位置为WEB-INF/目录中,此文件为所有的webapps提供默认配置
context.xml:每个web都可专用的配置文件,它通常由专用的配置文件context.xml来定义,其存放位置为WEB-INF/目录中,此文件为所有的webapps提供默认配置
tomcat-users.xml:用户的账号和文件
catalina.policy:当使用security选项启动tomcat时,用于为tomcat设置安全策略;
catalina.propreteis:java物定义文件,用于设定类加载器路径,以及一些与JVM高估相关参数;
logging.properties:日志系统相关的配置
4、tomcat-users.xml配置
vim /etc/tomcat/tomcat-users.xm
l
在<tomcat-users>段中加入以五内容
<role rolename="manager-gui"/> <user username="centos" password="centos" roles="manager-gui"/> <role rolename="admin-gui"/> <user username="centos" password="centos" roles="admin-gui"/>
重启服务后并重新访问http://172.16.254.248:8080/
点Server Status、Manager App、 Host Manager时输入用户名centos密码centos进行访问
5、提供一测试类应用,并冷部署;
#mkdir -pv /usr/share/tomcat/webapps/test/{classes,lib,WEB-INF} # vim /usr/share/tomcat/webapps/test/index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title> Test Page </title> </head> <body> <% out.println("hellow world"); %> </body> </html>
重新访问http://172.16.254.248:8080/test/时会显示hellow world
6、nginx + tomcat cluster
用nginx 反代两台tomcat主机 ,cluster(172.16.254.248[nginx]) ,node1(172.16.251.232[Tomcat A]),node2(172.16.251.74[Tomcat B])
cluster# yum -y install nginx && systemctl start nginx # vim /etc/nginx/nginx.conf
在http段中加入
upstream tomcat { server 172.16.251.232:8080; server 172.16.251.74:8080; }
# vim /etc/nginx/conf.d/default.conf
在location中加入
proxy_pass http://tomcat/;
nod1 # mkdir -pv /usr/share/tomcat/webapps/test/{WEB-INF,classes,lib} # vim /usr/share/tomcat/pwebapps/test/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.rj.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> nod2 # mkdir -pv /usr/share/tomcat/webapps/test/{WEB-INF,classes,lib} # vim /usr/share/tomcat/pwebapps/test/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatB.rj.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
访问http://172.16.254.248/test/时效果如果下
7、 httpd(proxy_http_module) + tomcat cluster
cluster#systemctl disable nginx && systemctl stop nginx # yum -y install httpd # vim /etc/httpd/conf.d/tomcat.conf <proxy balancer://tomcat> BalancerMember http://172.16.251.232:8080 BalancerMember http://172.16.251.74:8080 ProxySet lbmethod=byrequests </proxy> <VirtualHost *:80> ServerName www.rj.com ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / balancer://tomcat/ ProxyPassReverse / balancer://tomcat/ <Location /> Require all granted </Location> </VirtualHost>
访问http://172.16.254.248/test/时效果如下
启用balancer管理接口
cluster# vim /etc/httpd/conf.d/balancer-manager.conf <Location /balancer-manager> Sethandler balancer-manager ProxyPass ! Require all granted </Location>
访问其管理接口http://172.16.254.248/balancer-manager效果如下
8、 httpd(proxy_ajp_module)+tomcat cluster # cp /etc/httpd/conf.d/tomcat.conf /etc/httpd/conf.d/tomcat.conf.bak # vim /etc/httpd/conf.d/tomcat.conf <proxy balancer://tomcat> BalancerMember ajp://172.16.251.232:8009 BalancerMember ajp://172.16.251.74:8009 ProxySet lbmethod=byrequests </proxy> <VirtualHost *:80> ServerName www.rj.com ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / balancer://tomcat/ ProxyPassReverse / balancer://tomcat/ <Location /> Require all granted </Location> </VirtualHost>
访问http://172.16.254.248/test/时效果如下
三、常用组件配置
Server:代表tomcat instance, 即表现出一个Java进程:监听在8005端口,只接收“SHUTDOWN”。各server监听的端口不能相同,因此,在同一物理主机启动多个实例时,需要个改其监听端口为不同的端口;
service:用于实现将一个或多个connector组件关联至一个engine组件;
Connector组件
负责接收请求,常见的有三类http/https/ajp:
进入tomcat的请求可分为两类;
(1)standalone:请求来自于客户端的反代理服务器:
nginx –> http connector –> tomcat
httpd(proxy_http_module) –> http connector –> tomcat
httpd(proxy_ajp_module) –> ajp connecetor –> tomcat
属性:
port=”8080″
protocol=”HTTP/1.1″
connectionTimeout=”20000″
address:监听的IP地址;默认为本机所有可用地址;
maxThreads:最大并发连接数,默认为150;
enableLookups:是否启用DNS查询功能;
acceptCount:等待队列的最大长度;
secure:
sslProtocol:
Engine组件:Servlet实例,即servlet引擎,其内部可以一个或多个host组件来定义站点; 通常需要通过defaultHost来定义默认的虚拟主机;
属性:
name=
defaultHost=”localhost”
jvmRoute=
Host组件:位于engine内部用于接收请求并进行相应处理的主机或虚拟主机,示例:
<Host name=”localhost” appBase=”webapps”
unpackWARs=”true” autoDeploy=”true”>
</Host>
常用属性说明:
(1) appBase:此Host的webapps的默认存放目录,指存放非归档的web应用程序的目录或归档的WAR文件目录路径;可以使用基于$CATALINA_BASE变量所定义的路径的相对路径;
(2) autoDeploy:在Tomcat处于运行状态时,将某webapp放置于appBase所定义的目录中时,是否自动将其部署至tomcat;
示例:
<Host name=”tc1.magedu.com” appBase=”/appdata/webapps” unpackWARs=”true” autoDeploy=”true”>
</Host>
# mkdir -pv /appdata/webapps
# mkdir -pv /appdata/webapps/ROOT/{lib,classes,WEB-INF}
提供一个测试页即可;
四、tomcat cluster 升级为session cluster,使用deltaManager
配置node1,和node2
node1# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA"> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.32.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.254.130" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster> # scp /etc/tomcat/server.xml 172.16.251.74:/etc/tomcat/server.xml # cp /etc/tomcat/web.xml /var/lib/tomcat/webapps/test/WEB-INF/ # vim /var/lib/tomcat/webapps/test/WEB-INF/web.xml
在 <!-- listings is enabled? [true] -->的后面加入 <distributable/> # scp /var/lib/tomcat/webapps/test/WEB-INF/web.xml 172.16.251.74:/var/lib/tomcat/webapps/test/WEB-INF/ node2# vim /etc/tomcat/server.xml 更改 <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB"> address="172.16.251.74" node1,2 #systemctl restart tomcat
进入页测试http://172.16.254.248/test/
四、cluster将会话保存至memcached中
node1,2需要的操作
将deltaManager的配置还原
# cd /usr/share/tomcat/lib/ # wget ftp://172.16.0.1/pub/Sources/7.x86_64/msm/* && rm -rf memcached-session-manager-tc8-1.8.3.jar # yum -y install memcached libmemcached # systemctl start memcached # sustemctl enable memcached
node1(tomcat1,memcached1)
# memcached 1配置
vim /etc/tomcat/server.xml
修改
<Engine name="Catalina" defaultHost="localhost" jvmRoute="rjTomcat1"> 在Host段中添加 <Context path="/test" docBase="test" reloadable="true"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.16.251.232:11211,n2:172.16.251.74:11211" failoverNodes="n1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" /> </Context> node2(tomcat,memcached2)
# memcached 2配置
<Engine name="Catalina" defaultHost="localhost" jvmRoute="rjTomcat2">
在Host段中添加
<Context path="/test" docBase="test" reloadable="true"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.16.251.232:11211,n2:172.16.251.74:11211" failoverNodes="n1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" />
</Context>
关于tomcat memcached session绑定配置详解地址
https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#configure-memcached-session-manager-as–context-manager
此时cluster将会话保存于memcached 中
访问http://172.16.254.248/test/效果如下
五、Tomcat的常用优化配置
(1)内存空间:
/etc/sysyconfig/tomcat
JAVA_OPTS=”-server -Xms -Xmx -XX:NewSize= -XX:MaxNewSize= -XX:PermSize= -XX:MaxPermSize=”
-server:服务器模型
-Xms:堆内存初始化大小;
-Xmx:堆内存空间上限;
-XX:NewSize=:新生代空间初始化大小 ;
-XX:MaxNewSize=:新生代空间最大值 ;
-XX:PermSize=:持久代空间初始化大小;
-XX:MaxPermSize=:持久代空间最大值;
(2)线程池设置:
<Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”200000″ redirectPort=”8443″ />
常用属性:
maxThreads:最大线程数;
minSpareThreads:最小空闲线程数;
maxSpareThreads:最大空闲线程数;
acceptCount:等待队列的最大长度;
URIEnconding:URI地址编码格式,建议使用UTF-8;
enableLookups:是否启用dns解析,建议禁用;
compression:是否启用传输压缩机制,建议”on”;
compressionMinSize:启用压缩传输的数据流最小值,单位是字节;
compressableMimeType:定义启用压缩功能的MIME类型;
text/html,text/xml,text/css,text/javascript
(3)禁用8005端口
<Server port=”-1″ shutdown=”SHUTDOWN”>
Server=”SOME STRING”
(4)隐藏版本信息
<Connector port=”8080″ protocol=”HTTP/1.1″ connection Timeout=”20000″ redirectPort=”8443″ />
Server=”SOME STRING”
原创文章,作者:kang,如若转载,请注明出处:http://www.178linux.com/79187