Ok, got it working. I’m not sure if this is the best way, or if there are pitfalls to doing it this way that I will run into in the future, but for now it works.
The premise was to have a single install of WordPress core, themes, and plugins, in one folder that is accessed by all sites on the server. Each site would have its own database and uploads folder.
Here is what I did.
-
create a folder to hold WordPress and the themes and plugins
/home/wordpress
-
create a folder that holds my websites.
/home/wordpress-sites
Each site has its own folder using the sitename (example.com) as the folder name. Each folder contains a db-config.php file and an uploads folder to store the sites uploads
-
I edit the wp-config.php global file and remove the database connection parameters and the database prefix from the file.
I replace that information with the following:
$website = strtolower( str_replace( “www.”, “”, $_SERVER[“SERVER_NAME”] ) );
$website = preg_replace(’[^a-z0-9.-]’, ‘’, $website );
if ( file_exists( dirname(FILE) . “/…/wordpress-sites/$website/db-config.php” ) ) {
require_once( dirname(FILE) . “/…/wordpress-sites/$website/db-config.php” );
} else {
echo “Sorry, no database configuration defined.”;
die;
}
Inside the db-config.php file for each site I basically add in the database connection parameters and the database prefix.
define( 'DB_NAME', 'dbname' );
define( 'DB_HOST', 'localhost' );
define( 'DB_PASSWORD', 'password' );
define( 'DB_USER', 'user' );
$table_prefix = 'wp_';
That allows each site to have its own database
I then add in a custom uploads folder definition
/* Change uploads path */
define(‘UPLOADS’, ‘domains/example.com/files’);
Now there is a problem with this in that the UPLOADS path CANNOT be an absolute path, it has to be a relative path which sucks and I have no idea why they do this but there is a way around it.
Inside the /home/wordpress folder I create a symlink to a folder I call domains and link that inside the /home/wordpress-websites/ folder.
I do this so that I can still use a relative path when defining the UPLOADS folder.
Finally I add in a ‘files’ folder inside the /home/wordpress-websites/example.com/ directory. I have to do this for each site, so that each site has its own folder to store uploads.
I don’t think this is the most elegant way of doing it but it works for me so far.
My whole goal in doing this was to make it so that the caching plugins wouldn’t have to cache 50 copies of WordPress, and 50 copies of all the themes and plugins on the server. I’m guessing it takes about 40-50MBs of cache memory for each site on the server. Multiply that by 50 sites and the ram resources add up fast. This way I am hoping that each cache plugin sees all the files as the same, thus only having to cache one copy.
People much smarter than myself when it comes to caching would have to answer that question. I truly don’t know the answer yet, but will reply back when I do with the results.
Currently there is not a guide anywhere on the net that I could find that really talks about properly setting up a single codebase for multiple WordPress sites. I am hoping that this info will lead to a great article by the real WordPress experts so that we can all learn more.