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

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

just simple create new file in your root server(Let's call it custom.php), then write 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 gives you resulted output.


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

For that you have to create a new file(Let's call it custom1.php), and write 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 good way but sometime It helps.

| improve this answer | |
  • 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 '17 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 '17 at 10:11
  • 1
    Please check path of require DIR . '/app/bootstrap.php'; – Keyur Shah Jun 29 '17 at 10:12
  • 1
    Have you tried first way ?, but just writing only system('php bin/magento cache:status'); – Keyur Shah Jun 29 '17 at 10:13
  • 1
    Ahh, then I think second might be trick for you – Keyur Shah Jun 29 '17 at 10:14
0

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

| improve this answer | |

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.