How do I run php custom script on magento2

If you are using nginx configuration that comes with magento you need to put a file inside pub folder to allow access to it from the browser as pub is the document root of the vhost. Magento root dir is one level up. Second of all default config for nginx allows only to access index.php, get.php, static.php, report.php, 404.php and 503.php files. Any other are not processed by the php. You can see this in line with location ~ (index|get|static|report|404|503)\.php$ { in nginx.conf.sample. If you are not using it check your config for similar rule. To allow another file to be accessible from browser simple add another name after 503 or change entire brackets with location ~* \.php$ {


For example, to get product name by the custom script

Example 1:

Create test.php at the root of Magento var/www/html/magento2/test.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$id = 1;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($id);

echo $product->getName();

You can run script test.php by

http://127.0.0.1/magento2/test.php

Example 2:

step 1: create index.php at root of magento 2

var/www/htmlmagento2/test/index.php

<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

step 2: create customScript.php

/var/www/html/magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);

        echo $_product->getName();

        return $this->_response;
    }

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }

}

Now you can run this custom script by

http://127.0.0.1/magento2/test/

enter image description here


If you want to allow more than one php script to be executable like I need (import.php for ERP import product, stock.php for update the inventory with my ERP etc...) :

  • create a new directory scripts in the /pub folder
  • edit your magento vhost and add lines under ##Allow pub/srcipts/ folder to execute php custom

     listen 80;
     server_name example.com www.example.com;

     set $MAGE_ROOT /var/www/html/magento2;
     include /var/www/html/magento2/nginx.conf.sample;

     ## Allow pub/srcipts/ folder to execute php custom
     location /scripts/ {
        location ~* \.php$ {
                try_files $uri =404;
                fastcgi_pass   fastcgi_backend;
                fastcgi_buffers 1024 4k;

                fastcgi_read_timeout 600s;
                fastcgi_connect_timeout 600s;

                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
     }

With this you can now run your scripts by accessing them at :

http://www.example.com/scripts/your_custom_code.php