Understanding the 500 Error
When troubleshooting a 500 Internal Server Error with PHP files on a Linux hosting environment, you will want to review the .htaccess file in the existing directory as well as any directories above. The 500 error is a generic "something went wrong on the server" response that hides the real issue.
Step-by-Step Diagnosis
Check the .htaccess File
The most common cause of 500 errors in PHP is a malformed .htaccess file. Temporarily disable it by renaming:
# Rename .htaccess to disable it
mv .htaccess .htaccess.bak
This prevents the server from reading it as a configuration file.
Hard Refresh and Clear Cache
After disabling .htaccess, perform a hard refresh (Ctrl+Shift+R) or clear your browser cache and cookies to reload the page without cached data.
If the error disappears, the issue originates from the .htaccess configuration. You may wish to start with the most recent changes to your site to determine if any of this information was added to the .htaccess file. Use comments to disable lines for testing:
# Comment out a line by adding # at the start
# RewriteRule ^old-page$ /new-page [R=301,L]
Check Error Logs
The error log will tell you the exact cause. Common log locations:
# Apache error log
tail -50 /var/log/apache2/error.log
# cPanel per-domain error log
tail -50 /home/username/logs/error.log
# Nginx error log
tail -50 /var/log/nginx/error.log
# Watch logs in real-time
tail -f /var/log/apache2/error.log
Verify File Permissions
Incorrect permissions are a frequent culprit. Files should be 644 and directories should be 755:
# Fix permissions recursively
find /home/username/public_html -type f -exec chmod 644 {} \;
find /home/username/public_html -type d -exec chmod 755 {} \;
# Never set 777 permissions — it's a security risk
Check PHP Version Compatibility
If you recently upgraded PHP, some functions may be deprecated or removed. Check for compatibility issues:
# Check current PHP version
php -v
# Look for deprecated function warnings in logs
grep -i "deprecated" /var/log/apache2/error.log
Increase Memory Limit
PHP scripts that exceed the memory limit will throw a 500 error. Increase it in your configuration:
# In php.ini
memory_limit = 256M
# In .htaccess
php_value memory_limit 256M
# In your PHP script
ini_set('memory_limit', '256M');
Enable PHP Error Display
For development and debugging, you can temporarily enable error display to see exactly what's failing:
<?php
// Add these lines at the top of your PHP file
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
Never leave display_errors enabled on a production site. It can expose sensitive information like file paths, database credentials, and server configuration to visitors.
Common .htaccess Mistakes
- Missing
RewriteEngine Onbefore rewrite rules - Syntax errors in directives (typos, wrong flags)
- Infinite rewrite loops — rules that redirect to themselves
- Invalid PHP directives on servers using PHP-FPM (use
.user.iniinstead ofphp_value) - Wrong line endings — Windows line endings (
\r\n) can cause issues on Linux
If you're on shared hosting and can't access server error logs directly, check cPanel's Errors section under Metrics. It shows the most recent Apache errors for your account.