How to fix Error: laravel.log could not be opened? - php

I'm pretty new at laravel, in fact and I'm trying to create my very first project. for some reason I keep getting this error (I haven't even started coding yet)
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
I've read this has something to do with permissions but chmod -R 775 storage didn't help at all.

Never set a directory to 777. you should change directory ownership. so set your current user that you are logged in with as owner and the webserver user (www-data, apache, ...) as the group.
You can try this:
sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
then to set directory permission try this:
chmod -R 775 storage
chmod -R 775 bootstrap/cache
Update:
Webserver user and group depend on your webserver and your OS. to figure out what's your web server user and group use the following commands. for nginx use:
ps aux|grep nginx|grep -v grep
for apache use:
ps aux | egrep '(apache|httpd)'

Never use 777 for directories on your live server, but on your own machine, sometimes we need to do more than 775, because
chmod -R 775 storage
Means
7 - Owner can write
7 - Group can write
5 - Others cannot write!
If your webserver is not running as Vagrant, it will not be able to write to it, so you have 2 options:
chmod -R 777 storage
or change the group to your webserver user, supposing it's www-data:
chown -R vagrant:www-data storage

To fix this issue, you need to change the ownership of the directory to the unix user that the webserver uses.
Get out of the VM
Using the console, go to your synced folder (vagrant)
sudo chown -R $USER:www-data storage
chmod -R 775 storage
Even though I created the project within the VM using the VM user, the folder belonged to the user in the real computer; so, when trying to
Now it's working.
Thanks to all those that helped me figure this thing out
EDIT:
Actually, it still wasn't working, it still gave me a "permission denied" problem.
Here's what I did, I modified my Vagrantfile like this:
config.vm.synced_folder "./app","/var/www/", create:true,
:owner => "vagrant",
:group => "www-data",
:mount_options => ["dmode=775","fmode=664"]

It also may be SELinux. (Centos, RedHat)
Determine status of SElinux on terminal:
$ sestatus
If status is enabled, write command to disable SElinux
$ setenforce Permissive
Or you may execute this command
$ sudo setenforce 0

You need to adjust the permissions of storage and bootstrap/cache.
cd into your Laravel project.
sudo chmod -R 755 storage
sudo chmod -R 755 bootstrap/cache
You can try 777 if 755 doesn't work. 777 is not secure though!
Depending on how your web server is setup, you may be able to be more specific with your permissions, and only grant them to your web server user. Google WEB SERVER NAME Laravel file permissions for more information.
At the time of writing, this is for Laravel 5.4

It might be late but may help someone, changing directory permissions worked for me.
Assuming that your Laravel project is in /var/www/html/ directory. Goto this directory.
cd /var/www/html/
Then change permissions of storage/ and bootstrap/cache/ directories.
sudo chmod -R 775 storage/
sudo chmod -R 775 bootstrap/cache/
If permission 775 does not work, try setting 777. (Warning! This is the most relaxed permission, use with care).
sudo chmod -R 777 storage/
sudo chmod -R 777 bootstrap/cache/
cPanel: If you are on cPanel and don't have terminal available you can change permission by right clicking on the mentioned directory and it's sub-directories.

Add to composer.json
"scripts": {
"post-install-cmd": [
"chgrp -R www-data storage bootstrap/cache",
"chmod -R ug+rwx storage bootstrap/cache"
]
}
After composer install

Run following commands and you can add sudo at starting of command depends on your system:
chmod -R 775 storage/framework
chmod -R 775 storage/logs
chmod -R 775 bootstrap/cache

1- ‍The nginx user and php-fpm user and app owner-user must be the same:
run command sudo vi /etc/nginx/nginx.conf change like bellow:
user nginx nginx;
run command sudo vi /etc/php-fpm.d/www.conf change like bellow:
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx
then restart nginx and php-fpm service
run below command
sudo chown nginx:nginx -R "your_project_path"
2- change file SELinux security context by run the following commands in the project path
chcon -R -t httpd_sys_content_t .
chcon -R -t httpd_sys_rw_content_t .

For all Centos 7 users on a Laravel context, there is no need to disable Selinux, just run these commands:
yum install policycoreutils-python -y # might not be necessary, try the below first
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/storage(/.*)?" # add a new httpd read write content to sellinux for the specific folder, -m for modify
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/bootstrap/cache(/.*)?" # same as the above for b/cache
restorecon -Rv /var/www/html/ # this command is very important to, it's like a restart to apply the new rules
Lastly, make sure your hosts, ips and virtual hosts are all correctly for remote accessing.
Selinux is intended to restrict access even to root users, so only the necessary stuff might be accessed, at least on a generalist overview, it's extra security, disabling it is not a good practise, there are many links to learn Selinux, but for this case it is not even required.

If you use cmd
sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
If you use GUI
First go to the project and right click on the storage and check the properties and go to the Permissions tab
Change the permissions using below code
sudo chmod -R 777 storage
Then your file properties may be
Then check your settings and execute laravel command it will work :)

