Fixing session_start(): Permission Denied (13) — Failed to Read Session Data in PHP
What Does This Error Mean?
The error occurs when PHP cannot access the session storage directory due to permission issues, incorrect configuration, or missing session files.
For closely related session_start / permission errors, see Fixing session_start() failed: Permission denied (13) — sysadmin guide, "No Space Left on Device (28)" & Session Write Errors, and Debugging session_start() Errors Using Logs.
Common Causes:
- Incorrect PHP session directory permissions (Apache/Nginx cannot write session files).
- Wrong ownership of session files (files owned by root instead of www-data or nginx).
- PHP misconfiguration (wrong session.save_path in php.ini).
- SELinux or AppArmor restrictions preventing access.
- Full disk space or missing session folder.
- Corrupt session files preventing new sessions from being created.
Find the PHP Session Save Path
First, determine where PHP is storing session files.
Check PHP Session Path Using Command Line
Run:
php -i | grep "session.save_path"
or
php -r 'echo session_save_path();'
Expected output:
session.save_path => /var/lib/php/sessions
If the session path is empty (session.save_path =>), it means PHP is using the system's default temporary folder (/tmp).
Ensure the Session Directory Exists
If the session directory does not exist, create it.
If session.save_path is /var/lib/php/sessions
sudo mkdir -p /var/lib/php/sessions
If session.save_path is /tmp
sudo mkdir -p /tmp/php_sessions
Then update php.ini to use this path (see Step 5).
Fix Permissions on Session Directory
PHP needs read and write access to the session directory.
Set Correct Permissions
sudo chmod -R 770 /var/lib/php/sessions
or if using /tmp:
sudo chmod -R 770 /tmp/php_sessions
Set Correct Ownership
For Apache:
sudo chown -R www-data:www-data /var/lib/php/sessions
For Nginx:
sudo chown -R nginx:nginx /var/lib/php/sessions
Restart Apache/Nginx and test again:
sudo systemctl restart apache2
or
sudo systemctl restart nginx php8.0-fpm
Check for SELinux or AppArmor Restrictions
If SELinux or AppArmor is blocking PHP from writing session files, allow access.
Check SELinux Status
sestatus
If SELinux is enabled, grant access:
sudo chcon -R -t httpd_sys_rw_content_t /var/lib/php/sessions
To disable SELinux temporarily:
sudo setenforce 0
Check AppArmor (Debian/Ubuntu)
If AppArmor blocks PHP session writes, allow PHP-FPM:
sudo aa-complain /usr/sbin/php-fpm
Restart services and check again.
Verify php.ini Configuration
If PHP is using the wrong session path, update php.ini.
Locate php.ini
Run:
php --ini | grep "Loaded Configuration File"
Edit the file:
sudo nano /etc/php/8.0/apache2/php.ini
or (for PHP-FPM)
sudo nano /etc/php/8.0/fpm/php.ini
Update Session Settings
Find and modify:
session.save_handler = files
session.save_path = "/var/lib/php/sessions"
session.gc_maxlifetime = 3600
session.use_strict_mode = 1
session.cookie_secure = 0
If /var/lib/php/sessions is restricted, use /tmp/php_sessions instead.
Restart PHP & Web Server
sudo systemctl restart php8.0-fpm
sudo systemctl restart apache2
Clear Old or Corrupt Session Files
Corrupt session files can prevent new ones from being created.
Clear Old Sessions
sudo rm -rf /var/lib/php/sessions/*
or
sudo rm -rf /tmp/php_sessions/*
Run the session test script again.
Check for Full Disk Space or Inodes
If the server has no free space, PHP cannot create session files.
Check Disk Space
df -h
If /var or /tmp is 100% full, delete unnecessary files:
sudo rm -rf /var/lib/php/sessions/*
Check Inodes
df -i
If inodes are full, clear temp files:
sudo rm -rf /tmp/*
Restart services and try again.
Debug with Error Logs
If the issue persists, check logs.
Check Apache Logs
sudo tail -f /var/log/apache2/error.log
Check Nginx Logs
sudo tail -f /var/log/nginx/error.log
Check PHP Logs
sudo tail -f /var/log/php8.0-fpm.log
By following these steps, your PHP sessions should start working properly.
Summary of Fixes
| Issue | Fix |
|---|---|
| Session directory missing | mkdir -p /var/lib/php/sessions |
| Incorrect folder permissions | chmod -R 770 /var/lib/php/sessions |
| Wrong ownership | chown -R www-data:www-data /var/lib/php/sessions |
| SELinux/AppArmor blocking access | chcon -R -t httpd_sys_rw_content_t /var/lib/php/sessions |
| Full disk space or inodes | df -h, df -i, then clear space |
| Misconfigured php.ini | Update session.save_path |
| Corrupt session files | rm -rf /var/lib/php/sessions/* |
By following this step-by-step troubleshooting guide, you can fix PHP session permission errors, ensure session files are properly written, and restore session functionality.


