After deploying a new project or executing artisan commands on the server, you can encounter permission denied error in Laravel when it attempts to write logs or cache. Misconfigured write permissions or ownership on the storage directory cause this issue.
In this short article, I will explain why it happens, how to fix it, and avoid in the future.
Table of Contents
How to Fix Permission Denied in Laravel
Firstly, let’s look at why this error happens. Usually, when you install Laravel, you run the installation commands as the user you are logged in to the system or as the root user. In this case, all files and directories created by the installation command will belong to this user. To understand how it causes the issue, you must understand how Linux permissions work.
In Linux, file and folder permissions are applied for the file owner, the user group that owes the file, and all other users. You can read about it in detail in RedHat documentation. Each program in the system is run by a specific user, and it has the same access rights to files as the user has. By default in Linux, only the file owner and group that owns the file are permitted to write to the file. However, the Apache web server or PHP-FPM is almost never run as root. In most cases, it is the special user called www-data. Therefore, the PHP interpreter and all scripts it executes are considered “others” and can only read Laravel files but cannot write data.
Ideally, all Laravel files should belong to the user under which the PHP interpreter is running. However, this is optional. Laravel applications only need to have write access to the storage folder. Here are stored cache files, logs, and user-uploaded files.
If you set up the correct permissions for the storage folder after installing Laravel, permission issues will disappear. However, it can appear while using Laravel, and here’s why. If you execute any artisan command and an error occurs during its execution, a log file is created, and this log file will belong to your current user. And again, usually, it is different from the user on whose behalf the PHP interpreter is running. Therefore, PHP scripts that are executed on the web server will no longer be able to write data into this file. To get around this, you should configure Laravel to create log files with write permissions for all users in the system.
You can check which user is used to run php-fpm using the ps utility in Linux:
ps aux | grep php-fpm
There is one master process that is run by root, and two workers run by www-data. If you want to check what permissions are currently set for files and folders in the storage directory, use the ls command with the -l option. Execute it in the project folder:
ls -l storage/
The permissions are displayed as three groups: rwx rwx and r-x (r – Read, w – Write, x – Execute). This means that in the example above, the user haait has write permissions and users who are added to the haait group. Everyone else has read-only access only.
Please note that the PHP interpreter user must have at least read permissions for all folders above the Laravel project folder. You can quickly see the permissions for a folder hierarchy using the namei command with the -l option. For example, if the project is located in /var/www/html:
namei -l /var/www/html/storage/
Now let’s look at how to fix the error.
1. Fixing Permissions After Install (proper way)
If you know which user is used by the PHP interpreter that processes your project’s scripts, it is better to change the owner of the storage directory to this user. As I said before, this is usually www-data:
sudo chown -R www-data:www-data storage
Here, the first www-data is the user name, the second is the group name, and the -R option means that the owner will be changed for all files and all subfolders recursively. After this, the error will disappear:
The command above will help you fix an error with log write permissions after running artisan commands. However, it is better to configure default permissions to allow writing to all users for new log files in Laravel to avoid the issue in the future.
2. Fixing Permissions After Install (acceptable way)
If you do not know which user is used by the PHP interpreter and need to fix the issue quickly, the easiest way is to allow all users to read and write to this folder. The command will look like this:
sudo chmod -R ugo+rwx storage
Alternatively, you can use octal permissions syntax:
sudo chmod -R 777 storage
However, pay attention that you should use this carefully, because all users on the server will be able to write into the storage folder. It’s not safe.
3. Fix Logs Permissions
First, if this error happens, you should delete the log file or update its permissions, as shown above. For example, remove:
sudo rm -Rf storage/logs/laravel.log
By default, Laravel writes logs to a single file called storage/logs/laravel.log. To make this file writable to all users, add the following line to the config/logging.php file in the channels -> single section:
"permission" => 0777,
After that, all users in the system will be able to write into the log file. It is not secure, but it help to avoid this error:
If you use other logging channels that utilize files for logging, you need to add this parameter for them as well.
4. Run Artisan From www-data
Alternatively, you can always run the artisan and composer commands by the same user as the PHP interpreter using the sudo command with the -u option. For example www-data:
sudo -u www-data php artisan unknown:command
This is how the permissions and owner for the log will look like if you run artisan only by the www-data user:
So the owner will be set correctly automatically, and there will be no issues either when Laravel is deployed or when writing to the log.
In this short article, we’ve looked at how to fix the permission denied error in Laravel that occurs when the framework tries to write logs or cache to the storage folder. As you can see, everything is quite simple.