Magento 2 how to configure Nginx to allow execute other PHP files

Magento is powerful and flexible eCommerce platform. But, the official support for the Magento 1.x versions will stop in June 2020.

Therefore, if you are still on Magento 1, consider migrating to Magento 2.

The security features of Magento 2, you cannot run the external PHP files by default. 

The file names need to be added in Nginx configuration to execute or we can allow all external files to run but it can break the security of Magento 2.

Steps to run custom PHP files in Magento 2 in NGINX are:

1. You need to find the included Nginx Sample file in the main Virtual Host Configuration. The name could be different like magento2.conf or also should be in an included Directory.


2. Find the PHP location block (# PHP entry point for the main application) as shown in the below screenshot and add the custom PHP file, then you need change the line 181 at file <Root folder Magento>/nginx.conf.sample from:

location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
to :

location ~^/(index|get|static|errors/report|errors/404|errors/503|health_check|phpinfo).php$

If we want to allow all the custom PHP files to run then replace the above line as:

location ~* .php$ {

For example: If we need to add the phpinfo.php then add the phpinfo after health_check


3. The default Document root for Magento 2 in Nginx is “<Root folder Magento>/pub”. We need to put the external files in the pub directory to execute.

4. So as in this example move the phpinfo.php to “<Document Root>/pub”.

Then run command line:

sudo systemctl restart nginx

We hope you will now be able to run custom PHP files in Magento 2. If not,contact us, then our Magento 2 experts are here to help you.