简体   繁体   English

www文件夹中Amazon EC2中的PHP和cURL

[英]PHP and cURL in Amazon EC2 in www folder

PHP and cURL work fine in a non-webfacing directory in my amazon EC2 micro install, 12.04.2 LTS (GNU/Linux 3.2.0-40-virtual x86_64). PHP和cURL在我的Amazon EC2微型安装版本12.04.2 LTS(GNU / Linux 3.2.0-40-虚拟x86_64)中的非网络目录中可以正常工作。

I'm able to run some scripts on the command line using cURL: 我可以使用cURL在命令行上运行一些脚本:

    #!/usr/bin/php
    $command = '/absolute/path/to/running/process';
    $url = 'http://example.com';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 400);
    curl_setopt($ch, CURLOPT_TIMEOUT, 400);

    $resp = json_decode(curl_exec($ch), 1);
    $data1 = round($resp["data1"], 6);
    $data2 = round ($resp["data2"], 2);

    curl_close($ch);


    $c = array(
            'var1' => trim(shell_exec($command)),
            'var2' => trim(shell_exec($command)),
            'var3' => trim(shell_exec($command)),
            'var4' => number_format($data1, 6, '.', ''),
            'var5' => number_format($data2, 0, '.', ',')
    );
    var_dump($c);

This works fine. 这很好。

However, as soon as I put this same script in the web facing directory (I remove #!/usr/bin/php obviously), the page loads (I can type echo 'Hello World!' at the very top and see the text on a browser from a remote machine), but the page is entirely blank and adding echo statements after curl results in nothing. 但是,只要将相同的脚本放到面向Web的目录中(显然我删除了#!/usr/bin/php ),页面就会加载(我可以在最顶部键入echo 'Hello World!'并看到文本)在远程计算机上的浏览器上),但页面完全空白,在curl后面添加echo语句不会有任何结果。 If I comment out the curl function calls and try to simply execute the command, it seems to not work (The entire array of values is populated with NULL or 0 .). 如果我注释掉curl函数调用并尝试简单地执行命令,则它似乎不起作用(整个值数组都填充为NULL0 )。

  • What's going on with PHP? PHP发生了什么? Why won't cURL run in the /var/www/ folder but runs fine everywhere else? 为什么cURL不能在/ var / www /文件夹中运行,但在其他地方都能正常运行? I'm storing information to a database using cURL to get some JSON from public APIs. 我正在使用cURL将信息存储到数据库中,以从公共API中获取一些JSON。
  • Why can't I use shell_exec to run a script as I normally could using PHP on the command line? 为什么不能像以前通常在命令行上使用PHP一样使用shell_exec运行脚本?
  • Errors aren't showing up in PHP even though error_reporting(E_ALL) is set. 即使设置了error_reporting(E_ALL)错误也不会在PHP中显示。 I can't find a log of errors by using locate to find error_log or php.err , which I thought were the default names for the php error log. 我无法通过使用locate找到error_logphp.err来找到错误日志,我认为这是php错误日志的默认名称。

I feel like these two issues may be related, but I can't figure out why, maybe permissions? 我觉得这两个问题可能有关,但我不知道为什么,也许是权限? Something I noticed was that I cannot create or modify files in this directory without using the sudo command, which isn't usual. 我注意到的是,如果不使用sudo命令,就无法在此目录中创建或修改文件,这是很不常见的。

Another note: phpinfo() says nothing about cURL, but it works on the command line. 另一个注意事项:phpinfo()并未对cURL进行任何说明,但可以在命令行中使用。 I'm not sure why. 我不知道为什么。

Ubuntu has separate php.ini files for command line PHP and Apache2 PHP. Ubuntu对于命令行PHP和Apache2 PHP具有单独的php.ini文件。 Your config in those files most likely has different extensions loaded (or different security settings), which makes Apache not have the same functionality as the CLI. 这些文件中的配置很可能加载了不同的扩展名(或不同的安全设置),这使Apache不具有与CLI相同的功能。

$ ls -l /etc/php5/cli/php.ini
-rw-r--r-- 1 root root 65485 Jan 27 09:20 /etc/php5/cli/php.ini

$ ls -l /etc/php5/apache2/php.ini
-rw-r--r-- 1 root root 65839 Oct 31  2012 /etc/php5/apache2/php.ini

Just an observation, there are extra <'> single quotes in these lines: 只是观察,这些行中有多余的<'>单引号:

        'var1' => trim(shell_exec($command')),
        'var2' => trim(shell_exec($command')),
        'var3' => trim(shell_exec($command')),

other than that, I agree with Joachim's suggestion. 除此之外,我同意约阿希姆的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM