Host a Ghost Blog on Google Compute Engine

The Google Cloud Platform has everything you need. You can host application with automatic scalability on App Engine, store data in the Dastastore (NoSQL) or the Cloud SQL, manage large amount of data with Big Query, or store assets in Cloud Storage.
But you can also have a full server with Compute Engine, with an API to start it, shutdown, clone, etc. So when we decided to open a blog here at LumApps, and especially a Ghost blog, we didn't think twice before using Google Compute Engine.

Table of contents:

  1. Initialization
    1. Project creation
    2. Server creation
  2. Installation
    1. Ghost installation
    2. Startup script
  3. Finalization
  4. Conclusion

Initialization

First, we needed to create a new Google Cloud project and a new server.

Project creation

To create a project, go to your Google Developers Console, and click on Create Project. Enter a name and ID. The ID will be use for the access url (PROJECT_ID.appspot.com).

Google Developers Console - New project

Server creation

In the new project, under Compute > Compute Engine > VM Instances, creating a Compute Engine server is as easy as clicking in the New instance button. The power, the location and an image use to initialize the boot disk. It's possible to start from a blank disk, but since there is a Debian image available, we used it for our server. And in just a few minutes, we have a server with Debian & SSH access. And to use this SSH access, we can use a web client given by Google (useful, but not perfect, no copy/paste for example), or just copy a command line to use with the gcloud tool in a more classic Terminal.

Google Compute Engine SSH

Installation

Once we had set up our server, we need to install Ghost, serve the web server to the web, and finally make sure that Ghost is run at startup in case the server needs rebooting.

Ghost installation

First, we need to install Node.js. The Debian package available from the Compute Engine won't help, so we compiled it from the source. The Compute Engine documentation helped us:

sudo apt-get install python g++ make checkinstall  
mkdir ~/src && cd $_  
wget -N http://nodejs.org/dist/node-latest.tar.gz  
tar xzvf node-latest.tar.gz && cd node-v*  
./configure
sudo checkinstall  

It took a while because we used a low-powered instance. After that, we installed Ghost using the well written official documentation. We chose to have our Ghost folder in /var/ghost, owned by a ghost user:

curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip  
unzip -uo ghost.zip -d ghost  
sudo mv ghost /var  
sudo useradd -r ghost -U  
sudo chown -R ghost:ghost /var/ghost  

We configured the production profile of the Ghost's config.js with the right URL, the right database (we kept SQLite3), the mail account for sending mail, and the most import the host (0.0.0.0) and the port (80). A test by launching sudo npm start --production, and the blog was working.

Startup script

For the startup script, we could have managed it ourselves. But we wanted something simpler and with less writing code. So we tried some Node packages (forever, initd-forever, and some others), but there was bugs in all of theme. With forever + initd-forever for example, the startup script worked well, but the restart was not working. Forever couln't find the PID of Ghost. There are already a lot of tickets on there GitHub about that.
But then, we came across PM2. And it is perfect. First, we need to declare a process for PM2:

{
    "name": "ghost",
    "script": "/var/ghost/index.js",
    "log_date_format": "YYYY-MM-DD HH:mm Z",
    "watch": false,
    "env": {
        "NODE_ENV": "production"
    }
}

Then, we start the process: sudo pm2 start processes.json.
We create the startup file: sudo pm2 startup ubuntu.
And finally, we save the current PM2 state: sudo pm2 save.

Now, the server is ready, serving the blog, restarting it after reboots, and if we need to restart Ghost (to add a theme for example), a simple sudo pm2 restart ghost and it's all done!

Finalization

Because Compute Engine is awesome, we took a snapshot of our disk. Why? Because if one day we need an instance more powerful, we'll just create it from this snapshot, with all the configuration already done, and the new instance will be up and running in just a few minutes.
What about all the data created since the snapshot creation? We created a cron task that copy all the Ghost data in Google Cloud Storage. It costs us almost nothing, and we have a backup on another server. Perfect.
Finally, to really finish it up, we redirected blog.lumapps.com to the IP adress of our instance, as we would do with any other server.

Conclusion

As you can see it, Compute Engine offers a real server for almost no work. There is some scalability utilities integrated by Google (load blancing, autoscaler, snapshot, etc.) that can help maintain a fully working environment whatever the load. And as its almost fully automatic, you're sure you'll pay the minimum every month. Our blog is a simple app that won't ask too much resources, but if you think of hosting a full suite, or just a really big app, it's one of the better solution currently available. Much flexible than Google App Engine, and almost as easy to scale.

Marc Alexandre

Marc Alexandre

http://www.malexandre.fr

View Comments
Navigation