How can I run NodeJS and Apache together with an Apache virtual host file on Centos8?

Viewed 347

I have my NodeJS application running on example.com:3000. Obviously, this would be unpresentable to have to tell a user to type in :3000. I want it running on example.com, and I cannot set Node to :80 since my Apache PHP front end is on :80.

I am running CentOS 8. I created a file called proxy.conf in /etc/httpd/conf.d and dropped the following lines in it.

<VirtualHost *:80>
        ErrorLog /var/log/httpd/error.log
        CustomLog /var/log/httpd/access.log combined
        ProxyRequests On
        ProxyPass / http://localhost:3000
        ProxyPassReverse / http://localhost:3000
</VirtualHost>

But, it's just going to my regular non node page.

1 Answers

The question is, how does apache know you when you want the PHP server, or when you want the Node server. You need to split them by ServerName.

You need a ServerName attribute so that apache knows to send that VirtualHost to the other process.

<VirtualHost *:80>
    // PHP Server
    ServerName example.com www.example.com
</VirtualHost>
<VirtualHost *:80>
    // Node Server
    ServerName node.example.com
    ErrorLog /var/log/httpd/error.log
    CustomLog /var/log/httpd/access.log combined
    ProxyRequests On
    ProxyPass / http://localhost:3000
    ProxyPassReverse / http://localhost:3000
</VirtualHost>

Additionally, you will have to put node.example.com within DNS.

Related