Run a php app using tomcat?

Run a php app using tomcat?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Can You Run PHP on Tomcat Without Splitting Ports?

Yes, it is technically possible to run a PHP application alongside an existing Java application on Apache Tomcat without splitting traffic across different ports. The core constraint here is that both your legacy Java app and the new RoundCube installation need to share http (80) and https (443). Since Tomcat serves as the container for your Java code, it already satisfies the requirement for hosting the JAR or WAR file at /host/myapp. The challenge lies in getting Apache to proxy PHP requests correctly while maintaining this single-port architecture.

The standard approach involves configuring Tomcat as an internal application server rather than relying on its standalone connector for PHP. You achieve this by installing a PHP handler, such as mod_php, within the Tomcat environment or by using a reverse proxy setup where Apache sits in front of Tomcat. However, since you are already running Java on Tomcat, introducing mod_php directly into the Tomcat bin/ directory is generally not recommended because it conflicts with the Tomcat servlet container lifecycle.

Instead, the most robust architecture for this specific scenario is to install Apache HTTPD on the same server and configure it as a reverse proxy for Tomcat. This setup allows both technologies to coexist on ports 80 and 443 without conflict. You would install PHP-FPM or enable mod_php in Apache, then use Apache's <Proxy> directives to route specific paths to the Java application running inside Tomcat while serving PHP requests directly or via a different backend.

To implement this without changing your port structure, you must ensure that the virtual host configurations are distinct but bound to the same IP and ports. In Apache, you can define two VirtualHost blocks listening on port 80: one handling requests for /host/myapp which proxies to localhost:8080 (where Tomcat is running), and another handling /host/roundcube which serves PHP files directly.

Here is a conceptual example of how the Apache configuration might look in the virtual host file:

<VirtualHost *:80>
    ServerName yourdomain.com
    
    # Route existing Java app to Tomcat
    ProxyPass /host/myapp http://localhost:8080/host/myapp
    ProxyPassReverse /host/myapp http://localhost:8080/host/myapp
    
    # Serve PHP app directly
    # Ensure mod_php or php-fpm is configured for this path
    <Directory "/var/www/roundcube">
        Options +Indexes FollowSymLinks Multiviews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

In this configuration, Apache acts as the entry point. When a request hits /host/myapp, Apache forwards it to the Tomcat instance running on port 8080. When the request targets /host/roundcube, Apache processes it using its native PHP module or passes it to PHP-FPM, bypassing Tomcat entirely for that specific path. This effectively creates two separate application servers sharing a single network interface.

A critical trade-off in this architecture is the complexity of managing two different runtime environments (Tomcat and Apache/PHP) on one machine. You must ensure that any shared libraries or configuration files do not clash, particularly regarding SSL certificates if you are using HTTPS. Additionally, error logging becomes more fragmented; you will need to monitor both Tomcat's logs and Apache's error.log to troubleshoot issues specific to each stack.

If your goal is strictly to avoid port splitting, this reverse proxy method is the industry-standard solution. It maintains a single entry point for users while keeping the Java and PHP stacks isolated internally. There is no need to force Tomcat to handle PHP directly via mod_jk or similar connectors unless you are constrained by a specific hosting environment that forbids installing Apache alongside Tomcat. In such restrictive environments, using catalina.sh with an external PHP handler like php-cgi might be the only alternative, though it introduces significant complexity in managing process pools and timeouts.

Ultimately, the decision rests on your operational constraints. If you have control over the server software stack, running Apache alongside Tomcat provides a clean separation of concerns: Java handles the enterprise application at /host/myapp, while PHP runs natively for RoundCube at /host/roundcube. For developers seeking stability and standard support paths, this hybrid approach is widely adopted in shared hosting and enterprise environments alike.

Our network