Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Laravel is a popular PHP framework used by many developers to create web applications. A common issue that can occur when working with Laravel is the following error:
The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: permission denied
This error Laravel The stream or file
occurs when Laravel is unable to write to its log file because it doesn’t have permission to access the storage folder.
Another common error that can occur when the Laravel log permissions are not set correctly in Laravel is the following:
Error in exception handler: The stream or file "/var/www/laravel/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/laravel/bootstrap/compiled.php:8423
Table of Contents
One solution that is often suggested in forums and blogs is to apply the chmod 777 permission
, which exposes your server to security risks.
In some cases, other chmod or chgrp commands are recommended, which may not decrease the server’s security but don’t provide a definitive solution to avoid laravel permission denied
either.
Fortunately, there is a relatively easy solution to this problem. You can apply permissions and permission inheritance to the /var/www/html/storage/logs
folder on the server.
To do this, you will need to access the server where Laravel is hosted and execute some commands in the project folder (in this example, we are using /var/www/html/
).
Another important point is that I recommend adding the users who work with Laravel to the www-data group, so that after applying the solution, they can manipulate the files without causing issues.
Here is the script we should use:
cd /var/www/html/
sudo chgrp -R www-data bootstrap/ storage/ storage/logs/
sudo chmod -R g+w bootstrap/ storage/ storage/logs/
cd bootstrap/
sudo find -type d -exec chmod g+s {} +
cd ..
cd storage/
sudo find -type d -exec chmod g+s {} +
cd ..
cd storage/logs/
sudo find -type d -exec chmod g+s {} +
This Bash script is used to grant correct permissions to the bootstrap/
, storage/
, and storage/logs/
directories of a Laravel project located at /var/www/html/
. Specifically, the script sets the group owner of these directories to www-data
, which is the default user and group of the web server on Ubuntu. Additionally, the script ensures that these directories have write permissions for the www-data
group.
The part of the script that refers to permission inheritance is the following line:
sudo find -type d -exec chmod g+s {} +
This line sets the SGID bit on all directories inside the bootstrap/, storage/, and storage/logs/ directories. When the SGID bit is set on a directory, it ensures that all new files created within that directory inherit the group owner of the parent directory. This is useful to ensure that all files created within the bootstrap/, storage/, and storage/logs/ directories have the same group owner (www-data) and can therefore be read and written by the web server.
In summary, the script performs the following actions:
/var/www/html/
.bootstrap/
, storage/
, and storage/logs/
directories to www-data
.www-data
group to the bootstrap/
, storage/
, and storage/logs/
directories.bootstrap/
, storage/
, and storage/logs/ directories to ensure group permission inheritance for new files created within those directories.This way we avoid laravel log errors.
sudo: the sudo command is used to execute commands with superuser (root) privileges in the Linux or Unix operating system.
chgrp: the chgrp command is used to change the group owner of a file or directory.
-R: an option for the chgrp command that means “recursively.” It makes the command execute on all directories and subdirectories below the current directory.
In summary, the sudo chgrp -R command recursively changes the group owner of all files and directories below the current directory.
sudo: same as above
chmod: a command used to change the access permissions of a file or directory.
-R: same as above
g+w: an option for the chmod command that means “add write permission for the group.” “g” represents the group owner of the file or directory.
In summary, the sudo chmod -R g+w command adds write permission for the group owner to all files and directories below the current directory.
sudo: same as above
find: a command used to find files and directories based on specific criteria.
-type d: an option for the find command that means “find only directories.”
-exec: an option for the find command that executes a command on each found file or directory.
chmod g+s: the command to be executed on each directory found by find. “g” represents the group owner of the directory, and “s” is an option that means “set the setgid bit.” This ensures that all files created within the directory inherit the group owner.
{}: a placeholder for the name of the file or directory found by find.
+: indicates that the chmod g+s command should be executed once for each group of files or directories found.
In summary, the sudo find -type d -exec chmod g+s {} + command finds all directories below the current directory and executes the chmod g+s command on each of them, ensuring that all files created within those directories have the same group owner.
In a Laravel environment, file permissions and ownership are crucial for ensuring the smooth operation of your web application. One of the common groups associated with web servers, including those running Apache or Nginx, is the www-data group. Understanding the importance of managing users in this group and implementing best practices can help prevent laravel permission denied-related issues in your Laravel projects.
The www-data group is typically used by the web server to manage permissions for files and directories it needs to access. When users or processes that interact with your web server belong to this group, it ensures that the web server can read and write necessary files, such as configuration files, logs, and uploads.
If not properly managed, users in the www-data group can cause several issues:
chown
command:sudo chown -R www-data:www-data /path/to/your/laravel/project
With the correct permissions applied to the logs folder, Laravel should now be able to write to its log file without errors.
More tips about troubleshooting?
Visit: https://devopsmind.com.br/en/category/troubleshooting-en/