【问题标题】:Apache cannot make outgoing HTTP Requests using curlApache 无法使用 curl 发出传出 HTTP 请求
【发布时间】:2018-06-12 00:20:47
【问题描述】:

考虑以下系统配置:

  • Fedora 27
  • 已启用 SELinux
  • Apache/2.4.33
  • PHP 7.1.17

下面的代码 sn-p 用于向Sphere-Engine Compilers API发出http请求:

$ch = curl_init('http://xxxxxxxx.compilers.sphere-engine.com/api/v4/test?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 3.0);
$x = curl_exec($ch);
print_r($x);

当我运行以下 CLI 时,上面的脚本完全可以正常工作:

php script.php

我得到了预期的输出。

但是,当我尝试通过网络浏览器运行它时,它会产生:

CURLE_COULDNT_CONNECT (7) 无法连接()到主机或代理。

我找到了很多建议,例如添加

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

这对我来说并不是一个真正的解决方案。关闭 SELinux,我不会这样做。

注意:当我发出 URL 并给我预期的输出时,它本身工作正常。

有什么建议吗?提前致谢。

【问题讨论】:

  • 错误很明显,你能在浏览器中运行那个url吗?
  • 是的,完全没问题。
  • curl_setopt($ch, CURLOPT_VERBOSE, true); 添加它,看看你是否得到任何有用的东西
  • 实际上没有。
  • 同样的问题,Fedora 29,在我查看 /var/log/messages:audit[2320]: AVC avc: denied { name_connect } for pid=2320 comm="php-fpm" dest=80 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_port_t:s0 tclass=tcp_socket permissive=0 之前在任何地方都找不到错误,否则我不会记得 SELinux

标签: php apache curl selinux php-curl


【解决方案1】:

试试这个看看 SELinux 是否会让 web 服务器连接到网络:

getsebool httpd_can_network_connect

如果没有,请允许

setsebool -P httpd_can_network_connect on

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-managing_confined_services-the_apache_http_server-booleans

【讨论】:

  • 非常感谢。这是我花了几个小时寻找的大海捞针。
猜你喜欢
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 2016-12-18
  • 2019-05-07
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多