如何在PHP服务器上运行composer更新?

时间:2022-02-16 23:22:00

Is there a way to run composer update command on our production/test environment?

是否有办法在我们的生产/测试环境中运行composer update命令?

Problem is that i don't have access for Command Line.

问题是我不能访问命令行。

3 个解决方案

#1


5  

Yes. there is a solution. but it could demands some server configuration... and some of these are forbidden by default due to security risks!!

是的。有一个解决方案。但它可能需要一些服务器配置……由于安全风险,其中一些是被默认禁止的!!

download composer.phar https://getcomposer.org/download/ - this is PHP Archive which can be extracted via Phar() and executed as regular library.

下载的作曲家。phar https://getcomposer.org/download/ -这是PHP存档,可以通过phar()提取并作为常规库执行。

create new php file and place it to web public folder. i.e. /public/composer.php

创建新的php文件并将其放置到web公共文件夹。即公共/ composer.php

or download at https://github.com/whipsterCZ/laravel-libraries/blob/master/public/composer.php

在https://github.com/whipsterCZ/laravel-libraries/blob/master/public/composer.php或下载


Configuration

配置

<?php

//TODO! Some Authorization - Whitelisted IP, Security tokens...


echo '<pre>
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/  UPDATE
                    /_/

';
define('ROOT_DIR',realpath('../'));
define('EXTRACT_DIRECTORY', ROOT_DIR. '/composer');
define('HOME_DIRECTORY', ROOT_DIR. '/composer/home');
define('COMPOSER_INITED', file_exists(ROOT_DIR.'/vendor'));

set_time_limit(100);
ini_set('memory_limit',-1);

if (!getenv('HOME') && !getenv('COMPOSER_HOME')) {
    putenv("COMPOSER_HOME=".HOME_DIRECTORY);
}

Extracting composer library

提取作曲家图书馆

if (file_exists(EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
    echo "Extracted autoload already exists. Skipping phar extraction as presumably it's already extracted.\n";
}
else{
    $composerPhar = new Phar("../composer.phar");
    //php.ini set phar.readonly=0
    $composerPhar->extractTo(EXTRACT_DIRECTORY);
}

running Composer Command

运行作曲家命令

// change directory to root
chdir(ROOT_DIR);

//This requires the phar to have been extracted successfully.
require_once (EXTRACT_DIRECTORY.'/vendor/autoload.php');

//Use the Composer classes
use Composer\Console\Application;
use Composer\Command\UpdateCommand;
use Symfony\Component\Console\Input\ArrayInput;


//Create the commands
$args = array('command' => 'update');
if(!COMPOSER_INITED) { 
    echo "This is first composer run: --no-scripts option is applies\n";
    $args['--no-scripts']=true; }        
}
$input = new ArrayInput($args);

//Create the application and run it with the commands
$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
try {
    //Running commdand php.ini allow_url_fopen=1 && proc_open() function available
    $application->run($input);
    echo 'Success';
} catch (\Exception $e) {
    echo 'Error: '.$e->getMessage()."\n";
}

But Better will be performing composer install, according to composer.lock which is last dependency configuration tested from local environment

但是根据composer,安装的更好。锁,这是最后一个从本地环境测试的依赖配置

only change is

只有变化是

$args = array('command' => 'install');

$args =数组('command' => 'install');

#2


1  

The best idea is to NOT run Composer commands on the production server, but outside of it. Have a deployment script - your code has to be put on the server anyway, and it shouldn't matter if you add the dependencies ON the server after you uploaded the code, or before the upload.

最好的方法不是在生产服务器上运行Composer命令,而是在它之外。有一个部署脚本——无论如何,您的代码都必须放在服务器上,而且如果您在上传代码之后或上传之前在服务器上添加依赖项,也不重要。

The workflow would be like this: Have a local machine, checkout your code from the repo, run composer install, and then upload everything to the server. That sounds like a four line script to me:

工作流程是这样的:有一台本地机器,从repo签出代码,运行composer安装,然后将所有内容上传到服务器。这听起来就像一个四行字:

git archive master | tar -x -C /deploy/application
pushd /deploy/application && composer install
popd
scp /deploy/application user@remoteserver:/srv/www/htdocs

Yes, you'd need some error handling in case something goes wrong, to stop the script from deploying a nonworking site. Also, optimizing uploads by using rsync would be a suggestion.

是的,如果出现问题,您需要进行一些错误处理,以阻止脚本部署非工作站点。此外,使用rsync优化上传也是一个建议。

#3


-2  

You can do this also: 1) Download the latest composer.phar 2) Execute a command from PHP script to do the wor using downloaded composer file

你也可以这样做:1)下载最新的作曲家。phar 2)执行来自PHP脚本的命令,使用下载的composer文件执行wor

