php-fpm and nginx session problems
When a PHP application relies heavily on sessions, the most frustrating errors are those that appear sporadically but consistently prevent data persistence. The specific error you are encounteringâPermission denied (13) when trying to open /tmp/sess_*âis a classic symptom of a mismatch between the filesystem ownership and the user context in which PHP-FPM runs.
The stack trace clearly points to the session.save_path, which is set to /tmp. By default, most Linux distributions create this directory owned by root with restrictive permissions (often 1777 or 700). When PHP tries to write a session file, it fails if the process does not have write access.
In your configuration, you are running both Nginx and PHP-FPM under the system user nginx. This is visible in your Nginx config (user nginx;) and implied by the pool definitions often found in /etc/php-fpm.d/. While this setup secures your web server against unauthorized access, it creates a conflict with the standard Linux session storage location. The /tmp directory usually belongs to root, meaning any user without explicit write permissions cannot save sessions there.
The immediate fix is to change the ownership of the session directory to match the user running PHP-FPM. If your pool configuration (likely in /etc/php-fpm.d/www.conf or similar) defines a user and group, that must be applied to the session storage path. For example:
chown -R nginx:nginx /tmp
chmod -R 1777 /tmp
Alternatively, you can configure PHP to store sessions in a directory owned by root but writable by the nginx user, or move the sessions entirely outside of /tmp into a dedicated directory like /var/lib/php-sessions. This approach is often preferred in production environments because /tmp is typically mounted with noexec or cleared on reboot, which can cause issues with long-running state.
Another subtle but common pitfall involves the SELinux context or AppArmor profiles if your distribution enforces mandatory access controls. Even if file permissions are correct (chmod and chown), the security module might block writes to /tmp depending on the domain label of the process. Check the PHP-FPM error log at /var/log/php-fpm/error.log for SELinux denials, which usually appear as AVC: denied { write }.
If you are using a framework like Laravel or Symfony, ensure that your applicationâs session configuration explicitly sets the save path to a directory you have full control over. For instance, in Laravel's .env file:
SESSION_DRIVER=file
SESSION_PATH=/var/lib/laravel/sessions
SESSION_LIFETIME=120
Then create the directory with appropriate ownership:
mkdir -p /var/lib/laravel/sessions
chown -R nginx:nginx /var/lib/laravel/sessions
chmod -R 775 /var/lib/laravel/sessions
This ensures that regardless of the underlying OS defaults, your application has a guaranteed write location. Always verify the session.save_path directive in your php.ini or environment file matches the actual directory you intend to use.
Finally, remember that changing file permissions globally on /tmp can be a security risk if not handled carefully. It is better practice to isolate session storage to a dedicated path rather than relying on default system directories that may have inconsistent permission policies across different Linux distributions.