What is serverless PHP and how does it work?
Tags: php, serverless
Published: 2021-06-24 17:53:34

---
title: What is serverless PHP and how does it work?
published: true
tags: PHP,serverless
canonical_url: https://blog.ymirapp.com/serverless-php/
---

<!-- wp:paragraph -->
<p>Serverless computing is a new cloud computing model centred on <a href="https://en.wikipedia.org/wiki/Function_as_a_service">Functions as a Service</a> (FaaS). A serverless PHP application is simply a PHP application that runs on one of those serverless computing platforms. But what's so special about it, and why is there interest in using it instead of a regular server for PHP?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Well, as web developers, we always have to consider where we host our code. It doesn't matter whether we're using <a href="https://en.wikipedia.org/wiki/JavaScript">JavaScript</a>, <a href="https://en.wikipedia.org/wiki/PHP">PHP</a>, <a href="https://en.wikipedia.org/wiki/Python_(programming_language)">Python</a> or <a href="https://en.wikipedia.org/wiki/Ruby_(programming_language)">Ruby</a>. (Just to name a few.) They all need a hosting service where that code can run and render the HTML sent to the browser.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are a lot of different hosting services. You can pay for a server on <a href="https://www.digitalocean.com/">DigitalOcean</a> or some other some other <a href="https://en.wikipedia.org/wiki/Cloud_computing">cloud</a> provider. This is often the cheapest option, but then you have a server to manage.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you don't want to do that, you can use a <a href="https://en.wikipedia.org/wiki/Platform_as_a_service">Platform as a Service</a> (PaaS) like <a href="https://www.heroku.com/">Heroku</a>. You tell them how big of a server you want and they take care of the rest. On your end, you just need to deploy your code and that's it. <a href="https://en.wikipedia.org/wiki/WordPress">WordPress</a> hosting works similarly.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>While platforms as a service help you worry less about your server, they don't completely remove all server issues. You still have to wonder if you can handle spikes in traffic. That's because most of these services won't scale automatically to handle these scenarios.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://en.wikipedia.org/wiki/Serverless_computing">Serverless computing</a> offers a solution to this problem. It distills your cloud computing needs to its barest essence. Your cloud provider will run your code on-demand and only charge you for that.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This lets you only pay for what you use. (If your site receives no traffic, you pay nothing.) This architecture also lets you scale infinitely. In fact, serverless computing can scale to handle thousands of visitors almost instantly.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is a transformative change for all programming languages. But even more so for PHP, since it's a language uniquely positioned to leverage the benefits of serverless. Let's explore why that is.</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading {"level":1} -->
<h1>How does a traditional PHP application work?</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>PHP differs from most other programming languages because it's an <a href="https://en.wikipedia.org/wiki/Interpreted_language">interpreted language</a>. You don't need to compile your code. You can just upload your PHP files to a web server, and that's it.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":90,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://blog.ymirapp.com/uploads/2021/04/server-php-architecture.png" alt="PHP server architecture" class="wp-image-90"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The web server (<a href="https://en.wikipedia.org/wiki/Nginx">nginx</a> or <a href="https://en.wikipedia.org/wiki/Apache_HTTP_Server">apache</a> most of the time) does all the magic. It figures out what PHP file you're trying to access. Often, it's just the <code>index.php</code> file in the root directory which then loads other PHP files. (Fun fact: <a href="https://twitter.com/levelsio/status/1381709793769979906">Pieter Levels makes $100k/month from a single index.php file!</a>)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The web server then sends this PHP file to the PHP interpreter. The interpreter reads the file, parses it and then executes it. Once the execution completes, it'll return some output. That output is the generated HTML that the web server sends back to your browser as an HTTP response.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>What happens when you use serverless PHP</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now that we've gotten a basic idea of how a PHP application works, we can look at what happens to it when you use serverless PHP. First, serverless computing is <a href="https://en.wikipedia.org/wiki/Event-driven_programming">event-driven</a>. Your serverless PHP interpreter awaits for an event, and this triggers the PHP code to run.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In a way, it’s similar to how things work with a web server. Your web server receives an <a href="https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_message">HTTP request</a> (an event) and this event would cause the web server to tell the PHP interpreter to process your code. This is the primary reason serverless computing works so well with PHP.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So what's different about serverless PHP then? The big change is that, not only do you not have a server with serverless PHP, you don't have a web server anymore either. Instead of a web server, you use an API gateway which is a special service offered by the cloud provider. Let's look at what that looks like in more detail.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>The life of a serverless PHP request</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Below is a diagram showing what happens when you make a request to a serverless PHP application. As you can see, it looks very similar to what we had before.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":93,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://blog.ymirapp.com/uploads/2021/04/serverless-php-architecture.png" alt="" class="wp-image-93"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The browser sends the HTTP request to the API gateway. The gateway receives the HTTP request, converts it to an event, and forwards it to the serverless PHP application. It reads, parses, and executes the requested PHP file and returns the result to the API gateway. The gateway generates the HTTP response and sends it back to the browser.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Of note is the serverless PHP application. It has two parts: the PHP runtime and your PHP code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The PHP runtime contains the PHP interpreter. But it also has code to process the event sent by the API gateway and return it the response from the interpreter. Your PHP application code is the same PHP code you’d deploy to a regular server.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>These two parts get packaged together as a function (this is where the name Function as a Service comes from) when you deploy your serverless PHP application to the cloud provider. This function is what the API gateway sends an event to whenever it receives a request.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>What's the advantage of this?</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now that we've seen how serverless PHP application works, you might wonder, "Why would I want to use this instead of my trusty Linux server?"</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Near infinite scaling</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Once you've deployed a new version of your serverless PHP application, the cloud provider can create as many copies of it as necessary. It can also create these copies almost instantly. This allows your PHP application to handle large spikes of traffic without breaking a sweat. (For example, <a href="https://blog.ymirapp.com/why-serverless-perfect-hosting-woocommerce/">Ymir can help your WooCommerce site scale to handle hundreds of customers instantly</a>.)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>With a normal server setup, you need to have the server capacity already provisioned and ready to go. Otherwise, you need to have a <a href="https://en.wikipedia.org/wiki/Horizontal_scalability">horizontally scaling</a> setup ready to handle these traffic spikes. But these types of setup take a few minutes to scale which can make the application unavailable until then.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>No need to manage servers</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is a good way to bring up the next advantage of using serverless. You don't have a server to manage anymore. If you've ever had to manage servers, you know the responsibilities that come with managing one. (Being on call when it goes down, handling updates, keeping it secure, etc.)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>With serverless, all that you have to do is deploy your PHP code to your serverless platform and you're good to go! This gives you a peace of mind that's hard to put a price on. But once you've experienced it, it's hard to go back. (It's why I built <a href="https://ymirapp.com">Ymir</a>.)</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Pay per use</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The server doesn't just change how we host our code. It's also changes how we pay for hosting it. Before you'd pay for a server that had to stay on at all times. This was true regardless that you had a droplet on DigitalOcean or code deployed to a Heroku dyno.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>With serverless, you only pay when your code runs. (For example, <a href="https://en.wikipedia.org/wiki/AWS_Lambda">AWS Lambda</a> charges you by the millisecond!) In a lot of cases, this offers significant cost saving. <a href="https://mnapoli.fr/serverless-case-study-externals/">One case study</a> shows how <a href="https://externals.io/">external.io</a> went from paying $50/month on <a href="https://platform.sh">platform.sh</a> to ~$17/month on <a href="https://en.wikipedia.org/wiki/Amazon_Web_Services">AWS</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>The tradeoffs</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If serverless sounds too good to be true, that's because it generally is! That said, it's not the ideal solution in all situations. Here are some the tradeoffs you have to consider if you're looking to use serverless,</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Harder to predict costs</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When you pay for a server, you know it'll be $X/month. It's simple and easy to plan for. If you need a larger server, you know you'll go from paying $X/month to $Y/month.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>With serverless that whole cost calculation is more complicated, you have to think, "How many requests do I get? How long do they last on average?" So while pay per use pricing model offers great cost saving potential, you might prefer the predictability of regular hosting costs. Even if you might pay 2-3 times more for it.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Not cheaper than a low-end VPS</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you're running your PHP application on a $5/month <a href="https://en.wikipedia.org/wiki/Virtual_private_server">VPS</a>, serverless won't offer any cost saving. In general, serverless PHP becomes cost competitive when you're paying $25/month for hosting. $25/month isn't that expensive for hosting, but a lot of us just host projects on smaller VPCs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That said, <a href="https://twitter.com/JackEllis/status/1337425341103521792" data-type="URL" data-id="https://twitter.com/JackEllis/status/1337425341103521792">it's also disingenuous to just compare server costs like this</a>. You should also consider how much it costs to manage that server. Either it costs your time or you have to pay someone to do it.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Generally limited to AWS</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>While serverless computing exists on all the major cloud providers, your serverless architecture is very provider dependent. An API gateway on <a href="https://en.wikipedia.org/wiki/Microsoft_Azure">Microsoft Azure</a> won't work the same as the one on <a href="https://en.wikipedia.org/wiki/Google_Cloud_Platform">Google Cloud</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The reality is that, at this time, most of the energy around serverless PHP is focused on one provider: AWS. Not everyone can use AWS for a wide range of reasons. That said, if you can't use AWS, you're going to have a harder time finding resources or services (<a href="http://vapor.laravel.com/">Laravel Vapor</a> and Ymir are both AWS only) to create a serverless PHP application.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>A great alternative to servers</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The point of this article wasn't necessarily to show you you shouldn't host a PHP application on a server. Hosting a PHP application on a server still has a bright future ahead of it. It's sometimes more cost effective to do so. You can choose which provider you want to use, and there are countless resources and tools to help you manage one.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But if you're using an expensive hosting provider or a platform as a service or some complex horizontally scaling setup, serverless might be the thing you're waiting for. It's easier to manage and deploy to while often costing less.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And that's something to be excited about.</p>
<!-- /wp:paragraph -->