31 August 2008 6 Comments

Hosting multiple blogs on a single WordPress installation

As you can probably tell from this blog, WordPress is my favourite blogging tool. I’ve been using it for a couple of years now, and during that time I’ve been really impressed by it. So when I was recently asked if a single copy of WordPress could be used to power several blogs, I was optimistic that it would be up to the job.

And it didn’t disappoint me…

There are a number of ways you can host multiple blogs with WordPress:

  • WordPress MU (multi-user) – This is system that powers all blogs at wordpress.com, Le Monde, Harvard Univeristy etc…
  • Batch management of blogs with WP-Create and WP-Upgrade – These scripts let you install multiple blogs in parallel, however each one would get it’s own installation.
  • Modifying wp-config.php to choose a different database per hostname. This uses the standard wordpress scripts.

Since I wanted to use a standard wordpress installation, and I didn’t want to install it multiple times, I chose the 3rd option. Wordpress stores most of it’s configuration in it’s database, so all you need to do is modify wp-config.php to point at a different database depending on the hostname of the site being accessed:

// Ignore the www. part of a hostname
$host = eregi_replace('^www\.', '', $_SERVER['HTTP_HOST']);

switch ($host) {
        case 'site1.co.uk';
                $db = 'site1';
                break;
        case 'site2.co.uk';
                $db = 'site2';
                break;
        default:
                header("HTTP/1.0 404 Not Found");
                exit();
                break;
}

// ** MySQL settings ** //
define('DB_NAME', $db);    // The name of the database
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'hostname');

Simply add the existing database settings code with the code above, and then create a new empty database for each blog you want to host. You will need to run the install scripts for each blog, e.g. http://www.site1.co.uk/blog/wp-admin/install.php

References:

6 Responses to “Hosting multiple blogs on a single WordPress installation”

  1. antiquarian books 11 December 2008 at 1:15 pm #

    Thank you for this information. :)

  2. anmari 9 March 2009 at 12:56 am #

    Really, really hard to read your site – blue text on a dark blue background – the headings etc are okay in white, but the rest is awful. You need more contrast.

  3. Fossli 22 March 2009 at 2:23 pm #

    @anmari: wtf!?

  4. warren 14 June 2009 at 3:42 pm #

    I think this is not for noob, just mean for advance users.

  5. kurye 13 July 2009 at 9:17 am #

    Hosting multiple blogs on a single WordPress installation great article thank you.

  6. Paul Maunders 10 February 2010 at 8:37 pm #

    The site had a different design / theme in the past, and it was quite hard to read the text then so I have changed it to a better one!


Leave a Reply