Nginx Reverse Proxy with Ghost on Ubuntu 14.04

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 updateapt-get upgradeapt-get install python software-properties-common gcc g++ make -y  # auto installadd-apt-repository ppa:chris-lea/node.js -y

The output from these commands should be similar to:

gpg: keyring `/tmp/tmpvpe2ugzj/secring.gpg' createdgpg: keyring `/tmp/tmpvpe2ugzj/pubring.gpg' createdgpg: requesting key C7917B12 from hkp server keyserver.ubuntu.comgpg: /tmp/tmpvpe2ugzj/trustdb.gpg: trustdb createdgpg: key C7917B12: public key "Launchpad chrislea" importedgpg: Total number processed: 1gpg:               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/ghostnpm start --production

Output should look like this:

> ghost@0.5.2 start /usr/lib/node_modules/ghost> node indexMigrations: Database initialisation required for version 003...Migrations: CompleteGhost is running... Your blog is now available on 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-availabletouch 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;        proxy_redirect off;    }}

Symlink your configuration file:

cd /etc/nginxln -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 supervisorservice 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.jsdirectory = /usr/lib/node_modules/ghostuser = ghostautostart = trueautorestart = truestdout_logfile = /var/log/supervisor/ghost.logstderr_logfile = /var/log/supervisor/ghost_err.logenvironment = 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 ghostchown -R ghost /usr/lib/node_modules/ghost/supervisorctl rereadsupervisorctl 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

Submit your article
Suggest an update
Request an article

No comments

Powered by Blogger.