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 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 Find : If two different date ranges intersects

I was making a Odoo module today, it had a requirement to find the products that has offering within two ranges. Each of the product has different range for discounts/offers. It is sort of like the following:

For example, a shampoo discount starts from 01-06-2020 and ends at 20-06-2020, while another product like a soap discount starts from 10-06-2020 and ends at 22-06-2020. Now if I look for discounted credit notes to apply within a range, then we need to provide some time range, that these ranges intersect. For example, if I want to find discounts that were given within 02-06-2020 – 11 – 06 – 2020, then we should get both the discounts available here, as the discounts were still available for a day to the Soap and a few days for Shampoo. That means, we need to find if any of the given two ranges intersects with the ranges we have discounts for.

The easiest way to calculate this, is to check which date is max among the lower boundaries and which date is minimum in the lower boundaries. This should follow that the starting date would be less than or equal to the ending date, but never greater than the ending date. If greater than, then it does not intersect logically. In python, we can do this like the following:

# first we find all the vendor discounts, it has property start_date and end_date
vendor_discounts = self.env['vendor.discount'].search([])

# list to keep the vendor discount instances
vendor_discount_id_in_range = []

# from the form, we get date_from and date_to, where the dates intersect
for vendor_discount in vendor_discounts:
   # here is the logic to find intersects
    if max(self.date_from, vendor_discount.start_date) <= min(self.date_to, vendor_discount.end_date):
        vendor_discount_id_in_range.append(vendor_discount.id)

So, the logic going to be like the following:

if max(input_date_from, start_date_to_match) <= min(input_date_to, end_date_to_match)

How to Call a Controller Method from Tinker – Laravel

LARAVEL TINKER

If you love to debug and test things in an app shell like me, then you are also a big fan of Tinker in laravel. Tinker is the shell prompt for Laravel and can be used to test and run different commands in php inside the app. You may run the following to hop into the tinker shell in a laravel environment:

php artisan tinker

Once you hop in the tinker, you can call any model or run any php command from the shell.

HOW TO RUN CONTROLLER METHOD FROM TINKER

There are times, you might feel more interest into evaluating a large controller method. To run a controller method, we first need to enter the service container of laravel. Laravel providers a helper method called ‘app()’ to enter the service container. It can then use a method called ‘call’ to access and execute a method inside a controller namespace, like the following:

app()->call('App\Http\Controllers\AdminControllers@yourmethod');

Repace your controller name and the method name after @. One thing, you need to realize is that the method ‘call’ takes the method reference, not the function itself. That means, you can not add brackets () at the end of method name while giving it in the call method.

HOW TO PASS PARAMETERS TO CONTROLLER FROM TINKER

As discussed earlier, you are passing reference only, not the function, hence you can not pass parameters like we usually do in methods/functions. We need to pass this as an argument in array.

Here is a more constructive way to do this:

# let's make an instance of controller first, can be done using make method of service container
$controller = app()->make('App\Http\Controllers\AdminControllers');

# now let's call the method, inside the container, method name is 'getNewsByCatId'
app()->call([$controller, 'getNewsByCatId']);

# pass a parameter called id = 5
app()->call([$controller, 'getNewsByCatId'], ['id' => 5]);

Can You Use Elasticsearch as a Primary Database?

Well, this is an interesting topic. Earlier today, I answered the same question in a Elasticsearch Community Group in Facebook, thought to keep this documented as well.

Primarily, if you are aware of how Elasticsearch is storing data (The document like), you might think, it is a full fledged NoSQL database, but you need to know, it is not. If you have looked at several NoSQL databases, you might already be thinking, there is probably no standard definition of NoSQL databases, which is partially true, again, there still is definition of database management systems, where it doesn’t fit. Does that mean, we can’t use ES as the primary data store? No, that’s doesn’t mean that. You certainly can, but there are use cases. Let’s look at couple of points before we conclude.

Database Transactions

ES has no transaction. Even the algorithm that ES implements, Lucene Search, which has transaction in the original design, but ES doesn’t have any transaction. When there is no transaction, the first thing you need to remember, it has no rollback facility. That also means, every operation is atomic by design, and there is no way to cancel, abort or revert them. This also means, you have no locking standard in the system. If you are doing parallel writes, you need to be very careful about the writes here because ES is not giving you any guarantee to parallel writes.

ES Caching Strategy

Elasticsearch is a ‘near real’ time search engine. It is not a ‘real’ time search engine. It has a caching standard, that refreshes every 1 second. This is ‘by design’ of Elasticsearch. So, what’s the problem with it? The problem is, you may get tricked by the ES cache if you are immediately asking for a read for a quick write operation and it is within 1 second. But as most of the cases, ES is not written much often (Since most of the people do not use it as primary storage), most of the people thinks it is real time. But it is not. It’s cache can trick you in intense cases, where writes won’t be immediately available to you for read.

