一、下载相关软件包
①下载jdk
1
2
|
# wget
http:
//download
.oracle.com
/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64
.rpm?AuthParam=1443611456_fa9cf3c3ff6845d1bcd351d6983780a8
|
②下载tomcat
1
|
# wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.0.26/bin/apache-tomcat-8.0.26.tar.gz
|
③下载mod_jk
1
|
# wget http://apache.fayea.com/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
|
④下载httpd
1
|
# yum install -y httpd httpd-devel apr apr-util-devel autoconf libtool
|
二、安装jdk
1
|
# rpm -ivh jdk-8u60-linux-x64.rpm
|
1
2
3
4
5
6
7
8
|
# vi /etc/profile.d/java.sh
export
JAVA_HOME=
/usr/java/jdk1
.8.0_60
export
PATH=$PATH:$JAVA_HOME
/bin
# . /etc/profile.d/java.sh
# java -version
java version
"1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
|
注:如有多个jdk,可使用update-alternatives --config java命令进行选择。
二、安装tomcat
1
2
3
4
5
|
# tar -zxvf apache-tomcat-8.0.26.tar.gz
# mkdir /usr/local/tomcat
# mv apache-tomcat-8.0.26/* /usr/local/tomcat/
# catalina.sh start ##开启tomcat
# catalina.sh stop ##关闭tomcat
|
1
2
3
4
5
6
7
|
# ss -tlnp ##查看tomcat是否启动
# getenforce 0
# systemctl stop firewalld
# vi /etc/profile.d/tomcat.sh
export
CATALINA_HOME=
/usr/local/tomcat
export
PATH=$PATH:$CATALINA_HOME
/bin
# . /etc/profile.d/tomcat.sh
|
三、测试tomcat
①浏览器输入http://localhost:8080
1
2
|
# cd /usr/local/tomcat/webapps/ROOT/
# echo "hello,tomcat" > index.html
|
②浏览器输入http://localhost:8080/index.html
③浏览器输入http://localhost:8080/tomcat.jsp
1
2
3
|
# cd /usr/local/tomcat/webapps/ROOT/
# vi tomcat.jsp ##新建测试页
1
Now time is: <%=
new
java.util.Date() %>
|
④浏览器输入http://localhost:8080
1
2
3
4
5
6
7
8
|
# cd /usr/local/tomcat/conf/
# vi server.xml ##更改默认host
103
<Engine name=
"Catalina"
defaultHost=
"www.zhi.com"
jvmRoute=
"TomcatZHI"
>
<Host name=
"www.zhi.com"
appBase=
"webapps"
unpackWARs=
"true"
autoDeploy=
"true"
>
<Contest path=
""
docBase=
"webapps"
reLoadable=
"true"
/>
</Host>
# catalina.sh configtest ##测试语法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# cd /usr/local/tomcat/webapps/ROOT
# mv index.jsp index.jsp.bak
# vi index.jsp ##新建测试页
2
<%@ page language=
"java"
%>
<html>
<head><title>TomcatZHI</title></head>
<body>
<h1><font color=
"red"
>TomcatZHI </font></h1>
<table align=
"centre"
border=
"1"
>
<tr>
<td>Session ID</td>
<% session.setAttribute(
"abc"
,
"abc"
); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
# catalina.sh start ##开启tomcat
|
四、编译安装mod_jk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# tar -zxvf tomcat-connectors-1.2.41-src.tar.gz
# cd tomcat-connectors-1.2.41-src
# cd tomcat-connectors-1.2.41-src/native
# ./buildconf.sh
buildconf: checking installation...
buildconf: autoconf version 2.69 (ok)
buildconf: libtool version 2.4.2 (ok)
buildconf: libtoolize --automake --copy
buildconf: aclocal
buildconf: autoheader
buildconf: automake -a --foreign --copy
buildconf: autoconf
# ./configure --with-apxs=/usr/bin/apxs
# make
# make install
|
出现的错误1:
1
2
3
4
5
|
# ./buildconf.sh
buildconf: checking installation...
buildconf: autoconf not found.
You need autoconf version 2.59 or newer installed
to build mod_jk from SVN.
|
解决:
1
|
# yum install autoconf
|
出现的错误2:
1
2
3
4
5
6
|
# ./buildconf.sh
buildconf: checking installation...
buildconf: autoconf version 2.69 (ok)
buildconf: libtool not found.
You need libtool version 1.4 or newer installed
to build mod_jk from SVN.
|
解决:
1
|
# yum install libtool
|
五、配置Apache与Tomcat整合
①配置mod_jk配置文件
1
2
3
4
5
6
7
8
9
|
# vi /etc/httpd/conf/mod_jk.conf
JkWorkersFile
/etc/httpd/conf/workers
.properties
##加载tomcat works配置文件
JkLogFile logs
/mod_jk
.log
##日志文件
JkLogLevel warn
##日志级别
JkLogStampFormat
"[%a %b %d %H:%M:%S %Y]"
##日志数据格式
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
##其他配置项
JkRequestLogFormat
"%w %V %T"
##请求日志的格式
JkMount
/servlet/
* worker1
JkMount /*.jsp worker1
##JkMount用于控制URL与Tomcat workers的对应关系,JkMount表示用tomcat来解析,JkUnMount则相反
|
②配置tomcat配置文件
1
2
3
4
5
6
7
8
9
|
# vi /etc/httpd/conf/workers.properties
#
# workers.properties ##可配置多实例
#
worker.list=worker1
worker.worker1.
type
=ajp13
##worker的类型为ajp13
worker.worker1.host=127.0.0.1
##worker的主机(IP)
worker.worker1.port=8009
##worker的端口
worker.worker1.lbfactor=5
##worker负载均衡
|
③配置httpd配置文件
1
2
3
4
5
6
7
8
9
10
|
# vi /etc/httpd/conf/httpd.conf
#
# Dynamic Shared Object (DSO) Support
#
LoadModule jk_module modules
/mod_jk
.so
Include
/etc/httpd/conf/mod_jk
.conf
ServerName www.zhi.com:80
# service httpd configtest ##测试语法
|
六、测试整合效果
①直接输入http://localhost查看
httpd默认进入的是Apache的index.html页面
②输入http://localhost/inedex.jsp(不加8080端口)查看
③输入http://localhost/tomcat.jsp(不加8080端口)查看
tomcat已与Apache整合好了!
七、通过mod_proxy模块与tomcat连接
①查看httpd是否已装载相关模块
1
2
3
4
5
6
7
8
9
10
11
|
# httpd -D DUMP_MODULES | grep proxy
proxy_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
|
②配置httpd
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# vi /etc/httpd/conf/httpd.conf ##只需配置如下即可
ProxyVia Off
ProxyRequests Off
ProxyPreserveHost Off
<Proxy />
Require all granted
<
/Proxy
>
ProxyPass / http:
//192
.168.10.132:8080/
ProxyPassReverse / http:
//192
.168.10.132:8080/
<Location / >
Require all granted
<
/Location
>
# httpd -t ##检查语法
|
注:
a.ProxyVia {On|Off|Full|Block}:用于控制在http首部是否使用Via:,主要用于在多级代理中控制代理请求的流向。默认为Off,即不启用此功能;On表示每个请求和响应报文均添加Via:;Full表示每个Via:行都会添加当前apache服务器的版本号信息;Block表示每个代理请求报文中的Via:都会被移除。
b.ProxyRequests {On|Off}:是否开启apache正向代理的功能;启用此项时为了代理http协议必须启用mod_proxy_http模块。同时,如果为apache设置了ProxyPass,则必须将ProxyRequests设置为Off。
c.ProxyPreserveHost {On|Off}:如果启用此功能,代理会将用户请求报文中的Host:行发送给后端的服务器,而不再使用ProxyPass指定的服务器地址。如果想在反向代理中支持虚拟主机,则需要开启此项,否则就无需打开此功能。
d.ProxyPass :将后端服务器某URL与当前服务器的某虚拟路径关联起来作为提供服务的路径,path为当前服务器上的某虚拟路径,url为后端服务器上某URL路径。使用此指令时必须将ProxyRequests的值设置为Off。需要注意的是,如果path以“/”结尾,则对应的url也必须以“/”结尾,反之亦然。
e.ProxyPassReverse:用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器。
③测试
输入代理端http://localhost时出现:
已经代理过去了!
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
①原因1:SELinux问题
解决:
1
|
# setenforce 0 ##或设置selinux为enabled(需重启生效)
|
②原因2:在httpd中默认索引页面设置中添加了index.jsp也会造成503错误
1
2
3
|
<IfModule dir_module>
DirectoryIndex index.jsp index.htm index.html
<
/IfModule
>
|
解决:去掉index.jsp即可
附:tomcat主要文件简单解释
bin-----启动和关闭tomcat的脚本文件
conf-----存放tomcat的各种全局配置文件,以server.xml和web.xml最重要
lib-----存放tomcat所需的各种JAR文件
logs-----存放tomcat执行时的日志
temp-----存放临时文件
webapps-----tomcat的web目录
work-----存放JSP编译后产生的class文件