Redis and Memcached Setup
Redis and Memcached are in-memory caching systems that dramatically speed up web applications by storing frequently accessed data in RAM instead of querying the database repeatedly. This guide explains how to enable and configure these caching solutions on your hosting account.
Redis vs Memcached
| Feature | Redis | Memcached |
|---|---|---|
| Data structures | Strings, lists, sets, hashes, sorted sets | Strings only |
| Persistence | Optional disk persistence | No persistence |
| Replication | Built-in replication support | No built-in replication |
| Memory efficiency | Good | Slightly better for simple key-value |
| Use case | WordPress, Laravel, complex caching | Simple session/object caching |
| Recommended for | Most applications | Legacy applications |
Tip: For most modern PHP applications and WordPress sites, Redis is the recommended choice due to its richer feature set and better ecosystem support.
Enabling Redis on Shared Hosting
Via cPanel
- Log in to cPanel
- Navigate to Software > Select PHP Version (or MultiPHP Manager)
- Click on Extensions tab
- Find and enable the redis extension
- Click Save
Via DirectAdmin
- Log in to DirectAdmin
- Navigate to Account Manager > PHP Settings
- Enable the Redis PHP extension for your PHP version
- Save changes
Tip: On shared hosting, Redis runs as a shared service. Each account typically gets a dedicated Redis database number or prefix to isolate data. Check with support for your specific configuration.
Enabling Memcached on Shared Hosting
Via cPanel
- Log in to cPanel
- Navigate to Software > Select PHP Version
- Click on Extensions tab
- Enable the memcached extension (note: not memcache — they are different extensions)
- Click Save
Setting Up Redis on VPS
If you have a VPS with root access:
Install Redis
# AlmaLinux/Rocky
sudo dnf install redis -y
sudo systemctl enable redis
sudo systemctl start redis
# Ubuntu/Debian
sudo apt install redis-server -y
sudo systemctl enable redis-server
sudo systemctl start redis-serverVerify Installation
redis-cli pingExpected response: PONG
Basic Configuration
Edit /etc/redis/redis.conf (or /etc/redis.conf):
# Bind to localhost only (security)
bind 127.0.0.1
# Set a password
requirepass your-strong-password
# Set maximum memory
maxmemory 256mb
# Eviction policy (remove least recently used keys when full)
maxmemory-policy allkeys-lruRestart Redis after changes:
sudo systemctl restart redisConfiguring WordPress with Redis
Redis object caching can reduce WordPress database queries by 50-80%.
Step 1: Install a Redis Plugin
- In WordPress admin, go to Plugins > Add New
- Search for Redis Object Cache by Till Kruss
- Install and activate the plugin
Step 2: Add Redis Configuration to wp-config.php
Add these lines above the "That's all" comment:
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'your-redis-password');
define('WP_REDIS_DATABASE', 0);
define('WP_REDIS_PREFIX', 'wp_mysite_');Step 3: Enable Object Cache
- Go to Settings > Redis in WordPress admin
- Click Enable Object Cache
- Verify the status shows "Connected"
Tip: Use a unique WP_REDIS_PREFIX for each WordPress site to prevent cache collisions when multiple sites share the same Redis instance.
Configuring Laravel with Redis
Laravel has built-in Redis support:
Step 1: Install the PHP Redis Extension
Ensure the phpredis extension is installed, or use the predis/predis package:
composer require predis/predisStep 2: Configure .env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=your-redis-password
REDIS_PORT=6379Monitoring Cache Performance
Redis
Check Redis statistics:
redis-cli info statsKey metrics to monitor:
- keyspace_hits / keyspace_misses: Calculate hit rate (aim for >90%)
- used_memory: Current memory usage
- connected_clients: Number of active connections
Memcached
echo "stats" | nc localhost 11211Watch for get_hits vs get_misses to evaluate cache effectiveness.
Troubleshooting
Cannot Connect to Redis
- Verify Redis is running:
systemctl status redis - Check if the port is correct (default: 6379)
- Ensure the password matches in both Redis config and your application
- On shared hosting, confirm the Redis extension is enabled in PHP
High Memory Usage
- Set maxmemory to a reasonable limit (128-512 MB for most sites)
- Use allkeys-lru eviction policy to automatically remove old keys
- Run
redis-cli info memoryto check current usage
Cache Not Working (WordPress)
- Check if
wp-content/object-cache.phpexists — the plugin creates this file - Verify Redis connection in Settings > Redis
- Ensure no conflicting caching plugins are active
Related Articles
Need help setting up caching? Contact our support team at {{SUPPORT_EMAIL}} or open a ticket at {{SUPPORT_URL}}.