How to set up a Node.js app with Apache on Ubuntu 24.04
Node.js is a JavaScript runtime environment that allows you to easily create server-side applications. With the process manager PM2, you can set up a Node.js application 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 JavaScript runtime environment 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 automatically restart if the server reboots or if the application 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 information on using PM2.
What are the requirements?
Before proceeding with the steps to install Node.js with Apache and PM2, ensure that the following requirements are met:
- A server running Linux (Ubuntu 24.04).
- A functioning domain name pointing to the server.
- A functional and operational 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 -ybashDownload 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 repositories:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -bashNext, install Node.js:
sudo apt install -y nodejsbashThis will also automatically 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 extensions.
sudo apt install -y build-essentialbashHow to create an example Node.js application
For this example, we will first create a separate directory in the document root of your website to accommodate Node.js applications:
sudo mkdir /var/www/html/nodejsbashCreate the file hello.js in this directory:
sudo nano /var/www/html/nodejs/hello.jsbashThen 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/');javascriptSave and exit the file, and make it executable with the following command:
sudo chmod 755 hello.jsbashHow to check Firewall
Ubuntu 24.04 may enable the ufw firewall by default. To directly test your Node.js application, you need to ensure that port 8080 is open:
sudo ufw allow 8080/tcpbashThis step is especially 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 pm2bashStart the newly created example script hello.js with the command:
sudo pm2 start hello.jsbashAs root, add PM2 to the startup scripts so that it automatically restarts when the server is rebooted, and save the currently running processes:
sudo pm2 startup systemd
sudo pm2 savebash
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_httpbashAfter completing the installation, restart Apache to apply the changes:
sudo systemctl restart apache2bashNext, you need to adjust the Apache proxy configurations. Insert the following directives into the VirtualHost command block in the main configuration file of the Apache server.
You can usually find this Apache configuration file at the file path /etc/apache2/sites-available/example.com.conf on Ubuntu.
The location and filename of a website’s Apache configuration 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.confbashScroll through the file until you find the VirtualHost 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 VirtualHost 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 apache2bashAfter restarting Apache, you can test the application by viewing it in a browser. You should see the following message from the previously created test file:
Hello World! Node.js is working correctly.
