Ubuntu Lamp - 代理 - 防火墙 - Joomla curl 和 fsockopen 无法工作

Ubuntu Lamp - Proxy - Firewall - Joomla curl and fsockopen wont work

好的,我已经为此苦苦挣扎了几天。

我在我们网络的本地机器上为我们的内部网安装了 Joomla,还安装了 Jomsocial。

问题是,当我进入站点配置或编辑事件或导航到任何调用外部 api 的 joomla 模块时,我得到

CURL error : 7 Failed to connect to maps.google.com port 80: Connection timed out 

Connection timed out (110) 

问题绝对不是 Joomla 或 Jomsocial,我在同一台服务器上还有其他 php 应用程序 运行 也无法联系外部 api 的

服务器设置为

Ubuntu 14Lts PHP 5.5 阿帕奇 2.4.7 MariaDB

服务器位于代理后面,但可以从 CLI 完全访问互联网。所有必要的 php 扩展都已启用。我也在 apt config 中的 /etc/environment 中设置了全局代理变量,并在 Joomla 中设置了代理变量。我的 Joomla 更新和组件更新工作正常,但 fsockopen 函数的 curl 无法正常工作。

我不知道还能在哪里查找错误。我的想法是 www-data 用户可能没有足够的权限从浏览器执行 fsockopen 和 curl。

有什么建议吗?

更新,我已经在另一台不在公司网络(直接连接到互联网)上的机器上测试了该站点,并且一切正常。所以我很确定我的问题出在我的机器和网络权限上,特别是我的 www-data 用户。我该如何解决这个问题?

使用这个:

export http_proxy=http://your.proxy.server:port/

或者这个:

来自man curl:

-x, --proxy <[protocol://][user:password@]proxyhost[:port]>

 Use the specified HTTP proxy. 
 If the port number is not specified, it is assumed at port 1080.

看来http_proxy这个变量没有被PHP(mod_php)使用,即使是用PassEnv传递,或者直接用SetEnv。此外,在 PHP 脚本中调用 getenv('http_proxy') 时也能正确显示。

但是,有两种方法可以使其正常工作:

  • 在Apache envvars (/etc/apache2/envvars)中设置如下:

    export http_proxy=http://proxy.example.com:8080/
    

    并重新启动 Apache。

  • 放入加载应用程序的 PHP 文件(例如 index.php、bootstrap.php 等):

    putenv('http_proxy=http://proxy.example.com:8080/');
    

同样,如果您使用 getenv('http_proxy') 进行测试,您会发现它们设置正确。

我刚刚遇到了同样的问题,但设置非常接近(唯一的区别是 mysql 而不是 MariaDb 和 Joomla 3.4.1),我花了很长时间才把所有东西放在一起,所以我将在这里列出可能的绊脚石:

  1. 确保安装了 php5-curl。 Joomla 只能使用带有 CURL 的代理作为传输层。

    sudo apt-get install php5-curl
    
  2. 我发现在 Joomla 配置中输入代理没有用。唯一的好处是更新连接不会超时,而是立即return。

  3. 把环境变量放在/etc/apache2/envvars里是不行的,还需要在/etc/apache2/apache2.conf里用"PassEnv", 即(取自 )

    PassEnv http_proxy
    
  4. 此外,我需要将 HTTP_PROXYHTTPS_PROXY 作为 xml-列表通过 http 获取,稍后通过 https 获取文件(可能从 github 更新文件)。可能,您需要将这些变量设为小写,但在 joomla 配置页面上 "PHP information" 类似命名的变量以大写显示。

  5. 我不知道这到底有什么不同,但如下所示重新启动 apache2 似乎是正确的方法(而不是 apache2ctl)。

     sudo service apache2 restart
    

我整理了一些随意的代码来测试 curl 和 php 是否可以一起工作,其中大部分来自 。我只添加了大量的错误报告。将文件 test.php 放入 web 文件夹的根目录中,然后使用您喜欢的浏览器查看。

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$url = 'http://update.joomla.org/core/list.xml';

function get_page($url, $proxy=true) {
    if ($url!='') {
        $ch = curl_init ();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if ($proxy) {
            curl_setopt($ch, CURLOPT_PROXY, '<enter your proxy host here>');
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            curl_setopt($ch, CURLOPT_PROXYPORT, <enter your proxy port here>);
        }
        if (! $html = curl_exec($ch)) {
            echo '<br>Last CURL error is '.curl_error($ch).'<br>';
    } else {
            echo '<br>CURL without error.<br>';
    }
        curl_close($ch);
        return $html;
    } else {
    echo 'Empty URL.';
    }
}

echo 'Hello, getting pages via curl:';
$html=get_page($url);
var_dump($html);
echo bin2hex($html);
echo '<br>';
var_dump(get_page($url, false));
echo '<br>done.<br>';
?>