How to run composer with different PHP versions in Cpanel?

Question:

When we try to run a composer command, like update, we usually do the following:

composer update

Cpanel has multiple PHP binaries, but in this case, we are unable to select a specific PHP binary to use, instead we have to run it with the default one, how to run composer update with a different php binary in cpanel?

Solution

composer binary file, is a phar file. PHAR is necessarily a PHP Archive and usually automatically detect the running php. But as it is essentially written in php, you may explicitly run it with a different php binary, if you want. To run composer with different php binary, first, you need to find the location of composer. You may do so, using the following:

root@mirage [~]# which composer
/opt/cpanel/composer/bin/composer

Cpanel different php binaries are available under the following kind of directory:

/opt/cpanel/ea-phpXX/usr/bin/php

XX is the version number of PHP. So for example if you need to use PHP 7.4, you would need to run using the following:

/opt/cpanel/ea-php74/usr/bin/php

Now, to run composer update along with PHP 7.4 binary, you may do something like the following:

/opt/cpanel/ea-php74/usr/bin/php /opt/cpanel/composer/bin/composer update

First, make sure you are in the directory where you want to install laravel, for example, something like the following:

cd /home/username/public_html

Then, you may run the above command:

/opt/cpanel/ea-php74/usr/bin/php /opt/cpanel/composer/bin/composer update
or in case, you want to to install
/opt/cpanel/ea-php74/usr/bin/php /opt/cpanel/composer/bin/composer install
or may be, you wan to run update with no-scripts
/opt/cpanel/ea-php74/usr/bin/php /opt/cpanel/composer/bin/composer update --no-scripts

Uncaught ErrorException: require(/home/username/public_html/vendor/composer/../../app/Helpers/helper.php): failed to open stream: No such file or directory in /home/username/public_html/vendor/composer/autoload_real.php:71

Error Details

While trying to run any of the following with Laravel composer installer, you see an error similar to the following:

Uncaught ErrorException: require(/home/username/public_html/vendor/composer/../../app/Helpers/helper.php): failed to open stream: No such file or directory in /home/username/public_html/vendor/composer/autoload_real.php:71

How to fix this?

Solution

The error is appearing, most likely you forgot to add the ‘app’ directory of laravel in your root directory. Make sure, you have the ‘app’ directory in your root directory, then run any of the following:

If this is the first time, you need all laravel packages, run:

composer install

If this is not the first time, you may run the following:

composer update

Good luck.

How to Backup PostgreSQL Database from Command Line

Question

How to Backup PostgreSQL Database from Command Line

Solution

Postgresql comes with two utilities called ‘pg_dump and ‘pgsql’. These can be used to backup and restore database.

To backup postgresql database using command line, you can use this:

pg_dump database_name > database_name.sql

This would take the backup and store it in .sql file.

To restore postgresql (.sql) file to a database using command line, you can use this:

pgsql database_name < database_name.sql

Error: Package: 1:ea-php74-php-cli-7.4.15-1.el7.cloudlinux.x86_64 (cl-ea4) – Requires: ea-openssl11 = 1:1.1.1h

Error Details

The error appears when you try to run easyapache or try to install PHP 7.4 on Cloudlinux 7.9, it puts you on the following error:

Error: Package: 1:ea-php74-php-cli-7.4.15-1.el7.cloudlinux.x86_64 (cl-ea4)
Requires: ea-openssl11 = 1:1.1.1h
Installed: 1:ea-openssl11-1.1.1i-1.el7.cloudlinux.x86_64 (@cl-ea4)
ea-openssl11 = 1:1.1.1i-1.el7.cloudlinux
Available: 1:ea-openssl11-1.1.1d-1.el7.cloudlinux.x86_64 (cl-ea4)
ea-openssl11 = 1:1.1.1d-1.el7.cloudlinux
Available: 1:ea-openssl11-1.1.1f-1.el7.cloudlinux.x86_64 (cl-ea4)
ea-openssl11 = 1:1.1.1f-1.el7.cloudlinux
Available: 1:ea-openssl11-1.1.1g-1.el7.cloudlinux.x86_64 (cl-ea4)
ea-openssl11 = 1:1.1.1g-1.el7.cloudlinux
Available: 1:ea-openssl11-1.1.1h-1.el7.cloudlinux.x86_64 (cl-ea4)
ea-openssl11 = 1:1.1.1h-1.el7.cloudlinux

