Laravel Debug Errors in Homestead 2.0
Quick tip here that had me banging my head off the wall for a few hours last week. If you use the Laravel framework for building Web applications, you are likely using their fantastic Homestead feature to manage your local development environments. It’s basically a nicely package Vagrant box that is set up to host Laravel sites that allows you to keep your dev box itself nice and clean.
One issue I ran into last week when using Homestead 2.0, however, was that the nice pretty Laravel debug (see whoops) error messages were not displaying. Instead I was getting a 404 nginx error message. This makes life pretty miserable when building an app, so I dug in to try and figure out what was going wrong.
I ruled out it being a Laravel code issue by running the app using the local “serve” command — when I did this the errors displayed fine. So no issue with my Laravel app config or anything like that. I looked at the nginx config file for my site, and tried changing the
error_page 404 /index.php
directive to a different file to test, and it worked. Hmmm. At this point I was confused, thinking it actually could be my Laravel config after all. Alas no, after a lot of messing around I spotted the following line in the nginx config file within a location directive:
fastcgi_intercept_errors on;
Eek that doesn’t look right. I commented it out and restarted nginx and hey presto, those pretty Laravel debug messages were back. End of story? Not quite…
A few days later I created another Laravel project, so updated my Homestead config and used
vagrant provision
to refresh the VM. After doing this the change I made was undone. When you use this command, it resets the box’s configuration based on your original setup. To stop this from happening, it was time to make some config changes.
When you create the Homestead box, it creates a hidden directory in your Home directory named .homestead. In here, there is a Homestead.yaml file and an after.sh file that allows you to write some scripts that will be run after the box is provisioned. I used sed to change my nginx configuration for all sites with this script:
#!/usr/bin/env bash
config_files=($(find /etc/nginx/sites-available -type f))
for i in “${config_files[@]}”
do
sed -i '/ fastcgi_intercept_errors /s/^/#/' $i
done
service nginx restart
service php5-fpm restart
This basically finds the line in question and comments it out by prefixing it with the # symbol.
Now, when you provision the box, this script will run, comment out the line in question for any nginx sites that are configured, restart nginx and your pretty Laravel debug messages will be back up and running.