设为首页 收藏本站
查看: 752|回复: 0

[经验分享] centos下tomcat与apache整合

[复制链接]

尚未签到

发表于 2016-5-11 08:34:28 | 显示全部楼层 |阅读模式
  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@mail.com
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 为单位。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-215336-1-1.html 上篇帖子: CentOS 6 安装MAVEN及系统配置 下篇帖子: centos下配置mysql mm 步骤
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表