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/ 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.



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/ 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.

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/ {subdomain }
/home/foo/domains/ {subdomain }
/home/foo/domains/ {subdomain }
/home/foo/domains/ {subdomain }

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

exec('find -cmin +24 | xargs rm');
chdir('/home/foo/domains/'); // and so on
exec('find -cmin +24 | xargs rm');

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
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.


philmck's picture
Submitted by philmck on Fri, 02/28/2020 - 19:38

I've hit this problem as well. As far as I can tell it's a commonly reported problem, the session files are being created as expected, but the periodic cleanup is failing and that is supposed to be done by a cron job (in recent versions of Ubuntu).

There is a cron job that calls /usr/lib/sessionclean periodically. At the moment I can't see anything obviously wrong with that file - it seems to be called as expected and it's trying to do sensible things to parse the php.ini files, find the gc_maxlifetime settings (which I have confirmed are set to 1440), find the locations of the sess_* files and delete the old ones. I haven't found anyone with a solution so it'll have to be debugged the old-fashioned way.

Let me see if I can add support for cleaning up session files in all the ~/tmp directories automatically.

Needs work

By default in virtualmin a server in php-fpm mode puts the php sessions forever, it is very possible that this is the problem, you can check the configuration options of each virtual server among the php-fpm configuration options. session

philmck's picture
Submitted by philmck on Tue, 04/21/2020 - 10:23

I think @ciberweb is talking about setting session.gc_maxlifetime but it's more complicated than that. See for a good explanation. I have been fixing the problem by just commenting out the session.save_path setting. Note that the comment character in these files is a semicolon.

;php_admin_value[session.save_path] = /home/[your site]/tmp

No, by default virtualmin crushes many of the options that you can configure. When putting a virtual server in php-fpm mode, configure the default sessions in "FOREVER", you can see it in the php-fpm configuration section of each virtual server, in the session configuration icon. The cron reads that configuration so it never deletes the sess_ files. It is also true that in php-fpm mode it changes the storage directory of the sess_ files and looking at the cron script in case it fails. To me this has caused me problems denying the service for filling the inodes. At the moment I have not wanted to modify the configuration to analyze it more carefully and what I have done is create a very simple cron for the moment with the following script: find /home/*/tmp/* -mmin +600 -type f -delete

At the moment it works very well ... but it is something simple I'm going to analyze my own clean scrip but it seems the problem is that it doesn't look in the / home / * / tmp directories

And the "FOREVER" configuration of virtualmin seems to crush the manuals ... I have not tried to modify the duration of the sessions in the configuration zone of php-fpm which would possibly be the most logical and simple ... but with the problem of inodes, modify the php script that lifts many sessions on a web with thousands of visits ... and then I found this problem that ends up solving radically with the cron. I feel my English