ZenoCloud

Breeze and ServerGuy have united to offer superior managed services and web hosting services as ZenoCloud.

How To Run Custom PHP Files in Magento 2? (In NGINX)

Magento powers 12% of all the e-commerce stores all over the world. But, the official support for the Magento 1.x versions will stop after 2020.

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

Due to one of 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.

run custom php files in magento 2

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.

location ~^/(index|get|static|errors/report|errors/404|errors/503|health_check|test).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 test.php then add the test after health_check:-

custom PHP files in Magento 2 in NGINX

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

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

We hope you will now be able to run custom PHP files in Magento 2. If not, then our Magento 2 experts are here to help you. ServerGuy also offers Magento hosting services for a secure and high performing store.

You can also leave a comment below if you any other query, we will definitely get back to you.

Also Read:

Latest Magento Tips, Guides, & News

Stay updated with new stuff in the Magento ecosystem, including exclusive deals, how-to articles, new plugins, and more. 100% Magento Goodness, a promise!

0 Responses

  1. often I also facing the same error when the website has error about establishing connection error. Now using this method, everything is working fine. Thanks for your help and subscribed your blog..

  2. often I also facing the same error when the website has error about establishing connection error. Now using this method, everything is working fine. Thanks for your help and subscribed your blog..

Leave a Comment

Your email address will not be published. Required fields are marked *

Leave a Comment

Your emai address will not be published. Required Fields are marked *

We can help you. Right now.

Fast-growing merchants depend ZenoCloud
for high-performance hosting.

Let's get started.

Talk to our Sales representative

USA / Worldwide

+1.714.2425683

India

+91.9852704704