Install and run Ghost 1.x on Raspbian

If you have been following Ghost Pi, hopefully installing Ghost on your Raspberry Pi has already been a breeze and you have managed to get your blog up and running.

In my last post, I provided a guide on how to install Ghost 1.0 and above on Raspberry Pi using an Ubuntu based Pi optimised image. At the time, this was one of the few ways I managed to get the new Ghost CLI (Command Line Installer) to work properly, as Ghost is only officially supported on Ubuntu at the moment.


Ghost on Raspbian

Thankfully, you can now install Ghost 1.x on Raspbian, even though it is only officially supported on Ubuntu. There are a few guides online that do a great job of summarising the steps required to install Ghost 1.x on Raspberry Pi using Raspbian Stretch, but I just wanted to tweak a few minor details that I noticed as I followed the same steps.

Which Raspberry Pi?

I have tested this whole process on the Raspberry Pi Model B (512MB RAM version), which ran Ghost 0.11.x just fine, and on the Raspberry Pi 2 and 3, as I always like to keep my Pi 3 available for more taxing jobs (like RetroPie or running Kodi via OSMC etc.).

Unfortunately, I could not get Ghost 1.x running on the Raspberry Pi Model B due to issues installing Node.js, but it worked perfectly on the Pi 2 and 3. I opted to use my Pi 2 because the performance increase is not noticeable to your visitors, and I wanted to keep the Pi 3 in my RetroPie build.

Which Raspbian version?

Ghost Pi used the Raspbian Stretch Lite image (technically, it is called 2017-11-29-raspbian-stretch-lite and was released on 29 November 2017), but you should be fine to use the full version of Raspbian Stretch with Desktop (technically called 2017-11-29-raspbian-stretch) but you don't need to, and the extra pre-installed packages are not required.

Prepare your OS

Whatever version you choose, it is always a good idea to update the packages on it. Let's do that:

boo@ghostpi ~ $ sudo apt-get update && sudo apt-get dist-upgrade -y

Install the required packages

The new Ghost CLI tool requires a number of packages before it will work properly. We'll install these as follows:

boo@ghostpi ~ $ sudo apt-get install ufw nginx mariadb-server -y

This will install:

  1. UncomplicatedFirewall (ufw) - not essential, but highly recommended for securing your Ghost blog
  2. NGINX (nginx) - free, open-source, high-performance HTTP server and reverse proxy
  3. MariaDB (mariadb-server) - one of the most popular database servers in the world.

Configure UncomplicatedFirewall

To prevent any potential issues arising when installing Ghost on Raspbian, you need to enable a few rules within UFW. This is dead simple:

boo@ghostpi ~ $ sudo ufw allow ssh && sudo ufw allow http && sudo ufw allow https && sudo ufw allow 'Nginx Full'

You can run all those commands individually if you want, but the above will do the same in a shorter space of time. In short, this is telling UncomplicatedFirewall to allow SSH access, HTTP and HTTPS traffic and the NGINX rules accordingly.

Finish off by enabling UncomplicatedFirewall with:

boo@ghostpi ~ $ sudo ufw enable

Configure MariaDB

As Ghost needs a database, we first need to carry out some initial configuration of the MariaDB server we just installed. Start the ball rolling by running:

boo@ghostpi ~ $ sudo mysql_secure_installation

You will then be asked a series of questions - they can look a little daunting, but this should hopefully help! Firstly, it will ask you for your root password (for your database). You probably don't have one, so just press Enter to get started.

It is now a good time to set a root password, so follow the prompts and do just that. The next few questions are summarised as follows, including the answers Ghost Pi used:

  1. Remove anonymous users? - YES
  2. Disallow root login remotely? - NO
  3. Remove test database and access to it? - YES
  4. Reload privilege tables now? - YES

The wizard does provide additional information on the above steps, so we won't go into detail here. Once you have followed the above, you should be back at the command prompt. This is good!

Tweak the database

The Ghost CLI can create databases for you during the initial install, but there are a few tweaks needed before this can happen - just do the following:

boo@ghostpi ~ $ sudo mysql -u root -p

You'll be asked for your root password - you know, the one you just created a few minutes ago - so enter that (it won't show as you type for security through obscurity reasons). Then you'll need to enter the following commands in MariaDB:

MariaDB [(none)]> USE mysql;
MariaDB [(mysql)]> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
MariaDB [(mysql)]> FLUSH PRIVILEGES;
MariaDB [(mysql)]> exit;

Note, the commands are the USE mysql; parts and need to be capitalised as shown, and need to include the semi-colons (;) to denote the end of the line too.

Tweak NGINX

Some people have reported issues with NGINX, relating to the hash_bucket_memory_size but again, this is easily fixed:

boo@ghostpi ~ $ sudo nano /etc/nginx/nginx.conf

Find the line that has the hash_bucket_memory_size option (TIP: press CTRL+W then type hash_bucket_memory_size and finally ENTER to search for the line instead of trying to find it manually) then uncomment by removing the hash (#). The default setting of 64 can be left as is.

You can then test and reload your NGINX service as follows:

boo@ghostpi ~ $ sudo nginx -t
boo@ghostpi ~ $ sudo systemctl restart nginx

If you have any issues after running sudo nginx -t then the readout in the terminal should help identify them, usually a typo! Edit your config file again and retest.

Install Node.js on Raspbian

Thankfully, installing Node.js on Raspbian is really simple! The following commands will install the latest supported version (Ghost only supports certain version of Node.js):

boo@ghostpi ~ $ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash
boo@ghostpi ~ $ sudo apt-get install -y nodejs

Once this has been completed, you can now move onto installing the Ghost CLI tool!

Install Ghost CLI on Raspbian

Once Node.js has been successfully installed, you can now install the Ghost CLI tool:

boo@ghostpi ~ $ sudo npm i -g ghost-cli@latest

On my Raspberry Pi 2, this took a few minutes - the Pi 3 will undoubtedly install the Ghost CLI tool quicker, but this gives you a good opportunity for a quick break.

Create your Ghost blog folder

You will now need to create the Ghost install folders. You will need to do this first, and can be done as follows:

boo@ghostpi ~ $ sudo mkdir -p /var/www/ghost
boo@ghostpi ~ $ sudo chown [user]:[user] /var/www/ghost
boo@ghostpi ~ $ cd /var/www/ghost

Obviously you will need to replace the [user]:[user] part with your username (which is probably pi:pi if you have used a vanilla Raspbian image and made no other changes yourself).

Now that we're in the Ghost install directory (i.e. where your Ghost blog will be installed on Raspbian), we can begin the install.

Note: I did come across a few problems when I attempted to install the Ghost CLI on my Raspberry Pi, which appeared to be a result of a server issue on Ghost's side. I was pulling my hair out, but when I tried the same command the next day, it worked fine!

Fixing the Ghost update issue for v1.21.1 on Raspberry Pi

At the time of writing, version 1.5.1 of the Ghost CLI tool causes issues with installation on slower devices, such as the Raspberry Pi. Version 1.5.1 introduces a timeout check when restarting Ghost, and as it takes a little longer on the Pi, this causes the tool to incorrectly assume that Ghost will not start.

Thankfully, there is a quick fix to solve this. For a detailed explanation of this, take a look at the "Fixing Ghost Upgrade Issues" post, but in the meantime, we can fix it by doing the following.

The file we need to tweak is the port-polling.js file. This can be found in your NPM global root folder, which for us was /usr/lib/node_modules/ghost-cli/lib/utils/port-polling.js. Run this in your command line:

boo@ghostpi ~ $ sudo nano /usr/lib/node_modules/ghost-cli/lib/utils/port-polling.js

You'll need to change a few settings here, but please remember this is at your own risk as any changes here could potentially break your install if not done correctly!

We changed ours to look like the following (you can find the original over at the Ghost GitHub repository):

module.exports = function portPolling(options) {
    options = Object.assign({
        timeoutInMS: 2000,
        maxTries: 20,
        delayOnConnectInMS: 3 * 2000,
        logSuggestion: 'ghost log',
        socketTimeoutInMS: 1000 * 60
    }, options || {});

The key points here are the timings. We essentially doubled the timeoutInMS on line 8; the delayOnConnectInMS to 2000 and finally the socketTimeoutInMS to 1000 * 60. Finally press CTRL+X and Y to exit and save.

A fix is in the works

To help improve the installation process for Raspberry Pi fans, we submitted a pull request to Ghost's official GitHub repository to effectively include this amended port-polling.js file for a future release. If you are using a newer version of the Ghost CLI tool (than 1.5.1 then this fix should hopefully be included). You can check out the pull request over at Ghost's GitHub repository if you like.

Install Ghost 1.x on Raspbian

Now we get to the good stuff, actually installing Ghost on your Raspberry Pi running Raspbian Stretch! Firstly, let's just double check we're in the correct directory:

boo@ghostpi ~ $ pwd

You should see /var/www/ghost if you have just followed the steps above.

Now you can start the install with:

boo@ghostpi ~ $ ghost install

During the install, you'll be asked a few questions that need your response. They should be straight forward, and should obviously correspond to your own blog details, but this is how Ghost Pi answered:

  1. Continue anyway? - YES
  2. Enter your blog URL: /
  3. Enter your MySQL hostname: localhost (or simply press ENTER)
  4. Enter your MySQL username: root (or simply press ENTER)
  5. Enter your MySQL password: ********************** (enter your password here)
  6. Enter your Ghost database name: ghostpi_prod (or whatever you want)
  7. Do you wish to set up Nginx? - YES
  8. Do you wish to set up SSL? - NO (I use CloudFlare for this)
  9. Do you wish to set up "ghost" mysql user? - YES
  10. Do you wish to set up Systemd? - YES
  11. Do you want to start Ghost? - YES

All being well, your Ghost blog should now be installed on Raspbian! You'll need to make sure you configure your Pi to allow external access, and access from a custom domain name (which is covered on this guide from Ghost Pi) and you should now have Ghost 1.x up and running on Raspbian on your Raspberry Pi!


If you found this guide, as well as any other guides on Ghost Pi helpful, then if you wanted to buy Ghost Pi a coffee to show your support, then that would be greatly appreciated! As this site does not use any form of adverts, all the costs for running and maintaining the blog are done voluntarily so any extra would be fantastic!