php - Running composer by shell_exec or passthru don't work ← (PHP)

I'm trying to running an PHP code from a URL so when the URL is opened the script must to run at server (Nginx).

URL example: https://my-server.com/deploy.php

I have 3 code blocks inside this script:

  1. git pull (works fine);
  2. rm -rf vendor (works fine);
  3. composer install (not worked);

Trying to debug, I change this part of code to:

$composer = shell_exec('composer install 2>&1');
echo "$composer";

Note: before use shell_exec I try with passthru and this didn't work too.

So I got this error:

sh: 1: composer: not found

And another time this error:

[RuntimeException]
The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly

If a run the same file by SSH command, like:

php deploy.php

The code works fine and execute the composer install.

At first I thought could be the file permission, so I change to 777 and the error keeps happen.

Why the composer command didn't work when is called by URL? Anyone knows how to fix this?

Answer



Solution:

I resolve this, like Mario sad in comments adding the COMPOSER_HOME value.

So my code do like this:

$composer = shell_exec('
    export COMPOSER_HOME=/home/[user]/.composer;
    composer install 2>&1'
);
echo "$composer";

Source