Serve Ghost and Nginx on Ubuntu 14.04 LTS

Recently I switched from Linode to DigitalOcean because of some rumor that Linode website is blocked in China. Here is a memo of how I migrated a Ghost blog, otakism.org.

Some content came from several tutorials from DigitalOcean community. Credits are given at the end of this article.

Install Nginx

Install Nginx via apt-get.

sudo apt-get update
sudo apt-get install nginx

Create Site Directories

I prefer /var/www as the root directory. To do that I created the following folders to hold my website files.

sudo mkdir -p /var/www/rakugaki.me/html
sudo mkdir -p /var/www/otakism.org/html
sudo chown -R $USER:$USER /var/www/rakugaki.me/html
sudo chown -R $USER:$USER /var/www/otakism.org/html
sudo chmod -R 755 /var/www

Create Server Block Files

Server blocks configure ports and root directories for each website, as well as details about how they should be served.

Here I used rakugaki.me as the default domain of my server.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/rakugaki.me
sudo vim /etc/nginx/sites-available/rakugaki.me

Change the configuration like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/rakugaki.me/html;
    index index.html index.htm;

    server_name rakugaki.me, www.rakugaki.me;

    location / {
        try_files $uri $uri/ =404;
    }
}

Then I created the second server block file for otakism.org. Since it is a Ghost blog, the file should be like this:

server {
    listen 80;
    listen [::]:80;
    server_name otakism.org;
    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}

The files are done but we are not done. Enable the server block files.

sudo ln -s /etc/nginx/sites-available/rakugaki.me /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/otakism.org /etc/nginx/sites-enabled/

Delete the default site generated by Nginx.

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

Restart Nginx to apply new settings.

sudo service nginx restart

Install Ghost

Prerequisites first. Ghost requires Node.js and npm. These can be installed in various ways. Refer to this article for more options.

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

Download Ghost. Details about the remaining steps in this chapter can be found in the official guide at Ghost.org.

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

cd /var/www/otakism.org/html
sudo wget https://ghost.org/zip/ghost-latest.zip
sudo unzip -d ghost ghost-latest.zip

Install it.

cd ghost/
sudo npm install --production

Configure Ghost

Change the url and/or port under production section.

sudo cp config.example.js config.js
sudo vim config.js

I also needed to migrate data, images and themes. Copy or replace them under content/.

Keep Ghost Running

Create upstart configurations to make the ghost blog start on system boot. In this way I can also manage my ghost blog as a service.

sudo /etc/init/vim ghost-otakism.conf

Paste the following lines.

# ghost-otakism

start on startup

script
    cd /var/www/otakism.org/html/ghost
    npm start --production
end script

Finally I can summon my ghost.

sudo service ghost-otakism start

Contents about Nginx setup credits to Justin Ellingwood, original post at How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 14.04 LTS.