preload preload


Apache httpd mod_jk PHP/Java Alias issue

As we all know that, Apache httpd can be used as a front end webserver for Apache tomcat to provide the better handling of static files like css, javascript etc. It can also be used to allow running PHP and Java parallely over the same server. While configuring, we can define that all PHP requests should goto PHP interpreter and all the JSP should goto tomcat.


<VirtualHost *:80>

.....

<IfModule mod_jk.c>
 # Send JSPs for context /jsp-examples to worker named default
 JkMount /MyJava/*.jsp default
 # Send servlets-examples to worker named default
 JkMount /MyJava/* default
 # Testing Send any request to worker name default
 JkMount /MyJava/FeedbackServlet default
 </IfModule>

</VirtualHost>

These settings work fine as long as Java application is defined in any subdirectory of the webserver or it is not in the root context of server.

Suppose a scenario is like the Java application should be loaded at the root context of the url not on some application. For e.g. all requests for http://example.com/ should goto MyJava application. A sample setting in that case would be


<VirtualHost *:80>

.....

<IfModule mod_jk.c>
 # Send JSPs for context /jsp-examples to worker named default
 JkMount /*.jsp default
 # Send servlets-examples to worker named default
 JkMount /* default
 # Testing Send any request to worker name default
 JkMount /FeedbackServlet default
 </IfModule>

</VirtualHost>

Now, suppose we also want to add PHP support to the same server. The above settings will not work because JkMount /* will send all request even PHP also to the Java application.

Some of you might be thinking of using Apache httpd Alias for the rescue. Yup, that is correct. We can use Alias here to define that all the request to my MyPHP application should goto /home/MyPHP. We can define like


<VirtualHost *:80>

.....

<IfModule mod_jk.c>
 # Send JSPs for context /jsp-examples to worker named default
 JkMount /*.jsp default
 # Send servlets-examples to worker named default
 JkMount /* default
 # Testing Send any request to worker name default
 JkMount /FeedbackServlet default
 </IfModule>

Alias /MyPHP /home/MyPHP

<Directory "/home/MyPHP">
 Options -Indexes FollowSymLinks MultiViews
 AllowOverride All
 Order deny,allow
 Allow from all
 </Directory>

</VirtualHost>

But this will not work. We will still forwarded to the Java application even we requested MyPHP.

Apache httpd takes the precedence of JkMount over the Alias directive.

Now, to solve this issue we can use two directive, one of Apache httpd’s and other of mod_jk. They are SetEnvIf and no-jk. SetEnvIf allows us to set the environment based on the condition defined. no-jk is a directive which exludes the directory from the mod_jk execution.

In our case we will define it as


SetEnvIf Request_URI "/MyPHP/*" no-jk

The above line says that if the Request url consist /MyPHP (like http://example.com/MyPHP) then don’t apply mod_jk conditions. In a VirtualHost configuration, it can be defined as


<VirtualHost *:80>

.....

<IfModule mod_jk.c>
 # Send JSPs for context /jsp-examples to worker named default
 JkMount /*.jsp default
 # Send servlets-examples to worker named default
 JkMount /* default
 # Testing Send any request to worker name default
 JkMount /FeedbackServlet default
 </IfModule>

SetEnvIf Request_URI "/MyPHP/*" no-jk

Alias /MyPHP /home/MyPHP

<Directory "/home/MyPHP">
 Options -Indexes FollowSymLinks MultiViews
 AllowOverride All
 Order deny,allow
 Allow from all
 </Directory>

</VirtualHost>

Liked the content? Then why not share with your pals

  • One response to "Apache httpd mod_jk PHP/Java Alias issue"

  • Justin Koreska
    21:17 on November 3rd, 2012

    You’re a genius! Thank you.

  • Leave a Reply

    * Required
    ** Your Email is never shared