Apache 无法使用 curl 发出传出 HTTP 请求

And*_*uib 3 php apache curl selinux php-curl

考虑以下系统配置:

  • 软呢帽 27
  • 启用 SELinux
  • Apache/2.4.33
  • PHP 7.1.17

下面的代码片段用于向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);
Run Code Online (Sandbox Code Playgroud)

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

php script.php
Run Code Online (Sandbox Code Playgroud)

我得到了预期的输出。

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

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

我发现了很多建议,例如添加

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
Run Code Online (Sandbox Code Playgroud)

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

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

有什么建议?提前致谢。

use*_*349 12

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

getebool 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