I stuck on this issue tried different commands but these will help to solve the problem
php artisan route:clear
php artisan config:clear
php artisan cache:clear
Hope it's helped others too.

Just run the following command from Project root Directory -
sudo chmod -R 775 storage
sudo chown -R $USER:www-data storage

In Laravel, you should set ACL on storage and cache directory so that web server user can read/write on the directory. Open a new terminal and run following:
HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1)
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/
References:
https://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-setfacl-linux-bsd
https://linux.die.net/man/1/setfacl

Maximum people's are suggesting to change file permission 777 or 775, which I believe not an appropriate approach to solve this problem. You just need to change the ownership of storage and bootstrap folder.
In below Image you can see all my files/folder are under the root user(except storage and bootstrap, because I changed the ownership ),but I logged in as a administrator(before changing ownership) that's why it always giving permission denied. So I need to change the ownership of this two folder to administrator
So how I did this,
go to your project directory and run below commands.
sudo chown -R yourusername:www-data storage,
sudo chmod -R ug+w storage,
sudo chown -R yourusername:www-data bootstrap,
sudo chmod -R ug+w bootstrap

try this
cd /var/www/html
setenforce 0
service httpd restart

This is what I do if I'm running Apache:
sudo chown -R $USER:www-data my_laravel_project/
sudo chmod -R 775 my_laravel_project/storage
sudo chmod -R 775 my_laravel_project/bootstrap/cache
cd my_laravel_project
php artisan optimize:clear

This solution is specific for laravel 5.5
You have to change permissions to a few folders:
chmod -R -777 storage/logs
chmod -R -777 storage/framework
for the above folders 775 or 765 did not work for my project
chmod -R 775 bootstrap/cache
Also the ownership of the project folder should be as follows (current user):(web server user)

I managed to fix it as I was only granting permission to via this command:
Copy Code
sudo chmod -R 775 storage
The fix was to add this:
Copy Code
sudo chmod -R ugo+rw storage

I wasn't too keen on changing my folder permissions to 777. Here's how I went about fixing this issue.
First, I changed the user who is running the web server on my local machine(I run nginx, but the principles apply everywhere):
$> sudo vim /etc/nginx/nginx.conf
user <my_user> #inside nginx.conf
service nginx reload
Afterwards, I created another index.php file under the public/ folder to find out who was running my php-fpm version and where I would go about changing that:
<?php
phpinfo();
?>
Reloading the page, I found out that www-data was the user(under the environment section). I also found out I was running php 7.1. I proceeded to change the user:
$> sudo vim /etc/php/7.0/fpm/pool.d/www.conf
#Look for www-data or the following variables: user, group, listen.user, listen.group.
Finally, I gave the following permissions to folders:
sudo chmod -R 775 ./storage/
Now, I made sure that I was the owner of the folders by using a simple:
ls -al
If you set the server and php-fpm users to yourself and the folders are owned by root for example, then you will keep encountering this issue. This can happen if you did a sudo laravel new <project> as root. In that case, make sure you use a recursive chown command on your project to change the user:group settings. In most default cases, www-data is the main setting for the server and php, in that case it's a matter of making sure the folder isn't out of www-data's reach.
My project is setup in my home directory. On Ubuntu 16.04 and Laravel 5.5.

