Hello all! I’ve looked at the documentation for Trellis, and there doesn’t currently seem to be an official way of storing PHP sessions in Redis. Using Memcached for PHP sessions only seems kind of meaningless, so I decided to give storing them in Redis a try!
I’ve managed to get it to work and I’m posting here before making a pull request.
How to try it out:
- Open
trellis/roles/wordpress-setup/templates/php-fpm-pool-wordpress.conf.j2and add this at the bottom:
{% if redis_sessions %}
php_value[session.save_handler] = redis
php_value[session.save_path] = "tcp://{{ redis_host }}:{{ redis_port }}?database={{ redis_database }}&prefix=PHPREDIS_SESSION:{% if redis_password %}&auth={{ redis_password }}{% endif %}"
{% endif %}
(Maybe using an else if with the memcached one would be better? Otherwise inattentive users might add both)
-
Open
trellis/group_vars/all/main.yml(or the specific one for testing, staging, etc) and add this:
redis_sessions: true -
Run
trellis provision development -
Visit the site, add some sessions etc.
-
Open Redis Insight and check if the sessions are being added to Redis as they should.
Questions for people smarter than me:
- Is there a reason that Memcached is used for this by default rather than Redis? Basically, am I missing something here? Is using Redis for PHP sessions a bad idea?
- Anything that could be improved about the save_path? I’m currently using tcp but it seems like using “redis_unixsocket” should be possible. However, I can’t get it to work. This for example isn’t working:
php_value[session.save_path] = "unix://{{ redis_socket_path }}?database={{ redis_database }}&prefix=PHPREDIS_SESSION:{% if redis_password %}&auth={{ redis_password }}{% endif %}"
Any ideas for getting this to work is welcome!
Thanks in advance for any and all feedback!