How to Setup Apache As Frontend Proxy for Node.js
Abstract: 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
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.
Before Start- I am using Ubuntu 18.04 instance hosted on DigitalOcean.
- Installed latest Node.js version using this tutorial on this Linux machine.
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.
nano 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 ServerNow install the Apache 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. Apache packages is available under default repositories on almost operating systems.
sudo apt install apache2
### Debians based systemssudo dnf install httpd
### Fedora & CentOS 8
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
As you have configured and running a Node.js demo server. Now, start configuration with Apache web server. Create a virtual host configuration file for your domain under /etc/apache2/sites-available/ directory.
### Debian based system'ssudo nano /etc/apache2/sites-available/example.com.conf
### Redhat based system'ssudo vim /etc/httpd/conf.d/example.com.conf
and add the following content.
<VirtualHost *:80> ServerName example.com ProxyPreserveHost On ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost>1234567<VirtualHost *:80> ServerName example.com ProxyPreserveHost 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'sStep 5 – Verify Setupsudo a2ensite example.com
sudo systemctl restart apache2
### Redhat based system'ssudo systemctl restart httpd
Now access your server using the domain name, you will see the same page shows on http://127.0.0.1:3000/.
ConclusionYou 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.