Seagyn Davis
Setting the PHP version on the command line on CPanel Servers

Setting the PHP version on the command line on CPanel Servers

Reading time of post 1 min readDate when post was published29 October 2017
Disclosure: Some of the links in this post are affiliate links, meaning that if you click on one of the links and purchase an item, I may receive a commission. All opinions however are my own and I do not accept payments for positive reviews. This is just a little way for me to monetize the time I spend writing for you.

Over the last couple of days, I've been really 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 <a href="" target="_blank">Kinsta</a>.

Kinsta WordPress hosting loves developers

The following companies make my life awesome, maybe they'll make yours awesome too?