Cover Image

Setup Ghost Blog in a Subdirecotory over SSL on NGINX

Have you ever wanted to start a blog with your own domain (without the classic outdated WordPress :P)? Ghost Blog is the best publishing platform to use!

blog.domain.tld vs domain.tld/blog/:

When you start a blog on a subdomain search engines like Google treat the subdomain as a separate domain which will not benefit the SEO of the main domain.

Therefore, it is better to start our blog on domain.tld/blog/.

Note — Throughout this blog, I will be referring to domain.tld/subdirectory/; furthermore, subdirectory is the name of your subdirectory (example — If your URL is domain.tld/blog/, then blog is your subdirectory in this case)

What is Ghost? (ghost.org)

Ghost is a fully open source, adaptable platform for building and running a modern online publication. They even power blogs, magazines and journalists from Zappos to Sky News.

Note — Ghost has a paid package where they host your blog, but for this tutorial, we will be using the open source developer version which can be deployed on a custom web server.


Note — I have used Ubuntu 18.04 LTS for this tutorial. Some of the steps might differ on other operating systems.

I assume that you know how to set up an NGINX web server. To follow along through this tutorial, you need to set up your NGINX web server. To learn how to set up an NGINX web server, visit — https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04

Install SSL (Secure Socket Layer) certificate on web server

Once you set up NGINX and have your main website running, install an SSL certificate. I am using Let’s Encrypt; however, you can use any other SSL.

For this, I will use Certbot. Run the following commands for Ubuntu 18.04 LTS:

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install certbot python-certbot-nginx

$ sudo certbot --nginx

Enter the domains as domain.tld (Optionally, you can also enter www.domain.tld)
Run the following command to make the certificate auto-renew

$ sudo certbot renew --dry-run

How to install Ghost?

If you are using Ubuntu 16/18, visit https://docs.ghost.org/install/ubuntu/. Else, you could also use any of the alternative installation options available here https://docs.ghost.org/setup/.

Note — Before you run $ ghost install, make sure that you are in the subdirectory. Do not install Ghost in a new folder as mentioned in the tutorial. Install it in a SUBDIRECTORY! (To go into your subdirectory run $ cd /var/www/html/subdirecotory/)
 When you run the $ ghost install command, remember to use the following settings:

  • config-URL — https://domain.tld/subdirectory/
  • MySQL hostname — (localhost by default)
  • MySQL username/password — (Enter the password as created in the ghost database creation)
  • Ghost database name — (Name of Database)
  • Setup SSL — no
  • Setup Systemd — yes
  • Start Ghost — yes

Problem with running Ghost

Normally, webpages use port 443 to run over SSL (secure) connection and port 80 to run over a non-SSL (insecure) connection. However, Ghost runs on port 2368, so we have to use a proxy that will use port 2368 when we go to the /subdirectory/ location.

For this, head over to the NGINX configuration file by running this command:

$ cd /etc/nginx/sites-available/
$ nano configuration-file-name

The configuration file should look like this by default

server {

root /var/www/html;
index index.html index.htm index.nginx-debian.html;

server_name domain.tld www.domain.tld;

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

location / {
try_files $uri $uri.html $uri/ =404;
}


listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/snpranav.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/snpranav.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

Note — domain.tld refers to your own domain

Add this snippet inside the server block

location ^~ /subdirectory {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:2368;
proxy_redirect off;
}

Finally, the NGINX configuration file should look like this

Now, visit https://domain.tld/subdirectory/ghost/, and you should be able to create your Ghost Blog account as shown below.

Once you configure all the above setting properly, you will not get any kind of mixed content errors.

https://domain.tld/subdirectory/ghost/

Happy Blogging!


If you have any questions or any suggestions/ideas. Hit me up in the comments section :)

Stay up to date with my medium posts by following Pranav Shikarpur on medium.

My Website- snpranav.com


Credits

Nithya Shikarpur- Reviewed this post


Originally published at snpranav.com on March 2, 2019.