Learn everything about JavaScript & supporting libraries/frameworks

NodeJS website host on DigitalOcean Ubuntu Apache server

All Articles New Article

How to set up a node js application for production on ubuntu

Get Free DigitalOcean $100 Credit 👨‍💻 Grab it now. Deploy faster & scale easier with an application server that saves your team time & money.

Apache Server Configuration

Install Apache

sudo apt update && sudo apt install apache2

Configure Firewall

sudo ufw allow OpenSSH

sudo ufw allow in "Apache Full"

sudo ufw enable

sudo ufw status

ufw app list

ufw allow 7000 Open any port number example: 7000 open outsite localhost for more info about ufw firewall

Learn Ubuntu basic comands

cd .. Goto previous directory cd / Goto root directory pwd Print work directory ls Show current directory info md or mkdir make folder/directory apt-get update update new essential sudo used this if you not root user ssh [email protected] example: ssh [email protected] open server using CMD/powershel using password: then enter your password

Step 1: Install Node.js

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

Another way install NodeJS

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 / Goto root sudo nano server.js or vim server.js

Insert the following code into the file

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

    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}/`);

Paste and Save the file and exit.

vim: esc :wq enter or nano: ctrl+x y enter

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

Server continue on without downtime

pm2 startup systemd

Now your Node.js application is running in the background

Remove default Apache configuration.

sudo a2dissite 000-default

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

    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

Start an application

ufw allow 7000 open or allow access for outsite

ufw reload reload ufw rule

Now you can restart Apache.

sudo service apache2 restart

pm2 start server.js

Check your server active or not

curl http://localhost:7000

To list all running applications:

pm2 list

Managing apps is straightforward:

pm2 stop     <app_name|namespace|id|'all'|json_conf>
pm2 restart  <app_name|namespace|id|'all'|json_conf>
pm2 delete   <app_name|namespace|id|'all'|json_conf>

To have more details on a specific application:

pm2 describe <id|app_name>

To monitor logs, custom metrics, application information:

pm2 monit

Starting a Node.js application in cluster mode that will leverage all CPUs available:

pm2 start api.js -i <processes>

Hot Reload allows to update an application without any downtime:

pm2 reload all

Freeze your process list across server restart

pm2 save

Updating PM2

# Install latest PM2 version
npm install [email protected] -g
# Save process list, exit old PM2 & restore all processes
pm2 update

PM2 more commands

pm2 examples

Step 4 — Installing MongoDB

sudo apt update sudo apt install mongodb-org or sudo apt install mongodb

Open mongoDB type: mongo

Uninstall NodeJS from Ubuntu apache server

sudo apt-get remove nodejs

sudo apt-get purge nodejs

All Articles New Article
Learn everything about JavaScript & supporting libraries/frameworks