Hosting Multiple Blogs With a Single WordPress Installation

May 4, 2009 Development, Internet, Technology

This post is going to be the first in a short series outlining how anyone can take measures to simplify WordPress installations on shared or private hosting solutions, improving security and reducing maintenance time.

Getting Many from OneIt’s a common problem: We have dozens of WordPress installations on our web server(s), and updating each and every site when a plugin is updated or a new release of WordPress becomes available is a huge time sink.  WordPress MU looks nice but installation is pretty complicated for most people and it’s outright forbidden on most shared hosting accounts.  So what can we do to simplify software management?

Why not run each WordPress site from the very same source?

Madness, you say?  I disagree.  As of this post, I am running 9 web sites with a single installation of WordPress, and there will be another 8 added (a.k.a. rebuilt from backups) over the next week.

Here’s how you do it.

Multi-Site, Multi-Domain, or Both?

The first thing we need to identify is whether you’ll be hosting multiple sites (sub1.example.com, sub2.example.com, etc.) , multiple domains (www.domain1.com, www.domain2.com, etc.), or both configurations.  The reason this is important is because it will play a role in how we set up WordPress’ config.php file and where the installation files are put.  For the sake of this example, I’ll show you how to handle both multi-site and multi-domain.

First, you’ll need to install WordPress to some public-facing directory on your server.  For example, www.YourDomain.com/wp/.  From there, install WordPress normally.  We’ll be making the super-easy changes in a few minutes.

Note: If your main site (www.YourDomain.com) will be using the very same installation of WordPress, be sure to copy the index.php file in /wp/ to your website, making sure that you’re pointing to the WordPress installation folder.  Here’s an example:

/** Loads the WordPress Environment and Template */
require('./wp/wp-blog-header.php');

Be sure not delete index.php from the /wp/ folder. The original file will be used for every other site.

We’re almost done the hard stuff!  Now it’s time for the sweet stuff.  But, before getting too far ahead of ourselves, we need to answer a question:

Single Database or Multi-Database?

The nice little trick I’m about to show you will allow you to put every blog into a single database, or into multiple databases.  It’s really up to you how you proceed.  For the sake of this example, I’m going to show you how to handle multiple databases, including shared databases.

Add the following code just before the line that reads define ('DB_NAME', 'YourDatabaseName'); :

$host = $_SERVER['HTTP_HOST'];
$parts = explode('.',$host);
if ($parts[3] = "") {
$domain = "www." . $parts[0];
} else {
$domain = $parts[0] . "." . $parts[1];
}switch ($domain) {
case "www.YourDomain":
$pre = "wp0_";
$db = "DatabaseOne";
break;
case "sub1.YourDomain":
$pre = "wp1_";
$db = "SharedDatabase";
break;
case "sub2.YourDomain":
$pre = "wp2_";
$db = "SharedDatabase";
break;
case "www.DomainTwo":
$pre = "wp_";
$db = "DatabaseTwo";
break;
case "sub1.DomainTwo":
$pre = "sub1_";
$db = "DatabaseTwo";
break;
}

From here, we need to  put the variable names into the declaration lines like so:

define('DB_NAME', $db);
$table_prefix = $pre;

We’re almost done!  But, before going further, you have probably noticed that each one of these three databases would need to have the same username and password.  If you want to have different User/Pass credentials for different databases, you can add more variables to the case statement.

Now the magic …

Adding Domains and Sub-Domains

When adding a WordPress-powered domain or sub-domain, simply point the home address to your WordPress installation folder.  For example:

www.YourDomain.com -> /
sub1.YourDomain.com -> /wp/
sub2.YourDomain.com -> /wp/
www.DomainTwo.com -> /wp/
sub1.DomainTwo.com -> /wp/

You’re probably noticing that each of these domains will be using the very same wp-content/uploads directory for media.  If this is a problem, it can be changed in WordPress to something more your liking.  In my case, I created yet another sub-domain called “uploads” and pointed it to /wp/uploads/.  From there, each domain has their own subdirectory.  The advantage here is that all uploads are in their own little corner of the server and can be easily backed up or restored in the future.

Done and Done?

Weighing Pros and ConsSo, that’s all there is to it.  This is not incredibly difficult to implement and comes with advantages such as being able to update every blog’s core files and plugins simultaneously.  That said, there are some plugins that will not work well under this configuration.

If your sites experience high traffic volumes and you want to use WP Super Cache, it will only work on one site.  This is because the cache directory is set in wp-content/cache.  Unfortunately, this means that if two pages were to use WP Super Cache, they would write and access conflicting files.  I have also noticed some /tmp accessing problems while using an older version of FireStats, but I’ll be testing the more current releases to see if they’re more friendly to this configuration.

Of course, the biggst disadvantage to this setup would come if you are sharing sites with friends or family.  The newer versions of WordPress allow people to install plugins directly from the admin panel, and this can be a problem if one of these plugins contains a hidden exploit.  Rather than damange just one blog, it could damage many.  However, with proper education, you can make sure that other people do not cause trouble for everyone else.

So there you have it.  A quick and simple way to share one WordPress installation across multiple sites.  In the next installment of this series, I’ll show you how to take multiple WordPress databases and merge them into one big database without doing several Import/Export functions through the WordPress admin screens.

Tags: , , ,

Comments (6)

 

  1. Joe says:

    my god…I just started looking into this 5 minutes ago and this pops up in my rss reader :shock:

  2. … or you could simply install b2evolution and do all of the the above with no code hackery needed.

    http://b2evolution.net/

    :twisted:

  3. Jason says:

    @Joe – I hope this helps you along the way :eek:

    @Adrian – Where’s the fun in using a package that’s designed for such a thing? People don’t want to do things the easy way. Just look at Japan’s Pension system as an example of doing things the hard way for the sake of being hard :wink:

    That said, b2evolution still looks like something worth checking out.

  4. Victor Teixeira says:

    Actually I think WPMU is the best choice here since you can switch between dashboards without having to login on each blog.

    On WPmu plugins such WP Super Cache will work.

    And I have to tell you that the installation of wpmu is simpler than wordpress since it has an install script and you don’t have to edit any config file.

    On shared webhosts runing Cpanel the configuration of wildcard subdomains is just a matter of writing * as your subdomain. And if you want each blog to have their on domain name, there’s more than one plugin that does exactly this.

  5. Jason says:

    @Victor – For hosts that allow WordPress MU to be installed, you’re right. However, there are several popular shared hosts out there that will not allow WPMU to be installed for various reasons. The solution here is just one way to get around the problem :wink:

  6. KeHoeff says:

    hey this is a very interesting article!

Leave a Reply