Atlantic.Net Blog

How to Configure Reverse Proxy for Node.js Application Using Apache on Oracle Linux 8

Node.js is a very powerful tool used to host JavaScript-based applications on the web. However, Node.js has some vulnerabilities that can cause performance-related issues. It does not work well with IO-bound operations or rapid traffic growth. In this case, you can use Apache as a reverse proxy to manage port contention and improve the Node.js performance.

In this post, we will explain how to configure Apache as a Reverse Proxy for Node.js applications on Oracle Linux 8.

Prerequisites

  • A fresh Oracle Linux 8 server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Oracle Linux 8 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Oracle Linux 8 server, run the following command to update your base system with the latest available packages.

dnf update -y

Step 2 – Install Node.js

By default, the latest version of Node.js is not available in the Oracle Linux 8 default repo, so you will need to add the Node source repository to your system.

First, install all the required dependencies with the following command:

dnf install curl unzip -y

Next, add the Node source repository with the following command:

curl -sL https://rpm.nodesource.com/setup_16.x | bash -

Next, install the Node.js package with the following command:

dnf install nodejs -y

After the installation, verify the Node.js version using the following command:

node -v
You will get the Node.js version in the following output:
v16.17.0

Step 3 – Create a Node.js Application

First, create a directory to store your Node.js application with the following command:

mkdir nodeapp

Next, navigate to the create directory with the following command:

cd nodeapp

Next, create an app.js file:

nano app.js

Add the following code:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Welcome to Node.js Server');
}).listen(8000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8000/');

Save and close the file, then run your Node.js application with the following command:

node app.js

You will get the following output:

Server running at http://127.0.0.1:8000/

Press CTRL+C to stop the Node.js application.

Step 4 – Install PM2 to Manage Node.js Service

Next, you will need to install PM2 to manage the Node.js service. You can install it with the following command:

npm i -g pm2

Once PM2 is installed, start the Node.js application with the following command:

pm2 start app.js

Next, enable your application to start at system reboot:

pm2 startup

You can list all your application services with the following command:

pm2 list

Step 4 – Configure Apache as a Reverse Proxy for Node.js

It is a good idea to install and configure Apache as a reverse proxy for the Node.js applications. First, install the Apache package using the following command:

dnf install httpd -y

Next, create an Apache virtual host configuration file for the Node.js application:

nano /etc/httpd/conf.d/node.exampledomain.conf

Add the following configurations:

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName node.exampledomain.com
        ErrorLog /var/log/httpd/error.log
        CustomLog /var/log/httpd/access.log combined
        ProxyRequests On
        ProxyPass / http://localhost:8000
        ProxyPassReverse / http://localhost:8000
</VirtualHost>

Save and close the file, then start and enable the Apache service with the following command:

systemctl start httpd
systemctl enable httpd

Step 5 – Access Node.js Application

At this point, Apache is configured as a reverse proxy for the Node.js application. You can now access the Node.js application using the URL http://node.exampledomain.com. You should see your Node.js application page:
Node.js application page

Conclusion

Congratulations! You have successfully installed and configured Apache as a reverse proxy for the Node.js application. This will help you to increase your Node.js application performance. You can now host the Node.js applications on dedicated hosting from Atlantic.Net!

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year