Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Apache gives 500 for .php files while .html files load

Error500 Internal Server Error

Short 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.

  • Applies to Apache 2.4, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.log
  2. 2Match what you see, then jump to its fix

    1. 1The log line starts with PHP Fatal error, while the browser shows only the 500 page.A PHP fatal error whose message is hiddenFix
    2. 2The 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. 3The 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

What does the error look like?

  • 500 Internal Server Error
  • PHP 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 = On

3Verify

Reload the page; the error is gone or now printed with a file and line.

curl -I http://localhost/index.php

Confirmed by php.net

Did this fix it?

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" .htaccess

2Fix

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 = 16M

3Verify

Request the page again; the 500 is gone.

curl -I http://localhost/index.php

Confirmed by php.net

Did this fix it?

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 | tail

2Fix

Correct the syntax, or raise AllowOverride to the level that the directive's Override line requires.

# in the virtual host or directory block
AllowOverride FileInfo

3Verify

Test the config, reload Apache and request the page.

sudo apachectl configtest && sudo systemctl reload apache2
curl -I http://localhost/index.php

Confirmed by httpd.apache.org

Did this fix it?

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

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.