How to Configure Tomcat/JBoss and Apache HTTPD for Load Balancing and Failover

http://java.dzone.com/articles/how-configure-tomcatjboss-and

In this post we will see how to setup a load balanced JBoss or Tomcat environment with the fail-over capability.

This post assumes that both Jboss/Tomcat and Apache httpd are setup and running properly.

Configure Apache Httpd  Step 1: Configure apache’s workers.properties

Go to /usr/local/apache2/conf/extra and open workers.properties and configure the properties indicated in bold.

# for mapping requests
# The configuration directives are valid
# for the mod_jk version 1.2.18 and later
#
worker.list=loadbalancer,status

# Define node
# modify the host as your host IP or DNS name.

worker.node.port=8009
worker.node.host=192.168.0.3 #(IP or DNS name of the server on which Jboss is running)
worker.node.type=ajp13
worker.node.lbfactor=1

worker.node2.port=8009
worker.node2.host=192.168.0.4 #(IP or DNS name of the server on which Jboss is running)
worker.node2.type=ajp13
worker.node2.lbfactor=1

# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node,node2
worker.loadbalancer.sticky_session=1
worker.status.type=status

The property balance_workers is used to specify the nodes to load balance. For example, if we specify only ‘node’, all requests will be routed to the server named ‘node’. Modify this to test that requests are going to both servers.

Step 2: Configure mod-jk.conf

Go to /usr/local/apache2/conf/extra and open mod-jk.conf and configure the bold properties according to your requirements.

#Load mod_jk module
# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
JkWorkersFile conf/extra/workers.properties

# Where to put jk logs
JkLogFile /var/apache/logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel debug

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicates to send SSK KEY SIZE
# Note: Changed from +ForwardURICompat.
# See http://tomcat.apache.org/security-jk.html
JkOptions +ForwardKeySize +ForwardURICompatUnparsed -ForwardDirectories
JkOptions +FlushPackets
# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"

