Custom PHP files wont load (Outside of magento 2 installation ubuntu + nginx)

Custom PHP files wont load (Outside of magento 2 installation ubuntu + nginx)

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Custom PHP files wont load (Outside of magento 2 installation ubuntu + nginx)

Resolving Custom PHP File Loading Issues in Magento 2 on Ubuntu + Nginx

If you’re facing a 404 error when trying to load custom PHP scripts outside your Magento 2 installation directory, you’ve likely encountered a common issue with permissions and configuration. Let’s walk through the steps to troubleshoot and resolve this problem.

Understanding the Issue: Permissions and Configuration Mismatch

When you install and configure Magento 2 using Ubuntu and Nginx, the server is set up with specific user permissions and directory structures for security reasons. These configurations often prevent direct execution of PHP files outside the primary installation folder to protect against unauthorized access.

Initial Troubleshooting Steps Taken

You’ve already tried changing file ownership and permissions (CHMOD 777) without success, which suggests that the issue isn’t simply a matter of read/write access. The server configuration is likely preventing PHP execution for your custom scripts.

Step-by-Step Solution: Edit Nginx Configuration File

To resolve this, you’ll need to edit the Nginx configuration file and add a location block for your custom script’s directory. Here are the steps:

  1. Locate the Nginx configuration file for your Magento 2 site. It is typically located in /etc/nginx/sites-available/your_domain.conf, replacing your_domain with your actual domain name.
cd /etc/nginx/sites-available/
  1. Open this file using a text editor such as nano or vim:
nano your_domain.conf
  1. At the end of the <server> block but before the closing tag, add the following location block for your custom directory (replace /custom-scripts with your actual path):
location /custom-scripts/ {
    try_files $uri $uri/ =404;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

This location block tells Nginx to pass requests for the /custom-scripts/ directory to PHP using FastCGI.

Restarting the Nginx Service

After adding this configuration, save and close the file. Then, restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

Conclusion: Ensuring Successful Loading of Custom Scripts

By editing the Nginx configuration file and adding a location block for your custom directory, you should now be able to access and execute PHP scripts within that folder without encountering 404 errors.

Remember, it’s crucial to maintain proper security measures:

  • Limit the scope of the location block to the necessary directories.
  • Regularly review and update permissions as needed.
  • Ensure all custom scripts are properly secured.

For a comprehensive guide on Magento 2 development best practices, check out our article Optimizing Magento 2 Development Workflows.

Magento 2 Security Configuration Errors

Our network