Assumptions:
- You already have a working site deployed on 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), I had a request come in for assistance setting up WP-Rocket to work with a WordPress instance on Upsun. And just like last time, thought I would post the instructions here for all to benefit from.
While Upsun offers 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. There are two ways how to install it.
Installing WP Rocket via Composer
WP Rocket has documentation on their site for installing their plugin via composer. Therefore, you should be able to add their repository in your composer.json file and then require "wp-media/wp-rocket": "^3.0"
The API key (WP_ROCKET_KEY) is written dynamically into the license-data.php file when you download the plugin from your account on WP Rocket. Please add the key and email constants into your site (using environment variables and getenv()) Afterwards, you can revalidate your status and access the settings page again.
Composer may complain about parts of the wp-rocket codebase not following psr-4 properly. However, the PS-4 warnings seem to be almost exclusively related to the embedded Action Scheduler library.
Installing WP Rocket as a WordPress plugin
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
fi6. 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.
Comments
Please sign in to leave a comment.