超详细的Apache+Tomcat实现动静分离步骤

目录

1.Apache安装和启动

2.jdk和Tomcat的安装:

3.下载解压编译mod_jk.so:

4.查看是否mod_jk.so模块安装成功:

5.创建测试页面:

6.配置jk模块

7.在/usr/local/tomcat/conf/server.xml中开启8009端口:

8.启动apache和tomcat测试:

9.访问:不通过8080端口是否能完成mod_jk转发


1.Apache安装和启动

yum -y install httpd
systemctl start httpd

2.jdk和Tomcat的安装:

jdk1.8和Tomcat的两种安装方式_碧蓝幻想的博客-CSDN博客

3.下载解压编译mod_jk.so:

JK 是通过 AJP 协议与 Tomcat 服务器进行通讯的,Tomcat 默认的 AJP Connector 的端口是 8009

就是将apache收到的http请求给了Tomcat服务,相当于页面转发forward

jk模块的三个配置文件 说明
httpd.conf Apache 服务器的配置文件,用来加载 JK 模块以及指定 JK 配置文件信息
workers.properties 到 Tomcat 服务器的连接定义文件
uriworkermap.properties URI 映射文件,用来指定哪些 URL 由 Tomcat 处理
wget -c https://dlcdn.apache.org/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.48-src.tar.gz --no-check-certificate
tar xf tomcat-connectors-1.2.48-src.tar.gz -C /usr/local/src/
cd /usr/local/src/tomcat-connectors-1.2.48-src/native/
yum install -y httpd-devel     # httpd-devel提供apxs命令
yum provides apxs
yum install gcc gcc-c++ make -y     # 编译需要编译环境
./configure --with-apxs=/usr/bin/apxs
make && make install

4.查看是否mod_jk.so模块安装成功:

[root@localhost native]# find / -name mod_jk.so
/usr/lib64/httpd/modules/mod_jk.so                 # make install后出现
/usr/local/src/tomcat-connectors-1.2.48-src/native/apache-2.0/.libs/mod_jk.so        # make后出现
/usr/local/src/tomcat-connectors-1.2.48-src/native/apache-2.0/mod_jk.so              # make后出现

5.创建测试页面:

# 静态页面
echo "

static_page_from_apache

" > /var/www/html/index.html # 访问 http://192.168.178.52/
# 动态页面
echo "

dynamic_page_from_tomcat

" > /usr/local/tomcat/webapps/ROOT/test.jsp # 访问 http://192.168.178.52:8080/test.jsp

6.配置jk模块

实现Apache整合tomcat:以下配置文件请删除字段后面的注释

cat << eof > /etc/httpd/conf.d/mod_jk.conf
LoadModule jk_module modules/mod_jk.so                       # 加载模块
JkWorkersFile conf/workers.properties                        # 指定mod_jk的配置文件之一workers.properties
JkLogFile /var/log/httpd/mod_jk.log                          # 指定mod_jk的日志
JkLogLevel info                                              # 日志级别
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"                    # 日志的时间格式
JkOptions +ForwardKeySize +ForwardURICompatUnparsed -ForwardDirectories
JkRequestLogFormat "%w %V %T"

# 将什么样的请求转发给worker1处理
JkMount /*.jsp worker1
JkMount /servlet/* worker1
JkMount /*.do worker1
JkMount /*.action worker1
JkMount /jkstatus status                          # 状态页,http://192.168.178.52/jkstatus访问
JKMount /jk-manager
eof
# 到 Tomcat 服务器的连接定义文件
cat << eof > /etc/httpd/conf/workers.properties              
workers.tomcat_home=/usr/local/tomcat/                       # tomcat的主目录
# jdk的主目录,rpm -ql java-1.8.0-openjdk查看
workers.java_home=/usr/java/jdk1.8.0_131

# 转发节点
worker.list=worker1
# 配置worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=50
# worker.worker1.cachesize=10
# worker.worker1.cache_timeout=600
# worker.worker1.socket_keepalive=1
# worker.worker1.socket_timeout=300
# jkstatus状态页面
worker.list = status
worker.status.type=status
# 管理
worker.list = status
worker.jk-manager.type=status
eof

7.在/usr/local/tomcat/conf/server.xml中开启8009端口:

注意此内容在

vim /usr/local/tomcat/conf/server.xml
# 修改内容
    

8.启动apache和tomcat测试:

httpd启动错误可能是因为配置文件字段后面的空格和注释

systemctl restart httpd tomcat

9.访问:不通过8080端口是否能完成mod_jk转发

http://192.168.178.52/test.jsp

你可能感兴趣的:(apache,tomcat,java)