Laravel – Troubleshooting Apache 500 errors

Laravel Apache Error 500

During my time with Laravel, I have encountered a fair share of errors. One particular error I have encountered frequently is Apache’s 500 ‘internal server error’ – which means something has gone wrong on the server-side. While there are numerous StackOverflow threads explaining and troublehooting this Apache error, this is generally my troubleshooting experience with Laravel.

With regard to the 500 error, there are four possibilities as to the cause…

Check Laravel Error Logs.

One is that there are possible scripting or database issues. A good place to check diagnostic information is in the log files – which you can find in the ‘storage/logs’ directory.

Check Laravel Application Key.

After installing and updating libraries via Composer, you may need to run ‘php artisan key:generate’. This key is used for encryption of data (e.g. sessions, passwords), and is important for Laravel to function properly. When you run this command, the key is generated with a randomized string defined in your .env file (for example: APP_KEY=someRandomString).

Check Apache Server Logs.

To investigate further, you may need to look at your server logs as well. For example, if you are using an Apache-based server, you can view error logs in ‘/var/log/apache2’. Here, I would usually view contents of the ‘error.log’ file.

Properly Configured .htaccess File.

The .htaccess file is essential, and required (for Apache servers), for Laravel’s routing to work. Deleting the .htaccess file, or having an incorrectly-condifured .htaccess file can cause 500 errors. In an example file on my GitHub repository, I have included extra configuration to filter out spambots and address some rendering performance issues. If some of the extra configuration is causing issues, try using the default .htaccess configuration, as below:

<IfModule mod_rewrite.c>

    #Convert www to non-www
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

Other Considerations

If you are using Apache,  are you using the root domain or a sub-domain? Are you using shared hosting or a VPS? What does your site’s vhost file look like?

If you are using a VPS, your vhosts file should look similar to:

For a Root Domain.
# FOR ROOT DOMAIN
<VirtualHost youripadress:80>

    ServerName yourdomain.com
    ServerAdmin [email protected]
 
    DocumentRoot "/var/www/yourdomain.com/public"
    <Directory "/var/www/yourdomain.com/public">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
For a Sub-Domain.
# FOR SUBDOMAIN
<VirtualHost youripadress:80>

    ServerName subdomain.yourdomain.com
    ServerAdmin [email protected]

    # Here, I would place subdomain files in a separate directory
    # within 'yourdomain.com'. Depending on your hosting and domain
    # setup, you will need to create CNAME and A records for your
    # subdomain when pointing the domain to your hosting server.
    DocumentRoot "/var/www/yourdomain.com/subdomain/public"
    <Directory "/var/www/yourdomain.com/subdomain/public">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Collaboration.

If you still have trouble after using the above suggestions, please let me know. If you encounter Apache 500 errors with your Laravel project, feel free to comment about your experiences here. I’m sure we can all learn and collaborate from each-other’s feedback :).

Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*