From Laravel I got failed to open stream: Permission denied
You are seeing an ErrorException complaining that file_put_contents failed on a generated view file inside your storage/framework/views directory. The specific path in the error is /github/myweb/monitor/storage/framework/views/b38c7158.... This path reveals the root cause immediately: your application is running from a directory mounted at /github, but you want it running from /var/www/master/monitor.
The error isn't about the code logic; it is about the operating system's ability to write files to that specific location. In Laravel, when caching views or storing session data, the framework attempts to create new files in the storage folder. If the web server user (often www-data, apache, or nobody) lacks write permissions for that directory, the process halts with "Permission denied."
The fact that your local environment works perfectly while the production server fails confirms this is an environment-specific permission issue, not a syntax error in your views or controllers. The path /github/myweb/ suggests you might be using GitHub Pages, a specific hosting setup, or simply mounting a repository directly to a web root that wasn't configured with the correct ownership.
Why Local Works and Production Fails
On your local machine, the default user running PHP is likely your own user (e.g., john). You have full control over your home directory, so writing to storage/framework/views succeeds without friction.
In contrast, on a shared hosting environment or a server where you pushed code directly to /github/myweb, the web server process runs as an unprivileged user. This user typically does not own the uploaded files or directories. If you cloned the repo or copied files manually, they likely retain the ownership of your local user (or root if you used scp without preserving permissions), and the web server cannot write to them.
Changing the Document Root
You mentioned wanting the path to be /var/www/master/monitor. The most robust solution is to ensure the application runs from that standard location rather than fighting with the current mount point. You can achieve this by configuring your web server's document root.
If you are using Nginx, update your site configuration:
server {
listen 80;
server_name your-domain.com;
# Point to the desired location
root /var/www/master/monitor/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
# ... other locations
}
After updating the config, reload Nginx:
sudo nginx -t && sudo systemctl reload nginx
For Apache, you would update your VirtualHost definition similarly to point DocumentRoot to /var/www/master/monitor/public. You may also need to update the .htaccess file if it exists, though modern setups often handle this in the server config.
Fixing Permissions Without Moving Files
If moving the directory is not an option and you must work within /github/myweb, you must explicitly change ownership so the web server user can write to storage. Run these commands as root:
cd /github/myweb/monitor
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
Be careful here. While 775 allows group read/write, it can be overly permissive if the group is not strictly controlled. In production environments, 775 on storage is acceptable because Laravel's internal file locking and caching mechanisms generally handle concurrent access safely within the same group. However, sensitive files like .env should remain 600 or 640 owned by your app user, not the web server.
Addressing the Path Prefix Mystery
You asked how to remove the /github/myweb/ prefix from the path. You do not need to modify Laravel's configuration to change the physical path; that is determined entirely by where your files are located on the disk and what your web server is configured to serve. The error log shows the absolute path because that is where the file system actually resides.
If you see URLs starting with /github/myweb/ in your browser, that indicates a misconfiguration in your .htaccess or Nginx rewrite rules, causing the web server to serve files from the wrong directory or exposing the physical path. Ensure your RewriteBase (in Apache) or rewrite rules (in Nginx) align with your actual document root. If you set the document root to /var/www/master/monitor/public, ensure that all rewrites resolve relative to that new root, not the old /github location.
Final Verification
Once you have adjusted the document root and permissions:
- Clear your view cache to force Laravel to regenerate files in the correct location:
php artisan view:clear. - Clear compiled classes if necessary:
php artisan optimize:clear. - Reload your web server.
The application should now write generated views to /var/www/master/monitor/storage/framework/views (or the equivalent under your new root) without triggering permission errors. If you are still facing issues, check the specific user running PHP via phpinfo() or your process manager logs to ensure it matches the owner of the storage directory.