/ ghost

Custom domains with Ghost and Raspberry Pi

In the previous post, we looked at optimising your Ghost blog for Raspberry Pi using NGINX and by this point, you are probably wondering why you cannot access Ghost any more. Well fear not, as we will sort this out and start using a custom domain so that your visitors can get to your blog easier.

Custom domains

It now costs very little to buy your own domain name, and there are various places online where you can get them. I bought this one (https://ghostpi.pro) from Namecheap.com, purely because it cost me next to nothing. I'm not going to splash affiliate banners over this post, but the link to Namecheap.com above does include my affiliate code, so if you did purchase via that link I will get a small (15%) commission every time. Ok, enough of that - I just wanted to be open and transparent here!

Once you have decided on your own domain and have purchased it, we can now start using it with our Ghost blog.

Edit config.js again

In the first part of this guide, we edited the config.js file within our /var/www/ghost directory to use our Raspberry Pi's internal IP address (192.168.x.x) instead the default 127.0.0.1 setting. We did this so that we could verify that our Ghost blog was working properly, but this only allows visitors on your own network actually visit you blog, assuming they know the Raspberry Pi's IP address and this is far from ideal.

Because in the last post we configured NGINX, we now need to make a small change to the config.js file again to use 127.0.0.1 again. This is easily done:

[email protected] ~ $ cd /var/www/ghost
[email protected] ~ $ nano config.js

Then change this:

server: {
    host: '192.168.x.x',
    port: '2368'
}

Back to this:

server: {
    host: '127.0.0.1',
    port: '2368'
}

Whilst we're in the config.js file, we can now replace our production URL with our box-fresh domain name. You'll need to change this:

production: {
    url: 'http://my-ghost-blog.com',

To this:

production: {
    url: 'http://ghostpi.pro',

Now, you may notice this is http:// and not https:// at the moment, and there is good reason to. If we enabled SSL from the start, the chances are we'll get lots of warning messages and we don't want that just yet. We'll come back and change this bit later once we have set up SSL within Cloudflare in the next guide.

Lastly, exit CTRL+X and save Y to make the changes permanent.

Next we'll restart Ghost to make sure the changes are being used:

[email protected] ~ $ sudo supervisorctl restart ghost

Cloudflare

The tricky part with using a custom domain name on a Raspberry Pi is that the majority of home broadband users will have a dynamically assigned IP address. This means that your external IP address changes at the whim of your ISP. You could use your external IP address and allow your visitors to access your Ghost blog by entering something like http://26.716.928.26:2368 every time, but this is not recommended nor easy to remember, plus this could become http://25.276.763.82:2368 in an hour, a day, a week a month, without warning because your external IP address will have changed.

Thankfully, we can use Cloudflare and a nice script to always direct visitors towards your new custom domain name.

Signing up

The first thing to point out is that whilst you can pay for higher packages within Cloudflare, they do offer a free service which should provide you with all the necessary tools to make navigating to your site a breeze. Sign up by going to Cloudflare and create an account. I won't cover this here as it is very simple. Once you have an account, we can now add our site (i.e. the custom domain we just bought) to Cloudflare.

Again, I'm not going to cover all the details of how to add a site to Cloudflare here, because it is pretty straight forward but the following steps should be plenty:

  1. Click the "+ Add Site" link in the top right-hand corner of the screen within your Cloudflare account
  2. Enter your domain, e.g. ghostpi.pro and then select "Begin Scan"
  3. Cloudflare will now scan your domain to get your current DNS and nameserver settings
  4. Once added, you'll need to change your existing nameservers to what Cloudflare tells you to. To do this, you need to go into your control panel from wherever you bought the domain, and then look for something like "Advanced DNS" or "Nameservers"
  5. Once the nameservers have been updated to what Cloudflare told you, head back to Cloudflare and select "Recheck" so that Cloudflare can see if your nameservers have been updated - this process can be quick, but it can also take up to 24 hours or more depending on DNS propagation. You will usually get an email from Cloudflare once this has been completed.

DNS Settings

Now this part is a little tricky and can be somewhat confusing. What we need to do now is tell Cloudflare what our Raspberry Pi's external IP address is, so that when somebody visits your new domain, they will be directed to the right place.

If you are on the same network as your Raspberry Pi, the quickest way of finding your external IP is visiting an IP checking site, like IP Chicken, as every device on your device has the same external IP address usually. I recommend setting up your custom domain whilst on the same network as your Raspberry Pi to keep things simple.

Now you should have your external IP address (it is not 192.168.x.x, but something different) and we can now enter this under our site within Cloudflare.

  1. In Cloudflare go to the DNS tab
  2. Remove every record already present by selecting the cross on the right-hand side next to the record you want to remove - this part is optional, but it can save a lot of headaches later on
  3. Add a new "A" record where the "Name" will be your new domain (i.e. ghostpi.pro) and the "IPv4 address" will be our external IP address found via IP Chicken above
  4. Leave "Automatic TTL" as it is and then select "Add Record"

This now redirects whoever visits http://ghostpi.pro to the IP address specified above, but we're not finished yet.

CNAME

If you want to be sure everybody wanting to visit your site can, then it is usually a good idea to redirect anybody who types in www.ghostpi.pro to your blog. We can do this a number of ways, but the simplest is adding a CNAME record as follows:

  1. Choose "CNAME" from the dropdown menu instead of "A"
  2. In the "Name" box, enter "www" (without the quotes)
  3. Enter your domain (i.e. ghostpi.pro) in the "Domain" box
  4. Leave "Automatic TTL" as it is and then select "Add Record"

This is all we need to do right now, but there is still some work to do before we have a reliable way of visiting our blog online.


Port forwarding

So that we can access our Ghost blog on our Raspberry Pi in the real world, we need to forward the relevant port in our router, otherwise visitors will be blocked by your home broadband basic security. This process is extremely varied depending on your ISP and router, but usually it is done by logging into your router's admin section, going to NAT and then port forwarding.

Thankfully, somebody has gone to the extreme effort of making a website that can help the majority of users, called Port Forwarding (it does what it says on the tin!). Just find your router and tell it what you want to do and it should give you the necessary instructions.

The port used by any web browser is port 80 so in your router, you will usually be entering your Raspberry Pi's internal IP address (192.168.x.x), a Private port (usually 80) and a Public port (most likely 80).

Once done, you should be able to access port 80 on your Pi (for HTTP) externally (i.e. over the Internet) using your external IP address found via IP Chicken, but more importantly, if you have configured Cloudflare correctly, you should be able to visit your Ghost blog hosted on your Raspberry Pi using your custom domain. Hopefully.

Next steps

Now, this process should hopefully mean you can now access your Ghost blog using your own custom domain name. However, if our ISP changes our IP address, this will stop working (unless you are a lucky enough to have a static IP address)!

In the next guide, we'll be creating a script to update the DNS settings in Cloudflare using their API, so that if / when our external IP address changes, our Raspberry Pi will automatically update the relevant DNS "A" record in Cloudflare with the new IP address, so your visitors can still find you.


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!

Buy Me A Coffee
Wesley Archer

Wesley Archer

Digital Account Manager by day, and maker in his spare time. Also, a regular contributor to The MagPi Magazine for the Raspberry Pi Foundation.

Read More