Linux环境下搭建composer私服及内存限制的问题
在安装私服之前,我们需要先熟悉一下composer。安装
composer
Composer 是一个用于管理 PHP 项目中依赖关系的工具。这样您就可以声明您的项目所依赖的代码库,然后在项目目录(默认的vendor目录)中安装关联的依赖包。下载地址https://getcomposer.org/download/
# download 这里是下载 1.10.26 版本
# 当然你可以修改版本号,现在想要的版本
wget https://getcomposer.org/download/1.10.26/composer.phar
# rename
mv composer.phar /usr/local/bin/composer1.10.26
ln -s /usr/local/bin/composer1.10.26 /usr/local/bin/composer
# verify
# 这里需要注意,服务器上要安装有 PHP 程序,且有 php 命令才行
# 有时候安装PHP时候,会生成 类似 php-5.6 这样的命令, 没有默认的php命令
# 那么 做个软连接即可
# ln -s /bin/php5.6 /bin/php
/usr/local/bin/composer --version
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer version 1.10.26 2022-04-13 16:39:56常见用途有:
- •composer init初始化composer.json install
- •composer install安装相关依赖到composer.json•composer update为强更新,忽略锁文件composer.lock,更新composer.json中的依赖,然后创建新的锁文件
- •composer自我更新版本号更新composer自己的版本
- •构建composer-项目依赖包版本/依赖包路径,例如
composer作曲家项目源/项目目标目录1.0 - •composer配置主要用于设置存储库等composer相关的源地址。 composer 配置 -g secure -http false
composer 配置 -g repo.my_repocomposer https://mirrors.aliyun.com/composer/ - • 请求composer帮助 command-name 指定命令,非常有用
composer搭建私服
推荐两种私服搭建方案
- •周六https://getcomposer.org/doc/articles/handling-private-packages.md
- •Toran代理https ://toranproxy.com/
这里介绍Satis
1的安装和部署。创建一个项目
[php@test-liuchao-01-vm /data/project/temp Mon Apr 17 16:40:36]$ /usr/local/bin/composer create-project composer/satis --stability=dev --keep-vcs
Creating a "composer/satis" project at "./satis"
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Info from https://repo.packagist.org: #StandWithUkraine
Installing composer/satis (1.0.0)
- Installing composer/satis (1.0.0): Cloning f66ff72ce4
Created project in /data/project/temp/satis
Deprecation warning: require-dev.mikey179/vfsStream is invalid, it should not contain uppercase characters. Please use mikey179/vfsstream instead. Make sure you fix this as Composer 2.0 will error.
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 42 installs, 0 updates, 0 removals
- Installing symfony/process (v3.2.8): Downloading (100%)
- Installing symfony/finder (v3.2.8): Downloading (100%)
- Installing symfony/filesystem (v3.2.8): Downloading (100%)
- Installing symfony/polyfill-mbstring (v1.3.0): Downloading (100%)
- Installing psr/log (1.0.2): Downloading (100%)
- Installing symfony/debug (v3.2.8): Downloading (100%)
- Installing symfony/console (v3.2.8): Downloading (100%)
- Installing seld/phar-utils (1.0.1): Downloading (100%)
- Installing seld/jsonlint (1.6.0): Downloading (100%)
- Installing seld/cli-prompt (1.0.3): Downloading (100%)
- Installing justinrainbow/json-schema (5.2.1): Downloading (100%)
- Installing composer/spdx-licenses (1.1.6): Downloading (100%)
- Installing composer/semver (1.4.2): Downloading (100%)
- Installing composer/ca-bundle (1.0.7): Downloading (100%)
- Installing composer/composer (1.4.2): Downloading (100%)
- Installing twig/twig (v1.33.2): Downloading (100%)
- Installing mikey179/vfsstream (v1.6.4): Downloading (100%)
- Installing webmozart/assert (1.2.0): Downloading (100%)
- Installing phpdocumentor/reflection-common (1.0): Downloading (100%)
- Installing phpdocumentor/type-resolver (0.2.1): Downloading (100%)
- Installing phpdocumentor/reflection-docblock (3.1.1): Downloading (100%)
- Installing phpunit/php-token-stream (1.4.11): Downloading (100%)
- Installing symfony/yaml (v3.2.8): Downloading (100%)
- Installing sebastian/version (2.0.1): Downloading (100%)
- Installing sebastian/resource-operations (1.0.0): Downloading (100%)
- Installing sebastian/recursion-context (2.0.0): Downloading (100%)
- Installing sebastian/object-enumerator (2.0.1): Downloading (100%)
- Installing sebastian/global-state (1.1.1): Downloading (100%)
- Installing sebastian/exporter (2.0.0): Downloading (100%)
- Installing sebastian/environment (2.0.0): Downloading (100%)
- Installing sebastian/diff (1.4.3): Downloading (100%)
- Installing sebastian/comparator (1.2.4): Downloading (100%)
- Installing phpunit/php-text-template (1.2.1): Downloading (100%)
- Installing doctrine/instantiator (1.0.5): Downloading (100%)
- Installing phpunit/phpunit-mock-objects (3.4.3): Downloading (100%)
- Installing phpunit/php-timer (1.0.9): Downloading (100%)
- Installing phpunit/php-file-iterator (1.4.2): Downloading (100%)
- Installing sebastian/code-unit-reverse-lookup (1.0.1): Downloading (100%)
- Installing phpunit/php-code-coverage (4.0.8): Downloading (100%)
- Installing phpspec/prophecy (v1.7.0): Downloading (100%)
- Installing myclabs/deep-copy (1.6.1): Downloading (100%)
- Installing phpunit/phpunit (5.7.20): Downloading (100%)
symfony/console suggests installing symfony/event-dispatcher
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.5.1)
phpunit/phpunit suggests installing ext-xdebug (*)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Generating autoload files2。配置 satis.json
{
"name": "demo/repository",
"homepage": "http://www.colinspace.com",
"repositories": [
{"type": "git", "url": "ssh://git@gitlab.xxxx.com:22/module/staff.git"}
],
"require-all": true,
"require-dependencies": true,
"require-dev-dependencies": true
}3。施工
[php@test-liuchao-01-vm /data/project/temp/satis Mon Apr 17 17:26:07]$ php bin/satis build satis.json dist
Scanning packages
wrote packages to dist/include/all$83d09112b76234b329306152ed4f1f88a9f98baf.json
Writing packages.json
Pruning include directories
Writing web view4。配置NGINX
在配置NGINX之前,请确保在各自的主机上安装了PHP和php-fpm,然后运行php-fpm
server {
include local_port.conf;
server_name composer.xxx.com ;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.3.17:80;
}
access_log /data/logs/nginx/access/curr/composer.xxxx.com.log;
error_log /data/logs/nginx/nginx_error_composer.xxxx.com.log;
}。然后您就可以访问并使用号码
image-209678145171633 http://composer.xxx.com 5,在PHP项目的更新目录下设置并使用composer.json文件
{
"name": "project-name",
... ...
"repositories": [{
"type": "composer",
"url": "http://composer.kongfz.com"
}],
....
}然后在项目发布过程中出现了问题
内部composer的最后一个私有源,但项目开发过程中除了私有项目依赖之外,不可避免地会产生对公共第三方来源的依赖
因此,为了规范和方便管理,管理、运维人员按照主办方发布的内容进行操作在用户的主目录中。设置如下
[xxx]$ ls -l ~/.composer/config.json
-rw-rw-r-- 1 jenkins jenkins 253 Apr 17 15:03 /var/lib/jenkins/.composer/config.json
[xxx]$ cat ~/.composer/config.json
{
"config": {
"secure-http": false
},
"repositories": [
{
"packagist": false
},
{
"type": "composer",
"url": "https://mirrors.cloud.tencent.com/composer/"
}
]
}然后配置项目的私有composer源。
但是我遇到了一个问题:
1。开发者没有在PC个人用户主目录本地配置公共composer配置,与上面
2中Jenkins主目录中的配置类似,开发者直接从第三个配置 - 参与公共和私人资源
3,然后在发布过程中运行项目作曲家安装。报错如下
Updating dependencies (including require-dev)
PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 32 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 32 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.从错误信息来看和内存有关系,但是执行时同步监控服务器内存就足够了。
然后进行调试验证,两种解决方案
1。安装composer后,命令改为 php -d memory_limit=-1 /usr/local/bin/composer install
memory_limit 为 - 1 表示不限制内存使用
2。删除开发者在composer.json文件中添加的第三方公共源码配置。公共资源仅存储在运维时配置的服务器对应的用户主目录中。运行安装没有问题。
所以对比一下上面两种解决方案:我认为主要问题不是内存限制,而是对第三方来源的反复依赖。
但是,如果项目配置中配置了源(无论是第三方还是私有),是否应该首先使用该源,并且无需查找本地配置的默认源?
所以最终的根本问题并没有在这里找到。如果有人知道原因请赐教
附录
1。 Composer 更新或版本 1 和版本 2 共存
版本 2 安装很快
# download
wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - -q | php -- --quiet
# rename
mv composer.phar /usr/local/bin/composer2我在本文开头刚刚安装了 Composer 版本 1,如果你想让这个版本成为默认版本
,只需设置软连接。
ln -s /usr/local/bin/composer1.10.26 /usr/local/bin/composer1
# 默认1版本
ln -s /usr/local/bin/composer1 /usr/local/bin/composer或仅使用
/usr/local/bin/composer1 --version
/usr/local/bin/composer2 --version 版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
code前端网