搭建composer镜像服务器

Composer原理

Composer类似Java中的Maven,之前有PEAR,但由于PEAR的种种不好用,如配置复杂,容易出错,于是Composer应运而生,是PHP新时代的依赖管理工具,它的原理是:
composer原理
它首先去Packagist.org获取索引元数据,在去Github.com下载依赖,由于处于速度以及公司内外网的原因考虑搭建本地镜像仓库,可以使用toranproxy来搭建。

搭建步骤

搭建的大致步骤如下:

  1. 下载

    wget https://toranproxy.com/releases/toran-proxy-v1.3.2.tgz
    tar zxvf toran-proxy-v1.3.2.tgz

    解压后你进入目录toran你就会发现是symfony框架,拷贝一份来修改配置参数

    cp app/config/parameters.yml.dist app/config/parameters.yml
  2. 使用命令可以查看很多symfony命令行来查看

    php app/console --help

    我们可以使用命令也运行

    php app/console server:run -e prod 192.168.0.167:8081

    通过上面的地址即可浏览到
    预览

  3. 点击“Install”即可安装,

    Bootstrapping
    Toran Proxy is now set-up! To complete the setup you need to run the background update task once manually and then set-up a cron job.
    Note that all the instructions below should be executed as the php web user (www-data or equivalent) to avoid permission issues later on.
    The background tasks are centralized and automatically managed by the bin/cron php script. Run it once in verbose mode as php bin/cron -v to make sure everything works fine and so it can ask you for credentials should they be needed.
    Once the first run is complete, you should add a cron job running every minute or so that executes cd /home/james/composer_proxy/toran && php bin/cron - this will take care of keeping your packages up to date at all times.

    安装后可以使用命令

    php bin/cron -v

    来查看是否一切运行正常,我的提示

    You need to setup a GitHub OAuth token because Toran makes a lot of requests and will use up the API calls limit if it is unauthenticated
    Head to https://github.com/settings/tokens/new to create a token. You need to select the public_repo credentials, and the repo one if you are going to use private repositories from GitHub with Toran.
    Token:

    注册一个github的帐户,然后https://github.com/settings/tokens/new创建一个Token,然后输入Token,我的报了一个异常:

    [InvalidArgumentException]
    The “/home/james/composer_proxy/toran/web/repo/packagist/p” directory does not exist.

    使用mkdir创建目录

    mkdir -p web/repo/packagist/p

    然后在通过地址http://192.168.0.167:8081/访问即可看到使用方法

    预览

  4. 测试代码

    {
    "repositories": [
    {"type": "composer", "url": "http://192.168.0.167:8081/repo/packagist/"},
    {"packagist": false}
    ],
    "require": {
    "symfony/symfony": "2.8.*"
    }
    }

    更新

    php ../composer/composer.phar update -vvv

    报告异常

    Your configuration does not allow connection to http://192.168.0.167. See https://getcomposer.org/doc/06-config.md#secure-http for details.

    根据提示的文档可以清楚看到默认使用的是HTTPS,但我们的镜像是HTTP,因此需要修下配置,如下

    {
    "repositories": [
    {"type": "composer", "url": "http://192.168.0.167:8081/repo/packagist/"},
    {"packagist": false}
    ],
    "config": {
    "secure-http": false
    },
    "require": {
    "symfony/symfony": "2.8.*"
    }
    }
翟前锋 wechat
欢迎订阅我的微信公众号:zhaiqianfeng!