6

I'm running a website with a nginx server with PHP Fastcgi on a VPS. I tried to configure the php.ini (in /etc/php5/cgi) to have PHP session last longer than 3 days (259200 seconds), but it didn't work and my php sessions don't last more than one hour.

My current session config in the php.ini:

session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 259200
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 259200
session.bug_compat_42 = Off
session.bug_compat_warn = Off
session.referer_check =
session.entropy_length = 0
session.cache_limiter = nocache
session.cache_expire = 259200
session.use_trans_sid = 0
session.hash_function = 0

What may be the problem here?

NeDark
  • 1,212
  • 7
  • 23
  • 36
  • 1
    Are you sure that's the php.ini file being loaded into PHP? Have you tried double checking the "Loaded Configuration File" path in `phpinfo()` to make sure? – animuson Dec 04 '11 at 00:24
  • Try using `var_dump(ini_get('session.cookie_lifetime'));` in a PHP file across all the applicable settings in your PHP ini values to ensure that they are not being overridden somewhere else. – Treffynnon Dec 04 '11 at 00:31
  • @animuson: Yes, it's being loaded. Configuration File (php.ini) Path /etc/php5/cgi Loaded Configuration File /etc/php5/cgi/php.ini Scan this dir for additional .ini files /etc/php5/cgi/conf.d Additional .ini files parsed /etc/php5/cgi/conf.d/curl.ini, /etc/php5/cgi/conf.d/gd.ini, /etc/php5/cgi/conf.d/mysql.ini, /etc/php5/cgi/conf.d/mysqli.ini, /etc/php5/cgi/conf.d/pdo.ini, /etc/php5/cgi/conf.d/pdo_mysql.ini – NeDark Dec 04 '11 at 00:53
  • @Treffynnon: string(6) "259200" – NeDark Dec 04 '11 at 00:57
  • What's your fastcgi cache validity duration? – favoretti Dec 04 '11 at 01:34
  • @favoretti: How can I check that? – NeDark Dec 04 '11 at 01:52
  • @NeDark: either in nginx.conf or in your_site.conf, do you have anything like: fastcgi_cache_valid 200 1m; ? – favoretti Dec 04 '11 at 01:57
  • @favoretti: No, i don't have anything like that – NeDark Dec 04 '11 at 02:16
  • @NeDark: peculiar really. I read somewhere at some point that nginx doesn't honor php.ini's session.* parameters, but I can't find off-hand how to increase PHP's session validity via nginx. Good one really :) – favoretti Dec 04 '11 at 02:17

2 Answers2

12

This sounds a bit like Ubuntu or Debian on the server. If I rememeber correctly there is a cronjob somewhere (installed either by the php5 or the php5-common package) which cleans out your session directory more often.

I'd recommend you configure your sessions to be saved somewhere else (than the default). Adjust session.save_path and verify to cronjob doesn't empty it.

The cronjob is somewhere like /etc/cron.d/php - to be certain, run dpkg -L php5 or dpkg -L php5-common. Assuming you are on Ubuntu (or Debian) this should show you the location of all installed files.

Till
  • 22,236
  • 4
  • 59
  • 89
  • Finally I solved the problem by [storing sessions with memcached](http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/). However, your solution looks to be valid so I'll acept your answer. Thanks. – NeDark Dec 04 '11 at 07:48
4

To help you debug, you can check the current setting that the PHP cron job is "seeing" by executing:

/usr/lib/php5/maxlifetime

The maxlifetime script searches all your php.ini files for the session.gc_maxlifetime and uses the largest value. The value printed is in minutes.

bpossolo
  • 849
  • 5
  • 6