<article>

    <h2 id="vii_port_binding">VII. Port binding</h2>

    <h3 id="export_services_via_port_binding">Export services via port binding</h3>

    <p>Web apps are sometimes executed inside a webserver container. For example, PHP apps might run as a module inside
        <a href="http://httpd.apache.org/" target="_blank">Apache HTTPD</a>, or Java apps might run inside
        <a href="http://tomcat.apache.org/" target="_blank">Tomcat</a>.</p>

    <p>
        <strong>The twelve-factor app is completely self-contained</strong> and does not rely on runtime injection of a webserver
        into the execution environment to create a web-facing service. The web app
        <strong>exports HTTP as a service by binding to a port</strong>, and listening to requests coming in on that port.</p>

    <p>In a local development environment, the developer visits a service URL like
        <code>http://localhost:5000/</code> to access the service exported by their app. In deployment, a routing layer handles
        routing requests from a public-facing hostname to the port-bound web processes.</p>

    <p>This is typically implemented by using
        <a href="./dependencies">dependency declaration</a> to add a webserver library to the app, such as
        <a href="http://www.tornadoweb.org/" target="_blank">Tornado</a> for Python,
        <a href="http://code.macournoyer.com/thin/" target="_blank">Thin</a> for Ruby, or
        <a href="http://jetty.codehaus.org/jetty/" target="_blank">Jetty</a> for Java and other JVM-based languages. This happens entirely in
        <em>user space</em>, that is, within the app’s code. The contract with the execution environment is binding to a port
        to serve requests.</p>

    <p>HTTP is not the only service that can be exported by port binding. Nearly any kind of server software can be run via
        a process binding to a port and awaiting incoming requests. Examples include
        <a href="http://www.ejabberd.im/" target="_blank">ejabberd</a> (speaking
        <a href="http://xmpp.org/" target="_blank">XMPP</a>), and
        <a href="http://redis.io/" target="_blank">Redis</a> (speaking the
        <a href="http://redis.io/topics/protocol" target="_blank">Redis protocol</a>).</p>

    <p>Note also that the port-binding approach means that one app can become the
        <a href="./backing-services">backing service</a> for another app, by providing the URL to the backing app as a resource handle in the
        <a href="./config">config</a> for the consuming app.</p>

</article>
