title: PHP Website Curl connecting to AWS EC2 fails on timeout
body: <p>I'm having some real trouble getting to the bottom of this one.</p>
  
  <h2>The Problem</h2>
  
  <p>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.</p>
  
  <p>The client on cPanel site uses a PHP curl command to connect:</p>
  
  <pre><code>$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);
  </code></pre>
  
  <p>But I always receive an unhelpful error:</p>
  
  <blockquote>
    <p>An error ocurred: Failed to connect to ec2-xx-xx-xx-xx.eu-west-1.compute.amazonaws.com port 8080: Connection timed out</p>
  </blockquote>
  
  <p>Setting the curl to verbose, hasn't helped as I just get</p>
  
  <blockquote>
    <ul>
    <li>Trying xx.xx.xx.xx...</li>
    <li>TCP_NODELAY set</li>
    <li>connect to xx.xx.xx.xx port 8080 failed: Connection timed out</li>
    <li>Failed to connect to ec2-xx-xx-xx-xx.eu-west-1.compute.amazonaws.com port 8080: Connection timed out</li>
    <li>Closing connection 0</li>
    </ul>
  </blockquote>
  
  <p>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.</p>
  
  <h2>Things I have tried</h2>
  
  <ul>
  <li>A simple curl on the client with just a URL parameter to the same server - <strong>Same Error</strong></li>
  <li>The above simple call to google.co.uk - <strong>Worked fine</strong></li>
  <li>The exact same PHP website and curl code but on my localhost - <strong>Worked fine</strong></li>
  <li>Setting curl to verbose as mentioned above - <strong>Nothing useful</strong></li>
  </ul>
  
  <p>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.  </p>
  
  <p>Since the AWS security group is allowing all TCP traffic in, its a real head scratcher!</p>
  
  <p>Thanks for any help!</p>