Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

session_start() cannot open its session file because of Permission denied

ErrorWarning: session_start(): open(/var/lib/php/sessions/sess_abc123, O_RDWR) failed: Permission denied (13)

Short answer

With the default files handler, PHP writes one sess_ file per visitor into session.save_path. The warning means the PHP process cannot create or open files there. Read the path in the warning, then check that the folder exists, that the PHP user can write to it, that open_basedir allows it, and that any N; directory levels exist.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask PHP which save path and handler it is really using. Read the path in the warning and compare it with this value.

    php -i | grep -E 'session.save_path|session.save_handler'
  2. 2Match what you see, then jump to its fix

    1. 1open(.../sess_..., O_RDWR) failed: Permission denied (13) with a path that exists.The PHP user cannot write to the save path folderFix
    2. 2The folder has the right owner, yet PHP is refused; open_basedir is set for the site.open_basedir does not include the save pathFix
    3. 3session.save_path looks like "2;/path" and PHP cannot create session files.save_path uses the N; form but the sub-folders were never createdFix

What does the error look like?

  • Warning: session_start(): open(/var/lib/php/sessions/sess_abc123, O_RDWR) failed: Permission denied (13)
  • Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions)

Cause 1 of 3

The PHP user cannot write to the save path folder

1Check

The files handler creates the session files itself, so the user PHP runs as needs write access to the folder. Test whether the account PHP runs as (www-data on a default Ubuntu setup, used here as the example) can write there.

ls -ld /var/lib/php/sessions
sudo -u www-data test -w /var/lib/php/sessions && echo writable || echo not-writable

2Fix

Give the PHP user write access to that folder, or point session.save_path at a folder it owns. Do not use a world-readable folder such as /tmp for real sites: the manual warns other users could list the files and hijack sessions.

; php.ini or the pool config
session.save_path = "/var/lib/php/my-app-sessions"

3Verify

Create the folder, own it as the PHP user, reload PHP and load a page that calls session_start().

sudo mkdir -p /var/lib/php/my-app-sessions
sudo chown www-data:www-data /var/lib/php/my-app-sessions
sudo chmod 700 /var/lib/php/my-app-sessions
sudo systemctl reload php8.3-fpm

Confirmed by php.net

Did this fix it?

Cause 2 of 3

open_basedir does not include the save path

1Check

When open_basedir is set, PHP refuses to open files outside the listed directory trees. The session folder must be inside the list.

php -i | grep open_basedir

2Fix

Add the session folder to open_basedir (directories are separated by a colon on Linux), or move session.save_path inside an allowed directory.

open_basedir = "/var/www/my-app:/var/lib/php/my-app-sessions"

3Verify

Reload PHP and request a page that starts a session.

sudo systemctl reload php8.3-fpm

Confirmed by php.net

Did this fix it?

Cause 3 of 3

save_path uses the N; form but the sub-folders were never created

1Check

With N greater than 0, PHP spreads files over N levels of sub-folders and you must create all of them yourself. The manual points to the mod_files.sh script in ext/session for this.

php -i | grep session.save_path

2Fix

Create the sub-folders, or remove the N; prefix from session.save_path. The manual also warns that with N above 0 automatic garbage collection is not performed.

session.save_path = "/var/lib/php/my-app-sessions"

3Verify

Reload PHP, load a page that starts a session and look for a new sess_ file in the folder.

ls /var/lib/php/my-app-sessions

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Give each site its own session folder, owned by that site's PHP user and with mode 700.
  • Keep session.save_path in the same config file as open_basedir, so they are changed together.
  • After a server move or a PHP upgrade, start a session once and check the warning log before launch.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

PHP $_SESSION values are empty on the next page

session_start() has no effect and no cookie is sent

A session works only when the browser sends back the session cookie and session_start() runs before any output.

4 causes, each with a fix

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.