PHP FPM not cleaning session files in tmp folder

While creating virtual servers php-fpm create tmp folder under username in /home/username/tmp folder and assigns that folder to be used as session folder via configuration in /etc/php/7.0/fpm/pool.d/domain.com.conf. But as files keep growing PHP-FPM automatically does not clean the directory and directory becomes full of session files causing space not available problem.

I think system should automatically cleanup the expired session files.

I have my gc_maxlifetime as 1440 but still files keep building up.

Status: 
Needs work

Comments

Howdy -- you may want to try enabling automatic cleanup of temp files. That can be enabled in Webmin -> Webmin -> Webmin Configuration -> Advanced Options -> Maximum age of temporary files.

You could try setting it to something like 30 days for starters.

The Webmin setting "Maximum age of temporary files" won't help with files in ~/tmp , only in /tmp/.webmin .

It's really up to PHP to clean up the former ... are these files owned by the domain user on your system?

Yeap, the folder is owned by the domain user. I am running PHP-FPM so each home folder for user such as /home/foo is owned by the user "foo" and there is tmp folder under it. /home/foo/tmp which is not automatically cleaned and is building pile of files around 1.8 million in a month time. the tmp folder is also created in subdomains such as /home/foo/domains/dev.example.com/tmp and is not cleaned automatically.

Is it bug or missing feature or i did not configure properly?

Ah, it sounds like you may have a PHP application which is generating temp files and not cleaning them afterwards.

You may need to look into the individual PHP applications that are being used to either have them clean up their temp files, or create a script to clear out temp files that are left over, and then have that script run from cron.

I checked all those tmp folder and php-fpm is configured to use that folder for session storage. But php itself is not clearing those session files and smallsession files are piling up. Shouldn't php itself clear up old obsolete session files?

It's up to the PHP application to do that... it sounds like the PHP application in question isn't clearing those out.

However, you can always create a script to run from cron to do that for you.

https://stackoverflow.com/a/654547/3657200

Take a look to the first comment:

NOTE: If you are using the subdirectory option for storing session files (see session.save_path above), then garbage collection does not happen automatically. You will need to do your own garbage collection through a shell script, cron entry, or some other method. For example, the following script would is the equivalent of setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes): cd /path/to/sessions; find -cmin +24 | xargs rm

How can I run single cron file say every 1 hour and run the above command for all the tmp folders under "home" directory. currently even subdomains have their own tmp folders so eiher 1. setup each cron for all domains and subdomains (hectic) or 2. setup single cron on root so as to clean all the tmp folders under home directory.

My Directory looks like this

/home/foo/tmp {main user}
/home/foo/domains/dev.example.com/tmp {subdomain }
/home/foo/domains/eng.example.com/tmp {subdomain }
/home/foo/domains/nep.example.com/tmp {subdomain }
/home/foo/domains/blog.example.com/tmp {subdomain }

Please suggest what my php cron command should contain for it be able to clear all tmp folders under home

<?php
chdir('/home/foo/tmp');
exec('find -cmin +24 | xargs rm');
chdir('/home/foo/domains/dev.example.com/tmp'); // and so on
exec('find -cmin +24 | xargs rm');
die;

How can I do above task in single linux command?

In my case I have only 30days old session files in my /home/*/tmp folders but I can't understand wheere is configurated like this.

I searched in my config files but I can't figured out.

find -cmin +24 -path "tmp"  -not -path "Maildir" -not -path "mail" -not -path ".tmp"

Helped me to list the files 24 minutes older and following command helps me to delete

find -type f -cmin +24 -path "*tmp*"  -not -path "*Maildir*" -not -path "*mail*" -not -path "*.tmp*" | xargs rm

Maybe useful for others too :)

Status: Active » Closed (fixed)

I ran into the same issue. Running a virgin php7.x ini file. I did some digging and here is what I found:

One thing you might not have noticed is that in the Debian/Ubuntu distro, by default PHP disables its session garbage collection mechanism

(eg. the default php.ini contains the line ;session.gc_probability = 0 in Ubuntu).

Instead, it runs a cron job every half hour (see the script /etc/cron.d/php5) to purge session files in the /var/lib/php5/ directory. In most cases, this doesn't do the session cleanup job for us as session data may not be saved in files under the /var/lib/php5/ directory (like in Drupal). Thus by default PHP session garbage collection does not run in Debian/Ubuntu as many may expect. To solve this, you can modify the php.ini file by adding the line

session.gc_probability = 1
Status:
Closed (fixed)
»
Needs work

hello @roshanbudhathoki and others

did anyone fix the problem? I have the same issue. The topic is closed, however nothing been fixed.

Thanks