phpsessionredisflarum

PHP redis session save_handler not being used


I've installed the php7-redis extension in an alpine container running in k8s. Added a conf ini file that enables redis and sets the host. This shows up in phpinfo's output but the app being deployed (flarum) doesn't seem to be saving its sessions to redis. Basically needed because we want to run multiple replicas of flarum.

phpinfo output

session
Session Support enabled
Registered save handlers    files user redis rediscluster
Registered serializer handlers  php_serialize php php_binary
Directive   Local Value Master Value
session.auto_start  Off Off
session.cache_expire    180 180
session.cache_limiter   nocache nocache
session.cookie_domain   no value    no value
session.cookie_httponly no value    no value
session.cookie_lifetime 0   0
session.cookie_path /   /
session.cookie_samesite no value    no value
session.cookie_secure   0   0
session.gc_divisor  1000    1000
session.gc_maxlifetime  1440    1440
session.gc_probability  1   1
session.lazy_write  On  On
session.name    PHPSESSID   PHPSESSID
session.referer_check   no value    no value
session.save_handler    redis   redis
session.save_path   tcp://flarum-redis-master:6379?auth=<redis-password>
session.serialize_handler   php php
session.sid_bits_per_character  5   5
session.sid_length  26  26
session.upload_progress.cleanup On  On
session.upload_progress.enabled On  On
session.upload_progress.freq    1%  1%
session.upload_progress.min_freq    1   1
session.upload_progress.name    PHP_SESSION_UPLOAD_PROGRESS PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix  upload_progress_    upload_progress_
session.use_cookies 1   1
session.use_only_cookies    1   1
session.use_strict_mode 0   0
session.use_trans_sid   0   0

Can I deregister files user rediscluster leaving only redis or some other way force it to be used? Can't see what option I've missed or why no sessions are being saved to redis.

I've tested it should be working by using this script

 <?php
session_start();
$count = isset($_SESSION['count']) ? $_SESSION['count'] : 1;
echo $count;
$_SESSION['count'] = ++$count;

After refreshing this test.php file session data shows up in redis. Is there something in flarum preventing its use?

FYI php-fpm in use with nginx


Solution

  • This isn't as simple as telling PHP to use Redis. An extension is needed to overload the built in session storage. See the discussion at https://discuss.flarum.org/d/25889-how-to-use-redis-for-session-storage

    Since we're in Kubernetes I found flarum stores session files in /flarum/app/storage/sessions so I mounted a shared volume there that all replicas can use. Testing so far has shown this works well.