How to configure Redis on managed servers

This article describes how to use Redis on the following hosting packages:

  • VPS administrado
  • Managed Dedicated server
  • Turbo Boost and Turbo Max Web Hosting accounts
  • Turbo reseller accounts

Redis is an open-source memory object caching system that web sites can use to help accelerate page load times. Redis caches in RAM frequently accessed data, such as the results of API calls, database calls, and more.

Redis can significantly help improve site performance.

If you have a Turbo Boost or Turbo Max Web Hosting account, you should use the A2 Optimized plugin to configure and manage Redis for many popular web applications (such as WordPress, Drupal, Joomla, and several others). The following information only applies if you want to use Redis in an application you have developed yourself, or if you are using an application that the A2 Optimized plugin does not support.

Managed VPS and Dedicated servers

Redis is installed and ready to use on managed VPS and managed Dedicated servers.

Connection parameters

You can connect to Redis using the following parameters:

  • Hostname: localhost
    Alternatively, you can use the IP address 127.0.0.1.
  • Port: 6379
Diagnostics

To verify Redis is running, type the following command:

systemctl status redis

You can also use the redis-cli program to connect to Redis directly.

For example, the following commands demonstrate how to use redis-cli to dump all of the cached key-value pairs, and then display rolling statistics for Redis:

redis-cli KEYS '*'
redis-cli --stat

To clear the entire Redis cache at once, type the following command:

redis-cli FLUSHALL
Code sample

There are numerous PHP libraries available for integration with Redis. The following procedure demonstrates how to use the Predis library to connect to Redis and store a key/value pair:

  1. Log in to your server using SSH.
  2. At the command prompt, type the following commands:
    cd ~
    composer require predis/predis
    
  3. Using your preferred text editor, create a file named redis-test.php. Copy and then paste the following code into the redis-test.php file:

    <?php
    
    require './vendor/predis/predis/autoload.php';
    
    Predis\Autoloader::register();
    $client = new Predis\Client();
    
    $client->set('test', 'hello');
    $value = $client->get('test');
    
    print "test = " . $value . "\n";
    
    ?>
    
  4. Save your changes to the redis-test.php file.
  5. Type the following command to run the script:

    php redis-test.php
  6. You should receive the following output:

    test = hello
    

    To confirm the key/value pair is stored in the Redis cache, type the following command:

    redis-cli GET test

For more information about how to use PHP with Redis, please visit https://docs.redis.com/latest/rs/references/client_references/client_php.

Turbo Boost and Turbo Max Web Hosting accounts

To connect to Redis on a Turbo Boost or Turbo Max Web Hosting account, use the following Unix socket path. Replace username with your own account username:

/home/username/.redis/redis.sock

To verify that the Redis socket is active for your account, type the following command. Replace username with your own account username:

ls -l /home/username/.redis/redis.sock

If you receive a No such file or directory error message, then the socket has not been activated for your account yet. To do this, type the following command:

touch ~/.redis.on

The server checks for this file every five minutes, and starts the Redis process for the account if it does not already exist. After five minutes, run the ls command above again, and you should see the redis.sock file in the directory listing.

Before you try to use Redis with PHP on your account, make sure the correct PHP extension is enabled. To do this, follow these steps:

  1. Log in to cPanel.
  2. In the SOFTWARE section of the cPanel home screen, click Select PHP Version.
  3. In the list of PHP extensions, confirm that the Redis check box is selected. (If you do not see a list of PHP extensions, click the Extensions tab.)
Code sample

The following PHP code demonstrates how to connect to Redis and store a key-value pair in the cache.

If you run this code, remember to replace username with your own account username.
<?php

$redis = new Redis();
$redis->connect('/home/username/.redis/redis.sock');

$redis->set('test', 'hello');
$value = $redis->get('test');

print "test = " . $value . "\n";

?>

When you run this script, you should receive the following output:

test = hello

To confirm the key/value pair is stored in the Redis cache, type the following command:

redis-cli -s ~/.redis/redis.sock GET test

Note that you must explicitly provide the socket path to redis-cli, or the command will fail.

More Information

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

Usamos cookies con el fin de personalizar para usted nuestro sitio Web y analizar el uso de éste. Usted acepta esto haciendo clic en “Acepto” o al continuar usando el sitio Web. Hay más información sobre cookies en nuestra política de privacidad.