When troubleshooting scripts, sometimes, it is necessary to run PHP scripts via the command-line. This information is also useful when configuring Cron Jobs.
Procedure
Access the server via SSH
If you're a WHM Administrator
First, log in to the server via SSH as root, then switch to the user with the following command, being sure to replace $username with the username that owns the script you are trying to execute
su - $username -s /bin/bash
If you are a cPanel user
First, ensure you have shell access. The Terminal app in cPanel will be available if you have shell access. If you do not have the Terminal app in cPanel, your hosting provider will need to enable it. Then, log in via SSH as your cPanel user or open the Terminal app in cPanel.
Determine a user’s PHP version
You can determine a user’s PHP version with one of the following methods:
- Use WHM’s MultiPHP Manager interface (WHM >> Home >> Software >> MultiPHP Manager). This interface allows you to see and change a user’s default PHP version. This is the easiest method to determine a user’s PHP version.
- Use cPanel’s MultiPHP Manager interface (cPanel >> Home >> Software >> MultiPHP Manager).
- Navigate to the /home/username/public_html directory, where "username" represents the user’s username. Run the following command on the command line:
cat .htaccess
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
- In the above example, # Set the “ea-php73” package as the default indicates that the site uses the cPanel provided EasyApache PHP 7.3.
- Use WHM API 1’s php_get_vhost_versions function.
- Use UAPI’s LangPHP::php_get_vhost_versions function.
Locate the PHP binary
For EasyApache "ea" variants of PHP, replace the XX with the version number found in the previous step:
/opt/cpanel/ea-phpXX/root/usr/bin/php
For CloudLinux "alt" variants of PHP, replace the XX with the version number found in the previous step:
/opt/cloudlinux/alt-phpXX/root/usr/bin/php
Execute the script
First, ensure you are in the same directory as the script. Replace $user with the username of the user who owns the script, and $path with the actual path to the directory containing the file
cd /home/$user/$path
Next, ensure you are running as the user
whoami
Then, execute the script, replacing the path to the PHP binary with the one found in the previous section and replacing "script.php" with the name of the script
/opt/$vendor/xx-phpXX/root/usr/bin/php ./script.php
Comments
0 comments
Article is closed for comments.