Serverless computing is a cloud-based execution model that enables applications to be hosted as a service, without the need to maintain a server.

The service provider maintains the resource allocation on the server, and the user is billed based on the actual usage. The focus shifts to the core application one is creating, and the service provider handles the infrastructure. Serverless computing is also known as Function as a Service (FaaS).

In other words, Serverless PHP is the implementation of serverless computing with a PHP backend to give you an example.

In this guide, we’ll closely examine what Serverless PHP means, its main features, and its pros vs cons to better understand this approach toward cloud computing.

Specifically, we’ll look at the concept of serverless computing, its use cases, and scope, advantages and drawbacks, a simple implementation of serverless PHP with Bref, and the status of serverless PHP on the big three (Amazon, Microsoft, and Google).

Ready? Let’s start!

Why Use Serverless Computing?

Traditionally, you needed the hardware for a server to set up an application on the Internet. The server machine (or machines) would be physically connected to the Internet for someone to access your application. Server maintenance was an expensive affair.

Next, the evolution of hosting services allowed webmasters to purchase hosting space — each server could hold multiple applications. This lowered costs.

With the rise of cloud computing, economies of scale further reduced costs, as you could rent a tiny amount of space on a large, remote server farm. In fact, serverless computing allows you to only pay for the services you use. When not in use, you are practically not utilizing any space or resource on the cloud.

Serverless computing simplifies the software development process: it allows an organization to focus solely on development without worrying about deployment, server upkeep, and scaling.

Serverless PHP: The Basics

Trends for the term "serverless" on Google
Trends for the term “serverless” on Google

To deploy a serverless PHP application, you should first explore the concept of serverless computing. While the term serverless first appeared in this article on the future of software development in 2012 on ReadWrite, it gained popularity with the launch of AWS Lambda in 2014.

In this section, let us focus on a few key concepts related to serverless computing, and try to answer a common question surrounding this technology: is it really server-“less”?

Features of Serverless PHP

Deployment is an easy task without managing the server. You simply upload your code to the server, and the vendor takes the rest. Serverless technology allows you to have language-agnostic functions, interacting with each other.

For instance, if you have a messaging application, the login module may be coded in one language, and the function that updates your status may be coded in a different language.

While this is still possible without serverless hosting, it is definitely more difficult to interact with. Whenever an action triggers your function, an instance spawns to handle it.

This process of spawning an instance may be “warm”, where you use an existing instance, or “cold”, where you start a new instance. There is a slight delay in this process, especially a cold start, as compared to traditional hosting, where your server is always in standby mode to handle requests.

However, the situation between serverless and traditional hosting becomes the opposite when you have many requests to handle. Scalability comes inherently with serverless technology. If you have a sudden requirement of a thousand concurrent requests, the vendor will take care of them without any extra effort or configuration on your part.

Is Serverless PHP Really Serverless?

Do not let the term “serverless” confuse you. It does not mean that the “server” does not exist. When you use a serverless application, there is a server in the background that processes your input and computes the necessary output.

The “-less” is present in the term from the perspective of the developer, who is never exposed to various server elements. Therefore, if you deploy a serverless PHP application, an actual server running on the cloud caters to requests.

BaaS vs Serverless Architectures

While “serverless computing” isn’t tightly defined, it may also refer to BaaS (Backend as a Service) applications. BaaS also refers to a cloud computing model, wherein server operations are outsourced to a third party, and a developer just needs to focus on creating and maintaining the software.

The primary similarity between BaaS and Serverless is the fact that the developer does not focus on server management. Many organizations provide BaaS and FaaS packages under the same umbrella.

Here are the major differences between BaaS and serverless computing:

  • Components: BaaS applications are similar to their traditional counterparts, a developer may not change its structure to adapt to BaaS. In serverless, the application is divided into logical parts called functions, and each of these functions responds to an event and performs a specific task.
  • Scalability: Scalability is a crucial component in a serverless application — more resources are allocated with increased traffic. It is not a necessary module for BaaS applications, though some service providers provide it as an add-on.
  • Triggers: A serverless application is event-driven, which means that a particular activity triggers the application every time it occurs. On the other hand, a BaaS application may run in the background utilizing resources continuously, just like a traditional application.
  • Modular Architecture: In the serverless architecture, it is possible for various functions of an application to reside and execute on different servers, but run seamlessly due to their integration. A BaaS application may or may not follow this structure.

Serverless PHP: Use Cases

We have discussed various serverless computing aspects and how it differs from BaaS. While we have covered the basics of serverless computing, let us explore situations when you may wish to employ such an architecture.

You may have realized that it may not be a good idea to host complex applications on serverless technologies. However, you may deploy modules even if you decide not to deploy a complete application through serverless PHP.

We will discuss two examples of implementations of a serverless stack in this section: databases and file storage.

A serverless database is an on-demand database that lets you execute queries whenever required. Scaling is easy due to the serverless stack, and the vendor bills you only for the time you use the resource.

Amazon Aurora and Google Cloud Datastore are examples of serverless databases available in the market today. A serverless file storage system is implemented as object storage. Files are not treated as a hierarchy in a file system, but as objects, which contain the file’s data and metadata. Storage and retrieval are through a REST-like API.

IBM Cloud provides you with an object storage service. Other common use cases of serverless applications are APIs and mobile backends, the designs of which are based on small, logical, interdependent functions.

Serverless PHP: Advantages

In this section, we look at the main advantages of serverless computing and why it has been gaining traction in recent years.

Reduced Server Costs

