Completed

Installing WP Rocket (WordPress) on Platform.sh

Assumptions:

  • You already have a working site deployed on Platform.sh / Upsun
  • Your public/root directory is named `wordpress` 
  • Your wp-content directory is located at `wordpress/wp-content` 
  • You're using a Composer-based management for your WordPress site

If any of the assumptions don't match up with your situation, you'll need to make adjustments. 

WP Rocket 

Very similar to my last post (Installing WP Super Cache on Platform.sh), I had a request come in for assistance setting up WP-Rocket to work with a WordPress instance on Platform.sh. And just like last time, thought I would post the instructions here for all to benefit from. 

While both Platform.sh and Upsun offer caching at the router, it's fairly simplistic, with no options. It's a great option if the site doesn't receive many changes, but if you need more flexibility, you'll most likely need something else. 

Enter WP Rocket, one of the many different caching options you can choose from for WordPress. Unlike WP Super Cache, WP Rocket is a premium plugin. This means that there's no easy way to manage WP Rocket as a dependency with composer. Due to that, I'll assume you're committing the wp-rocket plugin code to a directory in your site's repository, and then copying that directory into the plugins directory during the build.  See the note at the end of this post for alternatives. 

1. Download the wp-rocket plugin archive from your account on WP Rocket.

2. Unzip the contents of the archive into a directory in your site's repo. As an example, in our WordPress Composer template, we have a plugin directory at the root of the repository.

3. In your wp-config.php file, add two new constants:

define('WP_CACHE', true);
define('WP_ROCKET_CONFIG_PATH',WP_CONTENT_DIR . DIRECTORY_SEPARATOR.'cache-config'.DIRECTORY_SEPARATOR);

4. Every caching plugin will contain an advanced-cache.php file. This file needs to be placed into the root of the wp-content directory. WP Rocket wants to write its copy into cache-config and then also into the wp-content directory. Since wp-content is read-only in the app container, we'll create a symlink between the version WP Rocket can write to in the mount and the one that's needed in wp-content.  In your Platform.sh (.platform.app.yaml) or Upsun (.upsun/config.yaml) configuration file, add the following to your build hook:

rsync -a plugins/* wordpress/wp-content/plugins/
ln -sf /app/wordpress/wp-content/cache-config/advanced-cache.php wordpress/wp-content/advanced-cache.php

Note: the rsync command copies the wp-rocket code from its location (step 2 above) into the wp-content/plugins directory.

5. In your Platform.sh (.platform.app.yaml) or Upsun (.upsun/config.yaml) configuration file, add the following to your deploy hook:

if [ ! -f wordpress/wp-content/cache-config/advanced-cache.php ]; then
    touch wordpress/wp-content/cache-config/advanced-cache.php
fi

6. In your Platform.sh (.platform.app.yaml) or Upsun (.upsun/config.yaml) configuration file, add two new mounts in addition to the one you have for uploads:

mounts:
    "wordpress/wp-content/uploads":
        source: storage
        source_path: "uploads"
    "wordpress/wp-content/cache":
        source: storage
        source_path: "cache"
    "wordpress/wp-content/cache-config":
        source: storage
        source_path: "cache-config"

Note: on Platform.sh source should be local

7. git add and git commit the changed files, push the changes

8. After the changes have deployed, log into the site's WordPress Admin console

9. Enable the WP Rocket plugin in the plugins panel

10. Visit the WP Rocket admin panel to make adjustments

WP Rocket will now generate cached versions of your content inside the /app/wordpress/wp-content/cache/cached/<url> location. When visitor visit a cached location, WP Rocket will intercept the request and serve the cached version instead of allowing WordPress to regenerate the page.

Note

After writing this post, I discovered WP-Media does have the WP Rocket code base on GitHub with a composer.json file so we should technically be able to add their repository in our composer.json file and then require "wp-media/wp-rocket": "^3.0". However, during testing composer complained about parts of the wp-rocket codebase not following psr-4 properly. Then when I proceeded to test it on the site, it removed my API key and refused to let me re-enter. I'll continue to test and see if I can get it working. 

0

Comments

1 comment
Date Votes
  • WP Rocket has documentation on their site for installing their plugin via composer. As for the issue I had run into with it removing my API key, I was unaware they were writing the API key (WP_ROCKET_KEY) dynamically into the license-data.php file when you download the plugin from their console (Step 1 from above). I had wrongly assumed they had stored the credentials in the database. After adding the key and email constants into my site (using environment variables and getenv()) I was able to revalidate my status and access the settings page again. 

    However, the PS-4 warnings are still present, but seem to be almost exclusively related to the embedded Action Scheduler library.  

    0

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post