Nginx Reverse Proxy with Ghost on Ubuntu 14.04

Published on: Fri, Sep 26, 2014 at 11:32 am EST
Linux Guides Node.js Ubuntu Web Servers

Ghost is a free and open source blogging platform written in node.js, completely customizable and dedicated for publishing.

Prepare the server: Update packages, Install Node.js and NPM

We'll demonstrate the installation by logging into the server as root, so that we will not need to add sudo before each command. If you are logged as another user, remember that you will need sudo.

On your server, run the following to update the package index, upgrade packages, and install nodejs and npm.

apt-get update
apt-get upgrade

apt-get install python software-properties-common gcc g++ make -y  # auto install
add-apt-repository ppa:chris-lea/node.js -y

The output from these commands should be similar to:

gpg: keyring `/tmp/tmpvpe2ugzj/secring.gpg' created
gpg: keyring `/tmp/tmpvpe2ugzj/pubring.gpg' created
gpg: requesting key C7917B12 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpvpe2ugzj/trustdb.gpg: trustdb created
gpg: key C7917B12: public key "Launchpad chrislea" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

Automatically, the package signing key will be added to the keyring (so that the packages downloaded can be verified), and the PPA will be added to APT's sources.list.d.

Now, let's run:

apt-get update  # again 
apt-get install nodejs -y

Run npm to test the installation. You will see npm's usage printed to the screen:

Usage: npm <command>
...
...
npm@1.4.28 /usr/lib/node_modules/npm

Now we can install Ghost.

Installation: Ghost and Nginx

Since Ghost is now considered "stable", it can be installed through npm. Install with the following command:

npm install -g ghost --production

Ignore any warnings for now.

Next, we will start Ghost and check if it's working properly.

cd /usr/lib/node_modules/ghost
npm start --production

Output should look like this:

> ghost@0.5.2 start /usr/lib/node_modules/ghost
> node index

Migrations: Database initialisation required for version 003
...
Migrations: Complete
Ghost is running... 
Your blog is now available on http://my-ghost-blog.com 
Ctrl+C to shut down

It works! Use Ctrl-C to shut down Ghost, and move on to the next step: installing (and configuring) Nginx.

Nginx is very simple to install. Run the following command:

apt-get install nginx

Nginx will be configured to allow connection from anywhere in the wild to port 80 (or 443, if using SSL) on your server, which is then forwarded ("proxied") to Ghost. This is how people connect to your blog.

Configuring Nginx is not that hard, either. Follow these steps to configure the Ghost proxy.

First, remove the default configuration file:

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

Then, make a new configuration file:

cd sites-available
touch ghost

Adapt the following lines to your need and use something like nano or vi to paste it in (you'll need to set server_name to your domain name):

server {
    listen 80;
    server_name yourdomain.tld;
    access_log /var/log/nginx/yourdomain.tld.log;  # if you want logging

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;       
        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
    }
}

Symlink your configuration file:

cd /etc/nginx
ln -s sites-available/ghost sites-enabled/ghost

Restart Nginx:

service nginx restart

At this point, Nginx is installed, configured, and running on your server.

Finally: Start Ghost Automatically

Supervisor is a process control system which allows you to run Ghost at startup without using init scripts. We will be installing Supervisor to manage Ghost.

To install Supervisor, run:

apt-get install supervisor
service supervisor start

Then, create a new script file in /etc/supervisor/conf.d/ghost.conf. Paste in these contents:

[program:ghost]
command = node /usr/lib/node_modules/ghost/index.js
directory = /usr/lib/node_modules/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"

Save and close the file.

Next, we need to create a user for Ghost and give it permissions to access the Ghost files and database. Run the following commands:

useradd ghost
chown -R ghost /usr/lib/node_modules/ghost/
supervisorctl reread
supervisorctl update

Our setup is complete!

Now you can control Ghost by executing supervisorctl start ghost and supervisorctl stop ghost.

Want to contribute ?

You could earn up to $300 by adding new articles