I installed the LEMP package from the marketplace, and I’d like to host a website that relies on having HTML files parsed as PHP. I can find instrucions for doing this with Apache, but not nginx. Is there an easy way to do this, or should I just start over with a LAMP installation?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Please check out How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 18.04.
Hi there,
Yes, this is also doable with Nginx as well as Apache.
You need to duplicate the .php location rule and add one for .htm$.
So the configuration will look like this, depending on yuor PHP configuration:
location ~ \.htm$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
Hope that this helps. Regards, Bobby
Update the Server Block Add a location block to handle .html files like .php files. Modify your server block to include the following:
server {
server_name yourdomain.com www.yourdomain.com;
root /var/www/html;
index index.php index.html;
# Existing PHP configuration
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust PHP version if needed
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Parse .html files as PHP
location ~ \.html$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/yourdomain-error.log;
access_log /var/log/nginx/yourdomain-access.log;
}
/var/www/html with the root directory of your website.php7.4-fpm.sock to match the PHP version installed on your server (e.g., php8.0-fpm.sock).Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.