How To Setup Node.js Application On Digital Ocean

How to Integrate Google+ authentication in Ionic V1
September 7, 2018
How To Create Bootable Pen Drive with Command Prompt
September 13, 2018

How To Setup Node.js Application On Digital Ocean

If you need to setup node.js application on digital ocean this tutorial assist you.

  • Create or login to your digital ocean account.
  • Create new droplets.
  • Click on “Create” button then droplets.
  • Select OS type, data center and other options.
  • Then click on “create” button.
  • Next, access digital ocean via terminal with root user, with below command.
 ssh root@167.99.182.163 

Lets install all packages required to run MEAN application

  • Install build-essential
 sudo apt-get -y install build-essential 
  • Now Install MongoDB
  • In order to install mongoDB, first of all import public key
 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 
  • Create list file for MongoDB
  

echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

  • Reload local package database.
 sudo apt-get update
  • Now Install the latest stable version of MongoDB.
 sudo apt-get install -y mongodb-org 
  • Now Install the latest stable version of MongoDB.
 sudo apt-get install -y mongodb-org 
  • Check the mongoDB status with following command.
 sudo service mongod status 
  • You will see such type of response in latest ubuntu version 16.04
  • If you want to install ubuntu 16.04 then use following command.
 sudo apt-get install -y --allow-unauthenticated mongodb-org 
  • In order to properly launch MongoDB as a service on Ubuntu 16.04, we additionally need to create a unit file describing the service.
  • A unit file tells the system – a way to manage a resource. The most common unit type is a service, which determines the way to begin or stop the service.
 sudo nano /etc/systemd/system/mongodb.service 
  • Paste the following code –

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

  • Exit file with Ctrl + X
  • It will ask to save modified file, enter Y.
  • then press enter
  • And run commands
 sudo systemctl start mongodb 
 sudo systemctl status mongodb 
 sudo systemctl enable mongodb 
  • You can verify by connecting to mongodb by typing mongo.
 mongo 
  • Install Node.js
 curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - 
 sudo apt-get install -y nodejs 
node -v 
 == 
  • Now to clone code on the server install git.
  • To run node app, install forever.
 sudo npm install -g forever 
  • Forever commands :
  • To list forever running instances
 sudo forever list 
  • To start server
 sudo forever start filename 
  • To stop server with given instance number.
 sudo forever stop instance_number 

Leave a Reply