How To Create a Ghost Blog on AWS

We build professional apps for Android, iOS and mobile web.
Have a look

This post will show you the process that we used to set up this blog on AWS. We're going to use ghostrunner – an open source module from jtribe – because it makes it easy to install Ghost as an npm dependency, which makes updating easy and also means that we can use something like express to add server-side functionality to our blog in the future.

ghostrunner is a simple command-line tool that creates Ghost blogs and provides OS-specific service wrappers (upstart, launchctl or initctl) so that the blog starts when the server reboots.

The prerequisites for this post are Node.js and Git.

Get Ghost

This whole process is done using the console, so fire up a terminal (or translate into your tools of choice for Windows).

Creating a new blog with ghostrunner is pretty easy (feel free to change dirName to whatever directory name you like).

dirName='my-blog'  
mkdir $dirName && cd $dirName  
npm install -g ghostrunner # install ghostrunner  
ghostrunner init # create your blog  
ghostrunner server # start the server  

If all goes well, you can now go to http://localhost:2368/ and see your wonderful new blog in all its un-styled glory. Pretty cool huh? Okay, now time to make it purty...

Get Styling

No go and find yourself a theme that you like and drop it into content/themes. Here's where I went looking for themes:

You'll need to restart Ghost before it'll allow you to select newly install themes, so go back to your console and press Ctrl-C to terminate Ghost and then run ghostrunner start to start it up again. Now go to http://localhost:2368/ghost/settings/general/ and select one of the themes that you added in the Theme section of the form.

Now add everything to git:

git init  
git add .  
git ci -m"Initial commit"  

Now, to the Cloud!

Okay, so if you don't already have an EC2 instance then create one using instructions from the web. Be aware that you'll want to use a "Security Group" that allows access over SSH and HTTP.

Set up Your Configuration

Now you need to tell Ghost the domain name of your blog, so open up config.js in your editor of choice and make the following changes in the production section:

  • Change my-ghost-blog.com to the hostname you're going to use to access the sever
  • Replace host: '127.0.0.1' with host: '0.0.0.0' to accept connections from anywhere

Add the following before the mail: in the production section:

      paths: {
          contentPath: path.join(__dirname, '/content/')
      },

Now add these changes to your Git repo:

git add -f config.js  
git commit -m"Updated config"  

Push Your Changes

Okay now we need to push our changes to a Git repository. I recommend you create one on Github or Bitbucket (Bitbucket will let you keep your code private if that's your thing). This post assumes Github, but the same applies to Bitbucket.

When you complete this process they will provide you with a URL that you can use to push your changes:

Github example

You'll need to go through the process of setting up your SSH keys so that you don't need to enter your username and password every time, and so that you can access your repo from the server later on.

Now, copy that URL and:

git remote add origin "{{the URL you copied before}}"  
git push origin master --set-upstream  

Congratulations, you've just pushed a Git repository!

Deploy the Amphibious Squadron

Now SSH to your instance so we can set up the blog. You'll want to use "SSH Agent Forwarding" so that you can access your Git repository from the server. You can do this by adding -A to your SSH command, but I recommend adding the following entry in your ~/.ssh/config file:

Host {{your domain name}}  
  Hostname {{public DNS e.g. ec2-*.compute.amazonaws.com}}
  User ubuntu
  IdentityFile ~/.ssh/{{path to your Amazon .pem file}}
  ForwardAgent yes

This allows you to SSH to {{your domain name}} and to then authenticate over SSH as though you're on your local machine:

ssh {{your domain name}}  

(Answer yes to the warning about the fingerprint.)

Okay, so you've SSHed to your instance. Now you'll need to install a few packages, accepting the defaults when prompted.

sudo apt-get update  
sudo apt-get install iptables-persistent git nodejs npm -y  

One slightly annoying thing about Ubuntu (and other Debian-derived distros) is that it doesn't put the node executable where it's expected to be, so you need to create a symlink.

sudo ln -s nodejs /usr/bin/node  

If you want to run multiple websites on this server then you'll need to set up Nginx or some other reverse proxy to listen on port 80 and then pass the requests to the appropriate process for each website. But that's outside the scope of this post, so we're assuming that this is the only site you're running on this instance.

To keep things secure we're not going to run our blog as the root user and we'll use iptables to forward port 80 to the blog server on an internal port.

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 2368  
sudo service iptables-persistent save  

Now, let's clone your Ghost repo on the instance. Feel free to change dirName to whatever you want.

dirName="/data/my-blog"  
repoUrl="{{the repository URL you copied from Github before}}"  
sudo mkdir -p $dirName  
sudo chown ubuntu $dirName  
cd $dirName  
git clone $repoUrl .  

(Answer yes to the warning about the fingerprint, then...)

npm install # install the ghost module  
sudo npm install -g ghostrunner # install ghostrunner  
sudo ghostrunner install # install the service  
ghostrunner start # start the service  

If everything's worked then you should be able to go to http://{{your domain name}}/ghost/settings/general/ and select your theme, and your shiny new blog will be online at http://{{your domain name}}/.

One final note: your server isn't set up to send email yet, but that's a topic for yourself to work out - I'm out of time here :)