Home > Software design >  Laravel permission issue with caching
Laravel permission issue with caching

Time:09-27

I use Centos 7.

I have an issue regarding OS permission. My all supervisor processes work as root user.

[root@ip-172-31-9-100 example.com]# ll storage/framework/cache/data/
drwxr-xr-x 3 apache apache 16 Sep 22 11:29 00
drwxr-xr-x 5 apache apache 36 Sep 26 10:27 02
drwxr-xr-x 3 apache apache 16 Sep 23 15:14 03
drwxr-xr-x 3 apache apache 16 Sep 22 11:30 04
drwxr-xr-x 3 apache apache 16 Sep 22 12:55 05
drwxr-xr-x 3 root root     16 Sep 22 10:47 06
drwxr-xr-x 3 apache apache 16 Sep 23 16:39 08

My supervisor configuration:

[program:api-horizon]
process_name=%(program_name)s
command=php /var/www/html/example.com/artisan horizon
autostart=true
autorestart=true
user=root
redirect_stderr=true
stopwaitsecs=86400

apache apache is created by laravel project and root root is created by supervisor processes. When laravel project wants to user root root permission cache file, I get an permission error:

[2021-09-23 09:00:05] production.ERROR: Unable to create lockable file: /var/www/html/example.com/storage/framework/cache/data/e9/a0/e9a039230d7835a69038c5a295dc7bfa88213125. Please ensure you have permission to create files in this location. {"userId":605,"exception":"[object] (Exception(code: 0): Unable to create lockable file: /var/www/html/example.com/storage/framework/cache/data/e9/a0/e9a039230d7835a69038c5a295dc7bfa88213125. Please ensure you have permission to create files in this location. at /var/www/html/example.com/vendor/laravel/framework/src/Illuminate/Filesystem/LockableFile.php:73)

Please, help me to fix this permission issue. Thanks

CodePudding user response:

Update your configuration to run as apache user:

[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:

sudo supervisorctl reread && sudo supervisorctl update && sudo supervisorctl restart all

This way, supervisor will run as apache user instead of root. The files it will create will be readable, writable by apache which should fix your problems.

Technically, the role of Supervisor is to ensure a process is always running. You have to configure it the way it would run if you were executing it manually.

In your case, php /var/www/html/example.com/artisan horizon should be executed by the user who own the project, which is apache.

If you don't, then everything Horizon does will be done by root.

Horizon will work - root can do anything - but when your apache user will try to retrieve data or write directories that root created... you guessed it, permission denied.


Don't forget to fix your permissions, because the root owned directories and files will stay and cause errors, even after you changed the configuration.

Execute sudo chown -R apache:apache /var/www/html/example.com/ so everything inside this directory is owner by apache again.

  • Related