Installing ghost with nginx proxy-cache

Prerequisite: You need to have node.js & npm installed.

Installing Ghost

Run following commands

mkdir /var/www/example.com/htdocs/
cd /var/www/example.com/htdocs/
git clone https://github.com/TryGhost/Ghost .
npm install -g grunt-cli
npm install
grunt init
grunt prod

npm install -g forever 
forever start index.js

Above will start ghost on http://localhost:2368/.

Goto http://localhost:2368/ghost/setup/ to create admin user and complete one-time setup.

Create Nginx Proxy

Create a config file /etc/nginx/sites-available/example.com and paste following:

 (following config is directly copied from https://gist.github.com/pnommensen/707b5519766ba45366dd)

upstream ghost_upstream {
    server 127.0.0.1:2368;
    keepalive 64;
}

proxy_cache_path /var/run/cache levels=1:2 keys_zone=STATIC:75m inactive=24h max_size=512m;

server {
   server_name domain.com;
   add_header X-Cache $upstream_cache_status;
   location / {
        proxy_cache STATIC;
        proxy_cache_valid 200 30m;
        proxy_cache_valid 404 1m;
        proxy_pass http://ghost_upstream;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
        proxy_ignore_headers Set-Cookie;
        proxy_hide_header Set-Cookie;
        proxy_hide_header X-powered-by;
        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;
        expires 10m;
    }
    location /content/images {
        alias /path/to/ghost/content/images;
        access_log off;
        expires max;
    }
    location /assets {
        alias /path/to/ghost/content/themes/uno-master/assets;
        access_log off;
        expires max;
    }
    location /public {
        alias /path/to/ghost/core/built/public;
        access_log off;
        expires max;
    }
    location /ghost/scripts {
        alias /path/to/ghost/core/built/scripts;
        access_log off;
        expires max;
    }
    location ~ ^/(?:ghost|signout) { 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://ghost_upstream;
        add_header Cache-Control "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0";
    }
}

If you are using EasyEngine, you may watch this issue. We are planning to add ghost support in EasyEngine soon! 🙂