Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Laravel: The stream or file laravel.log could not be opened in append mode: Permission denied

The stream or file "storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied means the PHP process cannot write Laravel's log file: storage is owned by another user, the file was created by root, or the code runs on a read-only filesystem. Make storage and bootstrap/cache writable by the PHP user, or in a container log to stderr with LOG_CHANNEL=stderr. The bootstrap/cache directory must be present and writable is the same problem one folder over.

By Livops team, the people who build and run LivopsUpdated

The error, as a stack trace or a white page shows it (paths vary):

The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: SQLSTATE[HY000] [2002] Connection refused

and its sibling, from composer install or the first request:

The /var/www/html/bootstrap/cache directory must be present and writable.

Both say the PHP process cannot write where Laravel expects to. This page explains which folders Laravel writes to, the causes in the order they turn up on a server and in a container, and the fix for each: the right owner, a log to stderr, or a configuration cached somewhere else.

What does the error mean?

Laravel logs through Monolog. The default channel is stack, which by default holds the single channel, a file at storage/logs/laravel.log[6]. Monolog’s StreamHandler creates the folder if it is missing and opens the file for appending; when PHP cannot open it, it throws The stream or file "…" could not be opened in append mode: followed by PHP’s own reason, with the fopen(…) prefix removed[1]. So Permission denied is the operating system refusing the write. When even the folder cannot be created, the message is a different one: There is no existing directory at "…" and it could not be created[1].

Note the second line. Monolog adds The exception occurred while attempting to log: and the message it was trying to write[2]. That message, here a refused database connection, is the error you were meant to see; the permission problem only hid it.

The second error comes from Laravel itself. It keeps a list of your packages in bootstrap/cache/packages.php, and before writing it checks the folder: if it is not writable, it throws The {dir} directory must be present and writable.[3][8]. The list is built whenever the app boots and the file is missing[3], and by php artisan package:discover, which the skeleton’s composer.json runs after every composer install[10].

Laravel’s deployment docs state the rule behind both: Laravel needs to write to bootstrap/cache and storage, so the web server process owner must be able to write to them[4]. Under storage it keeps the logs, sessions, file cache and compiled views[11][12].

Causes, most common first

1. storage belongs to another user

You cloned or copied the project as your own user, or as root, and PHP runs as another one. Under PHP-FPM, that user is the pool’s user directive, which is mandatory[13]. The folders are readable, so pages render, but the first log line fails.

Fix: give the two folders to that user, and keep write access to the owner and its group. With www-data standing for whatever your pool’s user says:

sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R ug+rwX storage bootstrap/cache

If you deploy as your own user, add yourself to that group instead of loosening the mode. chmod -R 777 also makes the error go away, by letting every user on the machine write your sessions and cache.

2. The log file was created by root

The folders are fine, but laravel.log itself belongs to root: a cron job, a queue worker or an artisan command run with sudo logged first and created it. The single, daily and monthly channels create the file with permission 0644 by default[5], so only its owner can write. The web user can then not append, and the error appears only after that command has run. The daily channel makes a new file every day[5], so the error can come and go by date.

Fix: remove or chown the file, and run every Laravel command as the same user as the web server:

sudo -u www-data php artisan schedule:run

If two users really must write the log, give them a shared group and set the channel’s permission option[5], for example 0664, in config/logging.php.

3. A container or platform with a read-only filesystem

In an image built as root and run as another user, or on a platform where the code is mounted read-only, storage/logs cannot be written at all, whatever its mode. Logging to a file is the wrong tool there.

Fix: log to stderr. The skeleton’s config/logging.php has a stderr channel, a Monolog StreamHandler on php://stderr[6], and the default channel comes from LOG_CHANNEL[6]:

LOG_CHANNEL=stderr

The lines then go to the container’s or the platform’s log. Under PHP-FPM, set catch_workers_output = yes in the pool, or the workers’ stdout and stderr are discarded[13].

Logs are only the first thing Laravel writes. For the rest of storage, either keep it off the disk (sessions and cache in the database or Redis), point it elsewhere, or both. VIEW_COMPILED_PATH moves the compiled Blade views[12], and LARAVEL_STORAGE_PATH moves the whole storage folder[8]; the new folder needs the same subfolders as the skeleton’s storage[11]. The files in bootstrap/cache can be moved one by one with APP_PACKAGES_CACHE, APP_SERVICES_CACHE, APP_CONFIG_CACHE, APP_ROUTES_CACHE and APP_EVENTS_CACHE[8].

4. The folder is missing

storage/logs or bootstrap/cache was never deployed: an archive or a .gitignore left out the empty folders. The skeleton keeps each one in Git with a .gitignore file inside[11]. Monolog tries to create a missing log folder and says There is no existing directory at "…" and it could not be created when it cannot[1]; for bootstrap/cache you get the must be present and writable error above[3].

Fix: restore the folders with their .gitignore files, or create them on deploy:

mkdir -p storage/logs storage/framework/{cache/data,sessions,views} bootstrap/cache

5. The path in the error is from another machine

