• 欢迎访问少将全栈,学会感恩,乐于付出,珍惜缘份,成就彼此、推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。
  • 吐槽,投稿,删稿,交个朋友,商务沟通v:ai_draw
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏少将全栈吧

centos下tomcat与apache整合

点滴 admin 11年前 (2013-06-30) 1478次浏览 已收录 0个评论 扫描二维码

1. 首先需要安装apache,安装步骤参见其官网,然后安装apache jk module

1.1. wget  http://mirror.bjtu.edu.cn/apache//tomcat/tomcat-connectors/jk/source/jk-1.2.31/tomcat-connectors-1.2.31-src.tar.gz
1.2. tar -xzvf tomcat-connectors-1.2.31-src.tar.gz
1.3. cd tomcat-connectors-1.2.31-src/native
1.4. ./configure –with-apxs=/usr/local/apache2/bin/apxs #/usr/local/apache2/是你apache的安装目录
1.5. make
1.6. cp apache-2.0/mod_jk.so /usr/local/apache2/modules/mod_jk.so, 然后重新启动apache
1.7. 进入apache安装目录,cd /usr/local/apache2/

2. 配置mod_jk.conf
2.1. cd conf, vi mod_jk.conf
2.2. 编辑如下内容:

# global config
# just configure log and load module
# load module
LoadModule jk_module modules/mod_jk.so   #表示导入jk模块

JkLogFile logs/mod_jk.log                # logs保存位置
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ” #log时间戳格式
JkRequestLogFormat     “%w %V %U %T”          #log记录的内容
JkLogLevel warn
JkWorkersFile conf/workers.properties      #加载worker配置文件

#all vhost should inherit this config
JkMountCopy All                      #所有的虚拟主机都复制该配置文件

2.3 JkRequestLogFormat参数说明:

%b    Bytes sent, excluding HTTP headers (CLF format)
%B    Bytes sent, excluding HTTP headers
%H    The request protocol
%m    The request method
%p    The canonical Port of the server serving the request
%q    The query string (prepended with a ? if a query string exists, otherwise an empty string)
%r    First line of request
%s    Request HTTP status code
%T    Request duration, elapsed time to handle request in seconds ’.’ micro seconds
%U    The URL path requested, not including any query string.
%v    The canonical ServerName of the server serving the request
%V    The server name according to the UseCanonicalName setting
%w    Tomcat worker name
%R    Session route name (available with 1.2.19 and up)

3. 配置workers.properties

vi workers.properties

# 所有可以访问的工作机器,status用来监控jk的状态,一个工作机器可以理解为一个tomcat实例
worker.list=balancer,master,status 

#type表示工作机器的类型,一般是type=ajp13 和 lb,lb表示负载平衡
worker.gmaster.port=8009       
worker.master.host=10.3.1.22  
worker.master.type=ajp13
worker.master.lbfactor = 1    #负载平衡因子,如果想要本机承载的负载大些,可以设置值大些
    
#define web app slave host
worker.slave.port=9009   
worker.slave.host=localhost
worker.slave.type=ajp13
worker.slave.lbfactor = 1

#define web app balancer
worker.balancer.type=lb   #表示负载平衡
worker.balancer.balance_workers=master,lave  # 负载平衡工作机器,此列表的workers可以不放在worker.list
worker.balancer.sticky_session=True                         # 有session的请求要指到原来tomcat上

#define status
worker.status.type=status            #指明监控工作机器,可以不设置

4. 配置uriworkermap.properties,它用来匹配哪些url被转发给tomcat

4.1. vi uriworkermap.properties

#configure status pattern
/jkstatus=status

#configure balancer patterns
#just forword *.jsp,*.do and page serverlet
/appname/*.jsp=balancer
/appname/*.do=balance
/appname/page=balance
/appname/*/page=balance
/appname/=balance
/appname=balance

# configure admin functionality,后台管理程序可以只匹配到一个工作机器,因为后台管理用户较少
/appname/admin/*.jsp=master
/appname/admin/*.do=master
/appname/admin/*.html=master
/appname/admin/page=master
/appname/admin/*/page=master
/appname/admin/=master

4.2. 匹配优先级说明:
The most restrictive URI pattern is applied first. More precisely the URI patterns are sorted by
the number of ’/’ characters in the pattern (highest number first), and rules with equal numbers
are sorted by their string length (longest first).

If both distinctions still do not suffice, then the defining source of the rule is considered.
 Rules defined in uriworkermap.properties come first, before rules defined by JkMount (Apache)
and inside workers.properties using the mount attribute.

All disabled rules are ignored. Exclusion rules are applied after all normal rules have been applied.

There is no defined behaviour, for the following configuration conflict: using literally the same
URI pattern in the same defining source but with different worker targets.

5.配置httpd.conf

vi httpd.conf

在最后面加载 Include conf/mod_jk.conf

6. 配置httpd-vhosts.conf

vi extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerName yourweb.com
    ServerAdmin your [email protected]
    DocumentRoot “/usr/local/apache2/your doc root”
        #放置Alias块
    RewriteEngine on
    RewriteRule ^/logging.php* yourapp/admin/ [PT]

    JkMountFile conf/uriworkermap.properties #加载配置规则,仅对本虚拟主机生效
</VirtualHost>

PT 表示 passthrough,直接发个apache内部
R 表示 redirect
RewriteRule 处理快于jk匹配,Rewrite规则可以参见apache官网,它像一个钩子,可以修改很多行为

7. 使用apache Alias 实现apache 静态文件转发功能,即静态文件请求tomcat,直接通过apache提供服务
把这一段放在 <VirtualHost *:80>  </VirtualHost>之间,一般放在DocumentRoot的下一行即可

Alias /yourapp/js /var/yourapp_static/js/
<Directory “/var/yourapp_static/js”>
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

你也可以放置多个这样的别名块

8. 配置tomcat
8.1. 配置jvm内存,vi bin/catalina.sh, 在最前面添加: JAVA_OPTS=’-Xms1000m -Xmx1000m’
8.2. 配置ajp connector,在server.xml中配置
 <Connector protocol=”AJP/1.3″ port=”0″
        channelNioSocket.port=”8009″
        channelNioSocket.redirectPort=”8443″
        channelNioSocket.maxThreads=”256″
        channelNioSocket.maxSpareThreads=”97″
        channelNioSocket.minSpareThreads=”50″
        channelNioSocket.URIEncoding=”UTF-8″
        URIEncoding=”UTF-8″
        channelNioSocket.connectionTimeout=”20000″
        channelNioSocket.keepAliveTimeout=”1000″
        channelNioSocket.bufferSize=”16384″/>

    使用了nio
8.3. 生产环境中你可以关掉http connector

9. 配置操作系统文件描述符数量

vi /etc/profile

加入 ulimit -SHn 51200
后保存
 
source /etc/profile

#一般系统的文件描述符是1024,你可以使用ulimit -a查看,当有大量tcp连接时,文件描述符可能不够用
参数说明:

-H      设置硬资源限制,一旦设置不能增加。     
-S     设置软资源限制,设置后可以增加,但是不能超过硬资源设置。
-a     显示当前所有的 limit 信息。
-c     最大的 core 文件的大小, 以 blocks 为单位。
-d     进程最大的数据段的大小,以 Kbytes 为单位。
-f     进程可以创建文件的最大值,以 blocks 为单位。
-l     最大可加锁内存大小,以 Kbytes 为单位。
-m     最大内存大小,以 Kbytes 为单位。
-n     可以打开最大文件描述符的数量。
-p     管道缓冲区的大小,以 Kbytes 为单位。
-s     线程栈大小,以 Kbytes 为单位。
-t     最大的 CPU 占用时间,以秒为单位。
-u     用户最大可用的进程数。
-v     进程最大可用的虚拟内存,以 Kbytes 为单位。

一、准备

