从代理服务器后面安装composer

时间:2017-02-17 11:05:31

标签: php proxy composer-php packagist

我正在尝试在代理服务器后面的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"

这也会返回相同的错误。

3 个答案:

答案 0 :(得分:2)

以下是使Composer在代理后工作的关键点(适用于Windows,不在Linux上运行):

1)网址对您的密码进行编码(特殊字符)

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

输入:

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

输出:

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

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

2)删除HTTP_PROXY和HTTPS_PROXY env变量周围的引号

自:

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

作曲家将无效带引号,它使用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将按以下顺序使用第一个可读文件或路径:

  • Env变量:

    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
    

答案 1 :(得分:0)

也许它也需要身份验证?如果是这样的话,试试这个:

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

答案 2 :(得分:0)

编码您的密码然后使用

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