Installing Ghost on Ubuntu, Nignx and MySQL - Part 3

May 12, 2014

In continuation of Installing Ghost on Ubuntu, Nignx and MySQL - Part 2

Finally we arrive at the Ghost part mostly...

Install Node.js

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

Step: Check node.js version (v4.5.0 - in my case). This will also install npm (node package manager - v2.15.9 - in my case). If by any case npm is not installed then do sudo apt-get install npm

node -v
npm -v

Install MySQL

sudo apt-get update
sudo apt-get install mysql-client mysql-server

This will install MySQL 5.7.13 by default, during installation process you will be prompted to enter / set password for the MySQL root user (make sure it is complex enough, do not ignore).

You can always change password of your root user of MySQL by following command:

sudo dpkg-reconfigure mysql-server-5.7

Check status of your MySQL and to start / stop MySQL use following command:

sudo netstat -tap | grep mysql
sudo service mysql start
sudo service mysql stop

Step: Setup database and user for Ghost. Go to MySQL CLI (will ask for root password you set during installation)

mysql -uroot -p

Step: Create database. Note: Do not forget the ; at the end of the line.

create database <database-name>;

Step: Create user (access local) and set password.

create user '<username>'@'localhost' identified by '<your password>';

Step: Grant privileges to newly created user on the newly created database

grant all privileges on <database-name>.* to '<username>'@'localhost';
flush privileges;
quit

Install Nginx

Nginx is a high performance, lightweight web server package.

Step: Install nginx

sudo apt-get update
sudo apt-get install nginx

Step: Start / stop nginx using follow command

sudo service nginx start
sudo service nginx stop

Step: Command to check if nginx is started

ifconfig eth0 | grep inet | awk '{ print $2}'

Step: Command to make sure nginx start after reboot

update-rc.d nginx defaults

Step: Create configuration for your Ghost site. Replace to the domain (or sub-domain) name you want to host your Ghost with.

sudo nano /etc/nginx/conf.d/<domain-name>.conf

Step: Paste or write down following lines. Note change the "domain-name" with your domain name (including brackets). Also port 2525 in below code can be any port as per your wish.

server {
	listen 80;
    server_name <domain-name> www.<domain-name>;
    
    client_max_body_size 10M;
    
    location / {
    	proxy_pass http://localhost:2368/;
        proxy_redirect off;
        proxy_set_header HOST $host;
        proxy_buffering off;
    }
    
    location ~^/ghost/(signup) {
    	return 404;
    }
}

Save this file (Ctrl + x > Y > Enter).

Note: You can skip

location ~^/ghost/(signup) {
    	return 404;
    }

in above code till you do not signup on your ghost blog. (This block of code will prevent anyone to signup or create account on your blog).

Step: Create cache directory for webserver

sudo mkdir /var/cache/nginx
sudo chown www-data:www-data /var/cache/nginx

Step: Create web root directory and directory for the ghost we need to install for your domain.

sudo mkdir /var/www
sudo chown www-data:www-data /var/www
sudo mkdir /var/www/<domain-name>
sudo chown www-data:www-data /var/www/<domain-name>

Step: Remove default file if found in any of the following directories:
/etc/nginx/conf.d/, /etc/nginx/sites-available/ or /etc/nginx/sites-enabled.

Get Ghost

Step: Prepare for "Go get ghost" step.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y zip wget

Step: Navigate to /var/www/<domain-name>/ directory and download ghost. Then we will unzip the ghost package and install ghost (this will install all packages that is require it to run).

cd /var/www/<domain-name>/
sudo wget http://ghost.org/zip/ghost-latest.zip
sudo unzip -d ghost ghost-latest.zip
sudo mv /var/www/<domain-name>/ghost/* /var/www/<domain-name>
sudo npm install --production

Step: Edit ghost - config file to modify and add Ghost configurations.

sudo nano config.example.js

Step: Go to ### Production section and modify it to make it look similar to this

production {
	url: 'http://<domain-name>,
    mail: {
    	transport: 'SMTP',
        options: {
        	service: 'Gmail',
            auth: {
            	user: 'mail-sender-email',
                pass: 'mail-sender-password'
            }
        }
    },
    database: {
    	client: 'mysql',
        connection: {
        	host: 'localhost',
            user: 'demouser01',
            password: '<db-user-password>',
            database: '<database-name>',
            charset: 'utf8'
        },
        debug: false
    },
    server: {
    	host: '127.0.0.1',
        port: '2368'
    }
},

You can use other mail provider to setup mail sending configurations. Change the 2525 port in server section to the post you decided to use (ref: /etc/nginx/conf.d/<domain-name>.conf)

Step: Check if Ghost is ready and runs as expected or not

sudo npm start --production

This will create a copy of config.example.js and create config.js which will be used by Ghost to run.
If Ghost was configured correctly then you will see ghost home page at http:// with casper (default theme).

Step: Navigate to http://domain-name/ghost/signup to create new user for writing ghost posts and access backend at http://domain-name/ghost/

Step: Setup forever to run this ghost instance continuously

npm install forever -g

Step: You can start and stop this ghost instance using following command

NODE_ENV=production forever start index.js
forever stop index.js
forever list

Here remember to run those commands from the root folder where your ghosts files are residing. In this example case it is /var/www/<domain-name>/

Enjoy your Ghost !!