Google Cloud Compute Engine

Setup Node.js with Apache Proxy on Ubuntu 18.04 for Production

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

Setup Node.js with Apache Proxy on Ubuntu 18.04 for production. This guide shows you how to install Node.js and configure it with Apache for production. You will also install and configure free Let’s Encrypt SSL and secure your installation.

This setup is tested on Google Cloud Platform which is running Ubuntu 18.04. This guide will also helps you set this up on AWS, DigitalOcean, or any other cloud or VPS or Dedicated Linux based servers.


If you plan to use Google Cloud for example you can follow this or you just need SSH access to your server with sudo privileges.

Step 1: Install Node.js

You can install the latest LTS release of Node.js

curl -sL | sudo -E bash -
sudo apt install -y nodejs

Once the installation is complete you can check the node.js version and npm version using the following commands

node -v
npm -v

Some packages requires compiling from source so you need to install the build-essential package.

sudo apt install build-essential

Step 2: Create a Node.js Application

Now you can create a demo Node.js app

cd ~/
sudo nano server.js

Insert the following code into the file

const http = require('http');
const hostname = 'localhost';
const port = 3000;

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 the file and exit.

Step 3: Install Process Manager

sudo npm install [email protected] -g

Now you can start your app using the process manager

pm2 start server.js

Now your Node.js application is running in the background

Step 4: Install Apache

sudo apt install apache2

Remove default Apache configuration.

sudo a2dissite 000-default

Step 5: Enable Apache modules

Now you need to enable Apache proxy module and rewrite modules.

sudo a2enmod proxy proxy_http rewrite headers expires

Step 6: Configure Apache Proxy

Once the above mentioned steps are done you can configure Apache to serve Node.js application.

Create a new virtual host configuration for Node.js.

sudo nano /etc/apache2/sites-available/domain.conf

Add the below configurations to the file.

<VirtualHost *:80>

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted

    ProxyPass /
    ProxyPassReverse /

Hit CTRL + X followed by Y and Enter to save and close the file.

Enable the newly created configuration.

sudo a2ensite domain.conf

Now you can restart Apache.

sudo service apache2 restart

Step 7: Test the setup

Now you can visit your domain name in browser, you should view the output of your server.js (Welcome to Node.js!).

Step 8: Install SSL Certificate

sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install python-certbot-apache

Now you have installed Certbot for Apache which automates the installation of Let’sEncrypt certificate.

You just need to execute the following command to install your certificate.

sudo certbot --apache -m your-email -d -d

When prompted you can choose to redirect all HTTP requests to HTTPS.

Once done when you visit your site you will be redirected with HTTPS.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system


Now you have learned to install Node.js, Apache and configure it to run on production.


  1. Mark Morris Reply

    Not sure, but why would your proxy configuration forward to port 8080 when your node server is listening on 3000?

  2. Thanks a lot. I tried resolving by myself for hours. This post made my day!

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.