Error Fix:

It is trying to look into a sub-sub version of openssl, which is 1h for PHP 7.4, but 1i is installed by default which is not an exact match with 1h, hence the error is appearing. To fix the problem, you can not just run yum update on openssl and fix this as you technically already have the latest version. To work with this, you need to first uninstall the ea-openssl rpm and install the require version. You may do this in the following way:

[root@stack4 yum.repos.d]# rpm -qa|grep ea-openssl
ea-openssl11-devel-1.1.1i-1.el7.cloudlinux.x86_64
ea-openssl-1.0.2u-1.el7.cloudlinux.x86_64
ea-openssl11-1.1.1i-1.el7.cloudlinux.x86_64
[root@stack4 yum.repos.d]# yum install ea-openssl11-1.1.1h-1.el7.cloudlinux.x86_64
Loaded plugins: fastestmirror, rhnplugin, universal-hooks
This system is receiving updates from CLN.
Loading mirror speeds from cached hostfile
 * cpanel-plugins: 185.125.185.32
 * cloudlinux-x86_64-server-7: xmlrpc.cln.cloudlinux.com
Package matching 1:ea-openssl11-1.1.1h-1.el7.cloudlinux.x86_64 already installed. Checking for update.
Nothing to do

Fix it with the following:

[root@stack4 yum.repos.d]# rpm -e --nodeps ea-openssl11-1.1.1i-1.el7.cloudlinux.x86_64
[root@stack4 yum.repos.d]# yum install ea-openssl11-1.1.1h-1.el7.cloudlinux.x86_64

Remember to use rpm uninstaller instead of yum remove, as yum also removes the dependencies, but with rpm you can skip the dependency uninstallation and patch it the above way.

How to Fix: Facebook\WebDriver\Exception\UnknownErrorException: unknown error: cannot find Chrome binary – Laravel Dusk

When run the following command in your Laravel Automation Tool Dusk:

php artisan dusk

You get something like the following error:

[elastic-keldysh@pl1 blog]$ php artisan dusk
PHPUnit 9.3.11 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:00.339, Memory: 18.00 MB

There was 1 error:

1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\UnknownErrorException: unknown error: cannot find Chrome binary

/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:139
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:370
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:136
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/tests/DuskTestCase.php:40
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:200
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/framework/src/Illuminate/Support/helpers.php:234
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:201
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:95
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:65
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/tests/Browser/ExampleTest.php:21

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

The error actually appears because you do not have Chrome browser installed on your system. Now, exit from your user shell, and drop back to the root shell. From the root shell, if you are using CentOS 7, you may run the following:

First Download the Latest Chrome RPM:

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

Run it with YUM:

yum install ./google-chrome-stable_current_*.rpm

If you are on CentOS 8, you may use dnf installer as following:

dnf localinstall google-chrome-stable_current_x86_64.rpm

Once the installation is done, you may now go back to the user shell and run laravel dusk:

[root@pl1 lib64]# su - elastic-keldysh
Last login: Thu Oct  1 15:39:18 IST 2020 on pts/0
[elastic-keldysh@pl1 ~]$ php artisan dusk
Could not open input file: artisan
[elastic-keldysh@pl1 ~]$ cd blog
[elastic-keldysh@pl1 blog]$ php artisan dusk
PHPUnit 9.3.11 by Sebastian Bergmann and contributors.

R                                                                   1 / 1 (100%)

Time: 00:02.367, Memory: 18.00 MB

There was 1 risky test:

1) Tests\Browser\ExampleTest::testBasicExample
This test did not perform any assertions

/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/tests/Browser/ExampleTest.php:16

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.

It shall work now. Hope this helps.

How to Fix : Failed to connect to localhost port 9515: Connection refused

If you are looking at this post, chances high that, you are trying to run laravel dusk and seeing the following kind of error:

[elastic-keldysh@pl1 blog]$ php artisan dusk
PHPUnit 9.3.11 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:00.331, Memory: 18.00 MB

There was 1 error:

1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"binary":"","args":["--disable-gpu","--headless"]}}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--disable-gpu","--headless"]}}}

Failed to connect to localhost port 9515: Connection refused

/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:331
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:136
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/tests/DuskTestCase.php:40
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:200
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/framework/src/Illuminate/Support/helpers.php:234
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:201
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:95
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:65
/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/blog/tests/Browser/ExampleTest.php:21

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Chorme/Chromium browser runs it’s on 9515 port and laravel dusk tries to connect to it through the Browser binary, but failing to do so. To resolve the case, we first, need to check if chromedriver for linux can run or not.

First, browse your laravel root directory, in my case, it’s the blog directory and then run the chromedriver for linux as following:

cd blog
vendor/laravel/dusk/bin/chromedriver-linux

This should return something like the following if the driver able to load in memory:

[elastic-keldysh@pl1 blog]$ vendor/laravel/dusk/bin/chromedriver-linux
Starting ChromeDriver 85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

But if it can not be loaded, it will return some kind of error like the following:

[elastic-keldysh@pl1 blog]$ vendor/laravel/dusk/bin/chromedriver-linux
vendor/laravel/dusk/bin/chromedriver-linux: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

As the error suggests, it is failing to access the library called libX11-xcb. To install it in CentOS, you may run the following from root shell:

[root@pl1 lib64]# yum install libX11-xcb*

Once you see the following:

Installed:
  libX11-xcb-1.6.8-3.el8.x86_64

Complete!

You may now, go back to the user shell, and run the chromedriver-linux again:

[root@pl1 lib64]# su - elastic-keldysh
Last login: Thu Oct  1 15:28:29 IST 2020 on pts/0
[elastic-keldysh@pl1 ~]$ cd blog
[elastic-keldysh@pl1 blog]$ vendor/laravel/dusk/bin/chromedriver-linux
Starting ChromeDriver 85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

Now, you may run the following to start using the laravel dusk:

php artisan dusk

After you are done resolving the 9515 error, you might also have to face another error with Chrome Binary, have a look at the following on how to resolve the error:

How to Install Laravel in Plesk using SSH

In plesk, to install laravel, we need to do two prerequisites first.

  1. Enable SSH for the Plesk User:

2. Add default PHP binary to Plesk Shell to allow using Composer:

Once the above steps are done, now, you may install the laravel installer with the following:

[elastic-keldysh@pl1 ~]$ composer global require laravel/installer
Changed current directory to /var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/.composer
Using version ^4.0 for laravel/installer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 12 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-php80 (v1.18.1): Downloading (100%)
  - Installing symfony/process (v5.1.6): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.18.1): Downloading (100%)
  - Installing symfony/polyfill-intl-normalizer (v1.18.1): Downloading (100%)
  - Installing symfony/polyfill-intl-grapheme (v1.18.1): Downloading (100%)
  - Installing symfony/polyfill-ctype (v1.18.1): Downloading (100%)
  - Installing symfony/string (v5.1.6): Downloading (100%)
  - Installing psr/container (1.0.0): Downloading (100%)
  - Installing symfony/service-contracts (v2.2.0): Downloading (100%)
  - Installing symfony/polyfill-php73 (v1.18.1): Downloading (100%)
  - Installing symfony/console (v5.1.6): Downloading (100%)
  - Installing laravel/installer (v4.0.5): Downloading (100%)
symfony/service-contracts suggests installing symfony/service-implementation
symfony/console suggests installing symfony/event-dispatcher
symfony/console suggests installing symfony/lock
symfony/console suggests installing psr/log (For using the console logger)
Writing lock file
Generating autoload files
10 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
[elastic-keldysh@pl1 ~]$

