Bryan Lee

Automating A Ghost Blog Install With Ansible

· Web Dev

We are all about efficiency as developers. Why hammer commands line after line into the terminal when you can write code to do it for you? As I setup this Ghost-powered blog, I set out to document down this process as repeatable code.

Ansible is a really good tool to add to your arsenal if you haven't. It lets you use code to run a set of tasks on your VPS/EC2. Instead of staying put in front of the screen issuing out commands, you run your Ansible script and go for a jog instead.

Requirements

  • EC2 instance or VPS
  • Ansible installed on your local machine
  • A domain name
  • SSL certificate

Steps

  1. Clone my Ansible Ghost blog script into your local machine https://github.com/bryanleetc/ghost-ansible
  2. Edit hosts file and replace YOUR_SERVER_IP
  3. Edit group_vars/web.yml and replace YOUR_SSH_USER
  4. Edit templates/nginx.conf.j2 and replace [YOUR_WWW_DOMAIN_NAME], [YOUR_DOMAIN_NAME] (lines 10, 11, 16,)
  5. Copy your SSL Certificate and key to files/server.crt and files/server.key
  6. Run this Ansible playbook on your local machine: ansible-playbook ghost.yml
  7. SSH into your remote server and finish setting up MySQL by following the prompts: sudo apt-get install mysql-server
  8. During the MySQL install, select no when asked if you want to enable the validate password plugin. It causes some problems with MySQL 5.7 apparently.
  9. Set up ghost by going to /var/www/ghost and running ghost install
  10. During the ghost install script, when prompted whether you want to set up Nginx and SSL, answer 'N'.

What's Going On

Let's look at what's going on in the Ansible playbook script when it's run.

- import_tasks: swap.yml
tags: swap

This runs the commands defined in swap.yml, which sets up a 1gb swap (I'm using a 512mb droplet). Disable if you don't want swap.

- name: 'Upload nginx site config'
template: src=nginx.conf.j2 dest=/etc/nginx/sites-enabled/ghost

We copy our own nginx.conf in the templates folder to our server. This is why we don't want ghost to set up the Nginx scripts for us.

- name: 'Upload SSL certificate'
copy:
src: 'files/server.crt'
dest: '/etc/ssl/certs/ssl.crt'
owner: root
group: root
mode: 0644

- name: 'Upload SSL certificate key'
copy:
src: 'files/server.key'
dest: '/etc/ssl/private/ssl.key'
owner: root
group: root
mode: 0640

Copies our SSL cert and private key to our server.

- name: Install mysql-server
apt: name=mysql-server state=present

- name: Install nodejs
shell: curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -; sudo apt-get install -y nodejs
tags: nodejs

- name: Install ghost-cli
command: npm i -g ghost-cli

- name: Create ghost directory
command: mkdir -p

Installs MySQL, NodeJS, ghost-cli and creates the directory for your ghost installation.

Finishing Up

After ghost-cli finishes the setup process, your blog is now viewable. Head to your blog URL and see if it's showing up fine.

With that, go to yourblog.com/ghost where you'll be guided to finish up setting the first admin account. This is also the URL for your ghost admin panel.