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

Run Custom PHP files in Magento2

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 modules, and more. 100% Magento Goodness, a promise!

1 thought on “How To Run Custom PHP Files in Magento 2? (In NGINX)”

  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..

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

We can help you. Right now.

Fast growing merchants depend ServerGuy for high-performance hosting. Experience counts. Let's get started.

Talk to a sales representative

USA / Worldwide

+1.714.2425683

India

+91.9852704704

Core Web Vitals Book COver

Is your website ready for Core Web Vitals?

Take this FREE book with you and optimize your store for speed.

Learn all about new Google new ranking factors and get that top ranking.