User Management

ES user management is no where near to a full fledged DBMS, only because ES doesn’t need to. They have never announced themselves as a NoSQL DBMS, hence they do not require to add the functionality in full square. They only implements the facilities that ES Ecosystems require.

So … ?

So, what does all these mean to you? If you have an extremely large read featured application, where updates are very rarely or occassionaly done, then you can definitely go with ES as your primary document storage engine. But if you have a write oriented application, requires complex user management or parallel threads require write access, then you better be choosing a standard DBMS for your business and use ES as secondary data store for your analytics and searches.

How to Use Sticky Session for CSRF submission on Highly Scalable Cloud App in Haproxy

HINT: If you are a nginx fan and used it in mass scale, then, you must have done this using ip_hash (Nginx Documentation). It follows the same purpose for Haproxy. Difference and benefits of using Haproxy over Nginx for L7 proxy in a highly scalable and reliable cloud app would be a discussion for another day.

Case Discussion:

Suppose, you have a Cloud app, that is load balanced & scaled between multiple servers using Haproxy, for example:

101.101.101.101
202.202.202.202
303.303.303.303

Now, if your app has a submission form, for example, a poll submission from your users, then, there is an issue in this Haproxy setup.

Let’s say, an User A, requests for the app, and gets the data from the server 101.101.101.101, the CSRF token he gets for the poll submission to his browser, also maintains the app hosted on 101.101.101.101. But when he press the submit button, HAProxy puts him on 202.202.202.202 app, and the app hosted on 202.202.202.202 instantly rejects the token for the session as the session is not registered for that app. For such cases, we need to maintain a ‘Sticky’ session based on the cookie set by the right server. That means, if the cookie is set by 101.101.101.101, HAproxy should obey and give the user 101.101.101.101 until the cookie or the session is reset or regenerated.

How To Do That:

What we need to do, let haproxy write the server id in the cookie, and make the directive ‘server’ to follow the cookie. Please remember, there are couple of other way to achieve this. There is another way of doing this is called ‘IP Affinity’, where you make sticky session based on IP of the user. There is another based on PHP session value. Setting sticky session based on php session should also work. I preferred the cookie based sticky session, just on random selection.

So, to write the server id in the cookie, you need to add the following in the haproxy ‘backend’ directive as following:

backend app-main
balance roundrobin
cookie SERVERID insert indirect nocache

In the cookie directive, you can see, we are taking the HAProxy variable ‘SERVERID’ and inserting that to the cookie attribute. Now, all you need to do, is to configure your balancing IPs to follow the cookie, like the following:

backend app-main
balance roundrobin
cookie SERVERID insert indirect nocache
server nginx1 101.101.101.101 cookie S1
server nginx2 202.202.202.202 cookie S2
server nginx3 303.303.303.303 cookie S3

S1, S2, S3 are just 3 different names of the cookies for the specific servers. After the above is done, you can now restart and see Haproxy is following stickiness based on the session you have.

Just to find out, how to test if you are using laravel, try to regenerate the session based on the session() helper method as following:

session()->regenerate()->csrf_token();

You should be able to see the content loading from different web servers when the session regenerates. But it will persists when the regenerate session method is not called.

TDD: Date Assertions – Laravel 7, PHP 7.* – Carbon 2 – Changes

If you follow a TDD approach to develop your software, and also a Laravel user, working with a JSON API, you might have experienced some date assertion issues while asserting Json with Laravel 7. Previously, when Laravel was using Carbon 1 for date management, it would not return the whole Carbon object for assertJson. Which is why, the following would work in a sample PHPUnit Test:

$contact = factory(Contact::class)->create();
$response = $this->get('/api/contacts' . $contact->id);

$response->assertJson([
'name' => $contact->name,
'email' => $contact->email,
'meeting_date' => $contact->meeting_date,
'company' => $contact->company,
]);

But as of now, Carbon 2, returns the whole Carbon object for $contact->meeting_date here, the assertion will fair, because the $response, didn’t get a Carbon object, instead a Json string here.

If you go through the Carbon documentation here, you can see the following under the section ‘Migrate to Carbon 2’:

$date->jsonSerialize() and json_encode($date) no longer returns arrays but simple strings: "2017-06-27T13:14:15.000000Z". This allows to create dates from it easier in JavaScript. 

This is basically what you need to use if you are on Laravel 7 with Carbon 2. You need to serialize the data as Laravel is not doing it for you automatically here to fix this up. Just change the ‘meeting_date’ to the following:

'meeting_date' => $contact->meeting_date->jsonSerialize(),

and this should let your assertion pass.

Remember, you might not need to explicitly do this in your controller return until you are following a TDD based development where an assertion to pass is important to continue the process. Laravel resource would implicitly serialize your data before returning them from controller.