on Install Ghost with Nginx on Ubuntu

With its beautiful design and simple interface Ghost is becoming one of the most popular blogging platforms today. In this article, we will look at how to install the latest version of Ghost on an Ubuntu machine.

Install Node.js

Ghost is built on Node.js, so we need to make sure it is installed on our machine. If you don't have Node.js, you can install the latest version from the chris-lea PPA:

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

Install and Configure Ghost

Ghost comes with SQLite as a backend by default, which requires no configuration but it also supports MySQL and PostgreSQL.

Run the following command to download the latest Ghost version into your home directory:

wget -P ~/ https://ghost.org/zip/ghost-latest.zip

Unzip the file in the myGhostBlog directory (or whatever directory you want) and then delete the zip file.

unzip ~/ghost-latest.zip -d ~/myGhostBlog
rm -f ~/ghost-latest.zip 

To install Ghost change to the directory you extracted to and run the following:

cd ~/myGhostBlog
npm install --production

Once the installation is complete, run the following command to start the Ghost:

npm start --production

If the output looks something like the following, it means Ghost was successfully installed.

Ghost is running... 
Your blog is now available on http://my-ghost-blog.com 
Ctrl+C to shut down

Stop the process with Ctrl+C before continuing with the next steps.

To change my-ghost-blog.com with the URL of your domain run:

sed -i 's/my-ghost-blog.com/myGhostBlog.com/g' ~/myGhostBlog/config.js

Do not forget to replace myGhostBlog.com with your own domain name.

Create upstart job

Next we will create an upstart job to handle the starting/stopping of our Ghost blog.

sudo vim /etc/init/ghost-myGhostBlog.conf
description "myGhostBlog Ghost"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
respawn limit 5 60

env name=ghost-myGhostBlog
env uid=<USERNAME>
env gid=<USERNAME>
env daemon=/usr/bin/node
env path=/home/<USERNAME>/myGhostBlog/index.js

env NODE_ENV=production
export NODE_ENV=production

script
    exec start-stop-daemon \
        --start \
        --make-pidfile \
        --pidfile /var/run/$name.pid \
        --name $name \
        -c $uid:$gid \
        -x $daemon $path >> /var/log/upstart/$name.log 2>&1
end script

Now we can start our Ghost instance by simply running:

sudo service ghost-myGhostBlog start

Install and Configure Nginx

We will use Nginx as a reverse proxy in front of our Ghost blog. To install the latest stable Nginx version , run the following commands:

sudo apt-get install python-software-properties  
sudo add-apt-repository ppa:nginx/stable  
sudo apt-get update  
sudo apt-get install nginx  

Create a new Nginx serverblock for our Ghost installation:

sudo vim /etc/nginx/sites-available/myGhostBlog.conf

and copy paste the configuration below.

 upstream ghost {
  server 127.0.0.1:2368;
}

server {
  listen 80;
  server_name myGhostBlog.com www.myGhostBlog.com;

  access_log /var/log/nginx/myGhostBlog.com.access.log;
  error_log /var/log/nginx/myGhostBlog.com.error.log;

  location ~ ^/(robots.txt) {
      root /home/<USERNAME>/myGhostBlog;
  }

  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   Host $http_host;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   X-NginX-Proxy    true;
    proxy_set_header   Connection "";
    proxy_http_version 1.1;
    proxy_pass         http://ghost;
  }

}

Again, do not forget to change myGhostBlog.com and <USERNAME> with your domain and username.

The last step is to enable our site and restart Nginx.

sudo ln -s /etc/nginx/sites-available/myGhostBlog.conf /etc/nginx/sites-enabled/myGhostBlog.conf

Finally open http://myGhostBlog.com/ghost in your browser of choice and create your admin user.