简体   繁体   中英

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).

I'm able to run some scripts on the command line using 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. 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 .).

  • What's going on with PHP? Why won't cURL run in the /var/www/ folder but runs fine everywhere else? I'm storing information to a database using cURL to get some JSON from public APIs.
  • Why can't I use shell_exec to run a script as I normally could using PHP on the command line?
  • Errors aren't showing up in PHP even though error_reporting(E_ALL) is set. 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.

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.

Another note: phpinfo() says nothing about cURL, but it works on the command line. I'm not sure why.

Ubuntu has separate php.ini files for command line PHP and Apache2 PHP. 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.

$ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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