Skip to content

Instantly share code, notes, and snippets.

@jdeathe
Last active July 30, 2022 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdeathe/efd5028a3aebbbb0e43ae116dd27baaf to your computer and use it in GitHub Desktop.
Save jdeathe/efd5028a3aebbbb0e43ae116dd27baaf to your computer and use it in GitHub Desktop.
CentOS-6 - Redis PHP Session Save Handler

CentOS-6 Redis PHP Session Handler

This guide is for installing redis as a replacement for the memcached php session store when using php71u IUS packages on a CentOS-6 environment.

References

Install IUS Packages

# rpm -q ius-release \
  &> /dev/null \
|| yum -y install \
  https://centos6.iuscommunity.org/ius-release.rpm \
&& rpm -q redis \
  &> /dev/null \
&& rpm -q php71u-pecl-redis \
  &> /dev/null \
||  yum -y install \
  redis \
  php71u-pecl-redis

Host Entries to Loopback Address

# if ! grep -qP \
  '^[0-9\.:]+\t+redis-cache' /etc/hosts; then \
  printf -- \
    '127.0.0.1\t%s\n::1\t\t%s\n' \
    redis-cache \
    redis-cache \
    >> /etc/hosts; \
fi

System Changes for Redis Support

# if ! grep -qP '^net.core.somaxconn ?=' /etc/sysctl.conf; then \
  { \
    echo 'net.core.somaxconn = 1024'; \
  } >> /etc/sysctl.conf; \
else \
  sed -i -r \
    -e 's~(^net.core.somaxconn ?= ?)[0-9]+$~\11024~' \
    /etc/sysctl.conf; \
fi \
&& if ! grep -qP '^vm.overcommit_memory ?=' /etc/sysctl.conf; then \
  { \
    echo 'vm.overcommit_memory = 1'; \
  } >> /etc/sysctl.conf; \
else \
  sed -i -r \
    -e 's~(^vm.overcommit_memory ?= ?)[0-1]+$~\11~' \
    /etc/sysctl.conf; \
fi \
&& sysctl -p \
&& printf -- \
  'never' \
| tee \
  /sys/kernel/mm/transparent_hugepage/enabled \
  /sys/kernel/mm/transparent_hugepage/defrag \
&> /dev/null \
&& grubby \
  --args="transparent_hugepage=never" \
  --update-kernel "$(
    grubby --default-kernel
  )"

Configure Redis as a Cache Service

To use redis as a replacement for memcached it can be configured with a memory limit and eviction policy.

The settings below set a limit of 64M and increase the sample size used to predict the lru (less recently used) values from 5 to 10.

# sed -i -r \
  -e 's~^(save [0-9]+ [0-9]+)~#\1~' \
  -e 's~^(# )?(maxmemory ).+$~\264mb~' \
  -e 's~^(# )?(maxmemory-policy ).+$~\2allkeys-lru~' \
  -e 's~^(# )?(maxmemory-samples ).+$~\210~' \
  /etc/redis.conf

Enable and Start Redis

# chkconfig redis on \
&& service redis condrestart \
&& service redis start

Enable Redis PHP Session Handler

# sed -i -r \
  -e 's~^;(session\.save_handler ?= ?redis)$~\1~' \
  -e 's~^;(session\.save_path ?= ?).*$~\1"tcp://redis-cache:6379"~' \
  /etc/php.d/50-redis.ini

Update MPM Session Settings

Configuration file locations for mod_php, fcgid and php-fpm vary. The session.save_handler and session.save_path values need to be updated in the appropriate configuration files for your Apache MPM.

Restart Apache

Apache needs to be reloaded for the configuration settings to be applied.

# apachectl graceful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment