The steps in this outline closely follow How To Set Up a Node.js Application for Production on Ubuntu 14.04.
Once you created a droplet with Node.js preinstalled, we want to make sure that we have the required minimum versions installed. In order to do this, run node --version
and npm --version
. Here is an example:
root@codefoundries-c001-sfo-njs-001:~# node --version
v4.4.3
root@codefoundries-c001-sfo-njs-001:~# npm --version
2.15.1
We need to have npm
at version 3 at minimum, andnode.js
at version 5 at minimum. If the versions you see are lower, like in the example above, we will need to upgrade them.
Npm can be upgraded by running
sudo npm update npm -g
Here is a sample output:
npm@3.9.5 /usr/lib/node_modules/npm
Node.js can be upgraded by executing the following commands:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
The output should look like this:
root@codefoundries-c001-sfo-njs-001:~# sudo npm cache clean -f
npm WARN using --force I sure hope you know what you are doing.
root@codefoundries-c001-sfo-njs-001:~# sudo npm install -g n
/usr/bin/n -> /usr/lib/node_modules/n/bin/n
/usr/lib
└── n@2.1.0
root@codefoundries-c001-sfo-njs-001:~# sudo n stable
install : node-v6.2.1
mkdir : /usr/local/n/versions/node/6.2.1
fetch : https://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-x64.tar.gz
######################################################################## 100.0%
installed : v6.2.1
After this change up the symbolic link to the new version:
sudo ln -sf /usr/local/n/versions/node/6.2.1/bin/node /usr/bin/node
Verify that node has been updated by running node --version
again.
We will use a reverse proxy sever to serve our content, so we Node.js will be configured to only serve content from the private network. In order to determine the private network IP, run:
curl -w "\n" http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address
Execute:
sudo apt-get update
sudo apt-get install git
in order to install git.
PM2 is a process manager for node which we will use to run our server. Install it using:
sudo npm install pm2 -g
In order to confirm that the installation is successful you can run:
pm2 --version
The output should look like:
Looking for a complete monitoring and management tool for PM2?
_ _ _ _
| | _____ _ _ _ __ ___ ___| |_ _ __(_) ___ ___ (_) ___
| |/ / _ \ | | | '_ ` _ \ / _ \ __| '__| |/ __/ __| | |/ _ \
| < __/ |_| | | | | | | __/ |_| | | | (__\__ \_| | (_) |
|_|\_\___|\__, |_| |_| |_|\___|\__|_| |_|\___|___(_)_|\___/
|___/
Features
- Real Time Dashboard
- CPU/Memory monitoring
- HTTP monitoring
- Event notification
- Custom value monitoring
- Real Time log display
Checkout
https://keymetrics.io/
-------------
[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
1.1.3
In order to make PM2 run when Ubuntu starts execute:
pm2 startup ubuntu
The result should be similar to:
PM2] Generating system init script in /etc/init.d/pm2-init.sh
[PM2] Making script booting at startup...
[PM2] -ubuntu- Using the command:
su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
Adding system startup for /etc/init.d/pm2-init.sh ...
/etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
[PM2] Done.
Then execute the command printed as super user:
su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
Create a new Ubuntu droplet and install NginX with the following commands:
sudo apt-get update
sudo apt-get install nginx
After that edit the configuration file for NginX:
sudo vi /etc/nginx/sites-available/default
Add a server section for the desired domain name pointing at the Node.js server:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Set up the server to start automatically upon boot:
sudo update-rc.d nginx defaults
point it at the NginX server public IP.
NS1.DIGITALOCEAN.COM
NS2.DIGITALOCEAN.COM
NS3.DIGITALOCEAN.COM
Associate the domain name to the floating IP