tomcat--基本安装配置

Posted

彼岸船夫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat--基本安装配置相关的知识,希望对你有一定的参考价值。

0x01  JDK和Tomcat安装

到oracle官网下载jdk,当前下载的版本是Linux x64 jdk-8u101-linux-x64.tar.gz

到apache官网下载tomcat,当前最新版本 Tomcat 8.5.4 Released

jdk和apache的安装都十分简单(这里不是采用源码编译,就像在windows上使用绿色软件那么方便),将下载的文件解压即可使用,然后引入几个环境变量就算是完成了。tomcat是使用java开发的,所以依赖于jdk。使用下面两个命令可以检测安装后是否正常使用。

[[email protected] tomcat]# java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
[[email protected] tomcat]# catalina -h
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh ( commands ... )
commands:
  debug             Start Catalina in a debugger
  debug -security   Debug Catalina with a security manager
  jpda start        Start Catalina under JPDA debugger
  run               Start Catalina in the current window
  run -security     Start in the current window with security manager
  start             Start Catalina in a separate window
  start -security   Start in a separate window with security manager
  stop              Stop Catalina, waiting up to 5 seconds for the process to end
  stop n            Stop Catalina, waiting up to n seconds for the process to end
  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
  configtest        Run a basic syntax check on server.xml - check exit code for result
  version           What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined

0x02  tomcat-users.xml

启用host manager功能:
编辑tomcat-user.xml,添加如下行:
<role rolename="manager-gui"/>                       #定义角色
<user username="tomcat" password="secret" roles="admin-gui"/>       #将用户加入上面所定义的角色中

而后重启tomcat。

启用Manager App和server status功能:
<role rolename="admin-gui"/>
<user username="tomcat" password="s3cret" roles="admin-gui"/>

注意:tomcat首页的三个管理功能需要在本机登录。

0x03  配置APR

使用catalina.sh configtest 发现如下报错,查询得知这是没有配置apr

INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

apr是什么?官方文档有简介,按照官方http://tomcat.apache.org/tomcat-8.5-doc/apr.html文档,需要APR, OpenSSL,tomcat-native(在tomcat的bin目录下)

1、安装apr

        apr-devel 使用源码安装,第一次使用yum install apr-devel安装的在编译的native无法报错,后来使用源码。

 

2、安装openssl

        出现错误:configure: error: Your version of OpenSSL is not compatible with this version of tcnative

        在openssl官网下载新版本的openssl 2016-May-03 13:57:13 openssl-1.0.2h.tar.gz 编译安装后再进行tomcat-native的安装

[[email protected] local]# tar zxf openssl-1.0.2h.tar.gz
[[email protected] openssl-1.0.2h-src]# ./config –prefix=/usr/local/openssl –fPIC             #加上-fPIC参数,否则编译native的时候会报错,如附一
[[email protected]host openssl-1.0.2h-src]# make
[[email protected] openssl-1.0.2h-src]#make install

        附一:编译native报错

       /usr/local/openssl/lib/libssl.a(s3_meth.o): relocation R_X86_64_32 against `.rodata‘ can not be used when making a shared object; recompile with –fPIC
       /usr/local/openssl/lib/libssl.a: could not read symbols: Bad value
       collect2: error: ld returned 1 exit status
       make[1]: *** [libtcnative-1.la] Error 1
       make[1]: Leaving directory `/usr/local/tomcat-native-1.2.8-src/native‘
       make: *** [all-recursive] Error 1

       在重新config之后,会提示使用make depend可以忽略,如下

       *** Because of configuration changes, you MUST do the following before
       *** building:

               make depend

3、安装native

[[email protected] openssl-1.0.2h-src]#cd /usr/local/tomcat-native-1.2.8-src/
[[email protected] tomcat-native-1.2.8-src]# cd native/
[[email protected] native]#./configure --prefix=/usr/local/tomcat-native --with-apr=/usr/bin/ --with-java-home=/usr/local/jdk --with-ssl=/usr/local/openssl
[[email protected] native]#make
[[email protected] native]#make install

     最后,将安装成功后的tomcat-native的lib目录下的文件复制一份到上述报错所包含的任一目录(/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib),这里我试过把整个lib做个软链接到这些目录下面,可是居然还是无法找到,所以最后还是直接复制了。

     此时使用catalina configtest 完整的提示如下

