Compute Engine Google Cloud

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

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.

Some of the hosting platforms are listed below.

PlatformRAMSSDCost
Alibaba Cloud512 MB20 GB$2.50/mo
Cloudways – Managed Cloud1 GB25 GB$10/mo
Kinsta – Google Cloud Managed WP1 Website10 GB$30/mo

Prerequisites

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 https://deb.nodesource.com/setup_10.x | 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>
    ServerName domain.com
    ServerAlias www.domain.com

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

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 domain.com -d www.domain.com

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.

Conclusion

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

Write A Comment

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