That is so PUNK ROCK!

17Dec/06Off

How to run multiple WordPress blogs on the same server using a single install base

Intro

I have two blogs. One is in Turkish Safsata.org, the other is this one, which as you might have guessed already, is in English. I wanted to find an easy way to maintain both blogs and keep up with updates and security fixes. Running two seperate complete installs would mean twice the effort so I decided to try and see if two domains can be run from one directory of source (that is, php source mostly) files.

There's already good documentation about a similar situation I found online. If it suits your needs, you should use that more complete solution.

Scenario

OK. Here's our scenario. We have access to a web server and we have two seperate domains on this server. These domains may or may not have their own domain names. It doesn't matter. We want to install the WordPress files on a single directory and have both (or more than two) blogs be served from this common base. You still have to have a seperate database for each blog since the data (posts, user accounts, comments, settings) etc would be different per blog.

Steps

I already had one of the blogs installed. I moved the source files from this directory to a wordpress sources directory in my home directory. Where you put the files really doesn't matter as long as the web server can access those files. Here's what that directory looks like.

-bash-2.05b$ ls -1a /home/oktay/wordpress/source
.
..
.htaccess
index.php
license.txt
readme.html
wp-admin
wp-atom.php
wp-blog-header.php
wp-comments-post.php
wp-commentsrss2.php
wp-config-sample.php
wp-config.php
wp-config.php.bkp
wp-content
wp-feed.php
wp-includes
wp-links-opml.php
wp-login.php
wp-mail.php
wp-pass.php
wp-rdf.php
wp-register.php
wp-rss.php
wp-rss2.php
wp-settings.php
wp-trackback.php
xmlrpc.php

There are two important files here which need to provide the different settings for each blog. These files are wp-config.php and .htaccess the latter only being necessary if you want to use pretty urls, and when I call them pretty, how can you not use it right? :)
Once we have the files in place, we need to make sure that the blogs point to these files on the filesystem.

On my setup I prefer to keep the base of the domain as a non-blog site (not that I have anything there yet) and have the blog on a subdirectory.

It looks like this for my sites:

On the document root for safsata.org there's a symlink to the source files by the name 'saf'. This way the blog ends up at http://safsata.org/saf .

saf -> /home/oktay/wordpress/source/

The link for thatissopunkrock.org looks like:

punk -> /home/oktay/wordpress/source/

Likewise, the blog url for this site is http://thatissopunkrock.org/punk . (Actually having a different subdirectory name for each blog causes a problem with .htaccess which we'll fix later on)

At this point you can access both sites on your browser. If you don't mind both blogs being identical that is. To me this feels slightly counter to the subject of this post, so we shall tackle this nuisance :).

The reason both sites are identical is because they are both using the same database. What we need to do is to create a dump of the existing database, and import it back into a database with a different name. (If you have only one database on the server, you can also play with the table prefix settings but that's not explained here). Please refer to documentation on how to actually accomplish this.

What follows is the actual meaty part of this article. If we're going to have seperate databases serving these blogs, we have to configure this in wp-config.php . However, since we're sharing this file we can't just edit it. We have to put a little bit of logic into the file. (This is but one solution. Many solutions are possible involving symlinks, more coding etc)

[php]

if($_SERVER['HTTP_HOST']=='safsata.org') {
define('DB_NAME', 'safsata_wp');
define('DB_USER', 'safsata_wp');
define('DB_PASSWORD', 'PASS HERE');
define('DB_HOST', 'localhost');
}elseif($_SERVER['HTTP_HOST']=='thatissopunkrock.org') {
define('DB_NAME', 'tispr_wp');
define('DB_USER', 'tispr_wp');
define('DB_PASSWORD', 'PASS HERE');
define('DB_HOST', 'localhost');
}else{
die("no such thing is setup here");
}

[/php]

Basically the config file now knows to check the hostname of the requested blog and look at the correct database accordingly.

However, we stil do have two identical blogs. (Darn!) Worse, we can't really change the options on the web interface of the second blog because the url parameters are still set to the first blog meaning every link you click will take you to the first blog.

We will fix this with a database query on the second blog's database.

[sql]

update wp_options set option_value = 'http://thatissopunkrock.org/punk' where option_name = 'siteurl';

[/sql]

Now you can log into the second blog and make all the rest of the changes through the admin interface. All previous themes, plugins etc will be available. However, future ones need to be activated on each blog seperately. (You're still only downloading those once into the common source directory)

Conclusion

So what's the benefit of this setup? Why not just install two instances?

First of all. That is a valid solution as well. If you'd rather do that, please go ahead and do it. However, if you are going to repeat the same process for fifty blogs, I think there's some value in using a common source directory.

As a bonus, you'll only need to upgrade your files once if there's a security fix or an upgrade. And you don't need to download/install themes/plugins more than once.

We're almost done. Almost. The pretty urls don't work yet, because the rewrite rules in .htaccess are pointing to the first blog. Now we could fix this the elegant way by modifying our web server configuration so that each host would have a seperate .htaccess file (such as .htaccess-saf and .htaccess.punk etc). However, there's a faster hack for this. Again, if you have the same subdirectory on each of your domains (like a normal person would :P), you won't need this hack.

The reason your pretty urls don't work is because (using my setup as an example) the second blog is still seeing the rewrite rules and looking for a directory called 'saf' which is not there. (Remember the blog is at 'punk'). We work around this by just creating a new symlink with the same name, so our rewrite rules will be happy. Since this all happens in the background, we'll never see 'saf' mentioned anywhere on the second blog.

So taking my setup as an example again, the new symlink on the document root for http://thatissopunkrock.org/punk (second blog) would look like this:

saf -> punk

Well. There you have it. Sorta. Questions/Comments, use the Contact form.

Filed under: Tips/Tricks Comments Off
Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.