Reputation: 1458
I'm stuck in a Laravel project, I've been using laravel a lot of years and never happened this.
I'm using Vagrant (as always) and only happens this with PHP 8, with other projects with php 7.X doesn't happen.
Every page shows this error:
Unable to create lockable file: /var/www/html/storage/framework/cache/....
Please ensure you have permission to create files in this location.
The USER and GROUP permisions are OK as always. I Ran chmod -R 777 storage
and bootstrap/cache a lot of times.
I ran all cache:clear.
I don't know why it's happening this.
Anyone had problems with Laravel and PHP 8?
Thanks!
Upvotes: 23
Views: 31125
Reputation: 1290
Try running the following commands, as the directories should have the same group and owner as the web server runs under. For Debian systems this is usually www-data
; on others it may be apache
or httpd
.
Also, see here for further reference.
sudo chown -R www-data storage bootstrap/cache
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
Upvotes: 16
Reputation: 1
I was also experiencing the same issue. What happens is that the system doesn't recognize the user www-data, and it creates a root file because it can't find this user. It is recommended to replace www-data with another user, such as apache, for example.
[program:api-horizon]
process_name=%(program_name)s
command=php /var/www/html/example.com/artisan horizon
autostart=true
autorestart=true
user=apache //instead of root
redirect_stderr=true
stopwaitsecs=86400
Then restart supervisor
Upvotes: -1
Reputation: 287
We had the same problem with the Laravel cache. I explain how we could address the problem using a few steps. The following steps we took:
First:
We checked the permission of the files placed in the Laravel cache directory at the path /var/www/html/storage/framework/cache/data/
. When you ll
, the following output is visible at first look:
drwxr-xr-x 3 www-data www-data 4096 May 14 10:32 03
drwxr-xr-x 3 www-data www-data 4096 May 14 10:04 07
drwxr-xr-x 3 root root 4096 May 14 10:31 24
drwxr-xr-x 3 www-data www-data 4096 May 14 10:21 27
drwxr-xr-x 3 www-data www-data 4096 May 14 10:21 31
drwxrwxr-x 3 www-data www-data 4096 May 14 09:47 44
drwxr-xr-x 3 root root 4096 May 14 10:30 48
As you can see above, a few files are owned by root
. This is the root of the problem. When you configure a Laravel project, it is supposed that the www-data
user creates all files. We must find the cause of files created by the root user.
Second:
We could find out that operating systems were executing a few commands and jobs under the root user. So, we needed a solution to modify the schedule table of the cron job manager and execute the jobs with the www-data
user. We first opened the cron table with the crontab -e
command to do this. Then, we modified the cron table file as follows:
* * * * * root su -c "php /var/www/html/artisan schedule:run >> /var/log/cron.log" -s /bin/bash www-data
By doing the abovementioned, the cron jobs are executed by a bash belonging to the www-data
user.
Third:
We had a few files created by the root
user in our project. To ensure that no file is owned by it, we highly recommend running the following command before purging the Laravel cache at the project's root folder (e.g., html).
chown -R www-data:www-data html
It recursively changes the ownership of all files and grants it to the www-data
user.
Fourth:
After doing all the first three steps, we needed to clear the Laravel cache. So, the cache files are purged and reconstructed. One may purge the Laravel cache with the php artisan cache:clear
command. After doing these steps, the output would be something like the following with no file owned by root
.
drwxrwxr-x 52 www-data www-data 4096 May 14 13:46 ./
drwxrwxr-x 3 www-data www-data 4096 Nov 15 2021 ../
drwxr-xr-x 3 www-data www-data 4096 May 14 13:03 01/
drwxr-xr-x 3 www-data www-data 4096 May 14 12:53 0e/
drwxr-xr-x 3 www-data www-data 4096 May 14 12:32 10/
drwxr-xr-x 3 www-data www-data 4096 May 14 12:52 25/
drwxr-xr-x 3 www-data www-data 4096 May 14 12:32 39/
drwxr-xr-x 3 www-data www-data 4096 May 14 12:52 3a/
drwxr-xr-x 3 www-data www-data 4096 May 14 12:12 3f/
drwxr-xr-x 3 www-data www-data 4096 May 14 13:46 42/
drwxr-xr-x 3 www-data www-data 4096 May 14 11:59 44/
drwxr-xr-x 4 www-data www-data 4096 May 14 12:52 4d/
Upvotes: 4
Reputation: 2096
Please use the following commands:
cd /home/vagrant/code/project_folder_name
sudo php artisan cache:clear
sudo chmod 775 /home/vagrant/code/project_folder_name
sudo chown -R vagrant:vagrant /home/vagrant/code/project_folder_name
Hopefully, it will solve your issue.
Upvotes: 6