How to get my session to write to apache

How to get my session to write to apache

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving Apache Session Permission Denies Beyond Basic Chmod

When session_start() fails with a "Permission denied" error, it is tempting to immediately assume the issue lies with the directory permissions and that a quick chmod command will fix it. However, as the provided logs demonstrate, simply running chmod o+rw /var/lib/php/session often leaves the problem unresolved. This indicates that the permission model on Linux is more complex than just granting read and write access to "others."

The error message explicitly states that the operation failed in /var/www/vhosts/alt.alternativedc.com/httpdocs/index.php. The script is attempting to open a session file located in /var/lib/php/session using O_RDWR. The core issue here is rarely just about read/write bits; it is frequently about ownership and the specific user context under which Apache runs.

Understanding the User Context

Linux filesystems track three distinct permission sets: owner, group, and others. When you execute your PHP script via Apache, the process does not run as root. Instead, it runs as a specific system user, typically named www-data on Debian/Ubuntu or apache on CentOS/RHEL.

If the /var/lib/php/session directory is owned by root:root, and your web server user does not have write permissions, changing the mode to 777 (which allows "others" to write) creates a security vulnerability known as world-writable directories. While this might seem like a quick fix, it exposes your entire application to session hijacking attacks where any user on the network can overwrite another's session cookie.

The Correct Approach: Ownership and Group

The robust solution involves aligning the directory ownership with the web server process. You must identify the user Apache runs as. On most modern distributions, this is www-data. The goal is to make the session directory owned by that user so it can write files without requiring world-writable permissions.

First, verify which user the Apache service is running as:

ps aux | grep apache2
# or for CentOS/RHEL
ps aux | grep httpd

Once identified (let's assume www-data), change the ownership of the session directory recursively:

chown -R www-data:www-data /var/lib/php/session

After changing ownership, you should ensure the directory permissions are secure but functional. A standard permission set for a session directory is 750 or 770. This allows the owner (www-data) full access and the group (if configured correctly) read/write access, while restricting "others" to no access.

chmod -R 770 /var/lib/php/session

If your application resides in a chroot environment or uses virtual hosting specifically (as hinted by the path /var/www/vhosts/), ensure that the PHP configuration (php.ini) points to a directory inside your specific vhost structure rather than the global system session folder. This isolates sessions per domain and prevents cross-contamination.

Checking SELinux Contexts

If you are running on RHEL, CentOS, or Fedora, standard chmod commands will not suffice if SELinux is enforcing. Even if permissions are correct for the OS level, SELinux may block the write operation if the security context does not allow the web server to write to /var/lib/php/session.

You can check the current context with:

ls -Z /var/lib/php/session

If the output shows a restricted context (often starting with unconfined_u or lacking httpd_sys_rw_content_t), you must restore the default context or explicitly allow writes. On these systems, you often need to set the correct type:

chcon -R -t httpd_sys_rw_content_t /var/lib/php/session

Alternatively, adding a rule to /etc/selinux/config to set SELINUX=permissive is a temporary workaround but not recommended for production environments.

Verifying the Fix

After adjusting ownership and permissions, restart the Apache service to ensure no stale processes hold locks or use cached configurations:

systemctl restart apache2
# or
systemctl restart httpd

Reload your homepage. The warning logs should disappear, and session_start() should successfully create new files in /var/lib/php/session. By correctly assigning ownership to the web server user and respecting SELinux policies where applicable, you maintain security while ensuring reliability across different server migrations.

Our network