How-to: Run multiple Ghost blogs (domains) on one machine

So, from the last post we’ve got our Amazon Linux EC2 instance running our Ghost blog. Its now running on port 80 and will auto start when the server boots. This guide will show you how to add more Ghost blogs to your EC2 instance using the same port.

Every web server listens to requests, and responds with resources on aport. Only one running process can use a given port at any time, so in order to have multiple Ghost blogs using port 80 we setup a proxy which will use port 80 (3000 actually, if you remember from the last guide) and pass traffic onto our blogs on ports 3001 and 3002.

Make sense?… Hopefully. Regardless, these steps should help you get it setup and learn through experience. You should do some research later on what http-proxy is capable of, and other options that are available to you.

The steps to do this:

  • Setup http-proxy
  • Adjust our existing blog’s config
  • Setup a 2nd blog

Let’s do this 🙂

Setup http-proxy

We’ll start by adding a new Node app for our proxy:

  • cd /home/ec2-user
  • mkdir http-proxy
  • cd http-proxy
  • vim app.js and add this javascript to the file:
    var http = require('http'),
        httpProxy = require('http-proxy');
    
    var options = {
      hostnameOnly: true,
      router: {
        'www.myblog1.com': '127.0.0.1:3001',
        'www.myblog2.com': '127.0.0.1:3002'
      }
    };
    
    var proxyServer = httpProxy.createServer(options);
    proxyServer.listen('3000');
    
  • npm install http-proxyThis installs the ‘http-proxy’ NPM module, like we installed forever in the last guide.
  • forever stopallThis stops running forever processes
  • node app.jsOk, you should see that the Node process starts fine. Go to your browser and browse to your blog url. You’ll see an error like this “An error has occurred: {“code”:”ECONNREFUSED”,”errno”:”ECONNREFUSED”,”syscall”:”connect”}” which is good. Its because we’ve setup the proxy but haven’t yet changed the config of the blog itself. Lastly, ctrl+c to exit Node.

Adjust our existing blog’s config

So port 80 is redirected to port 3000. We’ve configured the proxy to use port 3000 and we’ll now adjust our blog’s config to use port 3001 (as we’ve already configured the proxy to forward traffic for www.blog1.com to port 3001).

  • cd ../ghost
  • vim config.jsYour production server config should look like this:
    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 set this to `process.env.PORT`
                port: '3001'
    

Ok, save (Esc, :w) and exit (:q). Type the following:

  • NODE_ENV=production forever start index.jsWe’ve started our blog’s Node process again.
  • cd ../http-proxy
  • forever start app.jsWe’ve started our http-proxy’s Node process again.

Browse to your blog, it should appear correctly this time. If so, we have the blog operating correctly through the proxy. It’s time to add another blog!

Setup a 2nd blog

It’s almost the same as the first time we setup Ghost, except that we need to configure it on port 3002. Below are the steps:

  • cd /home/ec2-user
  • mkdir ghost2Use the name of your 2nd blog instead of ghost2!
  • cd ghost2
  • wget https://ghost.org/zip/ghost-0.3.3.zip
  • unzip *zip
  • rm -rf *zip
  • npm install –production
  • cp ../ghost/config.js .
  • vim config.js
    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 set this to `process.env.PORT`
                port: '3002'
    

Also edit the url to your blog address e.g. “http://www.bybigs.com“. To save and exit press Esc, :w, :q

If you now run “NODE_ENV=production forever start index.js” your blog will be accessible on your domain. If that’s true, then you now have two blogs operating through the http-proxy, both on port 80!!

The final step to ensure this setup remains after a reboot is to edit the shell script we created in the last guide. We’re nearly there.. the steps are below!

 

    • sudo vim /etc/profile.d/ghost.shEditing our shell script which runs on startup

Add the 2 lines before “fi”. These will auto-start our new Node server processes, the http-proxy and our 2nd blog. If you have more bogs, repeat the steps for adding the 2nd blog over, and over (and over…)

#!/bin/sh

if [ $(ps aux | grep $USER | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
  sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 2368 >/home/ec2-user/iptables.log 2>&1
  export PATH=/usr/local/bin:$PATH
  export NODE_ENV=production
  cd /home/ec2-user/ghost && forever --spinSleepTime 10000 start index.js >> forever.log 2>&1
  cd /home/ec2-user/ghost2 && forever --spinSleepTime 10000 start index.js >> forever.log 2>&1
  cd /home/ec2-user/http-proxy && forever --spinSleepTime 10000 start index.js >> forever.log 2>&1
fi

Next guide I write will probably be on making your own theme. Until then…….. Enjoy.

Leave a Reply