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 22.04 and Ubuntu 24.04.
Step 1 – Install Node.js
First, install the necessary dependencies using the following command.
apt-get install -y ca-certificates curl gnupg
Next, download the Node.js GPG key.
mkdir -p /etc/apt/keyrings curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
Next, add the NodeSource repo to the APT source list.
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
Then, update the repository index and install Node.js with the following command.
apt update apt-get install -y nodejs
Next, verify the Node.js version using the following command.
node -v
Output.
v22.15.1
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 (/usr/lib/systemd/system/nodeapp.service; disabled; preset: enabled) Active: active (running) since Fri 2025-05-16 07:10:05 UTC; 4s ago Main PID: 79449 (node) Tasks: 11 (limit: 629145) Memory: 8.3M (peak: 9.1M) CPU: 44ms CGroup: /system.slice/nodeapp.service └─79449 /usr/bin/node nodeapp.js May 16 07:10:05 cyber.example.com systemd[1]: Started nodeapp.service - Node.js Application. May 16 07:10:05 cyber.example.com node[79449]: 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.