Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save riodw/f75828636dad4225a3c2419dc8e3790d to your computer and use it in GitHub Desktop.
Save riodw/f75828636dad4225a3c2419dc8e3790d to your computer and use it in GitHub Desktop.

How to set up a new Production Server running Node on Ubuntu

START

Always start with an Update:

$ sudo apt-get update

Create folder for project:

$ mkdir production
$ cd production/

Install Node and GIT

$ sudo apt-get install git-all
$ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
$ sudo apt-get install nodejs
$ node -v
# Should be v6.9.1 or higher.
$ npm -v
# Should be 3.10.8 or higher.

GIT

New git environment:

$ git init
$ git remote add origin https://github.com/riodw/BloomBus-Server.git
$ git pull origin master
# see if all the files from GitHub have been downloaded
$ ls

Install all the Project dependencies

$ npm install
$ ls
# See if the folder "node_modules" has been created
$ npm ls | grep express
# Make sure Express is installed

MONGO DATABASE

Give the Bash Script file "mongod" execution permissions.

$ chmod a+x mongod

Install mongodb (not with npm because npm is weird about mongodb).

$ sudo apt-get install -y mongodb

Check to see if proccess "mongodb" is running - (press "q" to quit).

$ top

If it is, KILL it with the following command:

$ sudo killall mongod

Make sure proccess "mongodb" is NO LONGER running - (press "q" to quit).

$ top

Create required folder for MongoDB - (where the database is stored).

$ sudo mkdir -p /data/db/

START MONGODB

$ sudo mongod
# WAIT FOR MONGODB TO PRINT 'waiting for connections on port 27017'

NOW STOP MONGODB - "Ctrl-C"

$ sudo npm install pm2 -g

Make PM2 Persistent

$ pm2 startup ubuntu
# This next line will be provided by PM2: LOOKFOR IT (should be printed after executing the previous command)
$ sudo su -c "env PATH=$PATH:/opt/node/bin pm2 startup ubuntu -u bloombus --hp /home/bloombus"
$ pm2 save

Let PM2 run MongoDB: Need to make a Shell Script that starts 'mongod' - (a bash file that PM2 can run).

$ touch mongod.sh
$ nano mongod.sh

Paste these lines in the nano IDE - (All 2 lines, THE WHOLE THING):

#!/bin/bash
mongod
  • Hit "Ctrl-X"
  • Hit "y"
  • Hit "Enter" Make sure the 2 lines are saved in the file
cat mongod.sh

Port forwarding

$ sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

START THE SERVER

FIRST START THE DATABASE...

pm2 start mongod.sh

THEN.... Start the server - remember to pass the command line arg "real". P.S. Command line args go after the "--" if there are no args then leave out the "--"

pm2 start server.js -- real
# START
sudo apt-get update # always start with an Update
# CREATE FOLDER FOR PROJECT
mkdir production
cd production/
# GIT
git init # new git environment
git remote add origin git@github.com:riodw/BloomBus-Server.git
git pull origin master
ls # see if all the files from GitHub have been downloaded
# INSTALL ALL THE PROJECT DEPENDENCIES
npm install
ls # see if the folder "node_modules" has been created
# MONGO DATABASE
chmod a+x mongod # give the Bash Script file "mongod" execution permissions
sudo apt-get install -y mongodb # install mongodb (not with npm because npm is weird about mongodb)
top # check to see if proccess "mongodb" is running, if it is KILL it with the following command - P.S. press "q" to quit
sudo service mongodb stop
top # press "q" to quit
sudo mkdir -p /data/db/ # where the database is stored - this is a required folder for MongoDB
sudo chown `id -u` /data/db # change permissions on the folder so MongoDB can access it
# START MONGODB
mongod # WAIT FOR MONGODB TO PRINT 'waiting for connections on port 27017'
# NOW STOP MONGODB - <Ctrl-C>
# PM2
sudo npm install pm2 -g
pm2 startup ubuntu
sudo su -c "env PATH=$PATH:/opt/node/bin pm2 startup ubuntu -u bloombus --hp /home/bloombus" # This line will be provided by PM2: LOOK FOR IT
pm2 save
# LET PM2 RUN MONGODB
# Need to make a Shell Script that starts 'mongod' file that PM2 can run
touch mongod.sh
nano mongod.sh
# Paste these lines in the nano IDE - (All 2 lines, THE WHOLE THING):
#!/bin/bash
mongod
# Hit <Ctrl-X>
# Hit <y>
# Hit <Enter>
cat mongod.sh # Make sure the 2 lines are saved in the file
# START THE SERVER
pm2 start mongod.sh # FIRST START THE DATABASE... THEN!...
pm2 start server.js -- real # Start the server - remember to pass the command line arg "real". P.S. Command line args go after the "--" if there are no args then leave out the "--"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment