Saturday, January 9, 2016

Setting up a new NodeJS Server

The following is on Ubuntu, you may have slightly different default location or bash script names to edit on different Linux disros.

Install Node Version Manager (NVM):
First make sure the prerequisites are ready:

$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev
Now for the actual install command:
 $ curl -o- | bash 

Then I did some additional moving around. It automatically puts things in the users folder. I wanted them globally:
$ sudo mkdir /etc/nvm
$ sudo mv ~/.nvm/* /etc/nvm/
Then change the initialization so it's for all users:

$ sudo vim /etc/bash.bashrc
Add this to the bottom:
export NVM_DIR="/etc/nvm"
[ -s "$NVM_DIR/" ] && . "$NVM_DIR/"  # This loads nvm

Then remove/comment-out the old script location in your user bash:

$ vim ~/.bashrc
Remove or comment-out these two lines - should be at the very bottom:
#export NVM_DIR="/home/ubuntu/.nvm"
#[ -s "$NVM_DIR/" ] && . "$NVM_DIR/"  # This loads nvm

2. Logout and log back in again, then test if it's working:

$ nvm  [enter]
That should return a list of possible commands + options for using NVM, like this  [truncated]:

Node Version Manager

Note: refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

  nvm help                                  Show this message
  nvm --version                             Print out the latest released version of nvm
  nvm install [-s]                 Download and install a , [-s] from source. Uses .nvmrc if available

3. Now install the latest stable version of node:

$ nvm install stable

Then check the install:

$ nvm ls
That should display something like this:
->       v5.4.0
node -
> stable (-> v5.4.0) (default)
stable -
> 5.4 (-> v5.4.0) (default)
iojs -
> N/A (default)

4. Then set the default engine so it works forever (not sure why this isn't set my default if not set):

$ nvm alias default stable

Check it:

$ nvm ls
That should display something like this:
->       v5.4.0
default -
> stable (-> v5.4.0)
node -
> stable (-> v5.4.0) (default)
stable -
> 5.4 (-> v5.4.0) (default)
iojs -
> N/A (default)

Install Git:

1. Command line with apt-get:

$ sudo apt-get install git

2. Now make the command prompt show the current branch name whenever you are in a git repo directory:

$ sudo vim /etc/profile

Then add the following to the end of the file:

#Show the Current GIT Branch -- if GIT is initialized
function parse_git_branch {
  git branch --no-color 2
> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
function proml {
    local        BLUE="\[\033[0;34m\]"
    local         RED="\[\033[0;31m\]"
    local   LIGHT_RED="\[\033[1;31m\]"
    local       GREEN="\[\033[0;32m\]"
    local LIGHT_GREEN="\[\033[1;32m\]"
    local       WHITE="\[\033[1;37m\]"
    local  LIGHT_GRAY="\[\033[0;37m\]"

    PS1="\u@\h \$(date +%b%d@%I:%M:%S%p) \w$RED\$(parse_git_branch)$GREEN\$ "
> '
    PS4='+ '

mesg n

Set up your nodejs website:

1. Make the /var/www/ web directory with proper owners and permissions

$ cd /var
$ sudo mkdir www
$ sudo chown -R [your-username]:[your-username] www

2. Create a folder for each website:

$ mkdir [website-name]
$ cd [website-name]

3. Clone your git repo into the current directory (don't forget the period at the end)

$ git clone .

That's it!

No comments: