Atlantic.Net Blog

How to Configure Reverse Proxy for Node.js Application Using Apache on Ubuntu

Node.js is a free, open-source, and popular JavaScript runtime environment built on Chrome’s V8 JavaScript engine. It’s used for traditional websites and back-end API services.

Apache is an open-source and one of the most popular web servers in the world. You can also use Apache as a frontend proxy server for backend applications including, Node.js.

In this post, we will show you how to configure Apache as a reverse proxy for the Node.js application on Ubuntu. This procedure is compatible with Ubuntu 20.04 and Ubuntu 22.04.

Step 1 – Install Nodejs

First, you will need to install the required dependencies to your system. You can install them with the following command:

apt-get install curl gnupg2 build-essential wget -y

Once all the packages are installed, run the following command to install Node.js:

apt-get install nodejs npm -y

Once the Node.js is installed, verify the Node.js version using the command below:

node -v

You should see the following output:

v12.21.0

Step 2 – Create a Nodejs Application

Next, you will need to create a Node.js application to your server.

First, create a directory for your application with the following command:

mkdir project

Next, change the directory to project and create a nodeapp.js file:

cd project
nano nodeapp.js

Add the following lines:

const http = require('http');
const hostname = 'localhost';
const port = 8000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Welcome to Node.js!\n');
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Save and close the file, then run the application with the following command:

node nodeapp.js

If everything is fine, you should get the following output:

Server running at http://localhost:8000/

Press CTRL+C to stop the application.

Step 3 – Create a Systemd Service File for Nodejs

Next, you will need to create a systemd service file to manage the Node.js service. You can create it with the following command:

nano /lib/systemd/system/nodeapp.service

Add the following lines:

[Unit]
Description=Node.js Application
After=syslog.target network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/project
Environment=NODE_ENV=production
ExecStart=/usr/bin/node nodeapp.js

Restart=always

[Install]
WantedBy=multi-user.target

Save the file when you are finished, then reload the systemd daemon to apply the configuration changes:

systemctl daemon-reload

Next, start the nodeapp service and enable it so that it can start automatically at system reboot:

systemctl start nodeapp
systemctl enable nodeapp

Next, verify the status of the nodeapp service by running the following command:

systemctl status nodeapp

You should see the following output:

● nodeapp.service - Node.js Application
     Loaded: loaded (/lib/systemd/system/nodeapp.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-03-24 11:33:12 UTC; 7s ago
   Main PID: 10551 (node)
      Tasks: 11 (limit: 2353)
     Memory: 7.6M
     CGroup: /system.slice/nodeapp.service
             └─10551 /usr/bin/node nodeapp.js

Mar 24 11:33:12 ubuntu2004 systemd[1]: Started Node.js Application.
Mar 24 11:33:12 ubuntu2004 node[10551]: Server running at http://localhost:8000/

Step 4 – Configure Apache as a Reverse Proxy

First, install the Apache package with the following command:

apt-get install apache2 -y

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

nano /etc/apache2/sites-available/nodeapp.conf

Add the following lines:

<VirtualHost *:80>
    ServerName your-server-ip

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/
</VirtualHost>

Save and close the file, then enable the virtual host configuration file:

a2ensite nodeapp.conf

Next, disable the default virtual host configuration file and enable the required proxy modules with the following command:

a2dissite 000-default 
a2enmod proxy proxy_http rewrite headers expires

Finally, restart the Apache service to apply the changes:

systemctl restart apache2

Step 5 – Access Nodejs Application

Now, open your web browser and access your Node.js application using the URL http://your-server-ip. You should see your application in the following page:

Conclusion

In the above guide, you learned how to configure Apache as a reverse proxy for the Node.js application. This will help you to host your Node.js application from the local system to the production environment – try it on your dedicated hosting account from Atlantic.Net.

Learn more about our dedicated servers.

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