# 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](https://www.ionos.com/digitalguide/server/know-how/what-is-open-source/) JavaScript runtime environment [Node.js](https://www.ionos.com/digitalguide/websites/web-development/introduction-to-nodejs/) 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](https://pm2.keymetrics.io/ "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](https://www.ionos.com/digitalguide/server/know-how/ubuntu-the-linux-system-for-everyone/) 24.04).
- A functioning **domain name** pointing to the server.
- A functional and operational [Apache web server](https://www.ionos.com/digitalguide/server/configuration/how-to-install-an-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:

```bash
sudo apt update
sudo apt install curl -y
```

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 repositories:

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

Next, install Node.js:

```bash
sudo apt install -y nodejs
```

This 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.

```bash
sudo apt install -y build-essential
```

## How 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:

```bash
sudo mkdir /var/www/html/nodejs
```

Create the file *hello.js* in this directory:

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

Then insert the following example content into the file:

```javascript
#!/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/');
```

Save and exit the file, and make it **executable** with the following command:

```bash
sudo chmod 755 hello.js
```

## How 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:

```bash
sudo ufw allow 8080/tcp
```

This step is especially important if you want to access your server directly via the [IP address](https://www.ionos.com/digitalguide/server/know-how/what-is-an-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](https://www.ionos.com/digitalguide/server/know-how/what-is-a-proxy-server/) forwards the traffic.

## How to install PM2

Use `npm` to install PM2 with the following command:

```bash
sudo npm install -g pm2
```

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

```bash
sudo pm2 start hello.js
```

As `root`, **add PM2 to the startup scripts** so that it automatically restarts when the server is rebooted, and save the currently running processes:

```bash
sudo pm2 startup systemd
sudo pm2 save
```

## How to configure Apache

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

```bash
sudo a2enmod proxy
sudo a2enmod proxy_http
```

After completing the installation, restart Apache to apply the changes:

```bash
sudo systemctl restart apache2
```

Next, 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.

Note 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:

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

Scroll through the file until you find the VirtualHost command block, which will look like this:

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

Add the following to the VirtualHost command block:

```none
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:

```none
<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](https://www.ionos.com/digitalguide/server/configuration/restart-apache/) to apply the changes:

```bash
sudo systemctl restart apache2
```

After restarting Apache, you can test the application by viewing it in a [browser](https://www.ionos.com/digitalguide/websites/web-development/what-is-a-browser/). You should see the following message from the previously created test file:

```none
Hello World! Node.js is working correctly.
```


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/nodejs-for-a-website-with-apache-on-ubuntu/](https://www.ionos.com/digitalguide/websites/web-development/nodejs-for-a-website-with-apache-on-ubuntu/) for AI/LLM consumption.