file_put_contents(meta/services.json): failed to open stream: Permission denied

file_put_contents(meta/services.json): failed to open stream: Permission denied

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

Stefan Bogdanescu

Founder & Senior Architect

file_put_contents(meta/services.json): failed to open stream: Permission denied

The error file_put_contents(...): failed to open stream: Permission denied is a classic signal that your application has exhausted its ability to write files, even if it can read them. In the context of Laravel, this specific exception usually points to a conflict between how the web server runs and how your user accounts are configured, particularly when the application attempts to initialize service providers during the bootstrapping phase.

When you encounter file_put_contents(/var/www/html/laravel/app/storage/meta/services.json): failed to open stream, Laravel is trying to cache metadata about your service providers into a JSON file within the storage directory. This happens early in the request lifecycle, often before your controllers even execute. If this write operation fails, Laravel cannot proceed with normal execution and throws an exception that bubbles up to the handler.

The most common mistake developers make when facing this issue is relying solely on chmod -R 777. While this grants read, write, and execute permissions recursively, it is a security anti-pattern in modern Linux environments. It allows any user with access to the server to modify your application files. Furthermore, simply changing permissions to 777 does not guarantee that the specific process running the web server (e.g., www-data) owns the files; if the parent directory was created by a different user, subdirectories might inherit restrictive permissions regardless of what you set on the leaves.

A more robust approach involves ensuring the ownership matches the execution context. You need to identify which user the web server is running as. On many systems, this is www-data, but it could also be apache, nginx, or a dedicated application user like app. Once identified, you should change the group ownership of the storage and log directories to match that user's primary group. This ensures that any file created by the web server within those directories automatically inherits the correct permissions without needing global world-writable access.

You can achieve this with commands similar to the following:

sudo chown -R www-data:www-data /var/www/html/laravel/app/storage
sudo chown -R www-data:www-data /var/www/html/laravel/app/bootstrap/cache
sudo chmod -R 775 /var/www/html/laravel/app/storage

The 775 permission set allows the owner and group to read, write, and execute, while restricting others. This is significantly safer than 777. If your application runs as a different user (e.g., deploy), adjust the chown command accordingly.

Another frequent pitfall occurs when the file already exists with restrictive permissions from a previous deployment or failed run. Even if you set directory permissions to 775, an existing services.json file might be owned by root. In this case, file_put_contents will still fail because the web server cannot overwrite files owned by another user unless that user has write access. You must ensure both the directory and the specific files inside it are owned by the web server process. Running a recursive ownership change on the storage folder usually resolves this:

sudo chown -R www-data:www-data /var/www/html/laravel/app/storage/meta

It is also worth noting that Laravel creates these metadata files to optimize performance by avoiding repeated reflection operations. If you are developing locally using a virtual machine or Docker, ensure the volume mount permissions are correctly propagated from your host machine to the container. Mounting with :ro (read-only) will definitely trigger this error, as will mounting without proper UID/GID mapping if the host files were created by a different user than the container's default www-data.

If you are running Laravel on a shared hosting environment where you cannot use sudo, you may need to contact your provider to ensure the upload and storage directories have the correct group ownership, or look into configuring the application to run as a specific system user that matches the web server's permissions.

Finally, if you have recently updated PHP or Laravel, check for any SELinux or AppArmor policies that might be blocking write access even when file permissions appear correct. These mandatory access control systems can override standard chmod settings. If you encounter this and are certain the ownership is correct, reviewing the audit logs (/var/log/audit/audit.log) will often reveal whether a policy denial is the root cause rather than a simple permission issue.

Our network