title: How to make TLS connection from PHP in web server, and safely
body: <p>Suppose I have some PHP code running inside a web server, for example, running a simple CakePHP app. From this app, I want to occasionally make a TLS connection out to some server to exchange some data. How is this typically done? (I have little experience with PHP.)</p>
  
  <p>What PHP plug-ins or libraries or whatever, are recommended to accomplish TLS connections? Where are some good places to start looking? (My initial Google searches give me a huge ratio of noise to signal.)</p>
  
  <p>What are the security issues involved with having the X509 private key sitting on the web server? To make a TLS connection out, I'm going to need both the X509 certificate and the corresponding private key, as perhaps PEM files, or DER files, or in a Java keystore, or whatever. Is that private key going to be sitting somewhere vulnerable under the web root? How is this typically handled? What are the security issues, specifically, of securing the private key that is to be used to make a TLS connection out? What are the best practices?</p>
  
  <p>Edit: I forgot to mention that the server that the PHP app connects to requires a client certificate to be presented. The client <em>does</em> need the private key to connect. Also, the server I am connecting to is <em>not</em> an HTTP server -- I just need to read/write on a plain socket connection to the server (over the SSL/TLS connection).</p>