Virtual WordPress Hosts Made Easy (obsolete)

December 15th, 2008 by  |  Published in Wordpress  |  2 Comments

Currently, we are planing to consolidate all our blogs that we (quite carelessly) created all over the internet. At the moment, we have

  • three blogs hosted on Blogger (1, 2, 3)
  • two blogs running on their own WordPress (1, 2)
  • three private blogs running on Blogger and Serendipity
  • and one or two new ones to come

All in all, there’s quite a bit of blogging going on 🙂

Yesterday, I was looking into solutions on how to host all those blogs on a single installation of WordPress.

The first thing I stumbled upon was WordPress MU. However, I didn’t have a good feeling about it. I wasn’t sure about compatibility of plugins, themes or tutorials and the like. Well, just the normal problems you have if you do things differently than everybody else.

Still I like doing things differently 😉 That’s why I started playing around with an idea suggested by Richard Martin (german only). He suggested to use $_SERVER[‘HTTP_HOST’] inside wp-config.php in order to get a unique $table_prefix for each virtual host. While I liked the idea to dynamically create the prefix, I didn’t like that the wp-config.php file would become useless outside a web request (what about cron jobs?). So I came up with the following snippet of code:

$table_prefix = getenv('WORDPRESS_PREFIX');
if (empty($table_prefix)) {
    $table_prefix = "wp_";
}
define ('WPLANG', getenv('WORDPRESS_LANG'));

Now, it’s easy to change the prefix (and language) with an environment variable. In order to use this with Apache Httpd do the following:

<VirtualHost *:80>
 
ServerName foo.example.com
#ServerAlias www.foo.example.com, oldfoo.example.com
 
ServerAdmin admin@example.com
 
DocumentRoot /path/to/wordpress
 
CustomLog "| /usr/bin/cronolog /var/log/wordpress/foo/%Y-%m-access.log" common
 
DirectoryIndex index.php
 
# Wordpress Config
SetEnv WORDPRESS_PREFIX foo_
SetEnv WORDPRESS_LANG de_DE
 
<Location />
Order Deny,Allow
Allow from all
 
AllowOverride All
</Location>
</VirtualHost>

It might still take a little fine tuning, but it’s working perfectly so far. Only a single WordPress installation to maintain, a single plugin and theme repository for all blogs and a single database to backup. Quite cool if you’re the only admin of all blogs. It might also be cool to write a WordPress plugin to create virtual WordPress hosts from the admin console – I leave this up as an exercise to the reader though 😉

The only thing to worry about is that some plugins write to the file system directly (e.g. Google XML Sitemaps). Those will conflict with each other as all blogs share the same directory on the file system.

PS: This might have been the last post on blogger. Which means that it might have been the last time, you’ve had the pleasure to enjoy the lovely green of this blog 😉

EDIT: This article is obsolete since WP3 now supports multiple bloge, i.e. a network of blogs. See Create A Network.

Tags: ,

Responses

  1. Talk On Tech now Molindo Techblog | Molindo Techblog says:

    January 23rd, 2009 at 11:00 am (#)

    […] since August 2007. Now, after 1.5 years it was time to move on … Well, okay, we just want to consolidate all our blogs on a single self-hosted platform. In the course of doing that, we also changed the name from Talk on Tech (Let’s create a […]

  2. Litstyday says:

    April 22nd, 2009 at 3:02 pm (#)

    mm. really like it ))

Leave a Response