[[email protected] local]# catalina configtest
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
……
Aug 24, 2016 9:15:00 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Aug 24, 2016 9:15:00 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
Aug 24, 2016 9:15:00 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/usr/local/tomcat
Aug 24, 2016 9:15:00 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/usr/local/tomcat
Aug 24, 2016 9:15:00 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
Aug 24, 2016 9:15:00 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded APR based Apache Tomcat Native library 1.2.8 using APR version 1.5.2.                #可以看到这里的native和apr已经被识别了
Aug 24, 2016 9:15:00 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Aug 24, 2016 9:15:00 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
Aug 24, 2016 9:15:00 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized (OpenSSL 1.0.2h  3 May 2016)
Aug 24, 2016 9:15:00 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Aug 24, 2016 9:15:00 PM org.apache.tomcat.util.net.NioselectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 24, 2016 9:15:00 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Aug 24, 2016 9:15:00 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 24, 2016 9:15:00 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1878 ms

0x04  tomcat目录结构

├── bin                      执行文件bat为在windows下运行的,sh是linux下执行的
│   ├── bootstrap.jar
│   ├── catalina.bat
│   ├── catalina.sh     主要的执行程序,具有的若干个参数功能是通过调用此目录下的其他脚本实现
│   ├── shutdown.bat
│   ├── shutdown.sh
│   ├── startup.bat
│   ├── startup.sh
│   ├── version.bat
│   └── version.sh
├── conf                    配置文件目录
│   ├── catalina.policy
│   ├── catalina.properties
│   ├── context.xml
│   ├── logging.properties
│   ├── server.xml
│   ├── tomcat-users.xml
│   └── web.xml
├── lib                     包含Tomcat使用的jar文件,unix平台此目录下的任何文件都被加到Tomcat的classpath中
│   ├── annotations-api.jar
│   ├── catalina-ant.jar
│   ├── ……
├── logs                   Tomcat摆放日志文件的地方。
│   ├── catalina.log
│   ├── catalina.out
│   ├── host-manager.log
│   ├── localhost.log
│   ├── localhost_access_log.txt
│   └── manager.log
├── temp                JSP转化成Servlet存放的位置
│   └── safeToDelete.tmp
├── webapps
│   ├── docs
│   ├── examples
│   ├── host-manager
│   ├── manager
│   └── ROOT
└── work                存放JSP编译后产生的class文件        
    └── Catalina 

0x05  Apache+Tomcat(一)

        Apache与Tomcat的整合方式有两种,主要依靠apache的两个模块(mod_jk,mod_proxy)。两种模块都可以通过ajp,http/https协议与tomcat进行通信,但是一般mod_jk都使用ajp协议,并且mod_jk需要自行下载安装,而mod_proxy模块apache2.2之后,默认就带上。

从mod_proxy相关模块名称(mod_proxy_http,mod_proxy_ajp)可以发现http和ajp等是建立在proxy之上的,是单独的模块。

本次使用光盘自带的Apache/2.4.6 yum安装,默认情况下已经载入了相关模块。

ProxyVia on
ProxyRequests off                            #关闭正向代理
ProxyPreserveHost on                         #保留Http请求的Host头部

ProxyPass / http://172.16.4.22:8080/
ProxyPassReverse / http://172.16.4.22:8080/

上述使用的是mod_proxy模块以http方式代理,同理也可以使用ajp方式

ProxyPass / ajp://172.16.4.22:8080/
ProxyPassReverse / ajp://172.16.4.22:8080/

注意:

1、关闭firewall防火墙,保证apache能够访问正常。

2、没有关闭selinux,时出现了如下错误。

[mpm_prefork:notice] [pid 7654] AH00170: caught SIGWINCH, shutting down gracefully
 [core:notice] [pid 7825] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[suexec:notice] [pid 7825] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[auth_digest:notice] [pid 7825] AH01757: generating secret for digest authentication ...
[lbmethod_heartbeat:notice] [pid 7825] AH02282: No slotmem from mod_heartmonitor
[mpm_prefork:notice] [pid 7825] AH00163: Apache/2.4.6 (CentOS) php/5.4.16 configured -- resuming normal operations
[core:notice] [pid 7825] AH00094: Command line: /usr/sbin/httpd -D FOREGROUND
[proxy:error] [pid 7829] (13)Permission denied: AH00957: HTTP: attempt to connect to 172.16.4.22:8080 (172.16.4.22) failed
[proxy:error] [pid 7829] AH00959: ap_proxy_connect_backend disabling worker for (172.16.4.22) for 60s
[proxy_http:error] [pid 7829] [client 172.16.4.81:15757] AH01114: HTTP: failed to make connection to backend: 172.16.4.22