Theoretically, serverless computing leads to lower costs as compared to traditional hosting. The inherent reason is simple: you are using the service for specific amounts of time, and there are no upkeep costs during idle times. If you face constant traffic over time, adopting a serverless architecture may not lead to much cost difference.

Easier Deployment

Deployment of a serverless service does not require setting up your server and configuring it. Deployment of a serverless application is also through simple functions. It’s easier to create a version of the application and make it available on the cloud. The whole process of deployment, therefore, is easier and more efficient.

Scalability

In a traditional setup, one needs to make a lot of efforts to scale up to cater to higher traffic. On the other hand, the service provider takes care of resource allocation when there is an increase in traffic. Consequently, it is easier to scale up when you deploy to a serverless architecture.

Serverless PHP: Drawbacks

While serverless computing has its fair set of advantages, one must be aware of its potential drawbacks before committing to it.

Performance

Users’ primary issue with serverless computing is the dip in performance. While it is event-driven, it takes a few hundred milliseconds to spawn a micro-instance to serve a request.

This lag may turn out to be significant for time-critical applications. With an increase in the complexity of an application, components residing in various locations add to this lag. This additive time lag may prove to be detrimental to user experience.

(Suggested reading: Intro to Building Websites with Gatsby and WordPress)

Vendor Lock-In

While serverless architecture allows you to focus solely on your code, the vendor controls the infrastructure. Hence, you cannot change your vendor if you go serverless, as migration can be difficult.

Debugging

Vendors take care of the end-to-end deployment of serverless applications. Therefore, a developer must depend on the vendor to provide appropriate logs for debugging. Debugging a serverless application to identify the root cause is also difficult.

Serverless PHP: Getting Started with Bref on Lambda

While we have explored the serverless architecture, we will now see what you require to deploy a PHP application through a serverless service.

As you may have already guessed, the deployment of a serverless application is highly specific to the vendor. Therefore, this post addresses the implementation of a serverless PHP app on Amazon AWS. Bref, or brief in French, is a Composer package that allows you to deploy PHP applications on AWS through Lambda.

Bref is constantly evolving, so you should probably check Bref’s maturity matrix to assess whether it is a good idea to port your application to a serverless architecture.

Pre-Requisites for Serverless PHP with Bref

First, go to Amazon AWS and create an account. You will need it to deploy your application. Next, you need to install the serverless framework to manage your deployment.

npm install -g serverless

Next, generate your public-private key pair on AWS, and configure the serverless framework locally.

serverless config credentials --provider aws --key <key> --secret <secret>

Next, install Bref through Composer:

composer require bref/bref

Before deployment, you need to install Composer’s dependencies.

composer install --prefer-dist --optimize-autoloader --no-dev

Create a Hello World Application on Serverless PHP with Bref

To create a simple hello world application with Bref, we will write a function triggered by an event and return the “Hello World” string.

First, you must include Bref’s autoload.php script and then use its lambda function. You can optionally declare a context variable if you want to access data from the context.

require __DIR__.'/vendor/autoload.php';
lambda(function ($event) {
 return 'Hello world');
});

While the function is ready, you must create a serverless.yml configuration file. Here is a basic configuration file from Bref’s guide.

service: app
provider:
 name: aws
 runtime: provided
plugins:
 - ./vendor/bref/bref
functions:
 hello:
 handler: index.php
 layers:
 - ${bref:layer.php-73}

Bref creates this configuration file automatically when you run the following command.

vendor/bin/bref init

Now that you are ready with your function and the configuration, you can invoke the function to check it runs as intended using the invoke command of the serverless package:

serverless invoke -f hello

Here is a guide on the local deployment of serverless applications using the sam command-line tool from AWS. Once your project is ready, you can deploy it using the deploy command of serverless. Use the --verbose option to get details of the process of deployment:

serverless deploy

Other Deployment Options for Serverless PHP

Bref PHP on AWS Lambda is a popular choice. However, there are a few other options for your serverless PHP applications.

Vapor, launched by Laravel in July 2019, is a serverless deployment platform for Laravel on AWS Lambda. Vapor converts your Laravel application to a single lambda function. While Azure serverless does not officially support PHP, you can still give it a try using this deployment example.

Serverless PHP is gaining more attention as weeks pass by. But what is it exactly? Is it really server-'less'? How does it work? Get all the answers to these - and more - questions in this in-depth guide! ☁️🔌Click to Tweet

Summary

Here are key aspects that you should take away from this guide on serverless PHP:

  • Before considering using serverless PHP for your application, ensure that you are fully aware of serverless computing and its advantages and drawbacks.
  • There are three primary factors that you should consider when porting your application to a serverless PHP framework. Consider the complexity of the application, the time criticality of its components, and scalability in the future.
  • Serverless PHP is still fairly new to the market. Make sure you run a pilot with Bref on one of the vendors before committing fully to it.

While serverless is becoming very popular, it also requires a deep understanding of how the technology works.

For all other cases, using a managed WordPress host like Kinsta can simplify your workflow tremendously.


Get all your applications, databases and WordPress sites online and under one roof. Our feature-packed, high-performance cloud platform includes:

  • Easy setup and management in the MyKinsta dashboard
  • 24/7 expert support
  • The best Google Cloud Platform hardware and network, powered by Kubernetes for maximum scalability
  • An enterprise-level Cloudflare integration for speed and security
  • Global audience reach with up to 35 data centers and 275+ PoPs worldwide

Test it yourself with $20 off your first month of Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.