How to Set Up WP Rocket on Trellis and Bedrock

This is a guest post. Opinions and recommendations are the author’s own and not necessarily those of WP Rocket.

The team at Roots developed two distinct products that simplify managing WordPress projects and codebases on your own server (e.g., a VPS, Digital Ocean droplet, AWS EC2 instance, etc.).

These projects are Bedrock and Trellis, which handle server provisioning and deployments.

Setting up these two projects is outside of the scope of this article (but you can see my tutorial on CSS-Tricks about setting up Trellis): however, using WP Rocket with a Bedrock/Trellis setup requires a bit of manual tuning which this article will cover.

Trellis and Bedrock from the Roots team are my go-to for server creation, provisioning, and management for my web development agency. While it does have built-in FastCGI caching which can drastically speed up your site, that only covers expensive server-side computations.
There still are still other, mostly front-end optimizations that a caching plugin like WP Rocket can handle for you with minimal configuration.

WP Rocket comes packed with functionality to minify your HTML, CSS, and JavaScript, along with concatenating scripts (not as useful with HTTP/2.0’s multiplexing feature), automatic lazy loading images, disable oEmbeds and emojis and more.

What I hope to accomplish in this guide is a straightforward explanation of how to properly setup WP Rocket when using Trellis and Bedrock.

How to start setting up WP Rocket on Bedrock

Before we get started, since Composer powers Bedrock, we need to get WP Rocket into our project as a Composer dependency, which is not as straightforward as it may appear, at least at the time of this writing.

WP Rocket offers a package on packagist.org, the official Composer package repository, which can be found here; however, there is an issue with one of its dependencies, a5hleyrich/wp-background-processing.

To get around this, we need to add in a separate repository (https://github.com/tabrisrp/wp-background-processing) in our site’s composer.json file demonstrated below:

After adding that, simply execute composer require wp-media/wp-rocket and it should install properly.

Now that we set up the Bedrock side of things, we need to set up the plugin to work with Trellis.

How to make WP Rocket work with Trellis

First, you need your WP Rocket key and WP Rocket email. The way I did this was logging into the WP Rocket website, and from the License tab, I downloaded the plugin. The file licence-data.php in the root of the plugin contains the WP Rocket key and email which are defined in constants, similar to below:


if ( ! defined( 'WP_ROCKET_KEY' ) ) {
define( 'WP_ROCKET_KEY', '12345fake');
}


// Your email, the one you used for the purchase.
if ( ! defined( 'WP_ROCKET_EMAIL' ) ) {
define( 'WP_ROCKET_EMAIL', 'fake@fakeguy.com' );
}

We are going to store these two as variables in the production vault.yml file, located at /trellis/group_vars/production/vault.yml.

Open the file and add to the vault_wordpress_sites > {yourdomain.com} > env key as wp_rocket_key and wp_rocket_email.

Below is an example vault.yml:

Replace wp_rocket_key and wp_rocket_email with the values from your WP Rocket account.

Also, as a side note, make sure you are encrypting your vault files and not storing them in the clear in your repository.

Next, set the WP_CACHE constant to true and WP Rocket key and email in the environment(s) you want to cache.

I typically only cache production, so to accomplish this, I add the following to /site/config/environments/production.php :


if ( env( 'WP_ROCKET_KEY' ) ) {
define( 'WP_ROCKET_KEY', env( 'WP_ROCKET_KEY' ) );
}
// Your email, the one you used for the purchase.
if ( env( 'WP_ROCKET_EMAIL' ) ) {
define( 'WP_ROCKET_EMAIL', env( 'WP_ROCKET_EMAIL' ) );
}
define('WP_CACHE', true);

This code will grab the environment variable we set in the vault file and set it accordingly.

If you want to cache on all environments, you can drop the same code in the /site/config/application.php file anywhere below the initialization of the \DotEnv\DotEnv object.

Next, let’s set up the directories and advanced-cache.php file and WordPress’ access to them.
To do this, edit the trellis/roles/deploy/defaults/main.yml file and under the key project_shared_children add:

This block ensures that the proper directories, along with the advanced-cache.php file are created and access is properly set up.

Lastly, let’s make sure that the file advanced-cache.php exists when we deploy.
Update trellis/roles/deploy/hooks/build-after.yml by adding:

That should be all. Just provision, deploy, and setup WP Rocket as you normally would. Enjoy using WP Rocket and all its optimizations! ?

Author's avatar

1 comment
Add a comment
Your email address will not be published. All fields are required. Comment policy: We love comments and appreciate the time that readers spend to shader ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

Get a Faster Website in a Few Clicks

Setup Takes 3 Minutes Flat

Get WP Rocket Now What are you waiting for?

{"cart_token":"","hash":"","cart_data":""}