Looking for specifics about how PHP session garbage collection works

PHP has several session settings that control the probability that garbage collection (i.e. deletion of expired sessions) will occur.

My question is: does this probability apply to all running Apache threads on all virtualhosts on any given Apache server?

For example:

  • If the probability is set to 1/100, does that mean that any Apache thread that runs a PHP process goes into the pool of processes that may have gc run?
  • or, does that apply only to the threads runnning for any given VirtualHost? Or other sort of partitioning?
  • Finally, if a PHP process that triggers gc, does it perform gc on all PHP sessions? or just the sessions for that VirtualHost? Or other sort of partioning?

I'm trying to gauge wether I need to tune the gc_probability setting for a given VirtualHost, for the whole server, or what.


Solution 1:

If the probability is set to 1/100, does that mean that any Apache thread that runs a PHP process goes into the pool of processes that may have gc run?

If that process touches the session [session_start()], then yes, it may run garbage collection.

If you need different virtual hosts to have different garbage collection and sessions then you should use the session.save_path option to set a different path for each virtual host. You can do this in your apache virtual host config with a directive like `php_value session.save_path "/srv/www/www.example.org/sessions". If you have several different applications running with different session requirements you may need to set different paths, and GC.

Honestly though PHP session garbage collection isn't that great. You might be better off disabling it completely and using an script launched from cron to periodically clean up old sessions. Or another alternative, that requires support in the PHP application would be to store your sessions in a database.

If you are hosting stuff for external parties, then you almost certainly want to set up a separate session directory for each virtual host.