When troubleshooting scripts, sometimes, it is necessary to run PHP scripts via the command-line. This information is also useful when configuring Cron Jobs.
Access the server via SSH
If you are a WHM Administrator, log into 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:
- 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.
- 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:
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
# 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:
For CloudLinux "alt" variants of PHP, replace the XX with the version number found in the previous step:
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
Next, ensure you are running as the user
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