Dalton Sutton

Dalton Sutton

• Development

Raspberry Pi 4 + Ubuntu + Ghost

I recently bought a Raspberry Pi 4 and thought it'd be an awesome way to learn some new things. I'm absolutely in love with Ghost, a blogging platform that is aimed at being simple and user-friendly, not bogged down by a heavy-duty comment system or contact form configurations and the like.

I wanted to see how hard it would be to host some of my own websites using Ghost, on a Raspberry Pi, so I could save some money having to pay for a DigitalOcean Droplet.

So after a quick Google search, I found some ways that people installed Ghost using Raspbian and all the methods they used. Each method seemed like it took a little extra work compared to how easy it was to setup Ghost on a DigitalOcean Droplet. I felt like people were overcomplicating it, but then again, I had never set Ghost up on a Raspberry Pi before.

I want to share my approach to how I installed Ghost, using Ubuntu 18.04.4 LTS, on a Raspberry Pi 4 using the headless method.

Install Ubuntu 18.04.4 LTS

Instead of installing Raspbian on an SD card using balenaEtcher, go to Ubuntu's official site and download their image. This page has some great insight on what to download based on the Raspberry Pi you have. I have a Raspberry Pi 4, 4GB Ram, so I downloaded the 64-bit version.

Using balenaEtcher, select the Ubuntu image from your downloads folder, or wherever it was saved to, and select your SD card, just like you would if you were downloading Raspbian. Burn it to the card.

Now, I do a headless setup, which means I don't need a screen, keyboard or mouse to install this. So before you eject your SD card, on the root of the card, create a new file called "ssh". The file should be empty. It just needs a file named "ssh" on the root so the Pi knows to enable SSH on boot.

Port Forwarding and IP Address

I'm going to assume you know how to find your public IP address using your router. If you don't just Google search it, there are hundreds of great tutorials on it. I'm also going to assume you know how to port forward to your IP address. You just need to make sure your Pi is pointed toward your public IP so when a computer outside your network via the internet accesses your IP address, it points to your Pi. Depending on if your Internet Service Provider (ISP) assigned you with a dynamic IP or a static IP address is important to know. If your IP address is dynamic, that means your ISP can change your IP without you easily knowing. It's common practice that they do this. There are services such as no-ip.com which can help you with a more "static IP address" approach, which is ideal in our case. You don't want the IP address changing on you because then your sites could go down.

Once you have that sorted out, you should have a domain name purchased. In your domain's DNS settings, using an A record, point your domain to your public IP address if it's static. If it's dynamic, you may have to enter a CNAME pointing to the service you use. They'll have more instructions on this on their site.

Login via SSH

Login to your Pi using SSH. If you're using MacOS, you can use terminal, if you're using Windows, try using PuTTY.

ssh ubuntu@your_server_ip

The default password is ubuntu. As soon as you login for the first time, it'll ask you to change it. Please change it to a very secure password.

You now have a Raspberry Pi configured to run Ubuntu. This is awesome because it's just like running a new DigitalOcean Droplet.

Changing the root's password

One thing I did was I changed the root's password. You'll need to know what it is regardless. This was easy to do by entering the following command:

sudo passwd root

Installing Ghost

Alright, now to the best part, installing Ghost. Not going to lie, as soon as I got to this point, I just followed Ghost's official guide to installing on Ubuntu. I'll repeat a lot of their documentation and what I've changed when I ran through it.

Server Setup

This part of the guide will ensure all prerequisites are met for installing the Ghost-CLI.

Create a new user

Open up your terminal and login to your new server as the root user:

# Login via SSH
ssh root@your_server_ip

# Create a new user and follow prompts
adduser ghost-mgr

Note: Using the user name ghost causes conflicts with the Ghost-CLI, so it’s important to use an alternative name. I've used ghost-mgr.

# Add user to superuser group to unlock admin privileges
usermod -aG sudo ghost-mgr

# Then log in as the new user
su - ghost-mgr

Update packages

Ensure package lists and installed packages are up to date.

# Update package lists
sudo apt-get update

# Update installed packages
sudo apt-get upgrade

Follow any prompts to enter the password you just created in the previous step.

Install NGINX

Ghost uses an NGINX server and the SSL configuration requires NGINX 1.9.5 or higher.

# Install NGINX
sudo apt-get install nginx

If ufw was activated, the firewall allows HTTP and HTTPS connections. Open Firewall:

sudo ufw allow 'Nginx Full'

Install MySQL

Next, you'll need to install MySQL to be used as the production database.

# Install MySQL
sudo apt-get install mysql-server

MySQL on Ubuntu 18.04

If you’re running Ubuntu 18.04, a password is required to ensure MySQL is compatible with Ghost-CLI. This requires a few extra steps!

# To set a password, run
sudo mysql

# Now update your user with this password
# Replace 'password' with your password, but keep the quote marks!
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

# Then exit MySQL

