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 that one is creating and the infrastructure is handled solely by the service provider. 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 take a close look at what Serverless PHP means, its main features and pros vs cons to provide you with a better understanding of this approach towards 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!

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 purchases 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 focused 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 from 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 that surrounds 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 rest is taken care of by the vendor. 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. Whenever an action triggers your function, an instance spawns up 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 a standby mode to handle requests.

However, the situation between serverless and traditional hosting becomes exactly the opposite when you have a large number of 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 which 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 elements of the server. Therefore, if you deploy a serverless PHP application, there is an actual server running on the cloud that 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 make changes to 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 an increase in 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 be running 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 aspects of serverless computing, and how it is different 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, even if you decide not to deploy a complete application through serverless PHP, you may deploy modules.

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 you require them. Scaling is easy due to the serverless stack and the vendor bills you only for the amount of time that 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 stores. Files are not treated as a hierarchy in a file system, but as objects, which contain the data of the file itself and its 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 is 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, though, adopting a serverless architecture may not lead to much difference in costs.

Easier Deployment

Deployment of a serverless service does not require you to set up your server and configure 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

The primary issue that users highlight 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 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 gets total control of the infrastructure. Hence, you are unable to change your vendor if you go serverless, as migration can be a difficult task.

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. The process of debugging a serverless application to identify the root cause is difficult too.

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, deployment of a serverless application is highly specific to the vendor. Therefore, this post tries to address the implementations 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 that is triggered by an event and returns the string “Hello World”.

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

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

While the function is ready, you need to 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 you consider using serverless PHP for your application, ensure that you are fully aware of what serverless computing is, 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, 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 to take advantage of it.

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


If you enjoyed this article, then you'll love Kinsta’s WordPress hosting platform. Turbocharge your website and get 24/7 support from our veteran WordPress team. Our Google Cloud powered infrastructure focuses on auto-scaling, performance, and security. Let us show you the Kinsta difference! Check out our plans