Installing Ghost on debian + nginx + forever

This is my first Blog ever therefore please do not judge me for any mistake I would not know about the blogging world.
Let's just jump into the middle of it. Where I want to get to? Easy answer: I want to have Ghost running on my vps which is using Debian squeeze. First of all you will need node.js which can be downloaded directly from their website.

# apt-get update
# apt-get install git-core curl build-essential openssl libssl-dev python
# wget http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gz
# tar -xzvf node_v0.10.26.tar.gz
# cd node-v0.10.26
node-v0.10.26#./configure
node-v0.10.26# make && make install`

So far so good, next we will add the nginx to the repository and install it.

# cat >> /etc/apt/sources.list << eof
# nginx
#
deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx
eof
# wget http://nginx.org/keys/nginx_signing.key
# apt-key add nginx-signing.key
# rm -rf nginx-signing.key
# apt-get update
# apt-get -y install nginx

OK, we have installed the nginx successfully and it is the time to jump in to the configuration files.

First we will start editing the nginx.conf located in /etc/nginx

user  www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  768;
}


http {
    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
proxy_temp_path /var/tmp;
include       /etc/nginx/mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;

gzip  on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/sml-rss text/javascript;
gzip_buffers 16 8k;
upstream ghost_upstream {
    server 127.0.0.1:2368;
keepalive 64;
}

include /etc/nginx/conf.d/*.conf;
}

Now we will add a configuration file for our new Ghost website to the /etc/nginx/conf.d folder called ghost.conf.

server {
listen 80;

server_name YOUR_DOMAIN www.YOUR_DOMAIN;

if ($host = 'YOUR_DOMAIN' ) {
    rewrite  ^/(.*)$  http://www.YOUR_DOMAIN/$1  permanent;
}

#        location ~ ^/(ghost/signup/) {
#                rewrite ^/(.*)$ http://YOUR_DOMAIN/ permanent;
#        }

location ~ ^/(img/|css/|lib/|vendor/|fonts/|robots.txt|humans.txt) {
  root /var/www/ghost/core/client/assets;
  access_log off;
  expires max;
}

location ~ ^/(shared/|built/) {
  root /var/www/ghost/core;
  access_log off;
  expires max;
}

location ~ ^/(favicon.ico) {
  root /var/www/ghost/core/shared;
  access_log off;
  expires max;
}

location ~ ^/(content/images/) {
  root /var/www/ghost/;
  access_log off;
  expires max;
}

location / {
  proxy_redirect off;
  proxy_set_header   X-Real-IP            $remote_addr;
  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
  proxy_set_header   X-Forwarded-Proto $scheme;
  proxy_set_header   Host                   $http_host;
  proxy_set_header   Connection "";
  proxy_http_version 1.1;
  proxy_cache one;
  proxy_cache_key ghost$request_uri$scheme;
  proxy_pass         http://ghost_upstream;
}
}

We will install mysql server and configure it for your ghost install

# apt-get -y install install mysql-client mysql-server
# mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1427
Server version: 5.1.66-0+squeeze1 (Debian)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

At th mysql prompt please use the following commands:

create database ghostdev;
create database ghost;
grant all privileges on ghost.* to 'ghost'@'localhost' identified by 'YOUR_PASSWORD';
grant all privileges on ghostdev.* to 'ghost'@'localhost' identified by 'YOUR_PASSWORD';
flush privileges;
quit;

Now we can download ghost and install it.

# mkdir ghost
# cd ghost
ghost# wget --no-check-certificate https://ghost.org/zip/ghost-0.4.1.zip
# unzip ghost-0.4.1.zip
# rm ghost-0.4.1.zip

Will copy the ghost folder into the web path /var/www/

# cp -r ghost /var/www

We will switch to the ghost web path and run these commands

# cd /var/www/ghost
# npm install --production
# npm install mysql
# npm install forever -g

We are using forever to keep the Ghost running in the background and monitor the process and restart it if it does crash.
We have to create a configuration file into the /var/www/ghost folder. I prefer to copy the example file to config.js and then modify it for your needs

# cp config.example.js config.js

We will change the development and production section of the file to cover the database settings:

config = {
development: {
    database: {
        client: 'mysql',
        connection: {
            host    : 'localhost',
            user    : 'ghost',
            password: 'YOUR_PASSWORD',
            database: 'ghostdev',
            charset : 'utf8'
       },
       debug: false
   },
   server: {
      // Host to be passed to node's `net.Server#listen()`
      host: '127.0.0.1',
      // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
      port: '2368'
   }
},


production: {
    url: 'http://YOUR_DOMAIN',
    mail: {},
    database: {
        client: 'mysql',
        connection: {
            host    : 'localhost',
            user    : 'ghost',
            password: 'YOUR_PASSWORD',
            database: 'ghost',
            charset : 'utf8'
            },
        debug: false
    },
    server: {
        // Host to be passed to node's `net.Server#listen()`
        host: '127.0.0.1',
        // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
        port: '2368'
    }
},

Then we start the ghost and we restart the nginx

# NODE_ENV=production forever restart --sourceDir /var/www/ghost index.js
 # service nginx restart    

We will add the ghost startup command to the cronjob so that it would start when the server starts.

#crontab -e
@reboot NODE_ENV=production forever restart --sourceDir /var/www/ghost index.js

This are all the steps you need to create your own Ghost blogging system. Have fun and happy blogging.

Note: This tutorial asumes that you will have root access to your system.