解决不能通过 proxy 代理运行 apt 安装非官方deb PHP软件包

By 大伟哥 | 2022-04-12,周二, 12:22

最近趁着疫情封家,给自己的几台服务器和测试机器都升级到 PHP 8.1, 其中一台本地测试机器从一开始就不太顺利,因为这个源的服务器在国外的原因,丢包率太高了,正常添加源都很困难,更别说运行 apt update 了。给大家看一下上海移动网络的情况:

ping packages.sury.org
PING debsuryorg.b-cdn.net (212.102.50.49) 56(84) bytes of data.
64 bytes from edge-544.bunnyinfra.net (212.102.50.49): icmp_seq=7 ttl=47 time=97.1 ms
64 bytes from edge-544.bunnyinfra.net (212.102.50.49): icmp_seq=11 ttl=47 time=98.3 ms
64 bytes from edge-544.bunnyinfra.net (212.102.50.49): icmp_seq=17 ttl=47 time=100 ms
64 bytes from edge-544.bunnyinfra.net (212.102.50.49): icmp_seq=22 ttl=47 time=90.6 ms
^C
--- debsuryorg.b-cdn.net ping statistics ---
32 packets transmitted, 4 received, 87.5% packet loss, time 31283ms
rtt min/avg/max/mdev = 90.589/96.573/100.319/3.642 ms

丢包率实在是太感人了,经过无数次尝试之后,终于添加 packages.sury.org 源成功了。但是运行sudo apt update,总是不能成功,报错是这样的: 

Err:5 https://packages.sury.org/php bullseye InRelease
  Could not wait for server fd - select (11: Resource temporarily unavailable) [IP: 212.102.50.49 443]

无奈之下,只能使用代理服务了。在命令行下使用export http_proxy=http://xxx.xxx.xxx.xxx:xxx, export https_proxy=http://xxx.xxx.xxx.xxx:xxx 临时配置代理服务之后,报错是这样的:

W: Failed to fetch https://packages.sury.org/php/dists/bullseye/InRelease  Could not connect to packages.sury.org:443 (212.102.50.49). - connect (111: Connection refused)

还有这样的: 

Could not handshake: Error decoding the received TLS packet. [IP: 212.102.50.49 443]

用这些报错信息作为线索去搜索引擎查,基本上都说是网络的问题,是使用了代理服务的原因。这样看,还是我的代理服务没有配置好,导致apt 和 源服务器无法正确认证引起的。

再仔细查,发现了可以直接给 apt 配置代理服务的方法,那就是在 /etc/apt/apt.conf.d 目录下,新建一个文件,文件名可以是95proxies, 也可以是其他名字,这不重要,重要的是把代理服务的代码写清楚:

Acquire::http::proxy "http://xxx.xxx.xxx.xxx:xxxx";
Acquire::https::proxy "http://xxx.xxx.xxx.xxx:xxxx";

就是你的代理服务的ip地址:端口的格式,注意双斜线前面两个协议都是http.

保存文件,然后重新运行 sudo apt update,就可以成功进行后续操作了。

添加新评论

此字段内容将保密,不会被其他人看见。
  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
发布评论前,请通过下面的验证,以免被当作垃圾评论机器人屏蔽。
8 + 12 =
计算出这道简单的算术题并键入答案。例如、1+3,就输入 4。