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!

