PHP Webdriver Chrome not starting and loading for very long time

I have installed php-webdriver/webdriver version “1.7.*” through composer and trying to start chrome driver on amazon ec2 server.

use FacebookWebDriverChromeChromeDriver;
use FacebookWebDriverChromeChromeOptions;
use FacebookWebDriverRemoteDesiredCapabilities;

putenv('webdriver.chrome.driver=/path/to/chromedriver')

$desiredCapabilities = DesiredCapabilities::chrome();
$desiredCapabilities->setCapability('acceptSslCerts', false); //Disable accepting SSL certificates
$userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36';
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments(
    array_merge([
        '--headless',
        '--disable-gpu',
        '--no-sandbox',
        'user-agent=' . $userAgent,
    ], $headers));
$desiredCapabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
echo "reachedn";
$driver = ChromeDriver::start($desiredCapabilities);
$driver->get('https://www.example.com');
sleep(5);
$currentURL = $driver->getCurrentURL();
echo $currentURL;

When I am running from command line its loading and working fine.

php index.php

But when I am trying to run this same file through command line internally, let’s say there is a api file api.php at base endpoint https://api.example.com and I am hitting https://api.example.com/api.php

$result = shell_exec("cd path/to/root-of-site/; php index.php");
echo $result;

Its reaching till the code echo "reachedn"; but not starting the chrome driver at all.

Really appreciate the solution and it will help other people who literally have not idea what’s happening here.