How to Configure Redis for WordPress Object Caching via Unix Socket on Debian 12
Introduction
Redis is a powerful in-memory key-value store that can significantly improve the performance of WordPress by acting as an object cache. By default, Redis communicates over TCP, but using a Unix socket instead can provide better performance and security by reducing network overhead.
In this guide, I’ll walk you through how to configure Redis to work with WordPress via Unix socket on a Debian 12 server running Nginx, PHP 8.2, and MariaDB.
Step 1: Install Redis on Debian 12
First, ensure that Redis is installed and running on your server:
sudo apt update && sudo apt install redis-server -y
Enable and start Redis:
sudo systemctl enable redis
sudo systemctl start redis
Verify that Redis is running:
systemctl status redis
You should see active (running)
, confirming that Redis is operational.
Step 2: Enable Redis Unix Socket
Instead of using a TCP connection, we will configure Redis to communicate over a Unix socket.
Modify Redis Configuration
Open the Redis configuration file using VIM:
sudo vim /etc/redis/redis.conf
Locate and modify or add the following lines:
# Disable TCP listening (optional but recommended)
port 0
# Enable Unix socket communication
unixsocket /var/run/redis/redis-server.sock
unixsocketperm 770
This does the following:
- Disables TCP (
port 0
) to prevent unnecessary network exposure. - Enables Unix socket at
/var/run/redis/redis-server.sock
. - Sets permissions to 770 to restrict access to the Redis user and group.
Save and exit (Esc
, then :wq
).
Restart Redis to apply the changes:
sudo systemctl restart redis
Verify Redis is using the socket:
ls -lah /var/run/redis/
You should see:
srwxrwx--- 1 redis redis 0 Mar 8 16:50 redis-server.sock
To test if Redis is working via the socket, run:
redis-cli -s /var/run/redis/redis-server.sock ping
If everything is working, Redis should respond with:
PONG
Step 3: Allow PHP-FPM to Access Redis
By default, only the redis
user and group have access to the socket. Since WordPress runs under PHP-FPM, which typically uses the www-data
user, we need to grant access.
Add PHP-FPM User to Redis Group
Run the following command to add www-data
to the redis
group:
sudo usermod -aG redis www-data
Restart PHP-FPM and Nginx to apply the changes:
sudo systemctl restart php8.2-fpm nginx
Verify that www-data
is now in the redis
group:
groups www-data
Expected output:
www-data : www-data redis
This confirms that PHP-FPM can now interact with the Redis socket.
Step 4: Configure WordPress to Use the Redis Unix Socket
Now, we need to configure WordPress to connect to Redis via the Unix socket instead of TCP.
- Install the Redis Object Cache plugin by Till Krüss in your WordPress admin panel.
- Modify your WordPress configuration file (
wp-config.php
) for each site.
For Site 1 (example.com
):
define('WP_REDIS_SCHEME', 'unix');
define('WP_REDIS_PATH', '/var/run/redis/redis-server.sock');
define('WP_REDIS_DATABASE', 0);
For Site 2 (example2.com
):
define('WP_REDIS_SCHEME', 'unix');
define('WP_REDIS_PATH', '/var/run/redis/redis-server.sock');
define('WP_REDIS_DATABASE', 1);
Save and exit (Esc
, then :wq
).
Go to WordPress Admin → Settings → Redis → Click Enable Object Cache.
Step 5: Verify Redis Caching in WordPress
To confirm Redis is caching WordPress objects, run:
redis-cli -s /var/run/redis/redis-server.sock info keyspace
You should see:
# Keyspace
db0:keys=5,expires=0,avg_ttl=0
db1:keys=3,expires=0,avg_ttl=0
To check the cached keys:
redis-cli -s /var/run/redis/redis-server.sock -n 0 keys '*'
redis-cli -s /var/run/redis/redis-server.sock -n 1 keys '*'
If you see cached entries, Redis Object Cache is working correctly!
Step 6: Security Considerations
To ensure the best security practices:
- Keep unixsocketperm set to
770
unless needed. - Regularly check that only trusted users belong to the
redis
group:groups www-data
- Ensure no unnecessary system users can access the Redis socket:
ls -lah /var/run/redis/
Conclusion
Congratulations! 🎉 You have successfully configured Redis for WordPress Object Caching via Unix Socket on Debian 12 with Nginx, PHP 8.2, and MariaDB.
By switching from TCP to a Unix socket, you’ve improved performance and enhanced security while keeping WordPress caching optimized.
If you run into any issues, check the Redis logs:
sudo journalctl -u redis --no-pager | tail -n 50
Let me know in the comments if this guide helped you, or if you have any questions! 🚀
Considerations for Managed Hosting Environments
If you are using a managed WordPress hosting service instead of a self-hosted VPS or dedicated server, you may not have SSH access, the ability to modify system files like /etc/redis/redis.conf
, or control over Redis configurations.
Some hosting providers do not allow Redis at all, while others may have preconfigured Redis setups. If you are using shared hosting or managed WordPress hosting:
- Check with your host to see if Redis is available.
- Some hosts offer Redis as a service where you only need to configure your
wp-config.php
. - If you do not have SSH or root access, using a Redis plugin with a cloud-based Redis service (like AWS ElastiCache, Upstash, or RedisLabs) might be your best option.
If you are unsure, contact your hosting provider’s support team for guidance on enabling Redis Object Cache for your WordPress site.