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

3

好的,我现在已经为这个问题苦恼了几天了。

我们内部网络中有一个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 Apache 2.4.7 MariaDB
服务器位于代理后面,但是从CLI具有完全的Internet访问权限。所有必要的PHP扩展都已启用。我已在/etc/environment和apt配置中设置了全局代理变量,并在Joomla中设置了代理变量。我的Joomla更新和组件更新正常工作,但curl或fsockopen函数不起作用。
我不知道还能在哪里查找错误。我的想法是www-data用户可能没有足够的权限从浏览器执行fsockopen和curl。
有什么建议吗?
更新,我已在另一台不在公司网络上的机器上测试了该站点(直接连接到互联网),一切正常。因此,我非常确定我的问题在我的机器上和网络权限上,特别是我的www-data用户。我该怎么解决?

1
如果可能的话,请在Joomla的index.php文件中使用putenv('http_proxy=http://proxy.example.com:8080/');来设置代理,看看是否可以这样工作。 - VolenD
1
谢谢!问题已解决。虽然我必须在index.php和/administrator/index.php中设置变量。请将您的评论作为答案发布以领取奖励。 - Stroes
3个回答

1
似乎即使使用PassEnvSetEnv直接设置http_proxy变量,PHP(mod_php)也不会使用它。此外,在调用getenv('http_proxy')的PHP脚本中,变量将正确显示,但有两种方法可以使其正常工作:
  • Set it in the Apache envvars (/etc/apache2/envvars) as follows:

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

    and restart Apache.

  • Put in the PHP files that load the application (e.g. index.php, bootstrap.php and etc.):

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

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


1

我遇到了一个相似的问题(只是使用的是mysql而不是MariaDb,还有Joomla 3.4.1),花费了很长时间才解决,所以我会在这里列出可能的障碍清单:

  1. Make sure php5-curl is installed. Joomla can use a proxy only with CURL as transport layer.

    sudo apt-get install php5-curl
    
  2. I found no use in entering the proxy in the Joomla configuration. The only good it did was that the update connection would not time out but return immediately.

  3. It is not enough to place the environment variables in /etc/apache2/envvars, you also need to use "PassEnv" in /etc/apache2/apache2.conf, i.e. (taken from https://stackoverflow.com/a/21571588/1967646)

    PassEnv http_proxy
    
  4. Also, I needed to pass both HTTP_PROXY, HTTPS_PROXY as xml-lists were fetched via http and files lateron via https (probably update files from github). Possibly, you need to have these variables in lower case but on the joomla configuration page "PHP information" similarly named variables show up in upper case.

  5. I don't know where this really made any difference, but restarting apache2 as follows seems to be the right way (instead of apache2ctl).

     sudo service apache2 restart
    
我组合了一些杂乱的代码来测试curl和php是否能够一起工作,其中大部分来自https://stackoverflow.com/a/1477710/1967646。 我只添加了大量的错误报告。 将其放入名为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>';
?>

0

使用这个:

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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接