Apache gives 500 for .php files while .html files load
500 Internal Server ErrorShort answer
Static HTML never runs code, so a 500 only on .php files means PHP or its configuration failed. Read the Apache error log first. It usually shows a PHP fatal error that display_errors hides, a php_flag or php_value line in .htaccess that your PHP setup does not support, or a .htaccess directive Apache does not allow.
Fix it fast
1Find out which cause you have
Reproduce the 500 and read the newest lines of the Apache error log. It names the real cause; the browser only shows the status.
sudo tail -n 30 /var/log/apache2/error.log # path set by ErrorLog; XAMPP: apache/logs/error.log2Match what you see, then jump to its fix
- 1
The log line starts with PHP Fatal error, while the browser shows only the 500 page.A PHP fatal error whose message is hiddenFix - 2
The log names the .htaccess file and the php_flag or php_value directive.php_flag or php_value in .htaccess, but PHP is not the Apache moduleFix - 3
The log says a directive is not allowed here, or names a bad flag or delimiter in .htaccess.A directive in .htaccess is not allowed or has a syntax errorFix
- 1
What does the error look like?
500 Internal Server ErrorPHP Fatal error: Uncaught Error.htaccess: not allowed here
Cause 1 of 3
A PHP fatal error whose message is hidden
1Check
With display_errors off, PHP does not print the error, so the visitor sees only the status. If PHP also sends errors to the log, the message is there.
php -l public/index.php
php -i | grep -E "display_errors|log_errors|error_log"2Fix
Fix the error the log names (syntax error, missing file or class, removed function after an upgrade). To see errors while developing, set display_errors On in php.ini; on production keep it Off and log instead.
; php.ini (development only)
display_errors = On
error_reporting = E_ALL
log_errors = On3Verify
Reload the page; the error is gone or now printed with a file and line.
curl -I http://localhost/index.phpConfirmed by php.net
Cause 2 of 3
php_flag or php_value in .htaccess, but PHP is not the Apache module
1Check
These directives are only understood when PHP runs as an Apache module. Under CGI or PHP-FPM they are not valid, so Apache treats them as an invalid directive and answers with a 500.
grep -rn "php_flag\|php_value\|php_admin" .htaccess2Fix
Remove the php_ lines and set the same options in php.ini or a .user.ini file, or with ini_set() in the script.
; .user.ini (PHP-FPM or CGI)
display_errors = Off
upload_max_filesize = 16M3Verify
Request the page again; the 500 is gone.
curl -I http://localhost/index.phpConfirmed by php.net
Cause 3 of 3
A directive in .htaccess is not allowed or has a syntax error
1Check
A 500 after adding a directive means either AllowOverride does not permit it or the syntax is wrong. The log says which.
sudo grep "\.htaccess" /var/log/apache2/error.log | tail2Fix
Correct the syntax, or raise AllowOverride to the level that the directive's Override line requires.
# in the virtual host or directory block
AllowOverride FileInfo3Verify
Test the config, reload Apache and request the page.
sudo apachectl configtest && sudo systemctl reload apache2
curl -I http://localhost/index.phpConfirmed by httpd.apache.org
How do I stop it coming back?
- Always send PHP errors to a log file and read it after every deploy.
- Run php -l on every changed file before uploading it.
- Use php.ini or .user.ini for PHP settings unless you are sure PHP is the Apache module.
- Test .htaccess changes on a copy, one directive at a time.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Laravel returns 500 Internal Server Error on Apache
/var/www/html/.htaccess: RewriteCond: bad flag delimiters
Apache and Laravel report different 500s.
5 causes, each with a fixBack end
PHP shows a blank page and no error message
No Warning or Fatal error text, even though the script is broken
A blank page means PHP hit an error but was told not to print it.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fix