Setting the PHP version on the command line on CPanel Servers

Setting the PHP version on the command line on CPanel Servers

Over the last couple of days, I've been struggling to try to get Composer to install a project on a CPanel server. As you know, Composer can also check environment constraints and for some reason php -v would show PHP 7.4 but when running composer install or php composer install, it would say that I'm using PHP 5.6.

I tried using the full path to both PHP and Composer (to find that out, you can use which php and which composer) with no luck. After a bit of research, I finally found that EasyApache 4 (the system that manages Apache and PHP on CPanel servers) has the ability to pass an argument to the command allowing you to set the PHP version.

/usr/bin/php -ea_php 56 filename.php

You can replace 56 with the specific version of PHP that you want like 73 for PHP 7.3.x or 74 for PHP 7.4.x. My new command looked like this:

php -ea_php 71 /opt/cpanel/composer/bin/composer install

It's important to run Composer with the version of PHP that your web server is running as it will install the dependencies with those constraints in mind.

If you don't want to worry about managing servers then have a look at Kinsta.