3、出现如下错误是因为ProxyPass 和 ProxyPassReverse 后面的ip:port需要加“/”。

[proxy:error] [pid 7831] [client 172.16.4.81:15767] AH00898: DNS lookup failure for: 172.16.4.22:8080tomcat.png returned by /tomcat.png, referer: http://172.16.4.22/
[proxy:error] [pid 7830] [client 172.16.4.81:15766] AH00898: DNS lookup failure for: 172.16.4.22:8080tomcat.css returned by /tomcat.css, referer: http://172.16.4.22/

0x06  Apache+Tomcat(二)

使用mod_jk模块,先在官网下载Connectors,JK 1.2.41 Source Release tar.gz

        AJP(Apache JServ Protocol):AJP是面向数据包的基于TCP/IP的协议,它在Apache和Tomcat的实例之间提供了一个专用的通信信道。mod_proxy只有在apache 2.2.x系列的版本才直接提供,而对于apache 1.3.x和2.0.x来说mod_jk才更适用。

1、编译安装apache的mod_jk模块

[[email protected] ~]# tar -zxf tomcat-connectors-1.2.41-src.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/tomcat-connectors-1.2.41-src/native
[[email protected] native]# ./configure --with-apxs=/usr/bin/apxs           #编译第三方httpd模块需要用到apxs(在httpd-devel包中)
[[email protected] native]#make && make install

2、配置httpd

        安装完mod_jk之后,需要配置httpd代理至tomcat的功能。为了便于维护在httpd/conf.d/新增单独配置文件。

LoadModule jk_module modules/mod_jk.so
JKWorkersFile /etc/httpd/conf.d/workers.properties
JKLogFile /var/logs/httpd/mod_jk.log
JKLogLevel info
JKMount /* Tomcat            #Tomcat和stat是一个在workers.properties定义的名称,与mod_proxy不同的是这里的根需要“/*”
JKMount /status stat         #后面的自定名称,称为jvmRoute

        新增workers.properties文件,对于apache代理来说,每一个后端的Tomcat实例中的engine都可以视作一个worker,而每一个worker的地址、连接器的端口等信息都需要在apache端指定以便apache可以识别并使用这些worker。可以理解一个worker相当于一个在httpd后端的tomcat的实例或者是。

worker.list=Tomcat,stat         #名称列表
worker.TomcatA.port=8009   #每个名称的具体属性
worker.TomcatA.host=172.16.4.1
worker.TomcatA.type=ajp13  #类型有三种,表明后端服务是如何工作。
worker.TomcatA.lbfactor=1
worker.stat.type = status

        worker类型:

        ajp:当前worker为一个运行着的Tomcat实例
        lb:即load balancing,专用于负载均衡场景中的woker;此worker并不真正负责处理用户请求,而是将用户请求调度给其它类型为ajp13的worker,有专用的属性配置
        status:用户显示分布式环境中各实际worker工作状态的特殊worker,它不处理任何请求,也不关联到任何实际工作的worker实例

        各类型worker通用属性:

        host:Tomcat 的worker实例所在的主机;
        port:Tomcat 实例上AJP1.3连接器的端口;
        connection_pool_minsize:最少要保存在连接池中的连接的个数;默认为pool_size/2;
        connection_pool_timeout:连接池中连接的超时时长;
        mount:由当前worker提供的context路径,如果有多个则使用空格格开;此属性可以由JkMount指令替代;
        retries:错误发生时的重试次数;
        socket_timeout:mod_jk等待worker响应的时长,默认为0,即无限等待;
        socket_keepalive:是否启用keep alive的功能,1表示启用,0表示禁用;
        lbfactor:worker的权重,可以在负载均衡的应用场景中为worker定义此属性;

以上是关于tomcat--基本安装配置的主要内容,如果未能解决你的问题,请参考以下文章

免安装的Tomcat基本配置和安装

tomcat--基本安装配置

Tomcat基本安装和优化方法

Java基本开发环境之Tomcat

安装启动与基本配置

tomcat基本的服务配置搭建