PHP Website Curl connecting to AWS EC2 fails on timeout

I'm having some real trouble getting to the bottom of this one.

The Problem

I have a simple web service running on an AWS EC2 port 8080 and need to connect to it and upload a file from a website hosted on a cPanel host.

The client on cPanel site uses a PHP curl command to connect:

$file = fopen($targetFilePath, 'r');

// Connecting to website.
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($targetFilePath));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cache-Control: no-cache", "Content-Type: $mimeType"));
$response = curl_exec($ch);
$err = curl_error($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close ($ch);

But I always receive an unhelpful error:

An error ocurred: Failed to connect to port 8080: Connection timed out

Setting the curl to verbose, hasn't helped as I just get

  • Trying xx.xx.xx.xx...
  • connect to xx.xx.xx.xx port 8080 failed: Connection timed out
  • Failed to connect to port 8080: Connection timed out
  • Closing connection 0

For this troubleshooting I have opened up the required ports in the AWS security group to all IPs so this should not be blocking the connection. I will lock it down again once I get it working.

Things I have tried

  • A simple curl on the client with just a URL parameter to the same server - Same Error
  • The above simple call to - Worked fine
  • The exact same PHP website and curl code but on my localhost - Worked fine
  • Setting curl to verbose as mentioned above - Nothing useful

So in conclusion, the problem only exists on this particular combination of client and server. Other clients work ok with the server and the website on the troublesome client works fine connecting to other destinations.

Since the AWS security group is allowing all TCP traffic in, its a real head scratcher!

Thanks for any help!

1 answer

  • answered 2018-01-13 18:15 Rodrigo M

    You must make sure a local curl client, from the command line, connects to the remote service point, and returns a response.

    The message Connection timed out means that you are not connecting to the service. The service is either not running, or the service port is blocked by firewall or security group settings.

    Try this:

    1. Open port 8080 to the world on the instance or ELB security group.

    2. Open port 8080 to the world on the instance application firewall. (!)

    3. Use command line curl on the instance to make sure is running.

    Forget client code, until command line test works.