Once this is done, you may try to install laravel using the laravel command:

[elastic-keldysh@pl1 ~]$ laravel new blog
-bash: laravel: command not found

But, as you can see it is failed. It’s because the laravel binary is installed in the following path:

/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/.composer/vendor/bin

which doesn’t exist in our $PATH variable. Now you may add the above command to your PATH variable using the following tutorial:

I believe, you have already figured it out, it is as simple as the following command:

PATH=$PATH:/var/www/vhosts/elastic-keldysh.139-99-24-82.plesk.page/.composer/vendor/bin

Now, you may run the following to install laravel:

[elastic-keldysh@pl1 ~]$ laravel new blog

 _                               _
| |                             | |
| |     __ _ _ __ __ ___   _____| |
| |    / _` | '__/ _` \ \ / / _ \ |
| |___| (_| | | | (_| |\ V /  __/ |
|______\__,_|_|  \__,_| \_/ \___|_|

Creating a "laravel/laravel" project at "./blog"
Installing laravel/laravel (v8.0.3)
  - Installing laravel/laravel (v8.0.3): Downloading (100%)

So, yeah, as laravel says, Application ready! Build something amazing.

How to run Composer in Plesk User Shell / Plesk PHP

There are couple of ways you can run composer with Plesk Shell. My favorite one is to add php to your PATH variable, and it will automatically add the composer as well. You may follow through the following to modify your shell path variable to use Plesk PHP:

Once done, now you may run composer command and it shall work:

[elastic-keldysh@pl1 ~]$ composer -V
Composer version 1.10.5 2020-04-10 11:44:22
[elastic-keldysh@pl1 ~]$

The other way, is to directly use the composer.phar given from Plesk 9.0 library. This file is available under the following location:

/usr/lib64/plesk-9.0/composer.phar

So, you may run this using the following:

[elastic-keldysh@pl1 ~]$ /opt/plesk/php/7.4/bin/php /usr/lib64/plesk-9.0/composer.phar -V
Composer version 1.10.13 2020-09-09 11:46:34

This should work too. You may choose any, and it shall work for you. Good luck.

How to Update PATH Variable in Linux

A PATH variable is a system variable that stores the information about the binary files location that you may run for commands. When you log in as an user, or use a custom control panel like Plesk/Cyberpanel/Cpanel, you might want to add some custom paths as a user to take binary commands. One of the example, could be to change the default php path, or a laravel command location from vendor folder. To do this, you need to extend/update the PATH variable for a specific user.

PATH variable extends with the “:”. If you type the following, in your shell, you may see the existing paths in the PATH variable:

[elastic-keldysh@pl1 ~]$ echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

Now, if I want to extend this to take the php binary available in /opt/plesk/php/7.2/bin/php, then we can extend the PATH variable using the following:

PATH=$PATH:/opt/plesk/php/7.2/bin/

Now, if you check, the PATH variable again, you can see it is added:

[elastic-keldysh@pl1 ~]$ echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/plesk/php/7.2/bin/
[elastic-keldysh@pl1 ~]$

We have successfully modified the PATH variable, but only for the existing session. If you want to persist the changes, then, you need to add the command in .bashrc/.profile/.bash_profile file depending on your shell type and OS. You can add to either of the file and test with the following command:

[elastic-keldysh@pl1 ~]$ echo "PATH=$PATH:/opt/plesk/php/7.2/bin/" >> .profile

Replace .profile with .bashrc or .bash_profile depending on the file that works for you. You may logout and relogin, and then run the echo command again to see if the $PATH is persisting or not.

How to Fix /usr/bin/env: ‘php’: Permission denied in Plesk

If you are seeing an error like the following in your Plesk:

bash-4.4$ laravel
/usr/bin/env: ‘php’: Permission denied

Your binary to php is probably being used through redirection like .bashrc file and an alias is hooked for your php command to work. A better way to do this, is to hook the php binary to your PATH variable. You may do this and fix the error by following this tutorial:

Hope this helps. Thanks.