9

I wanted to add custom php script to magento2 root folder and run from the browser.I tried to add it in magento2 root folder but it redirects to 404 page.

I also tried to add it in pub folder but no success.

Also cleared cache and generation.

I am running magento2 on nginx server

  • Sounds strange. I created test.php script in magento2 root dir, then I just call it from browser http://%magento-base-url%/test.php and it's work. – Valery Statichnyi Jan 14 '16 at 11:07
  • I am running magento2 on nginx server – Ranjit Shinde Jan 14 '16 at 11:17
16

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$ {

  • Do i need restart nginxafter change ? – ravisoni May 8 '18 at 17:58
  • Yes, any change to vhost requires at least reload. – Zefiryn May 8 '18 at 20:22
9

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

  • 1
    I got an error 'class customScript not found' so I added 'require './customScript.php' before '$app = $bootstrap->createApplication('customScript');' and that worked. – Zankar May 7 '18 at 9:58
  • I'm getting page not found error – ravisoni May 8 '18 at 18:06
  • 1
    @ravisoni, that means you did something wrong. Make sure to create folder /test/ and two files index.php and other one customScript.php – electroid May 9 '18 at 7:13
  • 1
    @Zankar, you need to place your customScript.php file in the same folder (subfolder from sitedir) as index.php file. From your code ./customScript.php I can see that your customScript.php is one level up from index.php – electroid May 9 '18 at 7:16
  • @electroid in my case the customScript.php file is in the same directory as index.php. If I was including it from a folder one level up I would do ../customScript.php (note the two dots) – Zankar May 9 '18 at 9:48
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.