August, 17 2023

cPanel: Run Composer & PHP Commands Using Any Installed PHP Version — CentOS & CloudLinux

Deploying Laravel on Cpanel has been a tasking experience for most developers. One of these problems is running commands on the terminal. While most CPanel hosting provides you with a terminal, the experience can be difficult especially when you start upgrading your Laravel application.

As a Laravel developer, you will get to change your PHP version and run composer commands to get your app up to date. I assume that's why you are still reading this so let's jump in.

CentOS & Almalinux (cPanel)

If your CPanel is deployed on a Linux server with CentOS, then your versions of PHP installation can be found inside the /opt/cpanel/ directory. Also, your composer installation can be located inside the same directory.

To run composer commands with PHP8.1;

cd your/project/directory

Install Command

/opt/cpanel/ea-php81/root/usr/bin/php /opt/cpanel/composer/bin/composer install

Update Command

/opt/cpanel/ea-php81/root/usr/bin/php /opt/cpanel/composer/bin/composer update

Artisan Command

/opt/cpanel/ea-php81/root/usr/bin/php artisan inspire

That's all you need to know.

CloudLinux (cPanel)

For CouldLinux OS, I haven't fully figured it out because it doesn't have out-of-the-box support for multi-PHP on domains & directories.

From my discovery, PHP installations can be found inside the /opt/alt/ directory while the Composer installation can be found inside the /opt/cpanel/ directory.

The difference I noticed is that the default php.ini configuration necessary for most PHP extensions was not enabled for the PHP version. The php.ini configuration file that has these extensions enabled can be found inside the /opt/alt/default_php_ini directory.

To run PHP we need to tell it use any of the configurations inside this directory.

To run composer commands with PHP8.1, change to your project directory:

cd your/project/directory

Install Command

/opt/alt/php81/usr/bin/php -c /opt/alt/default_php_ini/php81.ini /opt/cpanel/composer/bin/composer install

Update Command

/opt/alt/php81/usr/bin/php -c /opt/alt/default_php_ini/php81.ini /opt/cpanel/composer/bin/composer update

Artisan Command

/opt/alt/php81/usr/bin/php -c /opt/alt/default_php_ini/php81.ini artisan inspire

That's all.

AlmaLinux

I am hoping to upgrade the Litehost server from CentOS to AlmaLinux by the end of the year. Hopefully, that will allow me to see what is obtainable from the OS.

My advice is to try the same approach for CloudLinux and see what is obtainable.

Drop your comment, and we can have a chat about this issue and possible solutions.

Join my inner circle newsletter

Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here

Hey, have you tried Litehost lately ?

Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now