PHP inside an .html file is shown as text or ignored
<?php echo ... ?> appears in the page sourceShort answer
Apache runs PHP only for the file types it is told to hand to PHP, normally .php. An .html file is sent as plain HTML, so the PHP tags are never run. Rename the file to .php, or add .html to the handler pattern in the Apache config. Without any handler, even .php files show their source.
Fix it fast
1Find out which cause you have
View the page source in the browser. Visible PHP tags mean the file was sent without being run. Then test a plain .php file in the same folder.
echo '<?php echo "php works";' | sudo tee /var/www/html/test.php curl http://localhost/test.php2Match what you see, then jump to its fix
What does the error look like?
<?php echo ... ?> appears in the page sourcethe PHP code shows up as text in the browser
Cause 1 of 2
The file ends in .html and only .php is handled by PHP
1Check
The PHP manual's Apache setup hands only files that match .php to PHP, through a FilesMatch block. Look for it in your config.
grep -rn "x-httpd-php" /etc/apache2/ /etc/httpd/ 2>/dev/null2Fix
The simple fix is to rename the page to .php and update the links and form actions that point to it. If the .html name must stay, add html to the pattern. This sends every .html file through PHP, so use it only where needed.
<FilesMatch "\.(php|html)$">
SetHandler application/x-httpd-php
</FilesMatch>3Verify
Test the config, reload Apache and request the page again.
sudo apachectl configtest && sudo systemctl reload apache2
curl http://localhost/page.htmlConfirmed by php.net
Cause 2 of 2
No PHP handler is configured for Apache at all
1Check
If the grep above prints nothing, Apache has no rule to hand .php to PHP. This happens on a fresh XAMPP or a hand-built install.
apachectl -M | grep -i php
grep -rn "SetHandler" /etc/apache2/ 2>/dev/null2Fix
Add the handler block from the PHP manual. The manual recommends SetHandler inside FilesMatch over AddType, so files such as exploit.php.jpg are not run as PHP. For new installs it advises PHP-FPM with mod_proxy_fcgi rather than mod_php.
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>3Verify
Reload Apache and request the test file.
sudo systemctl reload apache2
curl http://localhost/test.phpConfirmed by php.net
How do I stop it coming back?
- Keep pages that contain PHP named .php from the start, so no server setting is needed.
- Use the SetHandler and FilesMatch form, not a bare AddType, when you widen the pattern.
- Remove the test.php file after checking.
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 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 fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fixBack end
CodeIgniter 3 loads the model but the property is undefined
The model name you are loading is the name of a resource that is already being used: user
CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.
2 causes, each with a fix