Tutorial

How To Create a Blog with Ghost and Nginx on Ubuntu 14.04

Published on June 28, 2015
How To Create a Blog with Ghost and Nginx on Ubuntu 14.04

Introduction

Ghost is a lightweight (~7.5MB), open source blogging platform which is really easy to use. Ghost is fully customizable. There are loads of themes available for Ghost on the Internet, free as well as paid.

In this tutorial, we will go through the steps to get Ghost setup and running on your Ubuntu 14.04 system. We will also install Nginx to proxy ports and install forever, a node package, to keep Ghost running in the background.

Prerequisites

There is no minimum size requirement for a server to run Ghost. Consider how many visitors your blog will get and how much content you plan to share when deciding what size Droplet to create. This tutorial was tested on the smallest size DigitalOcean Droplet running Ubuntu 14.04.

Before you begin, you need the following:

  • Ubuntu 14.04 Droplet
  • Registered domain name pointed to the IP address for your Droplet
  • A non-root user with sudo privileges

This tutorial will help you setup your domain name to point to your Droplet.

All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by sudo. Initial Server Setup with Ubuntu 14.04 explains how to add users and give them sudo access.

Step 1 — Install Node.js and Npm

You need to update your local package index and install the zip and wget packages. We will use them later in this tutorial.

  1. sudo apt-get update
  2. sudo apt-get install zip wget

Ghost requires Node.js v0.10.x (latest stable). Unstable versions of Node, like v0.12.x, are not supported. Node.js v0.10.36 and npm v2.5.0 are recommended by Ghost.org.

Install Node.js with the PPA method from this tutorial.

Once you have Node.js installed, check the version installed by running:

  1. node -v

The output should be similar to this:

v0.10.38

Check if npm installed:

  1. npm -v

It should output the installed version of npm if it is installed:

1.4.28

If it outputs an error that npm is not installed, install it with this command:

  1. sudo apt-get install npm

Update npm to version 2.5.0 by running the following command:

  1. sudo npm install npm@2.5.0 -g

Check the version of npm installed:

  1. npm -v

The output should be:

2.5.0

Step 2 — Install Ghost

Next we need to install Ghost. Ghost.org recommends to install Ghost in var/www/ghost, so that is where we will install it.

First, we will create a directory /var/www/ and then download the latest version of Ghost from Ghost’s GitHub repository:

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

Now that we have obtained the latest version of Ghost, we have to unzip it. We’ll also change our directory to /var/www/ghost/:

  1. sudo unzip -d ghost ghost-latest.zip
  2. cd ghost/

Now we can install the Ghost dependencies and node modules (production dependencies only):

  1. sudo npm install --production

Ghost is installed when this completes. We need to set up Ghost before we can start it.

Step 3 — Setting up Ghost

Ghost’s configuration file should be located at /var/www/ghost/config.js. However, no such file is installed with Ghost. Instead, the installation includes config.example.js.

Copy the example configuration file to the proper location. Be sure to copy instead of move so you have a copy of the original configuration file in case you need to revert your changes.

  1. sudo cp config.example.js config.js

Your URL and mail settings, which are in the production section, are the key areas of information which need modification. The URL is necessary. Otherwise, the links will take you to the default http://my-ghost-blog.com page. Ghost can function without the mail settings, but this is recommended that you add them. At the time of writing this article, Ghost only requires the mail functioning in case the user forgets their account password, so it wouldn’t do much harm not to configure mail.

Open the file for editing:

  1. sudo nano config.js

You have to change the value of url to whatever your domain is (or you could use your server’s IP address in case you don’t want to use a domain right now). This value must be in the form of a URL. For example, http://example.com/ or http://45.55.76.126/. If this value is not formatted correctly, Ghost will not start.

Also change the value of host in the server section to 0.0.0.0.

The following shows the values that need to be changed in red:

/var/www/ghost/config.js
var path = require('path'),
    config;

config = {
    // ### Production
    // When running Ghost in the wild, use the production environment
    // Configure your URL and mail settings here
    production: {
        url: 'http://my-ghost-blog.com',
        mail: {
            // Your mail settings
        },
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            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 s$
            port: '2368'
        }
    },

(...)

Save the file and exit the nano text editor by pressing CTRL+X then Y and finally ENTER.

While still in the /var/www/ghost directory, start Ghost with the following command:

  1. sudo npm start --production

The output should be similar to this:

> ghost@0.6.4 start /var/www/ghost
> node index

Migrations: Database initialisation required for version 003
Migrations: Creating tables...
Migrations: Creating table: posts

[...]

If all goes well, you should be able to access your blog using port 2368: http://your_domain._name:2368 (or http://your_servers_ip:2368).

Press CTRL+C in your terminal to shutdown the Ghost instance.

Note: Ghost can be customized further. Ghost.org explains each configuration option in detail.

