Written by , Updated on January 9, 2020

Apache is the most popular web server. You can also use Apache as a frontend proxy server for backend running applications like Node.js. This tutorial will help you to set up your Apache server as a frontend proxy server for your Node.js application with easy steps.

Prerequsities

We are assuming you have pre-installed Node.js on your system. But still, if you want to install Node.js follow this tutorial.

Step 1 โ€“ Create Sample Node Application

As you are here ๐Ÿ™‚ You must have a running Node.js application on some port. We assume you are running your application on port 3000. For the demonstration purpose, I am creating a sample web application on Node.js and run on port 3000. So it will be easier to understand for you.

vi myapp.js

Then, add the following content in the javascript file.

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

Your Node application is ready to serve on port 3000. Letโ€™s start the Node.js application in the background.

node myapp.js &

And access this in the browser. You will see the result like below:

Step 2 โ€“ Install Apache Server

Now install the Nginx web server using the default Package manager. The Ubuntu and Debian based systems use apt, Fedora and CentOS/RHEL 8 use DNF and CentOS/RHEL 7/6 uses yum. Nginx is available under default repositories on almost operating systems.

sudo apt install apache2
sudo yum install httpd
sudo dnf install httpd

After installation of Apache web server, you must have enabled the Proxy module. This module is enabled in Apache for users who installed using rpm packages. If you donโ€™t have enabled edit your Apache configuration /etc/httpd/conf/httpd.conf or for Apache 2.4 /etc/httpd/conf.modules.d/00-proxy.conf file and uncomment the following lines or put them in the file.

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

For the Debian based systems use the following command to enable the Proxy module with Apache.

sudo a2enmod proxy proxy_http

Step 3 โ€“ Configure Apache VirtualHost

After starting a demo server with node.js. Now start configuration with Nginx. Create a virtual host configuration file for your domain under /etc/nginx/conf.d/ directory.

### Debian based system's sudo vim /etc/apache2/sites-enabled/example.com.conf

### Redhat based system's sudo vim /etc/httpd/conf.d/example.com.conf

and add the following content.

<VirtualHost *:80>

ServerName example.com

ProxyRequests On

ProxyPass / http://localhost:3000

ProxyPassReverse / http://localhost:3000

</VirtualHost>

After creating the configuration, restart the Apache webserver using the following command.

### Debian based system's sudo a2ensite example.com
sudo systemctl restart apache2

### Redhat based system's sudo systemctl restart httpd

Step 5 โ€“ Verify Setup

Now access your server using the domain name, you will see the same page shows on http://127.0.0.1:3000/.

Conclusion

You have done the configuration of Apache proxy to backend the Node.js application. Alternatively, Nginx has better performance as a Proxy server, so if you are not bounded with Apache, go with the Nginx proxy server.