In my particular case I had a config file generated and cached into the bootstrap/cache/ directory so my steps where:
Remove all generated cached files: rm bootstrap/cache/*.php
Create a new laravel.log file and apply the update of the permissions on the file using:
chmod -R 775 storage

Tried anything suggested here without success.
What worked for me was:
sudo chmod -R ugo+rw storage
sudo chmod -R ugo+rw storage/logs

In Linux
sudo chown -R www-data:root /var/www/name-project-Laravel
sudo chmod 755 /var/www/name-project-Laravel/storage

In Centos & Rockylinnux
chown root:nginx FOLDER_PROJECT -Rf
chmod 775 FOLDER_PROJECT -Rf
cd FOLDER_PROJECT
chmod 777 storage -Rf
setenforce 0
please rate :)

Mac OS solution
I fixed this problem simply by giving the necessary permission to my folders.
Right Click on the logs folder and click on Get Info
At the bottom, you will see Sharing & Permissions. Now give Read & Write access to the folder.
Follow the attachment.
Next step👇🏻

as you probably already know this issue is caused due to absence of write permission on the log folder which is a sub folder of storage.
To solve this problem walkedthrough these sequence of steps
Update composer
sudo composer self-update
Change storage folder write permission
sudo chmod -R ugo+rw storage
Now storage folder should have permission drwxrwxrwx
To check permissions run the following command from project root
ls -l
Also if you face the following error after the step above
ErrorException chdir(): No such file or directory (errno 2)
Just create a folder named public on the project root folder using
sudo mkdir public
ps. For more information about the chmod commands check this

below command would work for sure.
sudo chmod -R ugo+rw storage

Not write any command or not gives any permission simplest way to solved this issue
just restart your system and try it again
it's work for me

Related

Laravel permissions on nginx

I am deploying a project to Ubuntu on Digital Ocean, using these
instructions.
I am running into permissions issues whereby if I give these permissions
sudo chown -R www-data.www-data /var/www/project/storage sudo chown -R www-data.www-data /var/www/project/bootstrap/cache
I get access to my login page (though a 500 error on every other route), but I also get The stream or file "/var/www/project/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied whenever I try to run php artisan optimize, route:clear, etc.
If I set the permissions to this
sudo chown -R $USER /var/www/project/storage sudo chown -R $USER /var/www/project/bootstrap/cache
I get the opposite problem, wherein I can run artisan commands and write to the log, but no pages are accessible.
Any help would be appreciated
In addition to chmod -R www-data.www-data /var/www/project which sets the group and owner, you might also need to change the specific dir permissions.
sudo chmod -R 775 /var/www/project/storage/
sudo chmod -R 775 /var/www/project/bootstrap/cache
If you're trying to perform operations in your /var/www/project directory on the server, ensure that the user you are logged onto the server as is in the www-data group.
You can check the groups you're in simply by typing groups in your shell. If you don't see www-data in the list returned, add yourself.
usermod -aG www-data $USER
Once you've added yourself to the www-data group you will need to relog for the change to take effect.
You need to change boostrap cache folder and laravel.log file into read, write, and execute. Go to your laravel project, and put this code
sudo chmod 777 bootstrap/cache/
sudo chmod 777 storage/logs/*.log

Laravel Forbidden You don't have permission to access /storage/ on this server

recently, i deploy my laravel project to Centos 7 VPS
and i have images that saved in storage file
when i'm trying to access the images using URL i got this
Forbidden
You don't have permission to access /storage/companies/1597187294.jpg on this server.
i think it something related to permissions cause everything works fine in localhost
So, any ideas to fix this ??
Try to run below command
sudo chmod -R 775 /storage
or
run these blow commands step by step
sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
chmod -R 775 storage
chmod -R 775 bootstrap/cache
chmod -R 775 public

Is it possible to set the folder permissions on my local developement environment(WAMP) before uploading the files on the webserver?

Is it possible that I set the folder permission for the storage folder to 0775 on my local development environment which is WAMP before uploading the project on the webserver?
Edit: I want to set the permissions on the folder before uploading and the permission should be retained after the upload.
Go project directory
$ sudo chmod 777 -R bootstrap
$ sudo chmod 777 -R storage
$ sudo chmod 777 -R vendor
then
$ sudo chown -R www-data:www-data bootstrap
$ sudo chown -R www-data:www-data storage
$ sudo chown -R www-data:www-data vendor
If you use other web server. You need change www-data to your correct user.
I think you expecting to retain the folder permission after upload. My suggestion would be to do an folder existance check in code them if not exists create and set permission for code as an dded precausion.
if(!is_dir('upload')){
mkdir('upload');
chmod('upload',0755);
}

Wordpress plugin install: Could not create directory

I'm using WordPress on centos 6.
I try to install a plugin. But I got this error:
Installing Plugin: bbPress 2.5.9
Downloading install package from https://downloads.wordpress.org/plugin/bbpress.2.5.9.zip…
Unpacking the package…
Could not create directory.
How can I resolve this?
P/S: I run this command:
sudo -u root touch /var/www/html/wordpress/wp-content/plugins/test.txt
and it works. But I still get that error.
You only need to change the access permissions for your WordPress Directory:
chown -R www-data:www-data your-wordpress-directory
You can fix this by using the following commands. You should first be in the root folder of Wordpress.
sudo chown -R www-data:www-data wp-content/plugins/
sudo chmod 775 wp-content
sudo chown -R www-data:www-data wp-content/
The user that is running your web server does not have permissions to write to the directory that Wordpress is intending to create the plugin directory in. You should chown the directory in question to the user that is running Wordpress. It is most likely not root.
In short, this is a permissions issue. Your touch command is working because you're using it as root, and root has global permissions to write wherever it wants.
A quick solution would be to change the permissions of the following:
/var/www/html/wordpress/wp-content
/var/www/html/wordpress/wp-content/plugins
Change it to 775.
After installation, don't forget to change it back to the default permissions.. :D
I had to give ownership of /plugins and /upgrade to the server, nothing else.
$ cd /var/www/wordpress/wp-content
$ sudo chown www-data:www-data /plugings
$ sudo chown www-data:www-data /upgrade
Running Apache server on Ubuntu 18.04. Maybe more dirs will need to be changed later. Anyways, I plan to restore permissions once I finish editing, as suggested in this anwser.
If you have installed wordpress using apt, the config files are split in multiple directories. In that case you need to run:
sudo chown -R -h www-data:www-data /var/lib/wordpress/wp-content/
sudo chown -R -h www-data:www-data /usr/share/wordpress/wp-content/
The -h switch changes the permissions for symlinks as well, otherwise they are not removable by user www-data
To solve permission issue on plugins and themes on localhost or production quickly, you just run this
sudo chmod 757 wp-content/themes
sudo chmod 757 wp-content/plugins
if take care permission on production, you can run
sudo chown -R www-data:www-data wp-content/themes
sudo chown -R www-data:www-data wp-content/plugins
If you are mac user , using XAMP
Go to the htdocs folder and open the terminal on the folder , as shown in the screenshot
Then Type the following command on the Terminal
**sudo chmod -R 777 <your wordpress folder Name>/**
E.g sudo chmod -R 777 wordpress/
CentOS7 or Ubuntu 16
1.
WordPress uses ftp to install themes and plugins.
So the ftpd should have been configured to create-directory
vim /etc/pure-ftpd.confg
and if it is no then should be yes
# Are anonymous users allowed to create new directories?
AnonymousCanCreateDirs yes
lastly
sudo systemctl restart pure-ftpd
2.
Maybe there is an ownership issue with the parent directories.
Find the Web Server user name and group name if it is Apache Web Server
apachectl -S
it will print
...
...
User: name="apache" id=997
Group: name="apache" id=1000
on Ubuntu it is
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
then
sudo chown -R apache:apache directory-name
3.
Sometimes it is because of directories permissions.
So try
sudo chmod -R 755 directory-name
in some cases 755 does not work. (It should & I do not no why) so try
sudo chmod -R 777 directory-name
4.
Maybe it is because of php safe mode.
So turn it off in the root of your domain
vim php.ini
then add
safe_mode = Off
NOTE:
For not entering FTP username and password each time installing a theme we can configure WordPress to use it directly by adding
define('FS_METHOD','direct');
to the wp-config.php file.
If anyone using shared hosting and get same problem it may be disk space issue. In that case contact with your hosting support and ask them to increase disk space of your acocunt.
You need to change the permission of the directory
At first change the user HTML folder (replace yourcomputerusername)
sudo chown yourcomputerusername:yourcomputerusername /var/www/html
Next change the permission for the user
cd /var/www/html
sudo chmod -R goa=rwx wordpress
or
sudo chmod -R 777 wordpress/wp-content
I was on XAMPP for linux localhost and this worked for me:
sudo chown -R my-linux-username wp-content
What I end up doing is every time I create a WordPress project. in /www/html
I run below command
sudo chown www-data:www-data wordpress_folder_name -R
hope this will help someone.
For me the problem was FTP server that WP is using to upload update. It had writting disabled in configuration, so just enabling it fixed the problem.
Shame on WordPress for providing such misleading error message.
You could try
sudo chmod goa=rwx -R /var/www/html
None of the above work for me except this one.
sudo chown daemon:daemon /opt/bitnami/apache/htdocs/
Don't forget to change /opt/bitnami/apache/htdocs/ to your directory
Webserver user must have write access to directories to perform such operations, so you can try to change owner of files to webserver user (apache in this example, but can be differ from yours)
chown -R apache YOUR_BLOG_DIRECTORY
If you are using some app that wraps http, you have to set these user in the command.
sudo chown -R [desireduser]:[desireduser] wp-content/
By example, if you are using lampp, the users that init httpd is "daemon" and the command that works will be:
sudo chown -R [desireduser]:[desireduser] wp-content/
You can search in your httpd.conf file
To solve permission issue on ubuntu server, you just run this
sudo chmod 777 -R 'wordpress wp-content file location'
for example.
sudo chmod 777 -R /usr/share/wordpress/wp-content
wordpressProject is the project name.
/var/www/html/wordpressProject sudo chmod -R 777 wp-content
Thanks. It will work.
Absolutely it must be work!
Use this
chown -Rf www-data:www-data /var/www/html

RuntimeException: Unable to create the cache directory (/var/www/sonata/app/cache/dev)

I installed the Sonata admin bundle.
After installation i refresh my page there is the cache problem then i use the following command to remove the cache:
rm -rf app/cache app/log
Then I recreate the directory:
mkdir app/cache app/log
But I got the following error:
Runtime Exception : Unable to create the cache directory (/var/www/sonata/app/cache/dev).
It looks like a file/directory permission problem. The directory has to be writeable by the webserver. After creating the directory you should adjust the permissions with
chown -R www-data:www-data app/cache
chown -R www-data:www-data app/log
Or for Symfony 4+:
chown -R www-data:www-data var
This only works on linux systems. The user and group depends on your distribution. On Debian and Ubuntu this should be www-data, on CentOS it's afaik apache.
Another solution would be not to delete the whole folders but only their contents via
$ rm -rf app/log/* app/cache/*
But please be careful with this command.
This solution is correct : https://stackoverflow.com/a/20128013/2400373
But is necessary change the 2 commands in Symfony3:
First you should is inside the folder of project:
$ sudo chown -R www-data:www-data var/cache
$ sudo chown -R www-data:www-data var/logs
After delete the cache:
$ sudo rm -rf var/cache/*
$ sudo rm -rf var/logs/*
Regards
I solved it changing user and group of folder var/cache and var/logs, then I cleaned cache:
sudo chown -R www-data:www-data var/logs
sudo chown -R www-data:www-data var/cache
sudo rm -rf var/logs/* var/cache/*
And for centos:
chown -R apache:apache app/cache
if you're coming here for Symfony Help you might have to do this as well if you delete the entire app/logs folder
chown -R apache:apache app/logs
I found a similar problem when I was using the PHP symfony as the error picture.
I found it after running command
php bin/console cache:clear
I solved it by removed everything inside the folder /app/var/cache
Changing the CHMOD might help but in case the cache annoys you during the deveopment you can just deactivate it.
Navigate to your app config file (located in ../app/config/config.yml from your root directory). Scroll to the twig configuration settings (under twig:) and change the cache value (which should be pointing to the cache directory) to false like so:
twig:
cache: false
If you do not see any cache configuration entry, simply add the line above.
It is mostly the permission issue.
I have got resolved it on MAC using command: $ sudo chmod -R 777 <path/to/cache/directory>
You might try: $ sudo chmod -R 777 /var/www/sonata/app/cache
What is likely happening is that you are trying to create the file under apache/nginx. By default apache or nginx has umask set to 0022.
From: http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html
Explain Octal umask Mode 022 And 002
As I said earlier, if the default settings are not changed, files are created with the access mode 666 and directories with 777. In this example:
The default umask 002 used for normal user. With this mask default directory permissions are 775 and default file permissions are 664.
The default umask for the root user is 022 result into default directory permissions are 755 and default file permissions are 644.
For directories, the base permissions are (rwxrwxrwx) 0777 and for files they are 0666 (rw-rw-rw).
You will need to manually set the umask to 0002, and reset it back to its previous setting before you can create the directories.
On a mac computer it will be:
$ sudo chown -R _www:_www var/cache
$ sudo chown -R _www:_www var/logs
If none of above work to you, call a "phpinfo()" on your php file and find for "User/Group" value. That's the user group to give permission to.
Also check the path. In my case, I had
return dirname(__DIR__) . '../../../../var/cache/
instead of
return dirname(__DIR__) . '/../../../../var/cache/ (missing /)
I had this problem with phpmyadmin 5.1.0
The solution was to specify the full path of the tmp directory, instead of relative path.
On the configuration file config.inc.php I had:
$cfg['TempDir'] = 'tmp'
I changed it to:
$cfg['TempDir'] = '/usr/share/phpmyadmin/tmp'
Seems that the problem is something related to "matching" with some allowed paths.
Maybe this solution can be extrapolated to other software that uses twig.
to generalize so everyone can use this two commands can solve everyone problem.
sudo chown -R $USER var/cache
sudo chown -R $USER var/logs
Further Info: $USER automatically replaces your username so it can be used by anyone.
Just delete log and cache directories, then recreate them.

Categories