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.
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 thewget 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 directoryand 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.
I hope this tutorial helps you. If not, you can contact us on Facebookor Twitterfor 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 Redserverhostas 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.