The error occurs when PHP cannot access the session storage directory due to permission issues, incorrect configuration, or missing session files.
First, determine where PHP is storing session files.
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).
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).
PHP needs read and write access to the session directory.
sudo chmod -R 770 /var/lib/php/sessions
or if using /tmp:
sudo chmod -R 770 /tmp/php_sessions
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
If SELinux or AppArmor is blocking PHP from writing session files, allow access.
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
If AppArmor blocks PHP session writes, allow PHP-FPM:
sudo aa-complain /usr/sbin/php-fpm
Restart services and check again.
If PHP is using the wrong session path, update 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
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.
sudo systemctl restart php8.0-fpm
sudo systemctl restart apache2
Corrupt session files can prevent new ones from being created.
sudo rm -rf /var/lib/php/sessions/*
or
sudo rm -rf /tmp/php_sessions/*
Run the session test script again.
If the server has no free space, PHP cannot create session files.
df -h
If /var or /tmp is 100% full, delete unnecessary files:
sudo rm -rf /var/lib/php/sessions/*
df -i
If inodes are full, clear temp files:
sudo rm -rf /tmp/*
Restart services and try again.
If the issue persists, check logs.
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/php8.0-fpm.log
By following these steps, your PHP sessions should start working properly.
| 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.