WordPress is PHP based CMS that uses MySQL for its backend. It’s incredibly easy to use once configured. By design, every page load is dynamic, and content is selected out of the database. Even if content has not changed, every page load requires several queries and of course running a significant amount of PHP code. With a very popular WordPress blog or a lower end server, this can quickly become a problem. Loading just the index page of my WordPress blog requires 9 SQL queries (I have a few plugins that may contribute to this). Seeing as how I post only once every few months, wouldn’t it be nice if the content could be served as static content? It would not require PHP to be invoked at all, let alone MySQL.

Really Static Plugin

The Really Static plugin simply accesses all of the pages on your site and writes them out as HTML files. Unfortunately the documentation is a little lacking on a few points so I had to hack a few things to get it to work the way I wanted.

Installing Really Static

In your WordPress admin panel, search for “Really Static”. The author should be Erik Sefkown. As of the writing of this article the current version is 0.31. Click “Install Now.”

Building Static Content

On the plugins page next to the Really Static plugin click on “goto the 1-2-3 quicksetup.”
On the next page choose “running really-static in livemode.” On the next page you will need to configure how the plugin can access your filesystem. On my server I use “work with the local filesystem.” The default path mostly works (more on this shortly) and generates the files in DOCROOT/wp-content/plugins/really-static/static/.
For the last option, I changed the default to the original path of my WordPress blog http://www.patlathem.com/. Click Next.
Really Static will confirm that you have read-write access on this page if you used the filesystem option for writing out the content. Assuming everything is colored green, click Next.
Click Start Generating Files to begin writing the content as html files. This may take some time depending on how much content you have.

Configuring Your Web Server

We want to serve the static content as our live site, so we will need to change the document root of the blog to be where the static content is. For example My document root is /var/www/vhosts/patlathem.com/wp-content/plugins/really-static/static.
To be able to access the WordPress admin site, or simply view the website using the original non-static version you should setup a second virtual host that points at the original document root, like live.yourdomain.com. You may want to setup a redirect for /wp-admin to redirect to your second vhost, or you can not and use it as security by obscurity.

I couldn’t find much documentation, but I don’t think that Really Static is designed to be used with WordPress plugins. It even broke the original WordPress theme for me. To fix this remove the wp-content and wp-includes folder in the wp-content/plugins/really-static/static directory. Replace them with symlinks to the original WordPress directories:

# pwd
# ls -la |grep wp-
lrwxrwxrwx  1 nginx  nginx    23 Oct 12 14:01 wp-content -> ../../../../wp-content/
lrwxrwxrwx  1 nginx  nginx     24 Oct 12 14:02 wp-includes -> ../../../../wp-includes/

Make sure that your web server is configured to allow following symlinks.
To fix comments and some other content that will have to be dynamic make symlinks for those as well:

# find /var/www/vhosts/patlathem.com -maxdepth 1 -type f -name "wp-*.php" -print | xargs -I  \
{} ln -s {} /var/www/vhosts/patlathem.com/wp-content/plugins/really-static/static/

WordPress Config Change

If you try to login on the dynamic version of your site, you will be redirected to the static version. To work around this, add the following lines to your wp-config.php:

$base = "http://";
if ($_SERVER['PORT'] == 443) {
    $base = "https://";
define('WP_HOME', $base . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', $base . $_SERVER['HTTP_HOST']);

That’s it! When you make future changes to the site on the live version of it, the Really Static plugin will rebuild the relevant static pages. You can verify that the content is really static by stopping MySQL on your server. If you don’t get a database connection error, then you are serving static content.

PHP Health Check Script for Percona XtraDB Cluster arrow-right
Next post

arrow-left Automatic service restarts on failure with Monit
Previous post

  • George

    March 13, 2014 at 4:03 am |

    What if really static fails to write files even if its test passes by green box? I see my static/ folder empty!
    Nginx gives also 403 permission error on this folder.

    • pat

      April 9, 2014 at 9:01 am |

      Sounds like a permission problem. I would check that your PHP user and web server user can both write to the directory. Also, I stopped using this plugin because it’s pretty buggy and doesn’t seem to be maintained.