Skip to content

Instantly share code, notes, and snippets.

@desaiuditd
Last active November 8, 2017 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desaiuditd/4810a0615f7861313fac93bbb4edc481 to your computer and use it in GitHub Desktop.
Save desaiuditd/4810a0615f7861313fac93bbb4edc481 to your computer and use it in GitHub Desktop.
node+express+mongo deploy steps

Install EasyEngine (HTTP Proxy Server)

wget -qO ee rt.cx/ee && sudo bash ee

Install NVM

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash

Move nvm source commands to the top in .bashrc file. So that it works from deploy pipeline non-interactive shell.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Install Node

nvm install v8.1.4

Install pm2

npm install -g pm2@latest

Generate SSH Key

ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub

Copy the SSH key and add it to the deploy keys of the repository.

So that the server can pull latest changes while deploying.

Then run below command to save the imprint of Github server on production server.

ssh git@github.com -T

Install MongoDB

Import the public key

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

Create a list file for MongoDB For Ubuntu 16.04

echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

Reload local package database

sudo apt-get update

Install the MongoDB packages

sudo apt-get install -y mongodb-org

Start MongoDB

sudo service mongod start

Setup a user for MongoDB

Run following command to start the mongo terminal.

mongo

Run following commands into the Mongo terminal.

use your_db
db.createUser({
    user: 'your_username',
    pwd: 'your_password',
    roles: [{ role: 'readWrite', db:'your_db'}]
})

The above user will have read and write access for the given database.

Enable security authorization & open MongoDB access from outside

Edit your MongoDB config file.

sudo vim /etc/mongod.conf

Look for the net line and comment out the bindIp line under it, which is currently limiting MongoDB connections to localhost

Warning: Do NOT comment out the bindIp line without enabling authorization. Otherwise you will be opening up the whole internet to have full admin access to all mongo databases on your MongoDB server!

# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1  <- comment out this line

Scroll down to the #security: section.

Add the following line. Make sure to un-comment the security: line.

security:
  authorization: 'enabled'

Save the file. And done!

Restart the mongodb service.

  • sudo service mongod restart

Now You should be able to connect to the MongoDB with the above username and password.

Setup new user at later stage / Perform Admin tasks on MongoDB

  • Disable the security temoporarily in the MongoDB config file mentioned above.
    • Comment out the security section.
    • Save the file.
  • Restart the mongodb service
    • sudo service mongod restart
  • Add new user just like we did it earlier from the above commands.
  • Remove old user with db.removeUser('username')
  • Or perform any other admin tasks.
  • Enable the security again once you are done.
  • Restart the mongodb service

Additional References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment