Skip to content

Instantly share code, notes, and snippets.

@servercharlie
Last active October 7, 2019 16:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save servercharlie/9a7e0d0e1645b4c6fbfe5de566fcf1ca to your computer and use it in GitHub Desktop.
Save servercharlie/9a7e0d0e1645b4c6fbfe5de566fcf1ca to your computer and use it in GitHub Desktop.
Setting up NodeJS w/ NPM + PM2 on Ubuntu 16.10 (Yakkety Yak!)
> SSH Login to your home directory.
> Get APT updates & upgrades.
sudo apt-get update
sudo apt-get upgrade
> NodeJS & NPM
> https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions
> Installs the latest NodeJS & NPM, including the essential build tools (used by most packages).
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential
> Done.
> NPM Permissions Tweaks
> @ https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-2-change-npms-default-directory-to-another-directory
> Prevents some fuck-ups.
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
> Done.
> NPM Global Packages Tweaks
> Prevents more fuck-ups w/ globally-installed packages.
npm get prefix
> Copy the output you get from 'npm get prefix'
sudo nano ~/.profile
> scroll down, find PATH, append :ThePrefixYouGotAbove/bin
> ie: if 'npm get prefix' gives me '/home/YourUser/.npm-global'
> and my PATH is... PATH="$HOME/bin:$HOME/.local/bin:$PATH"
> it should become: PATH="$HOME/bin:$HOME/.local/bin:$PATH:/home/YourUser/.npm-global/bin"
> CTRL + X + Y (saves it)
source ~/.profile
> Done.
> Install PM2 (Optional)
npm install pm2 -g
> Should run smoothly if you followed all of the above steps.
@servercharlie
Copy link
Author

servercharlie commented Mar 10, 2017

Allowing user to do sudo pm2 :

First get your npm directory:

npm get prefix

For example, you get

/home/azureuser/.npm-global

Now you add /bin to it:

/home/azureuser/.npm-global/bin

That will be our NPM_BIN_PATH.

Now, login as root

sudo su

Then open / edit the sudoers file:

visudo

Change the following:

Change

Defaults        env_reset

To

Defaults        !env_reset

Then at

Defaults        secure_path="OTHER_SECURE_PATH_ENTRIES_GO_HERE"

Append your NPM_BIN_PATH and A COLON

In our example that will be: /home/azureuser/.npm-global/bin:

So it should look like:

Defaults        secure_path="/home/azureuser/.npm-global/bin:OTHER_SECURE_PATH_ENTRIES_GO_HERE"

CTRL + X then Y then ENTER to save it.
Done. you can now run sudo pm2 as a user.


Allowing root to run pm2 :

Now open / edit your etc/environment file:

sudo nano /etc/environment

At

PATH="..."

It should look like:

PATH="/home/azureuser/.npm-global/bin:OTHER_PATH_ENTRIES_GO_HERE"

CTRL + X then Y then ENTER to save it.
Done. you can now run pm2 as the root.


@servercharlie
Copy link
Author

servercharlie commented Apr 7, 2017

Concatenated Commands.

NodeJS:
sudo apt-get update && sudo apt-get upgrade -y && curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs && sudo apt-get install -y build-essential

NPM Globals Tweaks:
mkdir ~/.npm-global && npm config set prefix '~/.npm-global' && sudo chown -R $(whoami) $(npm config get prefix)/lib/node_modules; sudo chown -R $(whoami) $(npm config get prefix)/bin; sudo chown -R $(whoami) $(npm config get prefix)/share; printf "\nPATH=$(npm config get prefix)/bin:$PATH\nexport PATH" >> ~/.bashrc && source ~/.bashrc

PM2 w/ Authbinded Port 80:
npm install pm2@latest -g && sudo apt-get install authbind && sudo touch /etc/authbind/byport/80 && sudo chown $(whoami) /etc/authbind/byport/80 && sudo chmod 755 /etc/authbind/byport/80 && authbind --deep pm2 update && printf "\nalias pm2=\'echo -e \"\t Executing Authbinded PM2\" && authbind --deep pm2\'" >> ~/.bashrc && source ~/.bashrc

Authbind Port 443:
sudo touch /etc/authbind/byport/443 && sudo chown $(whoami) /etc/authbind/byport/443 && sudo chmod 755 /etc/authbind/byport/443

A; B    Run A and then B, regardless of success of A
A && B  Run B if A succeeded
A || B  Run B if A failed
A &     Run A in background.

@alan345
Copy link

alan345 commented Sep 1, 2017

THanks !! it works

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