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.

Step 1 – 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, list all available Node.js versions using the following command.

dnf module list --all nodejs

You will see the following output.

Name      Stream    Profiles                                Summary             
nodejs    10 [d]    common [d], development, minimal, s2i   Javascript runtime  
nodejs    12        common [d], development, minimal, s2i   Javascript runtime  
nodejs    14        common [d], development, minimal, s2i   Javascript runtime  
nodejs    16        common [d], development, minimal, s2i   Javascript runtime  
nodejs    18        common [d], development, minimal, s2i   Javascript runtime  
nodejs    20        common, development, minimal, s2i       Javascript runtime

Next, enable the Node.js version 20 repo using the following command.

dnf module enable nodejs:20

Next, install the Node.js by running the following command:

dnf install nodejs -y

Once Node.js is installed, you can verify the Node.js version using the following command:

node --version

You will get the following output:

v20.0.0

Step 2 – 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 3 – 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!