By the end of this tutorial you should have a ghost CMS instance setup (you could serve more than one) that you can access anywhere on the internet under a custom (free) domain name with SSL.
After four days of hard work, and multitudinous reinstalls of Raspbian OS and Ghost CMS, I’ve finally come up with the perfect solution to installing Ghost on my Raspberry Pi 4.
Whether this is your first attempt at installing Ghost CMS or you have faced an array of Nginx errors. I recommend returning to your last backup of your OS.
To follow this tutorial, you can either VNC, SSH or just use your Raspberry Pi. I wrote this guide on Ghost CMS 4.0 using a Raspberry Pi 4. This guide should work for >Ghost CMS 3.0 and Raspberry Pi 3 / 4.
If you just want to get started read the instructions below. If you want to find out more about Ghost then just scroll down to the bottom.
Prerequisites
Its' advised that you have a basic knowledge of Networking before following these tutorials, you will be opening a port on your router which could expose your router if not done correctly. Also, I am not a networking master, although I've had this article proof checked for vulnerabilities please proceed with caution. But don't let this scare you off, please continue if you feel comfortable / are willing to learn about network further. Thanks to all who have helped contribute to this article.
Part 1
In this section, you will obtain a domain name for free, if you already have one then you can use that. If not, then you can use freenom as I do.
We will also use Cloudfare to setup SSL and recieve the benefits such as if your site is under attack and has additional security benefits.
Step 1
If you already have a domain name you can skip this step, if not then carry on.
"Freenom is a domain registrar that provides unlimited free domains that last for one year. The only limit is that you can only get domains ending in .tk
, .ml
, .ga
, .cf
, or .gq
."
To save myself rewriting instructions which you can easily find on the internet, I have linked another article below outlining what you need to do.
Once you have finished setting the nameservers then continue with this article.
Awesome Article on how to get a free domain with Freenom and Cloudflare
Step 2
Find your routers private IP address - usually it will be: http://192.168.0.1 or http://192.168.1.1
If not then look here on how to find yours:
Crazy Article on How to Find Your Routers IP on Any Computer
Now, we will port forward so you will be able to access your Pi inside and outside your network.
This is how it looks on a Sky Router but it will probably look slighlty different for you: (do the same for service port 80 - HTTP & 443 - HTTPS)
The destination IPv4 LAN address should be your Raspberry Pi's local IP. You can find this by:
ping raspberrypi.local
This will return something like (192.168.0.77):
PING raspberrypi.local (192.168.0.77): 56 data bytes
64 bytes from 192.168.1.131: icmp_seq=0 ttl=255 time=2.618 ms
Step 3
Login to Cloudfare and navigate to your chosen domain name. You should setup the DNS records as shown below but replace your A
name record content with your Routers Public IP address NOT your private one.
The CNAME
redirects any WWW
domain fetch to the non-www
domain.
Part 2
In this section, you will use your new domain name to setup a site on the Raspberry Pi using Nginx (pronounced: engine-x) and MariaDB.
Step 1
Copy / Paste the steps below to install and update the necessary packages.
When typing this into the terminal of the Pi it is recommended that you do this as a root user.
# The first step – install / update package:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash
# Install NPM:
sudo apt-get install -y nodejs
# Update node to the latest release:
# Clear cache:
sudo npm cache clean -f
# Helper:
sudo npm install -g n
# Install Latest stable version of node.js:
sudo n stable
# Packages: Webserver database and nodejs
# -y flag is default yes for any questions asked
sudo apt install nginx mariadb-server nodejs -y
# Globally install ghost:
sudo npm install ghost-cli@latest -g
Step 2
Here we shall configure MariaDB (some older guides might use a SQL package but that is now depreceated and MariaDB has taken over)
Here you need to replace 'my_new_password' (keeping the inverted commas) with a password of your choice. Remeber this for later.
# Configure MariaDB
sudo service mariadb stop
# Restart in safe mode:
sudo mysqld_safe --skip-networking &
# Login to MariaDB
sudo mariadb -u root
# Reload GRANT tables
FLUSH PRIVILEGES;
# Change passcode for the root user
ALTER USER 'root'@'localhost' IDENTIFIED BY 'my_new_password';
FLUSH PRIVILEGES;
exit;
Step 3
Firstly, you need to reboot the pi to get out of safe mode, if you have any files open in the background make sure that you save them before continuing otherwise the reboot (turns RPI off then on again) may not save your latest changes.
Again, ensure you replace 'my_password' with your chosen password.
When you "mariadb -uroot -p" it will ask you for your password that you entered before. That is what the "-p" flag does. This will also create a console like the below. This is MariaDB on the command prompt so it will look different on your Raspberry Pi.
# Reboot to ensure any changes take place and get out of safe mode
Sudo reboot
# Create a database and DB user for ghost
# Login using new passcode:
mariadb -uroot -p
# Create new DB
CREATE DATABASE ghost_cms;
# Create new user and password
CREATE USER 'ghost_cms'@'localhost' IDENTIFIED BY 'my_password';
# Set permissions
GRANT ALL PRIVILEGES ON ghost_cms.* TO 'ghost_cms'@'localhost';
FLUSH PRIVILEGES;
exit;
Step 4
Here, you will create a system user for ghost, whenever you go into the directory ensure you change into the ghost user, this is shown in later steps!
# Creating a system user for ghost and make it a sudo user
sudo useradd www-ghost
sudo usermod -aG sudo www-ghost
# Create a home directory for new user with correct permissions:
sudo mkdir /home/www-ghost && sudo chown www-ghost:www-ghost /home/www-ghost
# Password:
sudo passwd www-ghost
Step 5
Setting up the Ghost CMS directory and changing user.
# Password:
sudo passwd www-ghost
# Set Ghost Up
sudo mkdir -p /var/www/ghost
# Set owner of directory
sudo chown www-ghost:www-ghost /var/www/ghost && sudo chmod 775 /var/www/ghost && cd /var/www/ghost
# Switch User:
sudo su www-ghost
Step 6
Now it is time to finally install Ghost into the directory. These are the prompts that you shall recieve.
It will alert you that your are not running on an Ubuntu device and if you wish to continue, just type in "y".
The MySQL hostname is localhost since that is where the database is stored. The Username and password we setup before.
We use Nginx instead of Apache in this installation since it is what Ghost reccomends during the installation. It creates something called a reverse port proxy .
# Install Ghost:
ghost install
# Message about compatibility: y
# Blog URL: {INSERT YOUR URL HERE WITH HTTPS://DOMAINNAME.IDENTIFIER}
# MySQL hostname: localhost
# MySQL username: ghost_cms
# MYSQL password: {from before}
# Ghost DB name: ghost_cms
# Do you wish to setup NGINX: y
# Do you wish to set up SSL: y
# Do you wish to setup Systemd: y
Step 7
Your site should be accessible through the URL that should be shown in the console. If you followed the steps above about getting a domain then it will be accessible there.
Like this: DOMAINNAME.IDENTIFIER/Ghost
Thanks for reading, if you have any questions then please feel free to ask them below.
Did you find this article valuable?
Support Aiyush Gupta by becoming a sponsor. Any amount is appreciated!