How to Host a Node.js Website With Apache on Ubuntu 20.04

In this guide, we’re using Ubuntu 20.04 and Apache to host a website built with Node.js. We will go through the steps of installing Apache and Node, and applying the necessary reverse proxy configuration. We’ll also enable SSL encryption for the site.

1. Let’s get started by updating Ubuntu and installing Node.js, npm, and Apache.

sudo apt update && sudo apt -y upgrade
sudo apt install nodejs npm apache2

2. Enable the following Apache modules, which are needed to run a reverse proxy server in Apache.

sudo a2enmod proxy proxy_http rewrite headers expires

Don’t worry about restarting Apache just yet.

3. Use nano or some other text editor to create a new virtual host for your domain and paste the following template. Replace the example domain with your own domain (or IP address if you’re not using a domain name). Note that this template is using port 3000, the default for Node. If you’re planning to use some other port, be sure to switch that as well.

sudo nano /etc/apache2/sites-available/
<VirtualHost *:80>

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted

    ProxyPass /
    ProxyPassReverse /

Save your changes to this file and close it.

4. Enable the new site and disable the default Apache site. Then, restart Apache.

sudo a2dissite 000-default
sudo a2ensite
sudo systemctl restart apache2

5. At this point, you should be able to view your website and see it running the Node.js app you’ve created. If you haven’t yet created one, you can use the following “hello world” script just for testing.

Create the file.

nano ~/app.js

Paste the “hello world” code.

const http = require('http');

const hostname = '';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');

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

Run the app you’ve just created.

node ~/app.js
Server running at

Open a browser and go to your domain name (or IP address) to see the “Hello World” text.

6. This next part is optional. We’re now going to use Let’s Encrypt to get a free SSL certificate for our site to use HTTPS only. Start by installing certbot.

sudo apt install certbot

7. Run certbot and answer the few questions it prompts you with.

sudo certbot --apache

When prompted, be sure to have certbot configure redirection so all HTTP requests go to HTTPS.


You should now have your Node.js app online and accessible via HTTPS. This was made possible on Ubuntu 20.04 by installing Apache and configuring a reverse proxy server to port 3000 (on which Node is running).

Bonus tip: Put your Node app in the background by either using the screen command or putting & at the end of the node command.

screen -S nodejs
Ctrl+A, D


~/app.js &

Leave a Reply

Your email address will not be published. Required fields are marked *