Skip to main content

How to configure Redis as a caching server

Caching data in RAM via Redis is one of the methods to speed up the site. This storage is highly efficient and can be used to cache not only sites, but also sessions, as well as a non-relational database.

Installing Redis is done in two steps:

  1. Connecting the backports repository . The version in the standard repository is too old.
  2. Install with aptitude install -t jessie-backports redis-server redis-tools

The content of the article:

We configure the optimal configuration

In Debian, the configuration file is located in the / etc / redis / directory and is called redis.conf .

First of all, you need to correct the error with incorrectly specified maximum number of tcp connections. This is relevant when using tcp sockets.

We print the cat /proc/sys/net/core/somaxconn command in the console and set the appropriate number:

  tcp-backlog 128 

For faster work, we enable the ability to work with a unix-socket.

  unixsocket /var/run/redis/redis.sock
 unixsocketperm 777 

We limit the maximum number of connected clients. If you need more than 1024 connections, you also need to change the limit on the number of simultaneously open files (ulimit).

  maxclients 1024 

Determine the amount of RAM allocated to the cache. In the case of a zero value, all available RAM memory will be used.

  maxmemory 64mb 

We determine the policy of working with memory. With this policy, during a shortage of memory, the oldest and least used keys will be deleted to make room for new ones.

  maxmemory-policy allkeys-lru 

Also, in order to avoid problems with the work of Redis (paragraph 3 of the manual , eng), you should disable the kernel function Transparent HugePages.

  # echo never> / sys / kernel / mm / transparent_hugepage / enabled 

Restart for changes to take effect.

  # service redis restart 

And we add the following lines to the /etc/rc.local file so that after rebooting the server this function is disabled.

  if test -f / sys / kernel / mm / transparent_hugepage / enabled;  then
    echo never> / sys / kernel / mm / transparent_hugepage / enabled
 if test -f / sys / kernel / mm / transparent_hugepage / defrag;  then
    echo never> / sys / kernel / mm / transparent_hugepage / defrag

Caching php sessions

You can configure php to store sessions in several ways, depending on the bundle used.

Directly in php.ini

 session.save_handler = redis
 session.save_path = "unix: ///run/redis/redis.sock" 

Apache2 and mod_php (in the virtual host file or apache2.conf)

  <IfModule mod_php5.c>
 php_admin_value session.save_handler "redis"
 php_admin_value session.save_path "unix: ///run/redis/redis.sock"
 </ IfModule> 

PHP-FPM (in the pool file)

  php_admin_value [session.save_handler] = "redis"
 php_admin_value [session.save_path] = "unix: ///run/redis/redis.sock" 

How do you rate the article?
Звёзд: 1Звёзд: 2Звёзд: 3Звёзд: 4Звёзд: 5 (No ratings yet)

Add a comment

Your email will not be published.