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?
- First of all, log into your cPanel account.
- Navigate to your root directory (public_html) and create a new File.
- Type any suitable name with the .php extension, and hit the Create New File button.
- Open the newly created PHP file by clicking over the Edit button.
- Once the editor window is opened, copy the below code and paste it there.
<?php $output = shell_exec('df -h'); echo "<pre>$output</pre>"; ?>
- After pasting the above command hit the Save Changes button and then close the window.
- 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.
- 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.
- Save the file and again hit the file from outside.
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.
You’ll see a blank page once you hit the file from outside.
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.
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.
- Again, hit the file from outside to execute the command.
- Once done, navigate to the directory where the PHP script is located, and find the tar file.
- 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.
- Open file from outside. For example, if the PHP file named “test.php“ is present inside your root directory and your domain name is “example.com“ then type “www.example.com/test.php” 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
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 Redserverhost 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.