# Mount your applications
JkMount /* loadbalancer

# You can use external file for mount points.
# It will be checked for updates each 60 seconds.
# The format of the file is: /url=worker
# /examples/*=loadbalancer
JkMountFile conf/extra/uriworkermap.properties

# Add shared memory.
# This directive is present with 1.2.10 and
# later versions of mod_jk, and is needed for
# for load balancing to work properly
# Note: Replaced JkShmFile logs/jk.shm due to SELinux issues. Refer to
# https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=225452
JkShmFile /var/run/jk.shm

# Add jkstatus for managing runtime data
<Location /jkstatus/>
  JkMount status
  Order deny,allow
  Deny from all
  Allow from 127.0.0.1
</Location>

The JkMount /* loadbalancer indicates that all requests are to be routed through the load balancer.

Step 3: Go to /usr/local/apache2/conf and edit httpd.conf

Add

1. Include conf/extra/mod-jk.conf

to the end of the file.

Configure Tomcat/JBoss

These settings need to be made for each of the nodes we intend on placing behind the load balancer.

We need to modify the server.xml file which is located at

  • For Tomcat
    • ../apache-tomcat/conf/
  • For JBoss
    • ../jboss-5.1.0.GA/server/default/deploy/jbossweb.sar 

Edit the following tag:

1. &lt;Engine name="jboss.web" defaultHost="localhost" jvmRoute="node"&gt;

where the jvmRoute attribute is configured to ‘node’ (which is the name we gave the first worker while configuring workers.properties.)

Next, locate the following tag and edit the port to be the same as while configuring the workers.properties.

1. &lt;Connector protocol="AJP/1.3" port="8009"address="${jboss.bind.address}" redirectPort="8443" /&gt;

Configure the other node(s) accordingly.

Next, Start Apache httpd and the Jboss/tomcat node(s) that you  have configured.

 

 

 

Published at DZone with permission of Faheem Sohail, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Enterprise Integration is a huge problem space for developers, and with so many different technologies to choose from, finding the most elegant solution can be tricky.  MuleSoft provides the #1 open source Enterprise Service Bus,  Mule, along with SOA integration cloud services that are used by customers like eBay, Amazon, and Walmart.  Learn More about  SOA Best Practices, Mobile E-Commerce, and  Legacy Integration.
<think> 我们正在解决 Apache 重启时出现的共享库加载问题。具体错误是: Cannot load modules/libphp.so into server: /usr/local/apache/modules/libphp.so: cannot open shared object file: No such file or directory 这个错误发生在 httpd.conf 文件的第4行。根据错误信息,Apache 试图加载的模块文件(libphp.so)不存在或无法访问。 根据引用[1]和引用[2]的类似问题,我们可以从以下几个方面排查: 1. 确保 libphp.so 文件确实存在于指定的路径(/usr/local/apache/modules/)下。 2. 如果文件存在,则检查文件权限和SELinux上下文(如果启用了SELinux)。 3. 检查是否有依赖的共享库缺失(如引用[1]中提到的libXpm.so.4缺失)。 但是,当前错误提示的是无法打开共享对象文件,即libphp.so本身找不到。因此,我们首先确认文件是否存在。 解决方案步骤: 步骤1:检查libphp.so文件是否存在 使用命令: ls -l /usr/local/apache/modules/libphp.so 如果文件不存在,则可能是PHP模块未正确安装或安装路径不对。需要重新安装或编译PHP模块,并确保将其放置到正确的目录。 步骤2:如果文件存在,检查文件权限 确保Apache运行用户(通常为www-data或apache)有读取权限。例如: chmod 755 /usr/local/apache/modules/libphp.so 步骤3:检查SELinux(如果系统启用了SELinux) 参考引用[2]的方法,可以临时禁用SELinux来测试是否是SELinux导致: setenforce 0 然后重启Apache。如果问题解决,则说明是SELinux的安全策略阻止了访问。我们需要为libphp.so设置正确的上下文: chcon -t textrel_shlib_t /usr/local/apache/modules/libphp.so 然后重新启用SELinux: setenforce 1 步骤4:如果文件不存在,则需要安装或重新编译PHP模块 根据你安装PHP的方式,可能需要重新编译PHP并指定--with-apxs2选项(如果你的Apache是使用apxs编译的)。例如: ./configure --with-apxs2=/usr/local/apache/bin/apxs ... [其他配置选项] make make install 安装后,libphp.so应该会被复制到Apache的模块目录。 步骤5:检查httpd.conf中的路径是否正确 确保httpd.conf中LoadModule指令的路径正确。例如,如果文件在/usr/local/apache/modules/libphp.so,那么指令应该是: LoadModule php_module modules/libphp.so 注意:这里的相对路径是相对于ServerRoot(在httpd.conf中设置)的。如果ServerRoot设置为/usr/local/apache,那么实际路径就是/usr/local/apache/modules/libphp.so。 步骤6:检查依赖库 如果错误提示是找不到其他共享库(如引用[1]中的libXpm.so.4),则可以使用ldd命令检查libphp.so的依赖: ldd /usr/local/apache/modules/libphp.so 如果发现缺失的库,则需要安装相应的软件包。 根据以上步骤,我们通常可以解决该问题。 另外,引用[3]中提到了在httpd.conf中添加处理.php文件的配置,但这与模块加载错误无关,而是在模块加载成功后处理PHP文件所需的配置。因此,在解决模块加载问题后,你还需要确保有正确的配置来处理PHP文件。 总结:我们首先检查文件是否存在,然后检查权限和SELinux,最后检查依赖和配置。 相关问题: 1. 如何编译PHP模块以与Apache一起使用? 2. 如何正确配置SELinux以允许Apache加载自定义模块? 3. 如何调试Apache模块加载失败的问题? </think> ### 解决 Apache 无法加载 libphp.so 问题的完整指南 当执行 `/usr/local/apache/bin/apachectl restart` 时出现 `Cannot load modules/libphp.so` 错误,表明 Apache 无法加载 PHP 模块。以下是系统化的解决方案,基于您提供的引用信息: #### 核心原因分析 1. **文件缺失或路径错误**(最常见) - `libphp.so` 文件不存在于指定路径 `/usr/local/apache/modules/` - Apache 配置文件 `httpd.conf` 中的路径配置错误 2. **权限问题**(引用[2]相关) - SELinux 安全策略阻止访问(常见于 CentOS/RHEL) - 文件系统权限不足 3. **依赖库缺失**(引用[1]相关) - PHP 模块依赖的共享库(如 `libXpm.so.4`)未安装 --- ### 解决步骤 #### 步骤1:验证 PHP 模块文件是否存在 ```bash # 检查文件是否存在 ls -l /usr/local/apache/modules/libphp.so # 如果不存在,重新安装 PHP 模块 cd /path/to/php/source/ # 进入 PHP 源码目录 ./configure --with-apxs2=/usr/local/apache/bin/apxs make clean && make && make install ``` #### 步骤2:检查 Apache 配置文件 编辑 `httpd.conf`(通常在 `/usr/local/apache/conf/httpd.conf`): ```apache # 确保这行配置正确指向文件 LoadModule php_module modules/libphp.so # 在文件末尾添加 PHP 文件处理配置(引用[3]) <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch> ``` #### 步骤3:解决 SELinux 问题(引用[2]方案) ```bash # 临时禁用 SELinux 测试 setenforce 0 /usr/local/apache/bin/apachectl restart # 如果成功,永久修复 SELinux 上下文 chcon -v -R -t textrel_shlib_t /usr/local/apache/modules/libphp.so semanage fcontext -a -t textrel_shlib_t "/usr/local/apache/modules/libphp\.so" restorecon -v /usr/local/apache/modules/libphp.so setenforce 1 ``` #### 步骤4:检查依赖库(引用[1]相关) ```bash # 检查依赖库 ldd /usr/local/apache/modules/libphp.so | grep "not found" # 安装常见缺失库(以 CentOS 为例) yum install libXpm libjpeg libpng libzip ``` #### 步骤5:验证修复 ```bash # 测试 Apache 配置 /usr/local/apache/bin/apachectl configtest # 重启 Apache /usr/local/apache/bin/apachectl restart # 检查 PHP 模块是否加载 curl -I localhost | grep X-Powered-By ``` --- ### 关键注意事项 1. **路径一致性**: - 确保 PHP 编译时的 `--with-apxs2` 路径与 Apache 的 `apxs` 路径一致 - 推荐使用绝对路径:`LoadModule php_module /usr/local/apache/modules/libphp.so` 2. **权限修复**: ```bash chown apache:apache /usr/local/apache/modules/libphp.so chmod 755 /usr/local/apache/modules/libphp.so ``` 3. **安装验证**: - 创建测试文件 `/usr/local/apache/htdocs/info.php`: ```php <?php phpinfo(); ?> ``` - 访问 `http://your-server/info.php` 验证 PHP 信息 通过以上步骤,99% 的类似问题都能解决。如果问题持续,请提供: 1. `apachectl configtest` 完整输出 2. `ls -l /usr/local/apache/modules/` 结果 3. 操作系统发行版和版本
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值