apache和tomcat总体有两种连接方式
第一种:通过http_connector或ajp_connector协议,把请求代理到tomcat
第二种:通过连接mod_jk模块
这次重点记录下模块连接,通过配置可以实现tomcat集群,session会话绑定,最后实现架构Apache-tomcat-mysql
首先说一下版本问题,jdk、tomcat、Apache、tomcat-connector、mysql、mysql-connector版本没有特别要求,不要版本过低即可,直接在官网下载即可或者到我的百度云下载(预留),为保证效果先把防火墙和selinux关闭
You can use the connection module mod_jk with any version of Apache starting with 1.3 and any version of Tomcat starting with (at least) 3.2.
1、安装Apache
# 安装编译环境
[root@centos7 ~]# yum groupinstall "development tools"
[root@centos7 ~]# tar zxvf httpd-2.2.34.tar.gz
[root@centos7 ~]# cd httpd-2.2.34
[root@centos7 ~]# ./configure --prefix=/usr/local/apache24
[root@centos7 ~]# make
[root@centos7 ~]# make install
[root@centos7 ~]# /usr/local/apache24/bin/apachectl -k start
测试是否正常工作
2、安装JDK
解压即可使用
[root@centos7 ~]# tar xf jdk-8u151-linux-x64.tar.gz -C /usr/local/
[root@centos7 ~]# mv /usr/local/jdk-8u151-linux-x64 /usr/local/jdk
[root@centos7 ~]# vi /etc/profile.d/java.sh 修改JAVA_HOME路径如下
# Java environment
export JAVA_HOME=/usr/local/jdk1.6.0_37
export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
[root@centos7 ~]# . /etc/profile.d/java.sh
[root@centos7 ~]# java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
3、安装tomcat
[root@centos7 ~]# tar xf apache-tomcat-7.0.82.tar.gz
[root@centos7 ~]# mv apache-tomcat-7.0.82 /usr/local/tomcat7
[root@centos7 ~]# /usr/local/tomcat7/bin/startup.sh
访问8080端口测试tomcat是否正常工作
4、mod_jk模块配置(下载地址)
[root@centos7 ~]# tar xf tomcat-connectors-1.2.42-src.tar.gz
[root@centos7 ~]# cd tomcat-connectors-1.2.42-src/native
[root@centos7 ~]# ./configure --with-apxs=/usr/local/apache24/bin/apxs
[root@centos7 ~]# make
[root@centos7 ~]# cp ./apache-2.0/mod_jk.so /usr/local/apache24/modules/
接下来配置/usr/local/apache24/conf/httpd.conf 文件,在配置文件末尾添加如下代码:
LoadModule jk_module modules/mod_jk.so
<IfModule jk_module>
JkWorkersFile conf/workers.properties
JkMountFile conf/uriworkermap.properties
JkLogFile logs/mod_jk.log
JkLogLevel warn
</IfModule>
创建并配置/usr/local/apache24/conf/workers.properties文件,内容如下:
worker.list=wlb,jkstatus
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
# Define preferred failover node for tomcat1
#worker.tomcat1.redirect=tomcat2
worker.tomcat2.port=9009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
# Disable tomcat2 for all requests except failover
#worker.tomcat2.activation=disabled
worker.wlb.type=lb
worker.wlb.balance_workers=tomcat1,tomcat2
worker.jkstatus.type=status
#以上注释掉的两行如果开启,则worker2成为worker1的备用,在worker1不可用的情况下才会向worker2请求
创建并配置/usr/local/apache2/conf/uriworkermap.properties,这个文件是控制什么文件被Apache处理,实现动静分离,内容如下:
/admin/*=wlb
/manager/*=wlb
/jsp-examples/*=wlb
/servlets-examples/*=wlb
/examples/*=wlb
/*.jsp=wlb
/*=wlb
!/servlets-examples/*.jpeg=wlb
/jkmanager=jkstatus
!/*.gif=wlb
!/*.jpg=wlb
!/*.png=wlb
!/*.css=wlb
!/*.js=wlb
!/*.htm=wlb
!/*.html=wlb
然后找到DirectoryIndex index.html在后面添加 index.jsp
DirectoryIndex index.html index.jsp
把appBase改成Apache的项目路径
<Host name="localhost" appBase="/usr/local/apache24/htdocs/"
unpackWARs="true" autoDeploy="true">
以下引用可加可不加,目前测试没有问题,只做备注
/usr/local/tomcat7/conf/server.xml中在
<Host></Host>
标签之间加入,docBase路径要和Apache路径一样
<Context path="" docBase="/usr/local/apache24/htdocs" debug="0"
reloadable="true" crossContext="true"/>
5、测试
在/usr/local/apache24/htdocs下创建test目录,添加 index.jsp 文件或者直接把tomcat的ROOT目录下的文件考过来(要安装tomcat-webappsROOT目录下才会有文件),看是否可以通过tomcat访问
正常访问apache如下图:
访问动态jsp文件:
6、接下来安装mysql mysql的yum仓库下载
# 上传到 /usr/src 目录下
[root@centos7 src]# yum install ./mysql57-community-release-el7-11.noarch.rpm
[root@centos7 src]# yum install mysql-server -y
[root@centos7 src]# systemctl start mysqld
7、mysql5.7破解密码
1)、修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1
这一行配置让 mysqld 启动时不对密码进行验证
2)、重启 mysqld 服务:systemctl restart mysqld
3)、使用 root 用户登录到 mysql:mysql -u root
4)、切换到mysql数据库,更新 user 表:
update user set authentication_string = password('123456'), password_expired = 'N', password_last_changed = now() where user = 'root';
在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string
5)、退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容
6)、重启 mysqld 服务,再用新密码登录即可
另外,MySQL 5.7 在初始安装后(CentOS7 操作系统)会生成随机初始密码,并在 /var/log/mysqld.log 中有记录,可以通过 cat 命令查看,找 password 关键字
# 登录数据库,创建数据库测试数据
mysql> create database test;
mysql> use test;
mysql> create table if not exists student(id int no null auto_increment primary key,name varchar(20),age int);
mysql> insert into student (name,age)values("tom",20);
mysql> insert into student (name,age)values("jerry",20);
7、mysql-connector安装 mysql-connector下载
[root@centos7 ~]# tar xf mysql-connector-java-5.1.45.tar.gz
[root@centos7 ~]# cd mysql-connector-java-5.1.45/
[root@centos7 ~]# cp mysql-connector-java-5.1.45-bin.jar /usr/local/tomcat7/lib/
[root@centos7 ~]# /usr/local/tomcat7/bin/shutdown.sh
[root@centos7 ~]# /usr/local/tomcat7/bin/startup.sh
测试是否能连接上mysql
[root@centos7 ~]# vi /usr/local/apache24/htdocs/testmysql/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.naming.*" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>My first DB</title>
</head>
<body>
<h1>
<%
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接 注:下面的test为数据库名字,root为mysql用户名 123456 为root的密码
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
//创建状态
Statement state=con.createStatement();
String sql2="select * from student";
ResultSet rs=state.executeQuery(sql2);
while(rs.next()){
String id=rs.getString("id");
String name=rs.getString("name");
out.println("id:"+id+" "+"name:"+name+"<br>");
}
out.println("success..."+"<br>");
state.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</h1>
</body>
</html>
浏览器输入http://192.168.10.135/testmysql/index.jsp
,出现结果:
id:1 name:tom
id:2 name:jerry
success…
表示成功
如果想配置tomcat集群可以参考下面文章
文章地址