Here’s How You can Run PHP Commands Using The Terminal

Here’s How You can Run PHP Commands Using The Terminal

If you’re troubleshooting scripts, you may find it almost inevitable to run PHP commands via the terminal or the command-line. If it’s your first time doing so, this article is for you. Learn how to run PHP commands using command-line to configure Cron jobs.

Step-by-Step Process

Here are the steps involved:
  • Access the Server using SSH
The first thing you need to do is access the server using the SSH. It’s different for WHM administrator and a cPanel User.

If you’re an administrator, you’d have to first log in to your server using SSH as the root user. Then, you need to change to another user using this command -

Code:
su - $username -s /bin/bash
Make sure to replace “$username” with the name of the user who is the owner of the script.

If you’re a cPanel user, first ensure that you have shell access. To confirm, look for the Terminal app within your cPanel dashboard. If you see it, you have shell access. Now, open the app and move on to the next step.
  • Determine your PHP Version
Next, you need to determine the current PHP version you’re using. Here are the methods to use:
  • On WHM, you can open MultiPHP Manager and by navigating into “WHM >> Home >> Software >> MultiPHP Manager”. You’ll find the current PHP version of the website
  • If you’re on cPanel, open the MultiPHP Manager under the Software section
  • You can navigate into the /home/username/public_html directory and run this command - “cat .htaccess”. Look for the line that says # Set the “ea-php73” package as the default “PHP” programming language. As you can see 7.3 is the PHP version.
  • Locate PHP binary

As the next step, you have to locate the PHP binary. For those using the EasyApache “ea” variants of PHP, you can locate it at - “/opt/cpanel/ea-phpXY/root/usr/bin/php”. Replace XY with the PHP version.

For this running CloudLinux “alt” versions of PHP, you can locate at -
Code:
/opt/cloudlinux/alt-phpXY/root/usr/bin/php
  • Execute the script
Before executing the script, you have to make sure that you’re on the same directory as the script. So you need to need to go into - cd /home/$user/$path. Replace $user with the username who is the owner of the script and $path with the path of the directory.

Next, ensure that you’re running as the user with - whoami.

Lastly, execute this script -

Code:
/opt/$vendor/xx-phpXY/root/usr/bin/php ./myscript.php
Replace myscript.php with the actual name of the script.

So that’s how run PHP commands using the terminal. For more assistance, get in touch with the customer support.
Author
kumkumsharma
Views
4,796
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top