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:
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 -
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.
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 -
Next, ensure that you’re running as the user with - whoami.
Lastly, execute this script -
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.
Step-by-Step Process
Here are the steps involved:
- Access the Server using SSH
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
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
- 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
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
So that’s how run PHP commands using the terminal. For more assistance, get in touch with the customer support.