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.

This entry was posted in Magento 2, Magento Admin, Magento Commands, PHP, PHP Framework, SSH, Terminal on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

Leave a Reply

Your email address will not be published. Required fields are marked *