How to Run Shell Commands via PHP File in cPanel?



In this tutorial, we’re gonna walk you through the simple steps to run Shell Commands via PHP file in your cPanel account. No need to log into the SSH to execute the Shell commands. Yup, you can easily run and execute Shell/SSH commands in cPanel using a PHP script.

Sometimes, we all don’t want to do something without a reason. So, if you don’t want to open your SSH terminal, and looking for an alternate method to run Shell commands from your cPanel itself. This tutorial is for you.

How to Run Shell Commands Via PHP File in cPanel?

  1. First of all, log into your cPanel account.
  2. Navigate to your root directory (public_html) and create a new File.

    Create new file
    Create new file
  3. Type any suitable name with the .php extension, and hit the Create New File button.

    Create a New File
    Create a New File
  4. Open the newly created PHP file by clicking over the Edit button.

    Edit File
    Edit File
  5. Once the editor window is opened, copy the below code and paste it there.
    $output = shell_exec('df -h'); 
    echo "<pre>$output</pre>"; 
  6. After pasting the above command hit the Save Changes button and then close the window.

    Save Changes Button
    Save Changes Button
  7. Now hit the file from outside, and you’ll be able to see the result of the df -h command which you’ve used in the above script.

    Running Shell Script From cPanel
    Running Shell Script From cPanel
  8. You can change the command inside your PHP script and refresh the page from the outside to see the changes. For example, now I want to list all contents present inside the document root (public_html). First, I’ll change the command by editing the test.php file.

    Chagne the Command in PHP file
    Change the Command in the PHP file
  9. Save the file and again hit the file from outside.

    Running Shell Command in PHP File
    Running Shell Command in PHP File

This is how you can perform almost every SSH command from your cPanel itself. Let’s take one more example, what if you want to download a large file and upload it inside your cPanel? The best way to do it is via the wget command from SSH. But what if you don’t have SSH access enabled in your cPanel account? This script will help you in this situation.

Use wget Command in PHP Script

To use the wget command using PHP script, follow the above steps and change the command with the below command.


As before, save the file and hit it from the outside.

Using wget command in PHP script
Using the wget command in the PHP script

You’ll see a blank page once you hit the file from outside.

Blank page wget
Blank page wget

Move back to the cPanel dashboard and navigate to your root directory.

You’ll see the URL has been downloaded and uploaded directly inside your cPanel.

wget command successfull in PHP script
used wget command successfully in PHP script

Tar/Untar files from cPanel using PHP script

To tar/untar a directory by executing SSH commands using a PHP file, follow the below steps:

  • Log into your cPanel & create a new PHP file.
  • Open that file, copy the code from above, and paste it inside the newly created PHP file.
  • Replace the ‘df -h’ command with your custom command.
tar -cvf filename.tar /home/path/to/dir
  • Hit the Save Changes button to save the command.

    using tar command in php script
    using tar command in php script
  • Again, hit the file from outside to execute the command.
tar command executed
tar command executed
  • Once done, navigate to the directory where the PHP script is located, and find the tar file.
tar command successfully completed
tar command successfully completed
  • That’s how you can easily tar any directory from a PHP script. Likewise, you can untar any file by using untar command. Let’s untar the recent tar file.
  • You can use the below command to untar any tar file.
tar -xvf filename.tar


tar -xvf /path/to/file/filename.tar
Note: You can use the first command if the file you’re trying to untar & the file from where you’re executing all the commands are located in the same directory. Otherwise, use the second command instead of the first one.
  • Hit the Save Change button.
Untar file in cPanel with PHP script
Untar file in cPanel with PHP script
  • Open file from outside. For example, if the PHP file named “test.php is present inside your root directory and your domain name is “ then type “” in your browser to hit the file from outside.

That’s how you can easily use tar/untar commands in a PHP script.

Also See: Blacklist/Whitelist IPs in Imunify360

That’s it.

I hope this tutorial helps you. If not, you can contact us on Facebook or Twitter for any queries or suggestions. Other than this, if you’re looking for a Web Hosting Provider but running out of money, I’ll recommend you to check  as we’re providing one of the Cheapest Web Hosting services across the internet with all the premium features that you would be getting at a much higher cost.

Scroll to Top