Host Ghost with an Apache reverse proxy on Ubuntu 16.04

If you're setting up your own blog, what's the first platform that comes to mind? Wordpress? Sometimes that can be overkill when all you need is a blog.

I found Ghost to be a better option for publishing & displaying content without any fuss.

I'll show you how I set it up for this blog, and hopefully you'll realise how cool it is. Not quite the Wordpress 5 minute install, but once you're up & running, the Ghost platform is a dream to use.

Prerequisites

  • A server, in my case Digital Ocean
  • Apache with the modproxy & optionally modssl installed (if you want SSL)

My Existing Setup

I already have a Digitalocean droplet, with Apache 2.4 & Ubuntu 16.04 LTS. It has a few sites already running on the droplet using Virtual Hosts, secured with SSL using LetsEncrypt (which I cover in another post).

Therefore I won't cover installing Apache itself, as there are plenty of guides for that, and Digital Ocean are the server setup tutorial masters.

Making a Ghost Virtual Host

If you Google ghost installation, you'll notice that it's usually talking about Nginx, which I'd have used if I didn't already have an established Apache setup.

In my case though, I didn't want to move all my other sites to Nginx, which meant Apache made the most sense. So I made a new Virtualhost for it to sit on, like this:

    <VirtualHost *:80>

      ServerName chrisjallen.com
      ServerAlias www.chrisjallen.com  
      Redirect permanent / https://chrisjallen.com/
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined

    </VirtualHost>

This listens on port 80 as standard, but as we want to use SSL on this site, we permanently redirect all traffic to the https URL, with the Redirect permanent / https://chrisjallen.com/ bit.

Next, I created another Virtualhost, for the SSL version of the site. This is also where I turn Apache into a reverse proxy, or gateway server. What it does is forward all HTTPS requests to the specific port that Ghost will use. This is because Ghost is a javascript app that sits on the server in its own 'compartment', listening and serving responses on port 2368.

Here's the whole thing:

  <IfModule mod_ssl.c>
    <VirtualHost *:443>
      ServerAdmin admin@chrisjallen.com
      ServerName chrisjallen.com
      ServerAlias www.chrisjallen.com
      DocumentRoot /var/www/chrisjallen.com/public_html
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
      SSLCertificateFile  /etc/letsencrypt/live/chrisjallen.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/chrisjallen.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    ProxyPass / http://127.0.0.1:2368/
    ProxyPassReverse / http:/127.0.0.1:2368/
    ProxyPreserveHost   On

</VirtualHost>  
</IfModule>  

Most of this is run-of-the-mill Apache virtualhost stuff, but let me go over these lines in particular.

ProxyPass / http://127.0.0.1:2368/  
ProxyPassReverse / http:/127.0.0.1:2368/  
ProxyPreserveHost   On  

These lines utilise the Apache mod_proxy module, which routes all requests to port 2368 so Ghost can handle them. Proxypass handles incoming requests, ProxpassReverse does outgoing, and ProxyPreserveHost makes sure that the exact same headers from the original request make it to the Ghost setup, as if there were no proxying.

Installing Ghost

To install Ghost, just follow these steps,
(I borrowed them from a Digitalocean tutorial, for Ubuntu.):

Grab Ghost:

wget https://ghost.org/zip/ghost-latest.zip  

Install unzip

sudo apt-get install unzip  

Unzip the downloaded package, (to /var/www/ghost in this case):

sudo unzip -d /var/www/ghost ghost-latest.zip  

Switch to the directory and run NPM install. The --production flag makes sure you're only installing the dependencies needed for a production (live) enviroment.

cd /var/www/ghost/  
sudo npm install --production  

I'll finish up by installing a node package called forever, which I highly recommend for keeping your Ghost installation rocking 24/7.

Keeping Ghost ticking using 'forever'.

As Ghost is a nodejs application within your server, it needs to be started up with a command. Forever is an awesome package that ensures your Ghost installation is up & running (forever).

To install this, type:

sudo npm install -g forever  

When that completes, enter the following command, making sure to substitute the /var/www/ghost path for your own path to ghosts entry point (ie. index.js)

sudo NODE_ENV=production forever start --sourceDir /var/www/ghost index.js  

If you ever want to stop Ghost, use this command:

forever stop [your-path-to-ghost]/index.js  

Next Steps

Hopefully this post has helped get a head start with a Ghost installation on Apache. Here are some useful resources for further reading.