/Users/you/shop/storage/logs/laravel.log on a Linux server means the configuration was cached elsewhere. config:cache combines all configuration into one file[7], written as the already computed values[9] into bootstrap/cache/config.php[8], so the log path that storage_path() gave on that machine stays in it. Clear it, and cache it again where the app runs[7]:

php artisan config:clear

Laravel’s docs say not to run config:cache during local development at all[7].

How to confirm the fix

Append one line as the user PHP runs as, the way Monolog does, then load a page that logs:

sudo -u www-data sh -c 'echo write test >> storage/logs/laravel.log'
sudo -u www-data test -w bootstrap/cache && echo bootstrap/cache is writable

If the first command says Permission denied, check the owner of the file and of every folder above it with ls -ld storage storage/logs storage/logs/laravel.log. In a container with LOG_CHANNEL=stderr, look for the line in the container’s log instead.

On Livops

  • Logs go to the Logs tab by default. The start command Livops writes for a Laravel app begins with LOG_CHANNEL=${LOG_CHANNEL:-stderr}, so unless you set LOG_CHANNEL yourself, Laravel logs to stderr and the lines show on the service’s Logs tab. Leave it that way: while an app runs, its code is read-only, so a log file inside the project is not the place for it.
  • The worker and the scheduler need it too. That default belongs to the web app’s start command only. A php artisan queue:work worker or a schedule:run cron service logs with Laravel’s own default, the stack channel and its laravel.log file[6]. Set LOG_CHANNEL=stderr once as a shared variable of the environment, and it reaches every service. New values take effect on the next deploy; to apply them now, restart the service.
  • The config is cached where it runs. The start command runs php artisan optimize as the app starts, with the project’s variables in place, so the cached configuration has your values and this server’s paths, not the build’s.
  • Files you need to keep go to /app/data, the project’s own disk; its path is in the DATA_DIR variable. Files written next to the code are gone after the next deploy, and /tmp is wiped on every restart. For customers’ data, use a database.

The full Laravel setup on Livops, with the start command, the worker and the scheduler: Laravel hosting. Where an app may write: Files and data. The error that often follows a fresh deploy: No application encryption key has been specified.

Questions

What does could not be opened in append mode mean?

Laravel writes its log through Monolog, which opens the log file to add lines at the end. When PHP cannot open it, Monolog throws this error and keeps PHP's own reason after the colon, here Permission denied: the operating system refused the write.

Should I just chmod -R 777 storage?

No. It makes the error go away by letting every user on the machine write your logs, sessions and cache. Give the folders to the user PHP runs as, and keep write access to that user and its group.

Why does it work for a while and then fail?

Usually because a command ran as root, a cron job or an artisan command, and created the log file first. A log file is created with mode 0644 by default, so only its owner can write it, and the web server's user cannot append to it afterwards. With the daily channel a new file is made every day, so whoever logs first that day owns it.

What is the error about bootstrap/cache being present and writable?

Laravel writes a list of your packages to bootstrap/cache/packages.php. When the folder is missing or not writable, it stops with The bootstrap/cache directory must be present and writable, often during composer install, which runs php artisan package:discover. The fix is the same as for storage: create the folder and give it to the right user.

How do I log in Docker or on a platform with a read-only filesystem?

Set LOG_CHANNEL=stderr. The skeleton's stderr channel writes to php://stderr, so the lines reach the container's log and no file is needed. Under PHP-FPM, also set catch_workers_output = yes in the pool, or worker output is discarded.

The path in the error is from my laptop. Why?

The configuration was cached on another machine. config:cache writes the computed values, the full log path included, into bootstrap/cache/config.php. Run php artisan config:clear and cache the configuration again where the app runs.

Where do Laravel logs go on Livops?

To the service's Logs tab. The start command Livops writes for Laravel sets LOG_CHANNEL to stderr unless you set another value. For a queue worker or a scheduler, set LOG_CHANNEL=stderr as a shared variable, so their logs show in their Logs too.

Sources

  1. Monolog source — Handler/StreamHandler.php (github.com). Checked .
  2. Monolog source — Utils.php (github.com). Checked .
  3. Laravel source — Foundation/PackageManifest.php (13.x) (github.com). Checked .
  4. Laravel docs — Deployment (13.x, docs source) (github.com). Checked .
  5. Laravel docs — Logging (13.x, docs source) (github.com). Checked .
  6. Laravel application skeleton — config/logging.php (13.x) (github.com). Checked .
  7. Laravel docs — Configuration, configuration caching (13.x, docs source) (github.com). Checked .
  8. Laravel source — Foundation/Application.php (13.x) (github.com). Checked .
  9. Laravel source — Foundation/Console/ConfigCacheCommand.php (13.x) (github.com). Checked .
  10. Laravel application skeleton — composer.json (13.x) (github.com). Checked .
  11. Laravel application skeleton — storage folder (13.x) (github.com). Checked .
  12. Laravel source — config/view.php (13.x) (github.com). Checked .
  13. PHP manual — FPM configuration (php.net). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Laravel app

Got a question? — @livops_uz