How to execute PHP code in HTML under nginx server

  • Post author:
  • Post last modified:August 13, 2024
  • Reading time:1 mins read

1.0 Error

PHP code was not getting executed in the index.html file when the concerned website was opened in a browser. nginx server was being used to serve the web pages.

2.0 Solution

2.1 Install php7.0-fpm

$ sudo apt-get install php7.0-fpm

2.2 nginx configuration

Add the following lines to the nginx configuration file, /etc/nginx.conf.

location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

Restart the nginx server.

$ sudo systemctl restart nginx

2.3 Rename file

Rename index.html to index.php.

$ mv index.html index.php

Karunesh Johri

Software developer, working with C and Linux.