①下载httpd-2.2.21.tar.gz
②下载apache-tomcat-7.0.23.tar.gz
③下载tomcat-connectors-1.2.32-src.tar.gz连接
二、安装
①安装apache:
解压:tar zxvf httpd-2.2.21.tar.gz
改名:mv httpd-2.2.21 apache
#./configure –prefix=/002/apache
#make
#make install
②安装tomcat
Tomcat无需安装解压就可以直接运行
③编译安装tomcat-connectors-1.2.32-src.tar.gz
1.解压:tar zxvf tomcat-connectors-1.2.32-src.tar.gz
2.改名:mv tomcat-connectors-1.2.32-src.tar.gz conn
3.安装:
# tar -xzvf tomcat-connectors-1.2.32-src.tar.gz 
#cd tomcat-connectors-1.2.31-src/native
#./configure –with-apxs=/002/apache/bin/apxs  #/002/apache是你apache的安装目录 
#make
# cp apache-2.0/mod_jk.so /002/apache/modules/mod_jk.so 
④配置tomcat apache开机自启动
手动添加tomcat服务到系统中以下为具体配置:
1、配置apache 打开终端 输入chkconfig 系统会列出所有服务
chkconfig httpd on 即可
2、配置tomcat
cd /etc/rc.d/init.d  
chmod 755 tomcat
vi tomcat
 
添加一下内容
startup script for the tomcat
#
# chkconfig: 345 80 15
# description: Tomcat is a Servlet+JSP Engine.
export JDK_HOME=/usr/java/javaeejdk/jdk7  
export JAVA_HOME=/usr/java/jdk1.7.0_02
#输入JDK位置
# Source function library.
. /etc/rc.d/init.d/functions
start(){
if [ -z $(/sbin/pidof java) ]; then
echo “Starting tomcat”
/002/tomcat/bin/startup.sh
touch /var/lock/subsys/tomcat
else
echo “tomcat allready running”
fi
}
stop(){
if [ ! -z $(/sbin/pidof java) ]; then
echo “Shutting down tomcat”
/002/tomcat/bin/shutdown.sh
until [ -z $(/sbin/pidof java) ]; do :; done
rm -f /var/lock/subsys/tomcat
else
echo “tomcat not running”
fi
}
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
/002/tomcat/bin/catalina.sh version
;;
*)
echo “Usage: $0 {start|stop|restart|status}”
esac
exit 0
添加服务: chkconfig –add tomcat
开机启动:chkconfig tomcat on
⑤添加apache开机自启动
        cp /002/apache/bin/apachectl /etc/init.d/httpd
        sudo vi /etc/init.d/httpd
        在文件开头加入下面几行:
        #!/bin/sh
# chkconfig: 2345 85 15
# description: Apache is a World Wide Web
添加服务
#chkconfig –add httpd
设置服务开机自动运行
#chkconfig httpd on
 
三、配置
① 配置apache支持
#vi /002/apache/conf/httpd.conf
加入以下内容
LoadModule jk_module modules/mod_jk.so
Include /002/apache/conf/mod_jk.conf
② 添加配置文件mod_jk.conf
进入/002/apache/conf
#cd /002/apache/conf
#vi mod_jk.conf 加入以下内容
JkWorkersFile /002/apache/conf/workers.properties
JkLogFile /002/apache/logs/mod_jk.log
JkLogLevel info
# Select the log format
JkLogStampFormat “[%a %b %d %H:%M:%S %Y]”
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat “%w %V %T”
JkMount /servlet/* worker1
JkMount /*.jsp worker1
#vi workers.properties
# Defining a worker named worker1 and of type ajp13
worker.list=worker1
 
# Set properties for 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 
③ 配置httpd.conf
#vi httpd.conf
 修改以下内容
Listen 80 改为 Listen 127.0.0.1:80
 ServerName 你的主机名:80
四、测试
在/var/web目录下写index.jsp测试文件
服务器能不能正确解析
测试通过
 
 
五、总结
 
本人刚刚接触linux 笔记内容均为手写 并非教程只是记录本人搭建服务器环境的过程,所以没有截图
本人想传递的只是思想 一种大致的步骤 供安装者参考,最好不要完全照搬
每个软件的安装配置都是在虚拟机上调试N次,文档也是自己手写的(部分代码除外)
在此感谢互联网上的前辈们提供经验支持

喜欢 (0)
[🍬谢谢你请我吃糖果🍬🍬~]
分享 (0)
关于作者:
少将,关注Web全栈开发、项目管理,持续不断的学习、努力成为一个更棒的开发,做最好的自己,让世界因你不同。
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址