This is a temporary solution but can work for the immediate need.

这是一个临时的解决方案,但是可以满足当前的需要。

#1


5  

Yes. there is a solution. but it could demands some server configuration... and some of these are forbidden by default due to security risks!!

是的。有一个解决方案。但它可能需要一些服务器配置……由于安全风险,其中一些是被默认禁止的!!

download composer.phar https://getcomposer.org/download/ - this is PHP Archive which can be extracted via Phar() and executed as regular library.

下载的作曲家。phar https://getcomposer.org/download/ -这是PHP存档,可以通过phar()提取并作为常规库执行。

create new php file and place it to web public folder. i.e. /public/composer.php

创建新的php文件并将其放置到web公共文件夹。即公共/ composer.php

or download at https://github.com/whipsterCZ/laravel-libraries/blob/master/public/composer.php

在https://github.com/whipsterCZ/laravel-libraries/blob/master/public/composer.php或下载


Configuration

配置

<?php

//TODO! Some Authorization - Whitelisted IP, Security tokens...


echo '<pre>
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/  UPDATE
                    /_/

';
define('ROOT_DIR',realpath('../'));
define('EXTRACT_DIRECTORY', ROOT_DIR. '/composer');
define('HOME_DIRECTORY', ROOT_DIR. '/composer/home');
define('COMPOSER_INITED', file_exists(ROOT_DIR.'/vendor'));

set_time_limit(100);
ini_set('memory_limit',-1);

if (!getenv('HOME') && !getenv('COMPOSER_HOME')) {
    putenv("COMPOSER_HOME=".HOME_DIRECTORY);
}

Extracting composer library

提取作曲家图书馆

if (file_exists(EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
    echo "Extracted autoload already exists. Skipping phar extraction as presumably it's already extracted.\n";
}
else{
    $composerPhar = new Phar("../composer.phar");
    //php.ini set phar.readonly=0
    $composerPhar->extractTo(EXTRACT_DIRECTORY);
}

running Composer Command

运行作曲家命令

// change directory to root
chdir(ROOT_DIR);

//This requires the phar to have been extracted successfully.
require_once (EXTRACT_DIRECTORY.'/vendor/autoload.php');

//Use the Composer classes
use Composer\Console\Application;
use Composer\Command\UpdateCommand;
use Symfony\Component\Console\Input\ArrayInput;


//Create the commands
$args = array('command' => 'update');
if(!COMPOSER_INITED) { 
    echo "This is first composer run: --no-scripts option is applies\n";
    $args['--no-scripts']=true; }        
}
$input = new ArrayInput($args);

//Create the application and run it with the commands
$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
try {
    //Running commdand php.ini allow_url_fopen=1 && proc_open() function available
    $application->run($input);
    echo 'Success';
} catch (\Exception $e) {
    echo 'Error: '.$e->getMessage()."\n";
}

But Better will be performing composer install, according to composer.lock which is last dependency configuration tested from local environment

但是根据composer,安装的更好。锁,这是最后一个从本地环境测试的依赖配置

only change is

只有变化是

$args = array('command' => 'install');

$args =数组('command' => 'install');

#2


1  

The best idea is to NOT run Composer commands on the production server, but outside of it. Have a deployment script - your code has to be put on the server anyway, and it shouldn't matter if you add the dependencies ON the server after you uploaded the code, or before the upload.

最好的方法不是在生产服务器上运行Composer命令,而是在它之外。有一个部署脚本——无论如何,您的代码都必须放在服务器上,而且如果您在上传代码之后或上传之前在服务器上添加依赖项,也不重要。

The workflow would be like this: Have a local machine, checkout your code from the repo, run composer install, and then upload everything to the server. That sounds like a four line script to me:

工作流程是这样的:有一台本地机器,从repo签出代码,运行composer安装,然后将所有内容上传到服务器。这听起来就像一个四行字:

git archive master | tar -x -C /deploy/application
pushd /deploy/application && composer install
popd
scp /deploy/application user@remoteserver:/srv/www/htdocs

Yes, you'd need some error handling in case something goes wrong, to stop the script from deploying a nonworking site. Also, optimizing uploads by using rsync would be a suggestion.

是的,如果出现问题,您需要进行一些错误处理,以阻止脚本部署非工作站点。此外,使用rsync优化上传也是一个建议。

#3


-2  

You can do this also: 1) Download the latest composer.phar 2) Execute a command from PHP script to do the wor using downloaded composer file

你也可以这样做:1)下载最新的作曲家。phar 2)执行来自PHP脚本的命令,使用下载的composer文件执行wor

This is a temporary solution but can work for the immediate need.

这是一个临时的解决方案,但是可以满足当前的需要。