Report this

What is the reason for this report?

Parse HTML as PHP w/ LEMP stack

Posted on April 6, 2021

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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;
}
  • Replace /var/www/html with the root directory of your website.
  • Adjust php7.4-fpm.sock to match the PHP version installed on your server (e.g., php8.0-fpm.sock).

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.