The recent vulnerability for PHP called phpAfterShell (CVE-2026-126714) can lead to unauthenticated RCE by exploiting a bug on PHP’s default file-based sessions.
I want to avoid this issue altogether by switching from files to Redis as session handler in my PHP servers. I already have Redis running as cache layer of Nextcloud and it should bring some gain in performance for free as well (probably negligible as load is usually low, but who can refuse more speed?). I won’t try to use a database as I want to avoid any unnecessary load on the MariaDB used by my Nextcloud.
Redis and php-redis
I’m not going to explain how to run Redis here. That’s already covered extensively elsewhere on the Internet. The following just serves to check that the service and module for PHP are available.
-
Check redis service
systemctl status redis -
Check php-redis module is available
php -m | grep redis
Enable Redis session handler
Note
I run Redis bound to a local socket only serving localhost. In the following replace the socket path
/var/run/redis/redis.sockwith the path to your socket or a TCP port if necessary.
We just need to define a target database in Redis and some prefix to enable Redis as session handler in PHP:
-
Check the databases available in Redis
$ redis-cli -s /var/run/redis/redis.sock CONFIG GET databases 1) "databases" 2) "16" -
Select a suitable database index In my case, Nextcloud already uses database 0 in Redis, so I’ll set PHP session handler to 1.
-
Define some arbotrary prefix for the session keys. For instance, PHP_SESSION:.
-
Set Redis as session handler with the following configuration parameters in
php.ini[Session] session.save_handler = redis session.save_path = "unix:///var/run/redis/redis.sock?database=1&prefix=PHP_SESSION:" session.use_strict_mode = 1 -
Enable session locking in php-redis to avoid collisions and prevent session corruption. The following parameters are the recommended locking settings from Nextcloud. Append them at the end of
php.ini[redis] redis.session.locking_enabled=1 redis.session.lock_retries=-1 redis.session.lock_wait_time=10000 -
Restart php-fpm service
systemctl restart php-fpm.service
Verification
Once PHP-FPM is restarted successfully, PHP session keys should start to populate Redis. Check the presence of those keys with the command
redis-cli -s /var/run/redis/redis.sock -n 1 KEYS 'PHP_SESSION:*'