# and login to your Ubuntu user again
su - ghost-mgr

Install Node.js

You will need to have a supported version of Node installed system-wide in the manner described below. If you have a different setup, you may encounter problems.

# Add the NodeSource APT repository for Node 10
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash

# Install Node.js
sudo apt-get install -y nodejs

Install Ghost-CLI

Ghost-CLI is a commandline tool to help you get Ghost installed and configured  for use, quickly and easily. The npm module can be installed with npm or yarn.

sudo npm install ghost-cli@latest -g

Once installed, you can always run ghost help to see a list of available commands.

Install Ghost

Once your server is correctly setup and the ghost-cli is installed, you can install Ghost. The following steps are the  recommended setup. If you would prefer more fine-grained control, the  CLI has flags and options that allow you to break down the steps and customise exactly what they do.

Note: Installing Ghost in the /root or home/<user> directories results in a broken setup. Always use a custom directory with properly configured permissions.

Create a directory

Create a directory for your installation, then set the owner and permissions.

# We'll name ours 'ghost' in this example; you can use whatever you want
sudo mkdir -p /var/www/ghost

# Replace <user> with the name of your user who will own this directory
sudo chown ghost-mgr:ghost-mgr /var/www/ghost

# Set the correct permissions
sudo chmod 775 /var/www/ghost

# Then navigate into it
cd /var/www/ghost

Running Multiple Instances of Ghost

If you're like me, you'd probably like to run many instances of Ghost at once with a lot of the sites you have. For this I recommend changing the directory format from "ghost" to something like "example" if your using "example.com" or "test" if you're using "test.com" for the domain.

# We'll name ours 'example' in this example; you can use whatever you want
sudo mkdir -p /var/www/example

# Replace <user> with the name of your user who will own this directory
sudo chown ghost-mgr:ghost-mgr /var/www/example

# Set the correct permissions
sudo chmod 775 /var/www/example

# Then navigate into it
cd /var/www/example

You'd just change the format above to however it fits your needs and run the command below after entering that site's directory. Follow each step and you'll have multiple sites running on your Raspberry Pi using Ghost.

Run the install process

Now you've made it this far, it's time to install Ghost with a single command 😀

ghost install

Install questions

During install, the CLI will ask a number of questions to configure your site.

Blog URL

Enter the exact URL your publication will be available at and include the protocol for HTTP or HTTPS. For example, https://example.com. If you use HTTPS, Ghost-CLI will offer to set up SSL for you. Using IP addresses will cause errors.

MySQL hostname

This determines where your MySQL database can be accessed from. When MySQL is installed on the same server, use localhost (press Enter to use the default value). If MySQL is installed on another server, enter the name manually.

MySQL username / password

If you already have an existing MySQL database enter the the username. Otherwise, enter root. Then supply the password for your user.

Ghost database name

Enter the name of your database. It will be automatically set up for you, unless you're using a non-root MySQL user/pass. In that case the database must already exist and have the correct permissions.

If  you provided your root MySQL user, Ghost-CLI can create a custom MySQL  user that can only access/edit your new Ghost database and nothing else.

Sets  NGINX up automatically enabling your site to be viewed by the outside  world. Setting up NGINX manually is possible, but why would you choose a  hard life?

If you used an https Blog URL and have already pointed your domain to the right place, Ghost-CLI can automatically set up SSL for you using Let's Encrypt. Alternatively you do this later by running ghost setup ssl at any time.

Enter your email
SSL certification setup requires an email address so that you can be  kept informed if there is any issue with your certificate, including  during renewal.

systemd is the recommended process manager tool to keep Ghost running smoothly. We recommend choosing yes but it’s possible to set up your own process management.

Start Ghost?

Choosing yes runs Ghost, and makes your site work.

Repeat the process above for each new site you want to install. Just make sure you change thing that make sense, like the MySQL database name and such.

Future maintenance

Once Ghost is properly set up it's important to keep it properly maintained  and up to date. Fortunately, this is relatively easy to do using Ghost-CLI. Run ghost help for a list of available commands, or explore the full Ghost-CLI documentation.

What to do if the install fails

If an install goes horribly wrong, use ghost uninstall to remove it and try again. This is preferable to deleting the folder to ensure no artifacts are left behind.

If an install is interrupted or the connection lost, use ghost setup to restart the configuration process.

For troubleshooting and errors, use the site search and FAQ section to find information about common error messages.

This is the method I used to install Ghost onto my Raspberry Pi and I didn't run into any problems whatsoever. I honestly was expecting a few but none popped up, making this the easiest way I've ever installed Ghost. I recommend it if you're dangerous enough, like me, to maintain all your Ghost instances yourself and the Ubuntu server as a whole. Next up on my to-do list, is to figure out the best way to back this all up routinely. That may be another post.

Share if you found this helpful! Happy blogging!

© 2020 Dalton Sutton. All rights reserved.