1

I am trying to create button to run php ../bin/magento indexer:reindex bash script via admin panel. It is working on my localhost but when I pushed the script on server nothing happens.

<?php if ($title == "Index Management") : ?>
    <a href="?reindex">Reindex</a>
<?php endif; ?>
<?php
    if (isset($_GET['reindex'])) {
        echo shell_exec('php ../bin/magento indexer:reindex');
        echo '<br>';
        //echo '<strong>Reindex finished successfully.</strong>';
    }
if(function_exists('exec')) {
    echo "exec is enabled";
} else {
    echo "exec is disabled";
}
echo '<br>';
echo shell_exec('whoami');

It says that "exec is enabled" and whoami says "www-data". Maybe that www-data doesn't have permissions to execute that command. What do you guys $think?

2 Answers 2

2

In server, If you don't have enough permission to execute I am using the below script (it's not a good way but It works ! ) , maybe It helps you

just simply create a new file in your root server(Let's call it custom.php), then write the below code in this file

system('php bin/magento cache:status'); // you can change it to your command

And run this script with http://your base url/custom.php

It will give you result in output.


Another way, but it doesn't give you any message(I mean success or fail), but you can use it if you don't have any option.

For that, you have to create a new file(Let's call it custom1.php), and write the below code in your file.

<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

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

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$k[0]='bin/magento';
$k[1]='cache:status'; // write your proper command like setup:upgrade,cache:enable etc...
$_SERVER['argv']=$k;
try {
    $handler = new \Magento\Framework\App\ErrorHandler();
    set_error_handler([$handler, 'handler']);
    $application = new Magento\Framework\Console\Cli('Magento CLI');
    $application->run();
} catch (\Exception $e) {
    while ($e) {
        echo $e->getMessage();
        echo $e->getTraceAsString();
        echo "\n\n";
        $e = $e->getPrevious();
    }
}

And run this script with http://your base url/custom1.php

It is not a good way but sometimes It helps.

7
  • It returns error that he can't find app/bootstrap file, are you sure we can add this script to the magento 2?
    – Milo
    Jun 29, 2017 at 10:10
  • 1
    No It is not good way to put this script in Magento 2 root directory, but sometime I am using this script If I don't have any options to execute command :) @Milo
    – Keyur Shah
    Jun 29, 2017 at 10:11
  • 1
    Please check path of require DIR . '/app/bootstrap.php';
    – Keyur Shah
    Jun 29, 2017 at 10:12
  • 1
    Have you tried first way ?, but just writing only system('php bin/magento cache:status');
    – Keyur Shah
    Jun 29, 2017 at 10:13
  • 1
    Ahh, then I think second might be trick for you
    – Keyur Shah
    Jun 29, 2017 at 10:14
0

Ok, I have changed the owner of var/generation folder myuser:myuser, and now it allows executing the reindex command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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