Setup your Ghost blog with Nginx

Ghost

/

Sat Jun 25 2016

Ghost is a Node.js application and handles all requests by itself. In theory, you could just let it be the main webserver. In my case however, I already had Nginx running and I wanted to use it together with Ghost. In this post I'll explain how to make Ghost play nicely with Nginx by setting up a reverse proxy. I tested this on Ubuntu 16.04, but the steps below should be similar on other flavours of Linux.

⚠️ Please note that this post was written for Ghost versions < 1.0.0. If you use Ghost 1.0.0 or higher, ghost-cli can setup nginx for you automatically.

Configuring Ghost

First, open up the config.js file in the root of your Ghost installation in a text editor. Make sure the production configuration contains the following:

production: { url: 'http://www.your-url.com/', mail: {}, database: { client: 'sqlite3', connection: { filename: path.join(__dirname, '/content/data/ghost.db') }, debug: false }, server: { host: '127.0.0.1', // aka localhost port: '2368' } }

As you can see I set the Ghost server IP to 127.0.0.1. This will make Ghost only reachable from within the local network. Replace http://www.your-url.com/ with your own domain name. Now quit Ghost if you already had it running and restart it with

npm start --production

Ghost should now be up and running with the new config.

Configuring Nginx

Of course, we want your blog to be reachable from the outside world, so we're going to set up a reverse proxy in Nginx that will route incoming requests from the outside to the Ghost application. You'll need to create a new config file in Nginx's sites-available/ folder. In my case, the full path name for this folder was /etc/nginx/sites-available/. Name the file something like ghostblog. Open the file for editing and create a new server block:

server { listen 80; server_name www.your-url.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass "http://127.0.0.1:2368"; } }

This bit of code tells Nginx that incoming requests on port 80 (the standard http port) need to be redirected to the Ghost instance running on port 2368. Next, symlink this file to the /etc/nginx/sites-enabled/ so Nginx knows it's enabled. You can do this by using

ln -s /etc/nginx/sites-available/ghostblog /etc/nginx/sites-enabled/ghostblog

Then all you need to do is reload the Nginx service by using the command

service nginx reload

And that's all there is to it! Your blog should now be reachable via the URL you specified. Happy blogging!

Tip: if you're looking for a professional, easy way to manage the Ghost process in a production environment check out my blogpost about the Node.js process manager pm2.