gpt4 book ai didi

php - 从代理服务器后面安装 Composer

转载 作者:行者123 更新时间:2023-12-02 04:00:06 26 4
gpt4 key购买 nike

我试图在代理服务器后面的 Windows 10 上运行 composer update,它给我一个错误。 enter image description here

然后我使用下面的命令设置 http 代理

SET HTTP_PROXY="http://192.168.1.6:808"
SET HTTPS_PROXY="http://192.168.1.6:808"

这也返回同样的错误。

最佳答案

以下是使 Composer 在代理后面工作的关键点(在 Windows 上工作,未在 Linux 上尝试):

1) URL encode your password (for special characters)

您可以使用 PHP 命令行轻松完成此操作,例如:

输入:

php -r "echo urlencode('P*a/$$/w!0%r$d');"

输出:

P%2Aa%2F%24%24%2Fw%210%25r%24d

使用生成的值设置 HTTP_PROXY 和 HTTPS_PROXY 环境变量。

2) 删除 HTTP_PROXY 和 HTTPS_PROXY 环境变量的引号

来自:

SET HTTP_PROXY="http://username:password@hostname:port"
SET HTTPS_PROXY="http://username:password@hostname:port"

收件人:

SET HTTP_PROXY=http://username:password@hostname:port
SET HTTPS_PROXY=http://username:password@hostname:port

带有引号的 Composer WON'T WORK,它使用 PHP 核心函数“parse_url”来解析变量:

带引号:

php -r "print_r(parse_url('\"http://username:password@proxy:8080\"'));"

Array
(
[path] => "http://username:password@proxy:8080"
)

没有引号:

php -r "print_r(parse_url('http://username:password@proxy:8080'));"

Array
(
[scheme] => http
[host] => proxy
[port] => 8080
[user] => username
[pass] => password
)

3) 确保为 HTTPS 连接正确设置证书颁发机构文件或路径

Composer 将按以下顺序使用第一个可读 文件或路径:

  • 环境变量:

    SSL_CERT_FILE
    SSL_CERT_DIR
  • php.ini:

    openssl.cafile
    openssl.capath
  • 文件:

        /etc/pki/tls/certs/ca-bundle.crt
    /etc/ssl/certs/ca-certificates.crt
    /etc/ssl/ca-bundle.pem
    /usr/local/share/certs/ca-root-nss.crt
    /usr/ssl/certs/ca-bundle.crt
    /opt/local/share/curl/curl-ca-bundle.crt
    /usr/local/share/curl/curl-ca-bundle.crt
    /usr/share/ssl/certs/ca-bundle.crt
    /etc/ssl/cert.pem
    /usr/local/etc/ssl/cert.pem
    /usr/local/etc/openssl/cert.pem
  • 非空文件夹:

        /etc/pki/tls/certs/
    /etc/ssl/certs/
    /etc/ssl/
    /usr/local/share/certs/
    /usr/ssl/certs/
    /opt/local/share/curl/
    /usr/local/share/curl/
    /usr/share/ssl/certs/
    /etc/ssl/
    /usr/local/etc/ssl/
    /usr/local/etc/openssl/

如果以上均无效,Composer 将使用其嵌入文件:

    composer.phar /vendor/composer/ca-bundle/res/cacert.pem

4) 如果您遇到“证书验证失败”错误

[Composer\Downloader\TransportException]
The "https://packagist.org/packages.json" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
failed to open stream: Cannot connect to HTTPS server through proxy

这意味着使用的 CA 文件或路径不包含所需的证书。

然后你可以:

  • 从 composer.phar 中提取默认文件:

    php -r "(new Phar('composer.phar'))->extractTo('/tmp/cacert/', 'vendor/composer/ca-bundle/res/cacert.pem');"
  • 然后将所需的证书(例如您的公司代理证书)添加到文件末尾

  • 并在 php.ini 中强制这个文件

    [openssl]
    ; The location of a Certificate Authority (CA) file on the local filesystem
    ; to use when verifying the identity of SSL/TLS peers. Most users should
    ; not specify a value for this directive as PHP will attempt to use the
    ; OS-managed cert stores in its absence. If specified, this value may still
    ; be overridden on a per-stream basis via the "cafile" SSL stream context
    ; option.
    openssl.cafile=/tmp/cacert/vendor/composer/ca-bundle/res/cacert.pem

关于php - 从代理服务器后面安装 Composer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42296335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com