In this post, we'll install Node.js, MySQL, Nginx (engine-x), Ghost and Supervisor. In subsequent posts, we'll secure the Ghost admin interface and install some free themes.

Install Node.js

Ghost is written in JavaScript and requires the Node.js library and runtime environment. To install Node.js, enter the following commands:

sudo apt-add-repository ppa:chris-lea/node.js 
sudo apt-get update 
sudo apt-get install nodejs

Note: If your missing 'apt-add-repository':

sudo apt-get install python-software-properties

To check that Node.js has been installed, enter the following command:

node -v

You should then see output like:

v0.10.26

Install MySQL

To install MySQL, enter the following command:

sudo apt-get install mysql-client mysql-server

It will prompt you to set a root password. Write it down and keep it in a safe place.

Now, we need to use MySQL's command line interface to create a database and a user for Ghost (it will prompt you for the root password):

mysql -uroot -p

You should then see output like:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23627
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

Copyright (c) 2000, 2013, 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> 

There are a few things we need to do but we'll take it a step at a time. At the mysql> prompt, enter the following command:

create database ghost;

Now that we have a Ghost database, we need to create a user:

create user 'ghost'@'localhost' identified by 'GHOST_PASSWORD';

Make sure to replace "GHOST_PASSWORD" with your password. Write it down and keep it in a safe place.

Now that we have a Ghost user, we need to setup the appropriate database permissions:

grant all privileges on ghost.* to 'ghost'@'localhost';

And, finally we need to ensure that all the changes are committed:

flush privileges;

Before we exit MySQL's command line interface:

quit

Install Nginx

To install Nginx, enter the following command:

sudo apt-get install nginx

The next step is to create a Nginx configuration file based on the default template:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/robferguson.org

Make sure you replace "robferguson.org" with your domain name.

Open your new configuration file with a text editor. I used nano:

sudo nano /etc/nginx/sites-available/robferguson.org

At this point, we only need to make a couple of changes:

server {
    listen                  80;
    server_name             robferguson.org;
    access_log 		     /var/log/nginx/robferguson_org.log;

    location / {
        proxy_pass          http://127.0.0.1:2368;
        proxy_redirect      off;
        proxy_set_header    Host             $host;
        proxy_set_header    X-Real-IP        $remote_addr;
        proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
    }	
}

We need to tell Nginx what port to listen to: 80, and our server's name: robferguson.org.

We also need to activate our site by creating a symbolic link between Nginx's sites-available directory and its sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/robferguson.org /etc/nginx/sites-enabled/robferguson.org

And finally, to avoid any confusion we need to delete the default Nginx configuration file:

sudo rm /etc/nginx/sites-enabled/default

To start Nginx, enter the following command:

sudo service nginx start

And, to ensure that Nginx will start automatically after a reboot, enter the following command:

sudo update-rc.d nginx defaults

Install Ghost

First, we need to get a copy of the most recent version of Ghost:

sudo mkdir -p /var/www
cd /var/www
sudo wget https://ghost.org/zip/ghost-latest.zip

Unpack it:

sudo unzip -d ghost ghost-latest.zip

Note: If you're missing 'unzip': sudo apt-get install unzip

And, install it:

cd /var/www/ghost
sudo npm install --production
sudo npm install mysql

The next step is to create a Ghost configuration file based on the example template:

sudo cp /var/www/ghost/config.example.js /var/www/ghost/config.js

Open it with a text editor. I used nano:

sudo nano /var/www/ghost/config.js

There are a couple of things we want to change but we'll take it a step at a time. Under the production declaration replace the default URL with yours:

production: {
    url: 'http://robferguson.org',

And, in the "database:" block update it as follows:

database: {
        client: 'mysql',
        connection: {
                host: 'localhost',
                user: 'ghost',
                password: 'GHOST_PASSWORD',
                database: 'ghost',
                charset: 'utf8'
        }
},

Make sure to replace "GHOST_PASSWORD" with your password.

Start, Stop and Restart Ghost

Supervisor is a popular Linux process control system that makes it easy to start, stop and restart applications.

Install Supervisor

To install Supervisor, enter the following command:

sudo apt-get install supervisor

The next step is to create a Supervisor configuration file for Ghost. Create a new configuration file with a text editor. I used nano:

sudo nano /etc/supervisor/conf.d/ghost.conf

And, update it as follows:

[program:ghost]
command = npm start
directory = /var/www/ghost
user = ghost
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/ghost.log
stderr_logfile = /var/log/supervisor/ghost_err.log
environment = NODE_ENV="production"

To start Supervisor, enter the following command:

sudo service supervisor start

To stop Supervisor, enter the following command:

sudo service supervisor stop

To start Ghost using Supervisor:

sudo supervisorctl start ghost

To stop Ghost using Supervisor:

sudo supervisorctl stop ghost

You should now be able to navigate to your new site:

robferguson.org

What's Next

In the next post, we'll secure the Ghost admin interface.