Tuesday, December 6, 2016

Magento 2.0 - Run Commands With PHP File

Hello,

As we know with Magento 2.0 they have introduced command line configuration. You can run command through terminal or SSH on remote server to manager and configure your Magento 2.0.

Recently for one of our project we have Magento 2.0 installed on shared server and we do not have access to terminal and SSH. So there was no way to run Magento 2.0 commands. So we solved it by running it through PHP file. Here is how we can do it.

First create a PHP file in root folder of your server where Magento is installed and name mage_command.php. Now open that file and put following code in it.

$commandToRun = '';
shell_exec($commandToRun);

That's it just two line of code. Now just replace value of $commandToRun with the command you want to run. For example reindex data.

$commandToRun = 'php bin/magento indexer:reindex';
shell_exec($commandToRun);

Now run that PHP file in browser with http://your_magento_host.com/mage_command.php

And it will execute that command. If you want to see the result, you can echo it.

$commandToRun = 'php bin/magento indexer:reindex';
echo shell_exec($commandToRun);

You can also run multiple commands.

$commandToRun = 'php bin/magento indexer:reindex';
echo shell_exec($commandToRun);

$commandToRun = 'php bin/magento cache:clean';
echo shell_exec($commandToRun);

Hope this helps you.

No comments:

Post a Comment