Your hosting renewal is coming up. You are not that happy with forking out the hard earned dollars. Its a Saturday night, plans have been cancelled. You are bored and there is some cold beer in the fridge. You have a spare Raspberry Pi, a fibre connection, a static IP, and a penchant for tinkering. Hmmm...
And so the decision to self-host came about :) Honestly, I made some mistakes and ended up rebuilding the solution three times! I learned much about configuring Linux, Nginx and the Ghost platform - and just how powerful and versatile the Raspberry Pi is!
I thought I would document the journey for you (and as a reminder for me :D). Next things to look at are high availability and backup.
- Raspberry Pi 2 with a 16GB card running debian stretch lite.
- Uncomplicated Firewall, adds a layer of security and comfort.
- Nginx, free, open-source, high-performance HTTP server and reverse proxy.
- MariaDB, popular database server.
- Ghost, lightweight and simple blogging platform.
First some assumptions,
- I will assume you have prepared your SD card and have SSH access to your Pi. I'll do a quick tutorial on this if interested :D
- I am assuming you know how to update your DNS and have a static IP and have port forwarding configured on your router.
These steps are pretty straight forward. I'll try to post some details on these if there is interest.
Ok, ensure you are using the latest packages and are up to date.
sudo apt-get update && sudo apt-get dist-upgrade -y
Configure the Firewall
Allow SSH from your workstation (adjust to suit).
Allow 'Nginx Full', which allows HTTP and HTTPS traffic.
Finally, enable the firewall - Check that IP address :D
sudo ufw allow from 10.10.1.110 to any port 22 sudo ufw allow 'Nginx Full' sudo ufw enable
Configure the Database
Start the SQL Configuration for MariaDB.
Follow the wizard and answer the questions as below. There are a few extra steps but they are pretty clear, so work through.
- Enter root password?
Click ENTER. You wont have set one yet. You'll be asked to do so shortly and it will be used down the track.
- Remove anonymous users?
- Disallow root login remotely?
- Remove test database and access to it?
- Reload privilege tables now?
Now, grant user root privileges so that Ghost CLI can create the databases automatically (Kudos Ryan Erickson!). You'll need that SQL root password here.
sudo mysql -u root -p
You'll end up in MariaDB shell. Enter the following commands and yes, they are case sensitive and you need the semi-colon.
USE mysql UPDATE user SET plugin='mysql_native_password' WHERE User='root'; FLUSH PRIVILEGES; exit;
Configure the Web Server
First, enable the hash_bucket_memory_size parameter by removing the comment marker from the nginx.conf file.
sudo nano /etc/nginx/nginx.conf
Whenever you make a change to the config its a good idea to test it.
sudo nginx -t
If passed ok, reload nginx for the changes to take effect. If you do encounter any issues, its usually a typo so go ahead and take another look.
sudo systemctl restart nginx
Install and Configure Ghost
Ghost only supports particular versions and this version is recommended at the time of writing. Be sure to check the documentation to ensure you have the optimal version.
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash sudo apt-get install -y nodejs
Install Ghost CLI
sudo npm i -g [email protected]
Make a new directory for your Ghost install. I have multiple for testing so name appropriate to your use case. Change the user reference (pi:pi) to sort your username.
sudo mkdir /var/www/ghost-prd sudo chown pi:pi /var/www/ghost cd /var/www/ghost-prd
As before you'll now answer a few questions. These are straight forward. My answers are below, adjust to suit your needs. For details, check out the CLI reference cli-install#section-prompts
- Continue anyway?
- Enter your blog URL:
- Enter your MySQL hostname:
localhost (or simply press ENTER)
- Enter your MySQL username:
- Enter your MySQL password:
(enter your password here)
- Enter your Ghost database name:
- Do you wish to set up Nginx?
- Do you wish to set up SSL?
No (I used Cloudflare and used the instructions here: Ghost Pi.
- Do you wish to set up "ghost" mysql user?
- Do you wish to set up Systemd?
- Do you want to start Ghost?
Or, you can do it all programmatically in one line. This is my preferred option.
Using Ghost-CLI programatically
Congratulations! You know have a working Ghost installation!
Some things to do,
- Add SSL functionality through Cloudflare... Check out this article from Ghost Pi
- Add some security enhancements to MariaDb... Check out this article from Digital Ocean
- Add some high availability with a Docker Swarm... Research and tinker...
- Add backup functionality... Research and tinker...
Good luck! I learned a lot going through this excercise and am looking forward to see how we can improve on the solution.