Tricks on Hosting Multiple WordPress Sites

(versi indonesia ada di sini)

Assume our server, with domain science.com, is prepared to host some wordpress-based sites. Each site has its own sub-domain called :

  • math.science.com
  • physics.science.com
  • chem.science.com

To satisfy the whole task, some steps are required:

  • adding subdomain entries at DNS server
  • creating virtual hosts at Web server
  • installing wordpress on each virtual host

Assume the subdomain entries are already there. Next, most administrators tend to copy wordpress’ folder three times, each for one site. Let’s say, there will be three folders :

  • wordpress-math/
  • wordpress-physics/
  • wordpress-chem/

After that, they make three different databases and its configurations, which written to wp-config.php file inside each wordpress’ folder. Finally, every folder above becomes the document root of its predefined virtual host. For example :

  • math.science.com -> wordpress-math/
  • physics.science.com -> wordpress-physics/
  • chem.science.com -> wordpress-chem/

This common technique provides simplicity, but resulting some maintenance limitations. For example, if administrator wants to add extra plugins or themes to all sites, he’ll have to copy them three times to seperate folders. Imagine if our server hosts as many as thousands or even millions sites, there will be no time for that.

I have tricky technique to resolve such problem. I just need one copy of wordpress’ folder as the document root for all sites or virtual hosts, let folder wp-master/ be it. Thus, if I add any themes or plugins inside the folder, they will be visible to all sites instantly.

To differ one site to another, I write different database configuration files, one for each site. This trick works by modifying the only one wp-config.php inside wp-master/ to something like this :
$curSite = $_SERVER[’SERVER_NAME’];
$defaultSite = ‘default.science.com’;
$pathToConfig = ‘/home/web/wp-config/’;
if (file_exists($pathToConfig . $curSite . ‘.php’))
include($pathToConfig . $curSite . ‘.php’);
else
include($pathToConfig . $defaultSite . ‘.php’)
$table_prefix = ‘wp_’;
define (’WPLANG’, ”);
define(’ABSPATH’, dirname(__FILE__).’/');
require_once(ABSPATH.’wp-settings.php’);

You see, this file doesn’t include any database definitions (eg. DBNAME, DBUSER), because those are the things we’re gonna separate to each virtual host. The above script calls another PHP file located inside wp-config/ folder. The filenames are predefined by SERVER_NAME variable. Thus, according to above illustration, there will be three files inside wp-config folder :

  • math.science.com.php
  • physics.science.com.php
  • chem.science.com.php

Below the sample content of each file :
define(’DB_NAME’, ‘wpdb_math’);
define(’DB_USER’, ‘wpdb_math_user’);
define(’DB_PASSWORD’, ‘wpdb_math_pass’);
define(’DB_HOST’, ‘localhost’);
define(’DB_CHARSET’, ‘utf8′);
define(’DB_COLLATE’, ”);
?>

Don’t forget to add default.science.com.php, as visitor might mis-typed the site’s URL.

With similar technique, we can use one database for all sites, but with different “db_prefix”. Happy wordpress-ing!

3 Responses to Tricks on Hosting Multiple WordPress Sites

  1. davidtong says:

    Hi,

    Thanks for writing this. I’m currently trying to create the same thing, 3 WP sites under one host.

    site1.mydomain.com
    site2.mydomain.com
    site3.mydomain.com

    I did what you assumed in your first few paragraphs, creating subdomains and having subfolders assigned to it.

    My prob is, i can only access site1.mydomain.com even if i’m trying to access site2 or site3 directly. Even if I enter site2.mydomain.com/wp-admin/profile.pho, for example, it brings me to site1’s WP login screen.

    I’m trying to figure out your simplified solution where only one master directory is required but I got confused.

    I assume the following lines will replace the first block of server instructions for wp-config.php?
    _______________________________________________
    $curSite = $_SERVER[’SERVER_NAME’];
    $defaultSite = ‘default.science.com’;
    $pathToConfig = ‘/home/web/wp-config/’;
    if (file_exists($pathToConfig . $curSite . ‘.php’))
    include($pathToConfig . $curSite . ‘.php’);
    else
    include($pathToConfig . $defaultSite . ‘.php’)
    $table_prefix = ‘wp_’;
    define (’WPLANG’, ”);
    define(’ABSPATH’, dirname(__FILE__).’/’);
    require_once(ABSPATH.’wp-settings.php’);

    ______________________________________-

    What I don’t understand are:
    1) What are the three new files when you said “Thus, according to above illustration, there will be three files inside wp-config folder :”
    2) Where should they be stored?
    3) What’s inside those three files?

    Sorry I’m a noob. Hope you can find time to reply🙂

  2. Sergiu says:

    Your solution is wonderful, but there is a problem.

    As you know, WordPress designers modify, from time to time, table names / database structure.

    You solved the files problem. But there is also the databases problem.

    Even if you use the same database, with different tables, when you upgrade the files you still have to change MANUALLY all the affected tables from the database. I admit that, on SOME WordPress upgrades, they don’t change the database structure. But, on other upgrades, the changes are huge.

  3. This publish have been very properly written, and in addition, it contains several beneficial information. I loved the expert way with words this sort of submit. You fully grasp this easy will certainly comprehend.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: