How To Deploy Node.js App on Ubuntu with Forever and Nginx

Hello, Developers finished working on your new awesome project using Node.js and Now, you want to deploy it on your favorite cloud Services provider like Digital Ocean or AWS etc then Don’t worry fellas this How to guide will make your work lot easier and less complex.You’ll learn How to Deploy Your Node.JS Application on UBUNTU with NGINX and Forever NPM Module on Digital Ocean or AWS EC2 or any cloud service. It’s very easy to deploy Node.js applications in the local development environment, and it’s become little bit complex process when you need to deploy it on the server because of following reasons:

  • When it comes to Linux, security is first, so your Nodejs application cant bind to ports which are less than 1024 and so 80 and 443 ports without having root access, which is considered as dangerous by Linux(here Ubuntu).
  • When your Node app crashes due to some unhandled exception or error, you need to manually restart your server to start the app until your app will be offline or unavailable.
  • Your Linux server will reboot in time, so you need to place the mechanism in place to restart your app with Ubuntu reboots.
  • But these complexities can be easily solved, all thanks evergrowing nodejs community.

[su_box title=”Note:” style=”bubbles” box_color=”#36bbea”]This guide is Tested with Ubuntu Server 14.04 LTS.[/su_box]

First, you need to create a system user,

  • To be on safer side we can’t use ubuntu account with root privileges, so we will create a user called nodeuser
  • This is to avoid some kind of remote code execution vulnerability found in your application.
sudo useradd --system --create-home nodeuser

This will also create home directory for new user in /home/nodeuser

Article Contents

Installing Node.js on Ubuntu:

Transferring your Nodejs app to Ubuntu from GitHub:

Now you have installed Nodejs on your machine, so let’s transfer your Node app from Github to Ubuntu.

  • Install git using following code
    Sudo apt-get install git
    
  •  Now switch nodeuser and navigate to nodeuser’s home directory
    sudo su nodeuser
    cd ~
  • create a folder inside home directory according to your projects name or simply naming it by app folder in /home/nodeuser/
    //creating folder called app
    mkdir app
    
  • Now clone your Nodejs app from GitHub to app folder
    git clone https://github.com/vithalreddy/skillsMeterified.git app
    
See also  How-To find the version of an installed NodeJS or NPM Package

Now that you’re ready with your project, let’s start on deployment:

How to Deploy Nodejs APP On Ubuntu with Forever NPM Module

  • Install all your Nodejs Dependencies by npm install command inside app folder of your home directory.
  • Make sure that your node app will listen HTTP requests on the port greater than 1024 and developer’s standard port are 3000, 8080  but you can choose any port.
  • To install Forever NPM Module run the following command with global -g flag
    sudo npm install -g forever
    
  • Forever is a simple CLI tool for ensuring that a given script runs continuously (i.e. forever) http://github.com/foreverjs/forever
  • Install Nginx to power your nodejs app, which is high-performance, open source web server.
  • Nginx will listen on port 80 on behalf of Nodejs app and forward all incoming requests directly to your app on port 3000(Whatever port you have set).
  • Edit default file of  /etc/nginx/sites-available the directory with the following code using  nano default command. To start Nginx  server service nginx start  and if want restart nginx after editing the default file use nginx -s reload
    server {
        listen 80;
        
        server_name yoursitename.com;
    
        location / {
            root /home/nodeuser/app   
            proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

     

  • Finally, to run your app forever 🙂 use the following command (replace app.js with your app’s entry point)
    sudo su nodeuser
    cd ~/app
    forever start app.js
  • Now your Nodejs app will run forever. but if your server reboots, do you think your app will be running, yes you’re right, No it will not be running, to fix that do the following things.
  • This is very easy to do via a @reboot entry in the nodeuser user’s crontab.The Linux (Ubuntu) crontab is a text file with a list of commands meant to be run at specified times like on reboot.let’s do that:
    crontab -e
  • To add our command at the bottom of crontab  file
    @reboot forever start /home/nodeuser/app/app.js
  • This file tells Linux to run forever start command on every reboot, to save file Press CTRL+O on nano editor.
  • To make sure your Nodejs app starts on reboot use sudo reboot on ubuntu account and verify.

Tha’s it, folks, you’ve successfully deployed your Nodejs app on Ubuntu with Nginx and Forever NPM Module and Thanks for reading the Guide :).

[yasr_overall_rating]

Leave a Comment