//Web Panel

Enable Per‑Account Redis Caching with CloudLinux PHP Selector

Isolate Redis caches per cPanel account on VPS with CloudLinux PHP Selector. Prevent cross-site data leaks and ensure consistent performance.

6 min read
Enable Per‑Account Redis Caching with CloudLinux PHP Selector

Running several websites on one VPS is common in India. When those sites share a single Redis instance, a mis‑configured cache can cause one account to read or overwrite another’s data. CloudLinux PHP Selector and cPanel provide the tools to isolate Redis object caches per‑account, keeping each site’s cache clean and secure while still benefiting from Redis’s speed.

Why Per‑Account Redis Caching Matters

  • Data isolation: Each cPanel account gets its own cache namespace, preventing cross‑site data leaks.
  • Performance consistency: A busy site cannot evict keys belonging to a quieter site, so response times stay predictable.
  • Easy troubleshooting: When a cache‑related issue appears, you can flush or debug a single account without affecting others.

Prerequisites

  1. A server running CloudLinux with cPanel/WHM installed.
  2. Root (or sudo) access to the server.
  3. Redis server already installed and running.
  4. PHP versions you intend to use are available in the PHP Selector.

Step 1 – Install Redis (if it isn’t already)

Redis is available in the default repositories for both Debian/Ubuntu and AlmaLinux/Rocky/RHEL. Choose the block that matches your OS.

Debian / Ubuntu (apt)

# Update package lists
apt update

# Install Redis server
apt install -y redis-server

# Enable and start the service
systemctl enable --now redis-server

AlmaLinux / Rocky / RHEL (dnf)

# Enable the EPEL repository (required for Redis)
dnf install -y epel-release

# Install Redis
dnf install -y redis

# Enable and start the service
systemctl enable --now redis

Both command sets fetch the latest stable Redis package and use systemctl enable --now to start the daemon immediately and on every boot.

Step 2 – Configure Redis for Multiple Namespaces

Redis itself does not enforce per‑account isolation, but it provides key prefixes that you can use to separate data. The simplest approach is to let each cPanel account prepend a unique prefix to its keys.

Edit the Redis configuration

# Debian/Ubuntu
vi /etc/redis/redis.conf

# AlmaLinux/Rocky/RHEL
vi /etc/redis.conf

Ensure protected-mode yes remains enabled to block unauthenticated external connections. Optionally add a password for extra protection:

# Example (optional)
requirepass YourStrongPasswordHere

Save the file and restart Redis to apply the changes:

systemctl restart redis   # works on both families

Step 3 – Create a Dedicated Redis User for Each cPanel Account

CloudLinux’s PHP Selector can pass custom INI values to PHP. We’ll create a separate Redis user (or password) for each account and store the connection details in a per‑account .ini file.

Generate a password for an account

# Replace USERNAME with the cPanel account name
USERNAME=exampleuser
REDIS_PASS=$(openssl rand -base64 12)
echo "$REDIS_PASS" > /root/redis_pass_$USERNAME.txt

This command creates a random 12‑character password and saves it securely. Repeat for every account.

Assign the password in Redis ACL (Redis 6+)

# Connect to Redis as the default admin (use the password set in redis.conf if you added one)
redis-cli

# Inside the Redis CLI, create an ACL user for the cPanel account
ACL SETUSER $USERNAME on >$REDIS_PASS ~* &* +@all

The ACL SETUSER command does the following:

  • on – enables the user.
  • >$REDIS_PASS – sets the password.
  • ~* – allows access to all key patterns (you can limit this later).
  • +@all – grants all command categories.

Step 4 – Configure PHP Selector to Use the Account‑Specific Redis Settings

CloudLinux lets you edit php.ini values per selector version. We’ll add a small snippet that reads the account name and builds a Redis prefix automatically.

Create a custom INI file

# Path where PHP Selector looks for custom INI files
CUSTOM_INI_DIR=/opt/cloudlinux/php/etc/custom

# Ensure the directory exists
mkdir -p $CUSTOM_INI_DIR

# Create the file (the same name will be used for every selector version)
cat > $CUSTOM_INI_DIR/redis_per_account.ini <<'EOF'
; Redis per‑account configuration
; The cPanel username is available as the environment variable USER
redis.prefix = ${USER}:
redis.password = $(cat /root/redis_pass_${USER}.txt 2>/dev/null)
EOF

Explanation:

  • redis.prefix tells the PHP Redis extension to prepend username: to every key.
  • redis.password reads the password we stored earlier, ensuring each account authenticates with its own Redis ACL user.

Tell PHP Selector to load the custom INI

# For each PHP version you want to support (example: 7.4, 8.0, 8.2)
for ver in 74 80 82; do
    /usr/local/cpanel/bin/php_selector --add-ini $ver $CUSTOM_INI_DIR/redis_per_account.ini
done

The loop registers the same redis_per_account.ini file for every selector version. After this, any script that uses the Redis extension will automatically use the per‑account prefix and password.

Step 5 – Verify the Setup from a cPanel Account

Log in to cPanel, open Terminal (or SSH in as the user), and run a quick PHP test.

PHP test script

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth(getenv('REDIS_PASSWORD')); // optional if you set redis.password in ini
$redis->set('test_key', 'Hello from '.getenv('USER'));
echo $redis->get('test_key');
?>

Save it as test.php in the public_html folder and access it via a browser. You should see a message like “Hello from exampleuser”. Then, check Redis directly (as root) to confirm the key name includes the prefix:

redis-cli keys '*'
# Expected output: exampleuser:test_key

If the key appears with the correct prefix, the per‑account isolation is working.

Step 6 – Managing Cache Flushes per Account

cPanel users may need to clear their own cache without affecting others. Add a simple script that each account can run.

Flush script (place in public_html)

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth(getenv('REDIS_PASSWORD'));
$prefix = getenv('USER') . ':';
$keys = $redis->keys($prefix . '*');
foreach ($keys as $key) {
    $redis->del($key);
}
echo "Cache cleared for ".getenv('USER');
?>

When a user visits yourdomain.com/flush_cache.php, only keys that start with their username are deleted, leaving other accounts untouched.

Best Practices and Maintenance Tips

  • Rotate Redis passwords regularly. Use a cron job to regenerate passwords and update the ACL entries.
  • Monitor Redis memory usage. Set maxmemory and an eviction policy (e.g., volatile-lru) to avoid a single account exhausting RAM.
  • Back up Redis data. Even though it’s a cache, a snapshot (RDB) can help diagnose issues.
  • Document custom INI files. Keep a version‑controlled copy of redis_per_account.ini so you can reproduce the setup on new servers.
  • Limit ACL key patterns. If you want tighter security, replace ~* with ~username:* for each user.

Conclusion

By combining CloudLinux’s PHP Selector with cPanel’s per‑user environment, you can turn a single Redis instance into a set of isolated caches—one for each hosting account. The approach requires only a few configuration steps: install Redis, create per‑account ACL users, add a small custom INI file, and provide a cache‑flush script. The result is a secure, performant caching layer that scales with the number of sites on your server, giving Indian developers confidence that their data stays separate while still enjoying Redis’s speed.

rediscpanelcloudlinuxphp selectorvpsdata isolationcachinglinux

Try it on your own server

Follow along on a Cloud VPS with full root access, or read the step-by-step knowledge base guides.

Guides: VPS for WordPress · Node.js hosting · VPS or dedicated server · Paying by UPI