Laravel

Fixing Laravel 'storage/logs/laravel.log could not be opened: Permission denied' (Cron, Queue & Docker)


You fixed your storage/ directory permissions yesterday, and your site ran fine. Then, right after midnight—or right after running an artisan command via SSH—your production application crashes with an uncaught UnexpectedValueException:

The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied

If running chmod -R 775 storage temporarily fixes the problem only for it to break again the next day, you do not have a folder permission problem—you have a Linux process ownership conflict.


Why This Error Keeps Coming Back After You Fix It

In a typical Laravel stack, multiple system users write to the exact same log file:

  1. PHP-FPM / Nginx (www-data): Handles incoming HTTP web requests.
  2. CLI / SSH User (ubuntu, forge, or root): Runs manual php artisan commands during deployments.
  3. Cron / Supervisor (root or deploy): Executes scheduled tasks (schedule:run) and background queue workers (queue:work).

If a root cron job or an SSH user triggers the very first error or log entry of the day, Laravel creates a brand-new log file owned by root:root with default 0644 (-rw-r--r--) permissions. Two seconds later, when an HTTP visitor hits your site, PHP-FPM (www-data) attempts to append to that file, gets rejected by the Linux kernel, and throws a 500 error.


Quick Diagnostic Reference

Symptom / Trigger Root Cause Jump To
Breaks immediately after manual SSH artisan commands Running CLI commands as root instead of www-data Step 1
Breaks every night at 00:00 UTC when new daily logs rotate Missing permission mask in config/logging.php Step 2
Crontab schedule:run creates root-owned log files Cron configured in /etc/crontab or root crontab Step 3
New files in storage/logs ignore group permissions Missing Linux setgid (g+s) bit and POSIX ACLs Step 4
Happens inside Docker / Laravel Sail containers Host bind-mount UID (1000) conflicts with container www-data (33) Step 5

Step 1: Inspect Current Log File Ownership & Reset Immediately

First, verify which user locked the log file by listing the contents of storage/logs/ with numeric and user ownership flags:

ls -la /var/www/html/storage/logs/

Look at the output:

-rw-r--r-- 1 root     root      14820 Nov  8 00:01 laravel-2026-11-08.log
-rw-rw-r-- 1 www-data www-data 204912 Nov  7 23:58 laravel-2026-11-07.log

Notice how laravel-2026-11-08.log is owned by root:root with 0644 permissions—meaning www-data has zero write access.

Immediate Recovery Command:

Reclaim ownership for your web server user and grant group write access:

sudo chown -R www-data:www-data /var/www/html/storage/logs
sudo chmod -R 664 /var/www/html/storage/logs/*.log
sudo chmod 775 /var/www/html/storage/logs

Step 2: Set Explicit File Permissions in config/logging.php (The Permanent Fix)

By default, Laravel’s Monolog single and daily drivers create new log files using your system’s default umask (0644), which strips group write permissions (-rw-r--r--). Even if your deployment user and www-data share a group, the second user cannot write to the file.

Open config/logging.php and explicitly set the 'permission' => 0664 key on both your single and daily log channels:

'channels' => [
    'single' => [
        'driver' => 'single',
        'path' => storage_path('logs/laravel.log'),
        'level' => env('LOG_LEVEL', 'debug'),
        'permission' => 0664, // Ensures group write (-rw-rw-r--) on creation
        'replace_placeholders' => true,
    ],

    'daily' => [
        'driver' => 'daily',
        'path' => storage_path('logs/laravel.log'),
        'level' => env('LOG_LEVEL', 'debug'),
        'days' => env('LOG_DAILY_DAYS', 14),
        'permission' => 0664, // Prevents midnight log rotation lockouts
        'replace_placeholders' => true,
    ],
],

Important: Note that 0664 is an octal integer (no quotes around it). Putting '0664' inside quotes will cause PHP’s chmod() function to misinterpret the mode and corrupt the file permissions.

After updating config/logging.php, clear and rebuild your configuration cache:

sudo -u www-data php artisan config:clear
sudo -u www-data php artisan config:cache

Step 3: Stop Running Cron & Supervisor Workers as root

If your scheduled tasks or queue workers run as root, every rotated log file or cached view they generate will belong to root.

1. Fix the Laravel Scheduler Crontab

Never place artisan schedule:run inside sudo crontab -e (the root crontab). Instead, remove it from root and install it under the www-data user crontab:

sudo crontab -u www-data -e

Add your scheduler entry there:

* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1

2. Fix Supervisor Queue Worker Configuration

Open your Supervisor configuration file (e.g., /etc/supervisor/conf.d/laravel-worker.conf) and verify the user directive is set to www-data (not root):

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/worker.log
stopwaitsecs=3600

Reload Supervisor to apply the user change:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart laravel-worker:*

Step 4: Enforce Group Inheritance with setgid (g+s) and Default ACLs

When your deployment user (e.g., ubuntu or deploy) runs php artisan via SSH or CI/CD pipelines, you want any newly created log file to automatically inherit the www-data group and retain read/write permissions for both users.

Run these four commands on your production server:

# 1. Add your SSH/deploy user to the www-data group
sudo usermod -aG www-data $USER

# 2. Set ownership of storage and bootstrap/cache
sudo chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

# 3. Enable the setgid bit (2775) so new files always inherit the 'www-data' group
sudo chmod -R 2775 /var/www/html/storage /var/www/html/bootstrap/cache

# 4. Set default POSIX ACLs so future files always grant rwx to www-data
sudo setfacl -R -d -m g:www-data:rwx /var/www/html/storage/logs
sudo setfacl -R -m g:www-data:rwx /var/www/html/storage/logs

(If setfacl is not installed on Ubuntu/Debian, install it via sudo apt-get install acl.)


Step 5: Fix Docker Container UID/GID Mismatches

In Docker environments, the container’s www-data user typically has UID 33 (Debian/Ubuntu base) or 82 (Alpine base), while your host Linux/macOS user has UID 1000. When bind-mounting ./storage:/var/www/html/storage, the container process is denied write access to files created by your host IDE or terminal.

Match the container’s www-data UID to your host user inside your Dockerfile:

FROM php:8.3-fpm

ARG UID=1000
ARG GID=1000

# Remap www-data inside the container to match host UID/GID 1000
RUN usermod -u ${UID} www-data && groupmod -g ${GID} www-data

WORKDIR /var/www/html

Or, when running artisan commands in a running container, always execute as www-data instead of the default container root user:

docker compose exec -u www-data app php artisan cache:clear

Prevention Checklist

  • Set 'permission' => 0664 (octal, unquoted) in config/logging.php.
  • Switch to the daily log channel (LOG_CHANNEL=daily in .env) so single log files don’t grow into multi-gigabyte locks.
  • Alias artisan on your production SSH profile (alias art="sudo -u www-data php artisan") so you never accidentally run CLI commands as root.