Sharing www/wordpress installation between two sites

Posted on October 20, 2016

I’ve been running a small Wordpress site for one friend of mine. I’ve set it up in simplest of possible ways: just installed it with pkg install php5-wordpress and then served /usr/local/www/wordpress directory from my web server.

After some time, I've got a need to have another Wordpress site. After some googling, I've found that usual way to have several Wordpress sites is to download and setup it manually for each site you want to run. I didn't like this solution because

To try to solve this I used nullfs to layer filesystem trees one over another. The solution would be much clearer and less hackish if unionfs FreeBSD implementation would work, but alas. Its man page states that unionfs generally doesn't work.

So, we are left with nullfs. This means that if we overlay directory b over directory a, we wouldn’t be able to see the contents of a/something if b also contained something. To work around that we'd need to carefully mount every such directory (actually, only those we are interested in).

Let's start by creating a directory for a new instance:

mkdir /usr/local/www/mysite

I wanted Wordpress itself to be mounted as read-only, as I intended to perform upgrades myself. Because of that we would need to create directories for every directory Wordpress expects to be writable:

mkdir /home/user/wp-root
mkdir /home/user/wp-content
mkdir /home/user/wp-content/plugins
mkdir /home/user/wp-content/uploads

wp-root is needed to hold user wp-config.php, wp-content/plugins and wp-content/uploads holds user’s site plugins and uploads respectively. You can also add themes to this list.

Now, to make things work automatically even after reboot, list nullfs mounts in /etc/fstab:

/usr/local/www/wordpress /usr/local/www/usersite nullfs ro 0 0
/home/user/wp-root /usr/local/www/usersite nullfs rw,union 0 0
/home/user/wp-content/plugins /usr/local/www/usersite/wp-content/plugins nullfs rw,union 0 0
/home/user/wp-content/uploads /usr/local/www/usersite/wp-content/uploads nullfs rw,union 0 0

The first line mounts Wordpress installed by FreeBSD package into site's web root. Note the ro attribute. Other lines layer directories of site's user over the web root, unioning their content (note the union attribute).

If you are eager try this out right away:

mount -a

Of course, this approach is far from being neat and simple, especially when the package you are sharing has complex directory structure. But if it is ok to keep it read-only, this method turns out to be pretty useful. And once unionfs start to work reliably, it could be used to do smart directories unioning, lifting much of complexity out of this method.