Node.js is a JavaS­cript runtime en­vir­on­ment that allows you to easily create server-side ap­plic­a­tions. With the process manager PM2, you can set up a Node.js ap­plic­a­tion to run as a service on Ubuntu 24.04.

How to run Node.js scripts as a service

Although you can run scripts from the open-source JavaS­cript runtime en­vir­on­ment Node.js directly from the command line, managing them as a service with the PM2 process manager provides a much more robust setup. When running as a service, your scripts auto­mat­ic­ally restart if the server reboots or if the ap­plic­a­tion crashes.

PM2 is a process manager for Node.js with a variety of features that allow you to control and manage your Node.js scripts. Visit the official PM2 website for more in­form­a­tion on using PM2.

What are the re­quire­ments?

Before pro­ceed­ing with the steps to install Node.js with Apache and PM2, ensure that the following re­quire­ments are met:

  • A server running Linux (Ubuntu 24.04).
  • A func­tion­ing domain name pointing to the server.
  • A func­tion­al and op­er­a­tion­al Apache web server

How to install Node.js

To install Node.js, you will need the command-line tool curl. Update your server’s packages and install curl using the following commands:

sudo apt update
sudo apt install curl -y
bash

Download the personal package archive (PPA) for Node.js and add the PPA to your server’s package cache. The archive contains a more current version of Node.js than the Ubuntu re­pos­it­or­ies:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
bash

Next, install Node.js:

sudo apt install -y nodejs
bash

This will also auto­mat­ic­ally install npm.

Finally, install the build-essential package for npm. build-essential installs important developer tools like compilers and Make, which are needed so that Node.js can correctly compile and run npm modules with native ex­ten­sions.

sudo apt install -y build-essential
bash

How to create an example Node.js ap­plic­a­tion

For this example, we will first create a separate directory in the document root of your website to ac­com­mod­ate Node.js ap­plic­a­tions:

sudo mkdir /var/www/html/nodejs
bash

Create the file hello.js in this directory:

sudo nano /var/www/html/nodejs/hello.js
bash

Then insert the following example content into the file:

#!/usr/bin/env node
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World! Node.js is working correctly.\n');
}).listen(8080, '0.0.0.0');
console.log('Server running at http://127.0.0.1:8080/');
javas­cript

Save and exit the file, and make it ex­ecut­able with the following command:

sudo chmod 755 hello.js
bash

How to check Firewall

Ubuntu 24.04 may enable the ufw firewall by default. To directly test your Node.js ap­plic­a­tion, you need to ensure that port 8080 is open:

sudo ufw allow 8080/tcp
bash

This step is es­pe­cially important if you want to access your server directly via the IP address or a browser outside the server. If Node.js is provided solely through Apache and the proxy, this step is optional, as the proxy forwards the traffic.

How to install PM2

Use npm to install PM2 with the following command:

sudo npm install -g pm2
bash

Start the newly created example script hello.js with the command:

sudo pm2 start hello.js
bash

As root, add PM2 to the startup scripts so that it auto­mat­ic­ally restarts when the server is rebooted, and save the currently running processes:

sudo pm2 startup systemd
sudo pm2 save
bash

How to configure Apache

To access the Node.js script from the web, install the Apache modules proxy and proxy_http with the commands:

sudo a2enmod proxy
sudo a2enmod proxy_http
bash

After com­plet­ing the in­stall­a­tion, restart Apache to apply the changes:

sudo systemctl restart apache2
bash

Next, you need to adjust the Apache proxy con­fig­ur­a­tions. Insert the following dir­ect­ives into the Vir­tu­al­Host command block in the main con­fig­ur­a­tion file of the Apache server.

You can usually find this Apache con­fig­ur­a­tion file at the file path /etc/apache2/sites-available/example.com.conf on Ubuntu.

Note

The location and filename of a website’s Apache con­fig­ur­a­tion file can vary.

Edit this file using an editor of your choice, for example, with the command:

sudo nano /etc/apache2/sites-available/example.com.conf
bash

Scroll through the file until you find the Vir­tu­al­Host command block, which will look like this:

<VirtualHost *:80>
ServerName example.com
<Directory "/var/www/example.com/html">
AllowOverride All
</Directory>
</VirtualHost>

Add the following to the Vir­tu­al­Host command block:

ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /nodejs>
ProxyPass http://127.0.0.1:8080
ProxyPassReverse http://127.0.0.1:8080
</Location>

Ensure that these lines are placed outside of directory command blocks. For example:

<VirtualHost *:80>
ServerName example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /nodejs>
ProxyPass http://127.0.0.1:8080
ProxyPassReverse http://127.0.0.1:8080
</Location>
<Directory "/var/www/example.com/html">
AllowOverride All
</Directory>
</VirtualHost>

Save and exit the file and then restart Apache to apply the changes:

sudo systemctl restart apache2
bash

After re­start­ing Apache, you can test the ap­plic­a­tion by viewing it in a browser. You should see the following message from the pre­vi­ously created test file:

Hello World! Node.js is working correctly.
Go to Main Menu