Linux环境搭建composer私服及memory_limit问题

2023年 7月 10日 22.0k 0

如何安装私服之前,我们先熟悉下 composer 相关。

composer 安装

Composer是 PHP项目中用来管理依赖(dependency)关系的工具,允许声明项目所依赖的代码库 ,然后在项目的某个目录中(默认是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 文件
  • • composer install 安装 composer.json 中关联的依赖
  • • composer update 强更新,无视 composer.lock 锁文件,把 composer.json 里面的依赖更新一遍,然后生产新的锁文件
  • • composer self-update version-number 更新composer 本身的版本
  • • composer create-project 依赖包/依赖包 路径 版本比如 composer create-project vendor/project target-directory 1.0
  • • composer config 主要是用来配置composer相关,比如仓库源地址composer config -g secure-http false composer config -g repo.my_repo composer https://mirrors.aliyun.com/composer/
  • • composer help command-name 查询指定command的用法 ,很有用

composer私服搭建

推荐的私服搭建有两种方案

  • • Satis https://getcomposer.org/doc/articles/handling-private-packages.md
  • • toran proxy 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 files

2、配置 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、进行build

[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 view

4、配置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;
}

然后就可以就可以通过 http://composer.xxx.com 来访问和使用

Linux环境搭建composer私服及memory_limit问题image-20230417173158968

5、在项目中配置使用

在PHP项目的更目录下的 composer.json中

{
    "name": "project-name",
    ... ...
    "repositories": [{
        "type": "composer",
        "url": "http://composer.kongfz.com"
      }],
      ....
}

然后在项目发布的过程中,执行

/usr/local/bin/composer install || exit 1
/usr/local/bin/composer dump-autoload --optimize || exit 1

遇到的问题

上次搭建了公司内部的私有 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、开发人员本地没有在 电脑个人用户家目录下配置公共的composer配置, 类似上面的Jenkins家目录下的配置

2、开发人员直接在 项目中配置了 第三方的公共源和私有源

3、然后在项目发布过程中执行 composer install 报错如下

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 install的之后,命令修改为 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 

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论