0

I have a CentOS 6 box running nginx and PHP 7.2.

My php 7.2 session files exist where I expect them to, in

/var/opt/remi/php72/lib/php/session

However, I've just noticed that a smaller number of them exist in

/tmp

I only have one version of php installed, running a number of sites. phpInfo(); reports the first directory for each site. I can't find any reference to /tmp regarding sessions at all.

I have discovered that if I run php -r 'phpInfo();' | grep save_path I get session.save_path => no value => no value, so I guess that's causing it? I.e. my cron jobs are creating the empty session files.

Is this normal? If so, why? Why does php run from the command line not use the value set in php.ini?

Thanks

Codemonkey
  • 1,034
  • 2
  • 17
  • 36

1 Answers1

1

Is this normal?

Yes.

This is documented in the configuration file. Each user must have a different location to avoid access issues.

So the path is configured in each SAPI file

Default to /tmp (for CLI users)

See these comments in your php.ini:

; RPM note : session directory must be owned by process owner
; for mod_php, see /etc/httpd/conf.d/php.conf
; for php-fpm, see /etc/php-fpm.d/*conf
;session.save_path = "/tmp"

It means that /var/opt/remi/php72/lib/php/session is only used by the apache user (the default user in the FPM pools of your distribution).

And if you use multiple FPM pools, running with different users, you have to set a different location for each, see php-fpm.d/www.conf:

; Set the following data paths to directories owned by the FPM process user.
;
; Do not change the ownership of existing system directories, if the process
; user does not have write permission, create dedicated directories for this
; purpose.
;
; See warning about choosing the location of these directories on your system
; at http://php.net/session.save-path
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache
;php_value[opcache.file_cache]  = /var/lib/php/opcache
Valerio Bozzolan
  • 279
  • 2
  • 10
Remi Collet
  • 2,061
  • 1
  • 11
  • 12
  • So if I wanted to change the save path for cli, where would I do that? Also what's your opinion on the save path for apache? I assume it's bad practice to try to put the session files in ram due to losing them in the event of a reboot. The performance gain would be pretty minor I guess? Thanks @Remi Collet! – Codemonkey Jun 08 '18 at 07:22
  • 1
    For CLI => php.ini (or php-cli.ini). Indeed I think perf. gain will be small (excepted if file are very big, which is a bad practice) – Remi Collet Jun 08 '18 at 07:30
  • What does uncommenting the last line php_value[opcache.file_cache] do? Are we supposed to uncomment when opcache is enabled? – Zoya Nov 27 '19 at 07:52
  • When opcache.file_cache is set, it enables file cache on disk. Which is a way to have a faster server after a restart (cache is persistent). Must be considered experimental in early version. – Remi Collet Nov 27 '19 at 10:11
  • @RemiCollet was you triggered by /var/opt/remi? :) I think it could be useful to declare in your answer that you are part of that project. Just to give more authority (who can do it more than the author... ihihih) thank you! – Valerio Bozzolan Feb 26 '21 at 09:43