How to configure Redis on managed servers

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

  • Managed VPS
  • Managed Dedicated server

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.

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.

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.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.