Question

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

Posted May 27, 2021 361 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 mydomain.com are served through node.js while calls to mydomain.com/api 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>
    ServerName mydomain.com

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

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

    <Location />
        ProxyPass http://127.0.0.1:8080/
        ProxyPassReverse http://127.0.0.1:8080/
    </Location>

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

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 / http://127.0.0.1:8080/

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.

Regards,
Bobby