The complete guide to self-hosted Ghost blog on Ubuntu/Debian

Written on 04 June 2016

I have used Ghost for year. I Install Ghost in Debian with Node LTS 4 and Nginx server.

Stack : Nodejs, Nginx, Sqlite,Debian, Systemd
1. Prepair to install Ghost
You need to install Nodejs , the best is Node LTS version

cd /tmp  
// For x86 
wget http://nodejs.org/dist/v4.2.5/node-v4.2.5-linux-x86.tar.gz -O node.tar.gz  
// For 64
wget http://nodejs.org/dist/v4.2.5/node-v4.2.5-linux-x64.tar.gz -O node.tar.gz

 sudo  tar --strip-components 1 -xzvf node.tar.gz  -C /usr/local
//Test Node
node -v && npm -v  
// Sample result
2.14.12  
v4.2.5  

2. Install dependencies package

apt-get update  
apt-get install build-essential unzip  

3. Download latest Ghost version

mkdir -p /var/www/ghost  
wget https://ghost.org/zip/ghost-0.7.5.zip -O ghost.zip  
unzip ghost.zip -d /var/www/ghost  

4. Install Ghost

npm install --production,  
//Test
npm start --production  
// change file owner
chown www-data:www-data -R /var/www/ghost  

Then edit config of production

   // ### Production
    // When running Ghost in the wild, use the production environment.
    // Configure your URL and mail settings here
    production: {
        url: 'http://exmaple.com',//Your url here
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

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

5. Setup Ghost as Proxy server behind Nginx

upstream ghost {  
    server http://localhost:2368;
    #keepalive 64;
}
proxy_cache_path /var/cache/nginx/blog levels=1:2 keys_zone=BLOG:75m inactive=24h max_size=512m;  
server {  
   server_name example.com;
 access_log   /var/log/nginx/exampe.com.access.log;
        error_log    /var/log/nginx/example.error.log;

   add_header X-Cache $upstream_cache_status;
   location / {
        proxy_cache BLOG;
        proxy_cache_valid 200 30m;
        proxy_cache_valid 404 1m;
        proxy_pass http://ghost;
        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 /var/www/ghost/content/images;
        access_log off;
        expires max;
    }
    location /assets {
        alias /var/www/ghost/content/themes/current/assets;
        access_log off;
        expires max;
    }
    location /public {
        alias /var/www/ghost/core/built/public;
        access_log off;
        expires max;
    }
    location /ghost/assets {
        alias /var/www/ghost/core/built/assets;
        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;
        add_header Cache-Control "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0";
    }

}

Then run command bellow to make site enable

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enable/  
sudo service nginx reload  

6. Run Ghost as a service use systemd

cd /etc/systemd/system  
vim ghost.service  

Then paste this

[Unit]
Description=Ghost blog example.org  
After=network.target

[Service]
Type=simple  
PIDFile=/run/ghost.pid  
WorkingDirectory=/var/www/ghost  
User=www-data  
Group=www-data  
ExecStart=/usr/local/bin/npm start --production  
ExecStop=/usr/local/bin/npm stop --production  
StandardOutput=null  
StandardError=null  
Restart=always  
SyslogIdentifier=Blog Ghost

[Install]
WantedBy=multi-user.target  

Enable ghost service

systemctl enable ghost.service

systemctl start ghost.service  

Now start blogging :)