How to run PHP in .html files with Apache on Ubuntu
To run PHP in an .html file, Apache needs a handler for that file. With PHP-FPM (FastCGI Process Manager), the FPM pool must also allow the .html extension. Limit the change to a directory of trusted, deployed code when legacy URLs must stay the same. Use .php for new PHP pages so the executable files are easy to identify.
Identify how web requests run PHP
The example assumes Ubuntu 24.04 LTS, Apache 2.4 and working PHP 8.3-FPM, with administrative access. If any path, name or port is already in use, choose another for the lab. Back up any existing configuration you edit. For another PHP version, check the installed service, binary, pool directory and socket path. Ubuntu 24.04 LTS
sudo apache2ctl -M
sudo apache2ctl -S
In the module list, php_module indicates PHP embedded in Apache. proxy_fcgi_module means Apache can connect to a FastCGI backend. Read the handler rules in the relevant virtual host too. The PHP command-line version does not tell you how web requests run. The PHP installation manual recommends FPM for new setups. PHP Manual · Apache
Create a dedicated PHP-FPM pool
Open the indicated file with sudoedit and save the pool configuration. The socket name must match the Apache configuration below. This pool allows .php and .html in place of the default .php and .phar. Keep the list explicit and leave shared pools alone. Three child processes are a lab setting. Pools using the same www-data account do not provide the isolation of separate operating-system users. PHP Manual · FPM
sudoedit /etc/php/8.3/fpm/pool.d/legacy-html-demo.conf
[legacy_html_demo]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm-legacy-html-demo.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = ondemand
pm.max_children = 3
pm.process_idle_timeout = 10s
security.limit_extensions = .php .html
Connect Apache to the dedicated socket
Create the empty lab directory and virtual-host file. This example listens only on 127.0.0.1:8080. If a Listen directive already covers that address and port, avoid adding a duplicate. Keep the document root writable only by the people who deploy code, and keep uploads outside the executable directory.
sudo mkdir -m 0755 /srv/legacy-html-demo && sudo mkdir -m 0755 /srv/legacy-html-demo/public
sudoedit /etc/apache2/sites-available/legacy-html-demo.conf
Listen 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
ServerName legacy-html-demo.local
DocumentRoot /srv/legacy-html-demo/public
<Directory /srv/legacy-html-demo/public>
Options -Indexes
AllowOverride None
Require local
<FilesMatch "\.(?:php|html)$">
SetHandler "proxy:unix:/run/php/php8.3-fpm-legacy-html-demo.sock|fcgi://localhost/"
</FilesMatch>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/legacy-html-demo-error.log
CustomLog ${APACHE_LOG_DIR}/legacy-html-demo-access.log combined
</VirtualHost>
FilesMatch and SetHandler send only lowercase .php and .html in this document root to the configured socket. If .htm is also needed, extend both the regular expression and the FPM allowlist. For .HTML, check the existing file-naming policy before broadening the match. Apache · SetHandler Apache · mod_proxy_fcgi Apache · Listen
Verify PHP execution and static responses
Reload FPM only after its configuration test succeeds. Do the same for Apache. Stop at the first failure and use the file name and line number in the error to troubleshoot. Apache’s Syntax OK confirms configuration syntax only.
sudo a2enmod proxy_fcgi
sudo php-fpm8.3 -t && sudo systemctl reload php8.3-fpm
sudo a2ensite legacy-html-demo.conf
sudo apache2ctl configtest && sudo systemctl reload apache2
In the document root, put the same PHP code below in check.php and check.html. Save the CSS line as style.css to check the static-file control. PHP_SAPI returns the name of the PHP execution interface. Request all three URLs and compare the full response bodies.
<?php
header('Content-Type: text/plain; charset=UTF-8');
echo 'PHP_EXECUTED', PHP_EOL;
echo 'SAPI: ', PHP_SAPI, PHP_EOL;
body { color: #333; }
sudo chmod 0644 /srv/legacy-html-demo/public/check.php /srv/legacy-html-demo/public/check.html /srv/legacy-html-demo/public/style.css
curl --fail --show-error http://127.0.0.1:8080/check.php
curl --fail --show-error http://127.0.0.1:8080/check.html
curl --fail --show-error http://127.0.0.1:8080/style.css
With FPM, both PHP responses should have two lines: PHP_EXECUTED and SAPI: fpm-fcgi. The CSS should be unchanged. A search for PHP_EXECUTED alone can also match the PHP source. If the response contains source or triggers a download, immediately restrict access to the file and inspect the active virtual host and handler.
Adapt an existing mod_php setup
If the server already uses mod_php, replace the virtual host’s FilesMatch block with the one below and skip the FPM pool. Enable, check and reload the Apache site. Keep the same scope and static control. Both PHP responses should now show SAPI: apache2handler. Older AddType examples depend on MIME and module integration, so do not assume they apply to your handler.
<FilesMatch "\.(?:php|html)$">
SetHandler application/x-httpd-php
</FilesMatch>
Troubleshoot and roll back
For a 403, distinguish Apache access restrictions from extension denial in the FPM log. For a 503, check the FPM service and the socket path and permissions. Before reverting an existing site’s configuration, move executable .html files out of the document root or block access to them to prevent source disclosure.
sudo a2dissite legacy-html-demo.conf
sudo apache2ctl configtest && sudo systemctl reload apache2
To undo the local exercise, disable the site, check the Apache configuration and reload. Confirm the lab pages are no longer served on port 8080. If you used FPM, archive only the lab’s pool file under a name without the .conf suffix, test and reload FPM, then clean up the test files. Leave shared services and other pools in place.