Step 4 — Install Nginx

The next step is to install Nginx. Basically, it will allow connections on port 80 to connect through to the port that Ghost is running on. In simple words, you would be able to access your Ghost blog without adding the :2368.

Install it with the following command:

  1. sudo apt-get install nginx

Next, we will have to configure Nginx by changing our directory to /etc/nginx and removing the default file in /etc/nginx/sites-enabled:

  1. cd /etc/nginx/
  2. sudo rm sites-enabled/default

We will create a new file in /etc/nginx/sites-available/ called ghost and open it with nano to edit it:

  1. sudo touch /etc/nginx/sites-available/ghost
  2. sudo nano /etc/nginx/sites-available/ghost

Paste the following code in the file and change the highlighted code in red to your domain name, or your servers IP address if you don’t want to add a domain now:

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

We will now symlink our configuration in sites-enabled:

  1. sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

We will restart Nginx:

  1. sudo service nginx restart

Next we will create a new user. This user would be granted only the privileges to do stuff in the directory /var/www/ghost. This is a security measure. If Ghost gets compromised, your system would be safe. This can be done by running this command:

  1. sudo adduser --shell /bin/bash --gecos 'Ghost application' ghost

We will grant privileges:

  1. sudo chown -R ghost:ghost /var/www/ghost/

You can now log in as the ghost user:

  1. su - ghost

Now we would need to start Ghost:

  1. cd /var/www/ghost
  2. npm start --production

You should be able to access your blog on port 80 as http://<your_server_ip>/ or http://<your_domain_name>/.

Step 5 — Keep Ghost Running with forever

The next step is to keep Ghost running in the background. forever is a node module which can be used to start Ghost in the background and monitor to make sure it stays up. If Ghost crashes, forever will automatically start another instance of Ghost.

Install forever with the following command from within your Ghost directory, i.e. /var/www/ghost. But before running the command log out of the ghost user and login to your non-root user:

  1. exit
  2. sudo npm install -g forever

Start Ghost as the ghost user. It also must be run from the Ghost directory:

  1. su - ghost
  2. cd /var/www/ghost
  3. forever start index.js

The output should be similar to this:

warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: index.js

By default, it loads in the development environment. This can be changed by running the following command:

  1. NODE_ENV=production forever start index.js

forever can be stopped by running this from the Ghost directory:

  1. forever stop index.js

Possible Errors

For the following error message:

Error: SQLITE_READONLY: attempt to write a readonly database

Start forever as a root user (type exit to logout the current user):

  1. sudo forever start index.js

If the last command says it cannot find ‘forever’, use the full path to the command:

  1. sudo /usr/local/bin/forever start index.js

If you see the following error:

error:   Cannot start forever
error:   script /home/ghost/index.js does not exist.

You are not in the /var/www/ghost directory. Change to this directory and execute the command again.

Conclusion

Congratulations! You have installed Ghost and learned how to proxy ports with Nginx. You have also learned how to keep tasks running with the forever node package.

There is a lot more you can do with Ghost. For example, a password-protected blog is one of the latest features.

Check out the other DigitalOcean tutorials on Ghost:

Also visit the following to learn more:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Default avatar
Tammy Fox

editor


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

wow - absolutely faultless tuturial !

1 thing:

In step 4 creating server block, the proxy_pass should be ‘0.0.0.0’ and not 127.0.0.1 (since we changed it in a previous step). Otherwise the domain name will not respond. Did not for me.

Thank you. Well written and easy to follow.

Hi there! Thanks for this tutorial. Just wondering, if I place a theme in my ghost/content/theme folder do I need to restart the service to see it in the ghost admin panel at my domain? I have one currently placed in there but when I look in the admin panel it only has the stock casper option. I don’t want to screw up anything so I’ll wait for a reply before monkeying about.

I was looking for a tutorial where you guys explain how to install Pencil Blue on a Ubuntu 14.04. Unfortunately, there is none. I was looking for alternatives for Ghost and i liked what i saw about Pencil Blue, so if someone has an article to share about installing Pencil Blue on Ubuntu 14.04 i will be very grateful.

Edit:

I’ve found it, even though it is written for Debian 8, I think that this tutorial will work for my server.

Its better to use supervisor instead of forever.

This tutorial does not cover how to run forever automatically after reboot, right?

If you get following error:

npm WARN This failure might be due to the use of legacy binary "node"

solve with this command before install ghost:

sudo apt-get install nodejs-legacy

Hi there, can you tell me how to install a Let’s Encrypt SSL certificate on a Ghost blog using the setup described here? I tried the instructions on the How To Secure Nginx with Let’s Encrypt on Ubuntu 16.04 tutorial but got an unauthorised error and invalid response. My domain is definitely pointed to the correct IP address and my Ghost configuration seems perfect.

Thanks.

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel