How to configure Ghost with Nginx (Ubuntu)

04 Jan 2015

This tutorial will guide you on how to setup Ghost blog with Nginx as reserve proxy.

Nginx is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server).

OS Setup

First step is to update Ubuntu and create non-root user to run your Ghost app.

Update Ubuntu packages to stable releases:

[gtheme_io@tutorial ~]# apt-get update -y

Install required package:

[gtheme_io@tutorial ~]# apt-get install git unzip -y

Create Ghost User

Install Ghost Blog

Next step is to prepare NodeJS runtime environment. You are recommend to install nvm to manage multiple NodeJS versions. The following shows how to install nvm.

[gtheme_io@tutorial ~]# curl https://raw.githubusercontent.com/creationix/nvm/v0.22.0/install.sh | bash

## reload profile
[gtheme_io@tutorial ~]# source ~/.profile

After install nvm, now we will install latest NodeJS:

[gtheme_io@tutorial ~]# nvm install v0.10
[gtheme_io@tutorial ~]# nvm alias default v0.10
[gtheme_io@tutorial ~]# nvm use v0.10

Note: nvm will automatically use latest v0.10.x version.

Next step is to download Ghost app and install:

## download ghost blog
[gtheme_io@tutorial ~]# wget https://ghost.org/zip/ghost-latest.zip

## unzip ghost archieve
[gtheme_io@tutorial ~]# unzip ghost-latest.zip -d ghost
Archive:  ghost-latest.zip
  inflating: ghost/Gruntfile.js      
...
...
  inflating: ghost/index.js          
  inflating: ghost/package.json

Next you can start to install Ghost Blog:

[gtheme_io@tutorial ghost]# cd ghost    
[gtheme_io@tutorial ghost]# npm install --production

Finally, start Ghost:

[gtheme_io@tutorial ghost]# npm start

Install Nginx

Next step is install Nginx for reverse proxy Ghost blog.

[gtheme_io@tutorial ghost]# sudo apt-get install nginx -y

Configure Nginx

With Nginx installed, we now need to tell Nginx that Ghost is ready for requests on port 2368. New Nginx configuration files can be added into /etc/nginx/conf.d/. We would recommend creating your configuration file in this directory and naming it something meaningful, like: example.com.conf. Once you have created your new Nginx conf file place the following into your file, replacing example.com with your domain name:

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

After configured Nginx, you can restart Nginx to use new configuration:

[gtheme_io@tutorial ghost]# sudo service nginx restart

Now your Ghost blog is accessible via default HTTP port(80) and proxy to Ghost's default port 2368. Happy blogging!