PHP sessions are essential for maintaining user state across web pages. The "No such file or directory" error is a common issue that prevents PHP applications from starting sessions, typically caused by problems with the session storage directory.
This error occurs when PHP cannot find or create session files in the specified session directory. It usually means:
1 First, identify where PHP is configured to store session files.
# Method 1: Using php -i command
php -i | grep "session.save_path"
# Method 2: Direct PHP command
php -r 'echo session_save_path();'
# Method 3: Create a simple PHP test file
echo "<?php echo session_save_path(); ?>" > /tmp/test_session.php
php /tmp/test_session.php
# Method 4: Check all PHP configuration
php --info | grep -A 5 -B 5 session.save
session.save_path => /var/lib/php/session/tmp as the default session directory.
# Check the directory from session.save_path
SESSION_PATH=$(php -r 'echo session_save_path();')
echo "Session path: $SESSION_PATH"
# Verify if the directory exists
if [ -d "$SESSION_PATH" ]; then
echo "Directory exists: $SESSION_PATH"
ls -la "$SESSION_PATH"
else
echo "Directory does NOT exist: $SESSION_PATH"
fi
# Check permissions and ownership
if [ -d "$SESSION_PATH" ]; then
stat "$SESSION_PATH"
fi
2 Create the session directory if it doesn't exist.
# Check if /var/lib/php/session exists
ls -la /var/lib/php/ 2>/dev/null || echo "/var/lib/php/ does not exist"
# Create the directory structure
sudo mkdir -p /var/lib/php/session
# Verify creation
ls -la /var/lib/php/session
# If the parent directory doesn't exist, create it
sudo mkdir -p /var/lib/php
sudo mkdir -p /var/lib/php/session
# If you prefer to use /tmp for sessions
sudo mkdir -p /tmp/php_sessions
# Verify
ls -la /tmp/php_sessions
# Note: Files in /tmp may be cleared on reboot
# Consider setting proper permissions
sudo chmod 1777 /tmp/php_sessions
3 If PHP cannot write session files, adjust directory permissions.
# Standard permissions for session directory
sudo chmod -R 770 /var/lib/php/session
# Alternative: More restrictive permissions
sudo chmod -R 750 /var/lib/php/session
# If using /tmp directory
sudo chmod -R 770 /tmp/php_sessions
# Verify permissions
ls -la /var/lib/php/session
stat /var/lib/php/session | grep -i access
# For Apache web server (Ubuntu/Debian)
sudo chown -R www-data:www-data /var/lib/php/session
# For Nginx web server (CentOS/RHEL)
sudo chown -R nginx:nginx /var/lib/php/session
# Alternative: Use the PHP-FPM user (common with Nginx)
# First, find the PHP-FPM user
grep -r "^user\|^group" /etc/php/8.0/fpm/pool.d/ 2>/dev/null || echo "Check php-fpm config"
# Typically for PHP-FPM with Nginx:
sudo chown -R www-data:www-data /var/lib/php/session # Ubuntu
sudo chown -R nginx:nginx /var/lib/php/session # CentOS
# Verify ownership
ls -la /var/lib/php/session
# For Apache
sudo systemctl restart apache2
# For Nginx with PHP-FPM
sudo systemctl restart nginx
sudo systemctl restart php8.0-fpm
# For standalone PHP-FPM
sudo systemctl restart php8.0-fpm
# Verify services are running
sudo systemctl status apache2 --no-pager -l
sudo systemctl status nginx --no-pager -l
sudo systemctl status php8.0-fpm --no-pager -l
4 Ensure PHP configuration points to the correct session directory.
# Find loaded configuration file
php --ini | grep "Loaded Configuration File"
# Common php.ini locations:
# Ubuntu/Debian Apache: /etc/php/8.0/apache2/php.ini
# Ubuntu/Debian CLI: /etc/php/8.0/cli/php.ini
# Ubuntu/Debian FPM: /etc/php/8.0/fpm/php.ini
# CentOS/RHEL Apache: /etc/php.ini
# CentOS/RHEL FPM: /etc/php-fpm.d/www.conf
# Check multiple possible locations
for conf in /etc/php/8.0/apache2/php.ini /etc/php/8.0/fpm/php.ini /etc/php.ini; do
if [ -f "$conf" ]; then
echo "=== $conf ==="
grep "session.save_path" "$conf" || echo "Not found in $conf"
fi
done
# Edit the main php.ini file (adjust path based on previous step)
sudo nano /etc/php/8.0/apache2/php.ini
# Or for PHP-FPM
sudo nano /etc/php/8.0/fpm/php.ini
# Search for session settings (press Ctrl+W in nano):
# session.save_handler = files
# session.save_path = "/var/lib/php/session"
# session.gc_maxlifetime = 3600
# session.use_strict_mode = 1
# session.cookie_secure = 0
# Make sure session.save_path points to an existing directory
# Example: session.save_path = "/var/lib/php/session"
# Save and exit (Ctrl+X, then Y, then Enter)
session.save_handler = files - Use files for session storagesession.save_path = "/var/lib/php/session" - Path to writable directorysession.gc_maxlifetime = 1440 - Session lifetime (24 minutes default)session.gc_probability = 1 - Probability of garbage collectionsession.gc_divisor = 100 - Divisor for garbage collection probabilitysession.use_strict_mode = 1 - Enhanced session securitysession.cookie_secure = 1 - Only send cookies over HTTPS (if using SSL)# For Apache
sudo systemctl restart apache2
# For Nginx with PHP-FPM
sudo systemctl restart php8.0-fpm
sudo systemctl restart nginx
# Verify the new settings are active
php -i | grep session.save_path
php -r 'echo "Session path: " . session_save_path() . "\n";'
# Test with a web PHP file if needed
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test_phpinfo.php
echo "Visit: http://yourserver/test_phpinfo.php and check session section"
5 Corrupt or locked session files can prevent new ones from being created.
# Remove all session files from standard directory
sudo rm -rf /var/lib/php/session/*
# Remove session files from /tmp if using that
sudo rm -rf /tmp/php_sessions/*
sudo rm -f /tmp/sess_*
# Alternative: Remove only old session files (older than 1 day)
sudo find /var/lib/php/session/ -type f -mtime +1 -delete
# Remove only PHP session files (files starting with sess_)
sudo find /var/lib/php/session/ -name "sess_*" -delete
# Check what was removed
echo "Files in session directory after cleanup:"
ls -la /var/lib/php/session/ 2>/dev/null || echo "Directory is empty or doesn't exist"
# Create a simple PHP session test
sudo tee /var/www/html/session_test.php << 'EOF'
<?php
session_start();
echo "Session test started<br>";
echo "Session ID: " . session_id() . "<br>";
echo "Session save path: " . session_save_path() . "<br>";
// Set a session variable
$_SESSION['test_time'] = date('Y-m-d H:i:s');
echo "Session variable set: " . $_SESSION['test_time'] . "<br>";
// Check if session file was created
$session_file = session_save_path() . '/sess_' . session_id();
if (file_exists($session_file)) {
echo "<span style='color: green;'>SUCCESS: Session file created: $session_file</span>";
} else {
echo "<span style='color: red;'>ERROR: Session file NOT created</span>";
}
?>
EOF
# Test via command line
php /var/www/html/session_test.php
# Or visit in browser: http://yourserver/session_test.php
6 Insufficient disk space prevents PHP from creating new session files.
# Check overall disk usage
df -h
# Check specific partitions (usually /var or /)
df -h /var
df -h /
df -h /tmp
# Check inode usage (important for many small session files)
df -i
df -i /var
df -i /tmp
# Check directory sizes
sudo du -sh /var/lib/php/session/ 2>/dev/null
sudo du -sh /tmp/ 2>/dev/null
sudo du -sh /var/log/ 2>/dev/null
# Find largest directories/files
sudo du -ah /var 2>/dev/null | sort -rh | head -20
sudo find /var -type f -size +100M 2>/dev/null | head -10
# Clean package manager cache
sudo apt clean # Ubuntu/Debian
sudo yum clean all # CentOS/RHEL
sudo dnf clean all # Fedora/CentOS 8+
# Remove old log files
sudo journalctl --vacuum-time=7d
sudo find /var/log -type f -name "*.gz" -delete
sudo find /var/log -type f -name "*.old" -delete
sudo find /var/log -type f -name "*.log.*" -delete
# Clean temporary files
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*
# Remove old kernel versions (Ubuntu/Debian)
sudo apt autoremove --purge
# Remove old downloaded packages
sudo apt-get autoclean
# Check and clean Docker resources if installed
docker system prune -a -f 2>/dev/null || true
7 If issues persist, check server logs for detailed error information.
# Check Apache error log
sudo tail -f /var/log/apache2/error.log
# Alternative Apache log locations
sudo tail -f /var/log/httpd/error_log # CentOS/RHEL
sudo tail -f /var/log/apache2/error.log # Ubuntu/Debian
# Search for session-related errors
sudo grep -i "session" /var/log/apache2/error.log
sudo grep -i "No such file" /var/log/apache2/error.log
# Check access logs for the failing request
sudo tail -f /var/log/apache2/access.log
# Increase PHP error logging temporarily
# Add to your PHP script or .htaccess:
# ini_set('display_errors', 1);
# error_reporting(E_ALL);
# Check Nginx error log
sudo tail -f /var/log/nginx/error.log
# Alternative Nginx log locations
sudo tail -f /usr/local/nginx/logs/error.log # Custom install
# Search for PHP-FPM errors
sudo grep -i "php" /var/log/nginx/error.log
sudo grep -i "session" /var/log/nginx/error.log
# Check Nginx access log
sudo tail -f /var/log/nginx/access.log
# Check PHP-FPM error log specifically
sudo tail -f /var/log/php8.0-fpm.log
# Check PHP error log
sudo tail -f /var/log/php_errors.log
# PHP-FPM specific logs
sudo tail -f /var/log/php8.0-fpm.log
sudo tail -f /var/log/php-fpm.log
# Search for session errors in PHP logs
sudo grep -i "session_start" /var/log/php_errors.log
sudo grep -i "No such file" /var/log/php_errors.log
# Enable detailed PHP error logging temporarily
# Add to your script or php.ini:
# error_log = /var/log/php_errors.log
# log_errors = On
# error_reporting = E_ALL
sudo tee /var/www/html/test.php << 'EOF'
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();
$_SESSION['test'] = 'Working';
echo "Session test: " . $_SESSION['test'];
?>
EOF
Then visit: http://yourserver/test.php
| Issue | Fix | Command/Solution |
|---|---|---|
| Session directory missing | Create directory | sudo mkdir -p /var/lib/php/session |
| Incorrect folder permissions | Set proper permissions | sudo chmod -R 770 /var/lib/php/session |
| Wrong ownership | Change ownership | sudo chown -R www-data:www-data /var/lib/php/session |
| Misconfigured php.ini | Update session.save_path | Edit /etc/php/8.0/apache2/php.ini or /etc/php/8.0/fpm/php.ini |
| Corrupt session files | Clear session files | sudo rm -rf /var/lib/php/session/* |
| Disk space full | Free up space | df -h, df -i, then clean up |
| Wrong PHP configuration | Check PHP logs | sudo tail -f /var/log/php_errors.log |
| Web server misconfiguration | Check web server logs | sudo tail -f /var/log/apache2/error.log |
| PHP-FPM user mismatch | Check PHP-FPM config | Review /etc/php/8.0/fpm/pool.d/www.conf |
| SELinux/AppArmor restrictions | Adjust security policies | sudo setenforce 0 (temporary test) or adjust policies |
By following this comprehensive troubleshooting guide, you should be able to resolve the "session_start(): No such file or directory" error and restore proper PHP session functionality to your web applications.