asadbekDev's avatar

Failed to open stream: Permission denied

i have deployed my laravel project in ubuntu server i faced this issue daily

The stream or file "/var/www/html/xxxx-laravel-project/storage/logs/laravel-2024-07-21.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file "/var/www/html/xxxx-laravel-project/storage/logs/laravel-2024-07-21.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file

sudo chmod -R 775 /var/www/html/xxxx-laravel-project/storage

if do this command working but tommorow it gives this issue again

0 likes
3 replies
Snapey's avatar

you probably have a cron job running near midnight, which writes to the log file.

The cron is running as you or root, and it creates a log file owned by you, so cannot be appended to by www-data user

A simple fix is to delete your cronjob and remake it against the www-data user

sudo crontab -e -u www-data

So that when crontab fires your scheduler it is doing so as www-data user

asadbekDev's avatar

@Snapey is this my cron job

sudo crontab -e -u www-data

0 0 * * * /usr/local/bin/fix_permissions.sh

inside fix_permissions.sh

#!/bin/bash
LOG_DIR="/var/www/html/xxxx-laravel-project/storage/logs"

# Set ownership
sudo chown -R www-data:www-data $LOG_DIR

# Set permissions for directories to 775
sudo find $LOG_DIR -type d -exec chmod 775 {} \;

# Set permissions for files to 644
sudo find $LOG_DIR -type f -exec chmod 644 {} \;

# Log the action
echo "Permissions fixed on $(date)" >> /var/log/fix_permissions.log

Please or to participate in this conversation.