Deploy a Node.js application in Apache server with php sub directory

Posted May 27, 2021 734 views
ApachePHPNode.jsUbuntu 18.04

I am running an Ubuntu 18.04 server with apache2.4 and I am trying to deploy a node.js application with a PHP Api on this same server.

I have successfully configured the server to read my node.js application on port 80 but I am not able to configure a sub directory to load php. I want to deploy my application in such a way that calls to are served through node.js while calls to are served by a php API.

I have configure my 000-default.conf as follows but I don’t know exactly how to remove the php sub directory from being rendered by node because now, everything called via port 80 is rendered by node.

<VirtualHost *:80>

    DocumentRoot </path_to_app>
    <Directory />
    Options -Indexes +FollowSymlinks
    AllowOverride None
    Require all granted

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full
    <Proxy *>
    Require all granted

    <Location />

    <Directory path_to_app/api>
         DirectoryIndex index.php
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi there,

As the Locaiton is set to / it catches all of the requests.

What you could do instead is remove the Location directive and use the following configuration instead:

Alias /api /var/www/html

ProxyPass /api !
ProxyPass / http://localhost:8080/
ProxyPassReverse /

That way your /api endpoint will be excluded from the reverse proxy, and it will serve the content of the /var/www/html folder.

Hope that this helps.