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.
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.
It takes a couple of minutes to build a barebones Ubuntu server for you.
$ ssh root@212.47.246.30
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 -i /Users/username/.ssh/scaleway_rsa.pub
$ cd /
$ apt-get install git
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
apt-get install nodejs
$ npm install pm2 -g
$ git clone https://github.com/glennjones/hapi-bootstrap.git app
$ cd app
$ npm install
process.env.HOST
and process.env.PORT
to set its network connection
$ export PORT=80
$ export HOST=0.0.0.0
$ export NODE_ENV=production
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.
$ pm2 list
$ pm2 monit
$ ssh root@212.47.246.30
$ cd /app
$ git pull
$ git clone https://{token}@github.com/glennjones/glennjones-net.git/ app
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.
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).
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.
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.
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.