Freebsd Installation to Speed Up WordPress Website with Redis Cache

Redis is a data structure storage in computer machine memory. Redis stores all data in the form of a dictionary, where keys are associated with their values. This technology will not be a bad solution to speed up and optimize sites running WordPress. Caching improves performance by storing data, code, and other objects in memory.

Object caching can seriously improve the performance of your WordPress site, but not every site can be accelerated with it. In this article, we will try to understand under what conditions Redis will be applicable and useful for WordPress, and in what cases it should not be used. We will also understand how it works, how to install and configure it.

For this tutorial, we will use the FreeBSD server. Because apart from being tough, it is also reliable for handling heavy tasks.




A. What is Redis Object Cache?

We need to understand that object cache is different from regular HTML content cache. WP Fastest Cache, WP-Rocket, LiteSpeed Cache, and other caching plugins can improve site loading speed by saving static copies of HTML pages and skipping PHP code execution. Redis caches the contents of the database, minimizing the number of requests to it and skipping the step of directly accessing the database.

Memcached is also a popular caching choice. However, today, Redis does everything Memcached can do, with a much larger and more refined feature set. This stack overflow page provides some general information as an overview or introduction for people new to Redis.

When a WordPress page loads for the first time, a database query is executed on the server. Redis will remember or store the request in its cache. This way, when another user loads a Wordpress page, the results are provided from Redis and from memory without having to query the database. The Redis implementation used in this tutorial functions as a persistent object cache for WordPress (no expiry date). Object cache works by storing SQL queries in memory that are required to load WordPress pages.

When the page is loaded, the resulting SQL query results are provided to Redis from memory, so the query will not go to the database. The result is much faster page load times and less server impact on database resources. If a query is not available in Redis, the database returns the results, and Redis adds the results to its cache.

To start studying this article, we recommend that you read our previous articles:
Configuring PHP FPM and Apache24 on FreeBSD
Wordpress on Freebsd - Installation and Configuration For Blog


B. WP-CLI

WP-CLI is a command line interface for managing your WordPress website. It allows you to manage your WordPress site using commands that can be entered on the command line. This means you can manage your site without having to log into your WordPress admin panel. WP-CLI is a useful tool for developers who want to quickly complete tasks on their websites.

WP-CLI is a command line tool, so to use it you need to have command line access on your server. To start using WP-CLI, you need to install it on your server. You can install WP-CLI through Composer or download it from the official WP-CLI website.

Once you have installed WP-CLI, you can start using it to manage your site. This tool is very convenient because:
  1. First, it will allow you to quickly complete some tasks that would take you much longer in the WordPress admin area. For example, updating plugins or generating random posts.
  2. Secondly, it contains commands that have no alternatives at all in the WordPress admin panel, for example, exporting a database or deleting a transit cache.
Once you have finished installing WordPress, download the wp-cli.phar file using wget or curl.

root@ns:~ # pkg install php82-phar
root@ns:~ # wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
root@ns:~ # chmod +x wp-cli.phar
root@ns:~ # cp -R wp-cli.phar /usr/local/bin/wp
If  WP-CLI is installed successfully, the results should look like this when running "wp --info".

root@ns:~ # wp --info
OS:     FreeBSD 13.2-RELEASE FreeBSD 13.2-RELEASE releng/13.2-n254617-525ecfdad597 GENERIC amd64
Shell:  /bin/csh
PHP binary:     /usr/local/bin/php
PHP version:    8.2.11
php.ini used:   /usr/local/etc/php.ini
MySQL binary:   /usr/local/bin/mysql
MySQL version:  mysql  Ver 8.0.33 for FreeBSD13.2 on amd64 (Source distribution)
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /root
WP-CLI packages dir:
WP-CLI cache dir:       /root/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.9.0
Continue by updating wp-cli.

root@ns:~ # wp cli update
Success: WP-CLI is at the latest version.


C. Redis Object Cache Configuration

The first step is to install the Redis library. This extension provides an API for communicating with Redis database.

root@ns:~ # cd /usr/ports/databases/pecl-redis && make install clean
root@ns:/usr/ports/databases/pecl-redis # pkg install php82-pecl-redis
And now we can safely proceed to installing and configuring the plugin Redis Cache for WordPress. Let me remind you that object caching stores the results of database queries that were requested. Then, on the next call (request), it serves them faster rather than re-requesting and generating them from the database, which naturally improves performance.

The main requirement for installing the Redis Object Cache plugin on WordPress is that our server computer must have Redis installed. Please read the previous article about installing Redis on a FreeBSD system


By default, the object caching plugin connects to the Redis server via port TCP 127.0.0.1:6379 and uses a database with key 0. To avoid conflicts with other sites, you need to configure the plugin by changing the constants in the file to "/usr/local/www/wordpresswp-config.php". Type the script below in the file.

// adjust Redis host and port if necessary 
define( 'WP_REDIS_HOST', '127.0.0.1' );
define( 'WP_REDIS_PORT', 6379 );

// change the prefix and database for each site to avoid cache data collisions
define( 'WP_REDIS_DATABASE', 0 ); // 0-15

// reasonable connection and read+write timeouts
define( 'WP_REDIS_TIMEOUT', 1 );
define( 'WP_REDIS_READ_TIMEOUT', 1 );
define( 'WP_REDIS_PASSWORD', 'gunungrinjani' );

/* That's all, stop editing! Happy publishing. */
require_once(ABSPATH . 'wp-settings.php');

Use WP-CLI commands to activate the Redis Object Cache plugin.

root@ns:~ # wp --path='/usr/local/www/wordpress/' plugin install redis-cache --activate --allow-root
Warning: redis-cache: Plugin already installed.
Activating 'redis-cache'...
Plugin 'redis-cache' activated.
Success: Plugin already installed.

root@ns:~ # wp --path='/usr/local/www/wordpress/' redis enable --allow-root
root@ns:~ # wp --path='/usr/local/www/wordpress/' redis update-dropin --allow-root
Done. WordPress will use Redis caching automatically when it detects the file object-cache.php. To make sure that it works correctly, run $ redis-cli monitor and refresh the page (while still logged in). You should see the Redis activity log with the installed and received keys.

Now, whenever your web server receives a request to the database, it will return cached data directly from RAM to save resources. If the requested data is not cached via Redis, the web server will retrieve it and process the data from the database as usual. This way, Redis cache significantly reduces server load and makes your site run faster.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post