Ghost (finally) playing nicely with apache on multi-site server

Finally got Ghost up and running! What better way to celebrate than to write a blog post to share my experience?

Maybe this can save you from piecing together all the other info elsewhere. Or, probably more likely than not, it becomes another confusing advice. Good luck, have fun.

To get started I installed Ghost locally in MacBook. No surprises there. It went smoothly: Installed node.js, downloaded source codes from http://ghost.org, played around with new themes, etc.

The tricky part was to set up Ghost to run in my server. It's a virtual server with Debian hosted in Rackspace. It was tricky because:

  • The server is already hosting multiple sites, each having its own domain name, using apache and its VirtualHost directive
  • Some of these sites are using wordpress
  • I'm not exactly a super-hero in server admin land

Btw, this was done using Ghost v0.3.3. Future versions may or may not be similar.

Install node.js

First I need to install node.js in debian. I followed the official node.js wiki on how to do that. Not exactly a no-brainer, but wasn't too bad.

One minor thing: the checkinstall command will ask you to "enter a description for the package". That's just a reference for yourself, probably so that you can refer back to/remove the package easily later. I think. I just entered nodejs and it was fine.

Install Ghost

Then I need to install Ghost. I made my first mistake here: I got the source codes by downloading from github as a zip file.

Don't do that. Otherwise, you are going to see the following error message in the browser when you go to your Ghost blog,

500

Failed to lookup view "index"

Stack Trace

Failed to lookup view "index"

at deliver (/root/ghost-src/Ghost-master/node_modules/when/when.js:319:7)
at runHandlers (/root/ghost-src/Ghost-master/node_modules/when/when.js:385:12)
at drainQueue (/root/ghost-src/Ghost-master/node_modules/when/when.js:836:3)

This SO post has helpfully pointed out that zip package downloaded from github does not contain the default theme. I actually then tried copying the default casper theme over, but it gave me some other errors.

Therefore, don't do that. Get the Ghost source codes from website instead: https://ghost.org/download/

Configure Apache

Next step is to configure apache so that my blog URL will point to Ghost. This is the scary part for me. Having used Apache to host multiple sites in the past, the VirtualHost directive in apache config file is still a bit of black magic to me.

I researched on how to run apache and node.js in parallel. In case you don't understand the problem yet: Apache listens on port 80 (usually), the default port for http request. Node.js listens on port 2368. If your server runs node.js only, that's a piece of cake, just change node.js so that it listens to port 80 instead. However if you run both apache and node.js, you will need to resolve this problem one way or another.

It seems that there are at least 3 approaches to do it:

I ended up using the 3rd approach of using VirtualHost in apache. You may want to divert traffic from a subdirectory or subdomain. See more details below.

Option 1: Set up Ghost blog URL as a sub directory to main site

(Edit: It has been confirmed that this option is not yet supported for Ghost v0.3.3, however it should work in v0.4.0)

I wanted to set up my Ghost blog on URL path like http://www.mydomain.com/blog/.

Before you continue reading this section, be aware that, I failed. Couldn't get it working. I got http://blog.mydomain.com/ working though, see the next section. I'll still share the experience of my failed attempt, may be it will help you, and I certainly hope to resolve it later on. I posted a question in the Ghost forum too.

I followed this SO post about running node.js and apache toegether and added this directive

ProxyPass /blog http://localhost:2368/

...to my apache VirtualHost configuration. Then, when I went to the expected URL http://www.mydomain.com/blog/ in browser, I saw the following in browser

Proxy Error

The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /blog/.

Reason: Error reading from remote server

...and the node.js process npm start crashed with this message in console.

The http request was apparently successfully diverted to node.js, as expected. But Ghost crashed for some reason.

Option 2: Set up Ghost blog URL as a sub domain of main site

I followed this post to make http://blog.mydomain.com/ work. Cut the long story short, I used the following directive in my Apache config file,

 <VirtualHost *:80>
     ServerName blog.example.com
     ProxyPreserveHost on
     ProxyPass / http://localhost:2368/
 </VirtualHost>

This way, traffic arriving to this machine for blog.example.com will be redirected to port 2368 in localhost, which is where node.js listens on. You will need to change blog.example.com to your own of course.

Finally

My Ghost blog is working along side my apache sites. I'm happy!

Ghost (finally) playing nicely with apache on multi-site server
Share this