Execute PHP scripts within Node.js web server

Execute PHP scripts within Node.js web server

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

Stefan Bogdanescu

Founder & Senior Architect

Executing PHP within Node.js: The Reverse Proxy Approach

You cannot execute PHP scripts directly inside a Node.js runtime because they require fundamentally different execution environments. Node.js runs JavaScript using the V8 engine, while PHP requires the PHP interpreter. Attempting to parse and run .php files natively in a Node application is technically impossible without complex, unstable wrappers that essentially reimplement the language. The industry-standard solution is to route PHP requests through an existing PHP handler or reverse proxy, such as Apache or Nginx, rather than trying to integrate the logic into Node itself.

To achieve this architecture, you must configure your Node.js server to act as a reverse proxy for specific URL paths while handling other paths natively. This setup allows a single domain to serve static assets and API endpoints via Node while delegating dynamic content generation to PHP. The most robust implementation usually involves Apache acting as the primary dispatcher or Nginx, with Node listening on a local port (e.g., 3000) or Apache handling all incoming traffic and forwarding specific routes.

Configuring Apache as the Dispatcher

The most common legacy pattern involves installing Apache alongside Node.js. In this configuration, Apache listens on port 80 and handles all PHP requests directly. For requests intended for your Node application, you define a .htaccess rule or use an httpd.conf directive to proxy them to the local Node instance.

You can configure Apache to rewrite specific paths using the mod_rewrite module. For example, if you want /api/* requests to be handled by a Node app running on port 3000 while keeping /blog.php under Apache control:

RewriteEngine On

# Route /api/* to Node.js
RewriteCond %{REQUEST_URI} ^/api/
RewriteRule ^(.*)$ http://localhost:3000/api/$1 [P,L]

# All other requests (including PHP) stay with Apache
RewriteRule ^(.*)$ / [L]

In this scenario, Apache receives the HTTP request. If the file extension is .php, Apache invokes the libphp module to process it immediately. If the path matches the rewrite rule, Apache acts as a reverse proxy, forwarding the request headers and body to your Node.js server using the mod_proxy or mod_proxy_http modules.

Using Nginx with Upstream

Nginx offers a cleaner separation of concerns for many modern stacks. You can configure an upstream block pointing to your Node process and use location blocks to route traffic.

upstream node_app {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name example.com;

    # Serve PHP files via internal handler or FastCGI
    location /blog/ {
        root /var/www/html;
        index blog.php;
        try_files $uri $uri/ =404;
    }

    # Proxy API requests to Node.js
    location /api/ {
        proxy_pass http://node_app;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Here, Nginx terminates the SSL connection and handles the PHP execution via its built-in FastCGI support (or a dedicated PHP-FPM pool). It then proxies only the /api/ paths to the Node backend. This prevents your Node application from ever seeing raw PHP files or needing to know about the file system structure of the PHP site.

Architectural Trade-offs and Pitfalls

The primary advantage of this hybrid approach is leveraging the strengths of each language: Node for high-concurrency I/O operations like WebSocket handling or real-time API responses, and PHP for complex business logic that was already written or requires specific libraries not yet ported to JavaScript.

However, there are significant considerations regarding session management and cookies. When Apache proxies a request to Node, cookies set by the PHP application need to be accessible to the Node backend if you intend to maintain state across the handoff, though this is often unnecessary since each request is typically stateless in modern APIs. The bigger pitfall arises with server-side redirects. If your PHP script issues a Location header, Apache might forward that redirect to Node instead of completing the response locally, leading to mixed content errors or broken redirect chains if Node doesn't handle the final response correctly.

Additionally, ensure that your firewall and load balancer configurations allow traffic between the web server and your local Node port. Security groups should permit localhost communication but restrict direct external access to the Node port if it only serves internal proxy requests.

For developers managing complex PHP applications within modern stacks, understanding these routing mechanics is essential. If you are building a new application that requires tight integration of full-stack capabilities with existing PHP legacy systems, platforms like Laravel often provide robust middleware and tools that simplify handling mixed environments.

Our network