Setup WP-Skeleton to optimize the opcache

Recently I started converting several blogs over to the setup created by Mark Jaquith called WP-Skeleton. This means that the core of WordPress is located in a sub-directory in your blog directory. As I run multiple blogs on the same server, and the server runs an opcache, in my case I run PHP 5.5 but it could be any opcache, it would benefit the cache if all the blogs used the same directory.

Setup

Each blog uses the principal of WP-Skeleton, I’m not going to explain the setup and/or installation of WP-Skeleton, I’m just going to discuss the changes that we need to do.
WP-Skeleton uses a submodule for the WordPress core files. We will not be using this, instead we will use a softlink to a general directory, located outside the blog.

I have two WordPress sites that are located in the following directories:

/var/www/blog1/html/
/var/www/blog2/html/

I have located the shared WordPress directory in

/opt/wordpress-shared/wp

Install WordPress core files

Install the WordPress core file in

/opt/wordpress-shared/wp

If you need help with this following step #1 on the installation page at wordpress.org.

Changes to the WP-Skeleton setup

To link the WordPress core files to the shared directory

cd /var/www/blog1/html/
rm -rf wp
ln -s /opt/wordpress-shared/wp wp

Do this for all your sites.

The result will look like this

/var/www/blog1/html/
                   ├── content
                   ├── index.php
                   ├── README.md
                   ├── wp -> /opt/wordpress-shared/wp
                   ├── wp-config.php
                   └── wp-content

/var/www/blog2/html/
                   ├── content
                   ├── index.php
                   ├── README.md
                   ├── wp -> /opt/wordpress-shared/wp
                   ├── wp-config.php
                   └── wp-content

Setup the WordPress configuration files

Due to to PHP handling of softlinks, this is unfortunately not the end of the setup. WordPress will actually not be able to find the

wp-config.php

file. It will be looking for the file in the

/opt/wordpress-shared

directory. We can’t use the same configuration file for every WordPress so we will use a slightly different setup

We go to the WordPress shared directory

cd /opt/wordpress-shared

We create a

wp-config.php

file with the following content

<?php
require_once( dirname(ABSPATH) . '/wp-config.'.$_SERVER['HTTP_HOST'].'.php' );

This will load a config file with a named based on the HTTP_HOST variable.
The blog1 site is accessible on the web as www.blog1.foo, while blog 2 is accessible as www.blog2.int.

We will create the following soft-links

 ln -s /var/www/blog1/html/wp-config.php wp-config.www.blog1.foo.php
ln -s /var/www/blog2/html/wp-config.php wp-config.www.blog2.int.php

Finally we set the rights for the directory shared directory and its subdirectories to the same user/group of your webserver. In my case this is www-data

chown -R www-data:www-data /opt/wordpress-shared

And that’s it. Restart PHP, if you run PHP-FPM or restart your webserver and check the status of your opcache page. You will see that the files cached are located on the shared directory and therefor are only stored once instead for each WordPress site you run.

Final thoughts

I don’t think I have to tell you but I will anyway. By using this setup each blog will run the same WordPress server, and if you update WordPress on one site, each site that is configured to use the above setup will also be updated. This can be both a good and bad thing. The update will be only downloaded and physically installed once, but you still have to go to each site’s dashboard to update the database for that site or use wp-cli. I’ll be writing an article soon about the setup of wp-cli for the above situation as it’s not really standard.

This article is filed under the category WordPress and has no tags associated with it.