Building a simple Node.js server on scaleway
This tutorial will help build a simple hosted Node.js server using scaleway in just a few minutes. We are going to use a pre-built server image from scaleway and a Node project from a Github repo. I have written this post to remind myself, but hopefully it will be useful to others.
Setting up the account
- Create an account on scaleway.com – you will need a credit card.
- Create and enable SSH Keys on your local computer, scaleway have provided a good tutorial https://www.scaleway.com/docs/configure-new-ssh-key/. It's easier than it first sounds.
Setting up the server
Scaleway provides a number of server images ready for you to use. The Node.js images is a little bit dated so we will use the latest Ubuntu image and add Node.js later.
- Within the scaleway dashboard navigate to the "Servers" tab and click "Create Server".
- Give the server a name.
- In the "Choose an image" section select the "Distributions" tab, page through the options until you can select the latest Ubuntu, currently "Ubuntu Vivid (15.04 latest)".
- Finally click the "Create Server" button.
It takes a couple of minutes to build a barebones Ubuntu server for you.
Logging onto your server with SSH
- Once the server is setup you will be presented with a setting page. Copy the "Public IP" address.
- In a terminal window log into the remote server using SSH replacing the IP address in the examples below
with your "Public IP" address.
If, for any reason you changed the SSH key name from id_rsa remember to provide the path to it.$ ssh root@212.47.246.30
$ ssh root@212.47.246.30 -i /Users/username/.ssh/scaleway_rsa.pub
Adding Git, Node and your GitHub project onto the server
- Move into the top most directory in the server
$ cd /
- Install Git onto the server
$ apt-get install git
- Install Node.js - you can find different version options at github.com/nodesource/distributions
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs apt-get install nodejs
- Install PM2 - this will run our Node app for us
$ npm install pm2 -g
- Clone your Node repo from GitHub. The space and word "app" at the end of the command tells git to place
the project contents into a new directory called "app"
$ git clone https://github.com/glennjones/hapi-bootstrap.git app
- Move into the new app directory
$ cd app
- Install Node packages
$ npm install
- Setup the basic environmental properties. Note your app needs to use
process.env.HOST
andprocess.env.PORT
to set its network connection$ export PORT=80 $ export HOST=0.0.0.0 $ export NODE_ENV=production
Running Node server
Rather than running Node directly we will use PM2. It has two major advantagaes to running Node directly, first is PM2 deamon that keeps your app alive forever, reloading it when required. The second is that PM2 will manage Node's cluster features, running a Node instance on multiple cores, bringing them together to act as one service.
$ pm2 start app.js -i 0
The -i 0
flag tells PM2 to run in cluster mode and use as many cores as it can find. The
PM2 cheatsheet is useful to find other commands.
For feedback on the state of the server
- List -
$ pm2 list
- CPU / Memory Monitoring -
$ pm2 monit
View your running app in the browser
View your app from the "Public IP" in any browser
Other useful information
Updating app using Git pull
If you wish to update your app just log on to the server using SSH and use git pull- SSH into server -
$ ssh root@212.47.246.30
- Move into new app directory -
$ cd /app
- Git pull latest code -
$ git pull
Using a private GitHub repo
There are a few ways to pull a private GitHub repo using SSH keys from a remote server. I use the "Personal access token". You can generate these tokens in the settings area of GitHub and use git clone with this URL structure:
$ git clone https://{token}@github.com/glennjones/glennjones-net.git/ app
Saving server setup for later use
You can setup your own images based on your running server for easy deployment by using snapshot and image features of the service. I did this to create a basic Ubuntu 15 and Node 4 image for this project.
Performance
You will often hear Scaleway C1 servers being compared to the Raspberry Pi 2 in terms of spec, which makes you think it maybe slow. Using all 4 cores they seem perfectly fine for my personal projects. I have yet to find a good set of benchmarks against other services, which would be useful. This blog is running on scaleway as of (2015-09-30).
Mongodb Issue
Scaleway C1 servers and Mongodb do not play very well together. There is a version you can get to work, but it's not well supported or considered production level.
- https://community.scaleway.com/t/mongodb-64-bit/465
- https://community.scaleway.com/t/datacenter-and-64bit-plans/917
Solutions maybe to move db operations onto dedibox by the same company or use one of the Mongodb hosting services and live with some network latency.
More complex deployments
You can deploy much more complex setups with multiple Node servers, load balancers etc. I am very interested in trying out the Electric Search. At the moment building all this into images that you can deploy is quite complex for none devops people.
I am still playing with this service but so far its proved interesting and I have moved at least 5 of my small personal sites over.