12

I have seen some questions which look similar, but they all ended up with multisite. For maintainability, performance and security, I don't want to use multisite. So please bear with me.

This is what I'm thinking about:

.
|_____branch1 // for branch1.domain.com
|  |_____themes
|  |_____plugins
|
|_____branch2 // for branch2.domain.com
|  |_____themes
|  |_____plugins
|
|_____branch3 // for branch3.domain.com
|  |_____themes
|  |_____plugins
|
|_____index.php
|_____WordPress
|_____wp-config.php

As you can see, each domain has its own database and content directory but just one WordPress instance. Now, themes, plugins and databases become smaller and independent. Then, it would be much easier to maintain, scale...

But is it possible? If you have experienced the same problem before, please share your thoughts! I really appreciate your help.

6
  • 1
    Why was multisite eliminated as a possibility? Doing this will involve creating a fragile system that will be less maintainabable than multisite, have slower performance than multisite, and worse security than multisite. Some of the largest installs of WordPress are multisite installs, and it's all the same code that runs on a standard single site
    – Tom J Nowell
    Jun 13, 2016 at 14:35
  • I manage multiple WP multisites including one that has over 500 sub-sites. Performance will be the same whether you use one multisite or 500 WP instances unless you have the 500 instances on separate VMs and databases. Maintainability sucks with multisite? Try maintaining 500 number of single sites.
    – user42826
    Jun 13, 2016 at 18:43
  • @TomJNowell I'm not sure if that largest installs using only one database as it is but I think this and this video might bring us to a same page. We're mainly using WordPress for CRM and user privacy is very important.
    – wpclevel
    Jun 13, 2016 at 20:03
  • @user42826 Currently, my company don't have that much sites. I also cannot convert current site to a multisite and benchmark it. And hardware and other things might different from you. So I just want to ask about an optimal installation architecture in my case. As far as I know, multisite works with subdomains but it cannot works for different domains.
    – wpclevel
    Jun 13, 2016 at 20:10
  • Multisite works with different domains. We use a main domain, *.domain.com, and a few other domains, www.domain2.com and www.domain3.com. We use WP Domain mapping plugin to accomplish multidomains, but from what I hear WP core supports multidomain mapping now natively.
    – user42826
    Jun 13, 2016 at 20:16

2 Answers 2

11

As @tom-j-nowell said in comment to OP, multisite can make this easier.

Performance and security are not really a problem for multisite (at least, not more than they are for regular installations), but I do agree that multisite can sometimes be a problem, because a lot of plugins (either custom or 3rd party) may not work properly on multisite, or maybe because you want to keep users of different websites completely separated.

That said what you want to achieve is not that hard.

What you need to change between installation is:

  • plugins folder
  • themes folder
  • database settings

Those configuration can be done using constants in wp-config.php your only problem is how to switch them based on URL.

The server variable 'SERVER_NAME' should work for you, at least if your webserver is configured properly.

For example you can create a folder named /conf at the same level of wp-config.php file and /WordPress folder.

In that folder you can add some files:

  • branch1.domain.com.conf
  • branch2.domain.com.conf
  • branch3.domain.com.conf

inside each of them you can do something like

$branch = 'branch1';
$base_dir = dirname( __DIR__) . "/{$branch}";

defined( 'WP_CONTENT_DIR' ) or define( 'WP_CONTENT_DIR', $base_dir );

// be sure WP understand URLs correctly
defined( 'DB_HOME' ) or define( 'DB_HOME', "{$branch}.example.com" );
defined('WP_SITEURL') or define('WP_SITEURL', "{$branch}.example.com/WordPress");

// adjust DB settings  as needed
defined( 'DB_NAME' ) or define( 'DB_NAME', $branch );
defined( 'DB_USER' ) or define( 'DB_USER', $branch );
defined( 'DB_PASSWORD' ) or define( 'DB_PASSWORD', '********' );

unset( $base_dir, $branch );

This will change on each configuration file according to the "branch".

After that, in your unique wp-config.php you can so something like:

$defaults_conf = [
  'WP_CONTENT_DIR' => __DIR__ . '/branch1',
  'DB_HOST'        => 'localhost',
  'DB_NAME'        => 'branch1',
  'DB_USER'        => 'branch1',
  'DB_PASSWORD'    => '********',
];

$host = getenv('WORDPRESS_HOST') ?: $_SERVER['SERVER_NAME'];

if ($host && file_exists(__DIR__."/conf/{$host}.conf")) {
  require __DIR__."/conf/{$host}.conf";
}

array_walk($defaults_conf, function($value, $name) {
   defined($name) or define($name, $value);
});

unset($defaults_conf, $host);

What happen above is that based on the server name you load a different configuration file (if found) and if the configuration file does not define any of the default configuration (or if the file is not found) configuration are set per default.

Nice thing is that to add a new branch, you just need to create the branch folder and provide a .conf named after the new branch domain, and you are done, there's nothing to change on WP side.

The line:

 $host = getenv('WORDPRESS_HOST') ?: $_SERVER['SERVER_NAME'];

is where I get the domain name. As first option I'm using an environment variable, because there're chances $_SERVER['SERVER_NAME'] will not work on a command line context, such us when using WP CLI. In those situations you can set an environment variable to force WP to use settings from a specific branch.

Note that in branch-specific config files I'm changing the WP_CONTENT_DIR and that will automatically set plugins and themes folder to the related /plugins and /themes branch subfolders.

A possible issue here is if you want to share the /uploads folder (where files get uploaded).

By default that folder is a subfolder of content dir, so using workflow above it will be an /uploads subfolder of each branch root folder.

If that is not a problem for you, than just go with it, otherwise the easiest solution would be to make /uploads in each branch folder a symlink to the real uploads folder you want to share.

3
  • Thank you! I really like your idea even though $_SERVER['SERVER_NAME'] is not reliable. /uploads dir isn't problem. I have also tested with WP CLI, if we pass in --url parameter for each site, it works normally :)
    – wpclevel
    Jun 13, 2016 at 20:16
  • 1
    @Dan I said in the answer: "The server variable 'SERVER_NAME' should work for you, at least if your webserver is configured properly." It means that you need to configure your server properly :) In fact, as long as you setup server_name in nginx or ServerName in Apache or whatever fits your web server, $_SERVER['SERVER_NAME'] will just works. Even if WP CLI may works using --url parameter, other command line tools may have issues if you dont use the environment variable. WP CLI "mocks" the URL of the request in a CLI context, other commands probably won't do it.
    – gmazzap
    Jun 14, 2016 at 3:14
  • 1
    Of course, I will make sure that SERVER_NAME is configured properly. About env vars, I have used phpdotenv to fix it. Currently, everything seem to work great :)
    – wpclevel
    Jun 14, 2016 at 4:48
0

This is possible with a symlinking and a bit of planning. I did some scouring on the net for the same thing. Finally, put together all the stuff and got it working.

I have been running a few websites, they all share the same theme and plugin folder. Same folders work for multisite and single sites. But you have to be careful about certain plugins which can be Multi/Single site only and be quirky.

I have made a directory like master-tnp/themes and master-tnp/plugins. Then symlink to your wordpress directory using ln -s command.

Pitfalls are in server configurations as well. Ensure follow symlinks directive is set to allow.

If you want to use a Single WordPress installation, I have put together a detailed guide as to how I did it at https://vaish.co/multiple-sites-single-wordpress-directory

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.