php - Running composer by shell_exec or passthru don't work
Get the solution ↓↓↓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:
- git pull (works fine);
- rm -rf vendor (works fine);
- 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";
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.