PHP in AWS Lambda: 4 Ways to Deploy & Run Serverless (2023)

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Are you looking to run & deploy your PHP app into AWS Lambda?

Then you’ve come to the right article.

We’ve written a comprehensive guide to deploying PHP applications on AWS Lambda.

As more developers explore the benefits of serverless architecture, it’s essential to understand the available options for running your PHP applications in such environments. AWS Lambda, a popular serverless platform, offers various deployment options to suit your needs.

Deployment OptionCustom Runtime with Serverless FrameworkBref with Serverless FrameworkDocker ContainersLaravel Vapor
DifficultyModerate to AdvancedEasy to ModerateModerateEasy
Setup TimeMore time-consumingQuick and straightforwardModerateQuick
Control Over PHP Runtime and ExtensionsHighModerateHighLow
Integration with the Serverless FrameworkYesYesYesNo
Specific to Laravel ApplicationsNoNoNoYes
Consistency Across Platforms and EnvironmentsNoNoYesNo
Community Support and DocumentationLimitedGoodGoodGood
Subscription FeeNoNoNoYes
Options to Use PHP with Lambda
Deploy & Run PHP in AWS Lambda
Running PHP in AWS Lambda

Maximize the Value You Get AWS for Your Applications

Stop running in circles and develop your applications faster and cheaper in AWS. This guide will walk you through ways to maximize AWS to generate real value for your needs. We dive into picking the right services to scale, tighten security and maximize costs.

Download our free guide now and get started with confidence.

Article Highlights

  • Can PHP be Serverless
  • Can you run PHP in AWS Lambda
  • Deploying PHP on AWS Lambda using Bref and Serverless Framework
  • Deploying PHP on AWS Lambda using Docker
  • Easiest: Laravel Vapor managed service

Table of Contents

  1. Can PHP be Serverless
  2. Can you run PHP in AWS Lambda
  3. Deploy PHP on AWS Lambda using Custom Runtime.
  4. Deploying PHP on AWS Lambda using Bref and Serverless Framework
  5. Deploying PHP on AWS Lambda using Docker
  6. Deploying Laravel Applications on AWS Lambda with Laravel Vapor

Can PHP be Serverless

Yes, PHP can be run in a serverless architecture. Serverless computing allows developers to build and run applications without managing any underlying infrastructure. Instead, the cloud provider automatically provisions resources, scales the application based on demand, and charges only for the compute time consumed.

Although PHP is traditionally associated with server-based web applications, it can also be used effectively in serverless environments. With the advent of custom runtimes in platforms like AWS Lambda, it’s now possible to run PHP applications in a serverless architecture.

Serverless PHP applications can benefit from reduced operational overhead, automatic scaling, and cost optimization, as you only pay for your application compute time. This makes PHP a viable option for developers looking to leverage the advantages of serverless computing.

List of Serverless Platforms to Run PHP

  1. AWS Lambda: AWS Lambda is a popular serverless platform that supports custom runtimes, including PHP. You can deploy PHP applications using custom PHP runtimes, Docker containers, or third-party tools like Bref and Laravel Vapor.
  2. AWS App Runner: AWS App Runner is a newer service from AWS that allows you to follow a Platform as a Service model like Heroku. You push your PHP application to Github, and App Runner will manage the deployment, autoscaling and infrastructure. You can read our article about it here.
  3. Google Cloud Functions: While Google Cloud Functions does not have native support for PHP, you can deploy PHP applications using custom runtimes or containers. Utilize Google Cloud Buildpacks for deploying PHP applications in a serverless environment.
  4. Azure Functions: Azure Functions is Microsoft’s serverless platform that allows running PHP applications using custom handlers and Docker containers. You can deploy your PHP application using the provided Linux-based images or create custom images.
  5. Vercel: Vercel is a serverless platform known for deploying frontend applications, but it also supports deploying serverless functions using custom runtimes. You can deploy PHP applications on Vercel using the vercel-php runtime.
  6. Heroku: While Heroku is a Platform as a Service (PaaS), it provides serverless-like features through Heroku Dynos. You can deploy PHP applications on Heroku using the PHP buildpack or Docker containers.

These serverless platforms allow PHP developers to leverage the benefits of serverless architecture, such as automatic scaling, reduced operational overhead, and cost optimization. Remember that each platform may have specific requirements and limitations when running PHP applications, so reviewing its documentation and guidelines for deploying PHP applications is essential.

Can you run PHP in AWS Lambda

Yes, you can run PHP in AWS Lambda. Although PHP is not officially supported as a native runtime, AWS Lambda allows you to use custom runtimes, making it possible to execute PHP applications in a serverless environment.

There are a few methods to run PHP on AWS Lambda:

  1. Custom PHP Runtime: You can create a custom PHP runtime using the provided AWS Lambda Runtime API. This involves creating a custom PHP runtime layer and a bootstrap file that handles Lambda events and interacts with the Lambda Runtime API.
  2. Bref and Serverless Framework: Bref is a popular open-source project that provides a pre-built custom PHP runtime and integrates with the Serverless Framework, simplifying deploying PHP applications on AWS Lambda.
  3. Docker Containers: AWS Lambda supports custom container images, enabling you to package and deploy PHP applications as Docker containers. You can use the official AWS Lambda base images for PHP or create your custom Docker images.
  4. Laravel Vapor: Laravel Vapor is a serverless deployment platform designed explicitly for Laravel applications. It integrates seamlessly with AWS Lambda, making it easy to deploy, scale, and manage Laravel applications in a serverless environment.

Using one of these methods, you can deploy and run your PHP applications on AWS Lambda and take advantage of the serverless architecture benefits, such as automatic scaling, reduced operational overhead, and cost optimization based on compute time.

Deploying PHP on AWS Lambda using Custom Runtime and Serverless Framework

A custom PHP runtime allows you to run PHP applications on AWS Lambda using the AWS Lambda Runtime API. Combined with the Serverless Framework, you can streamline the deployment and management of your PHP applications in a serverless environment.

Strengths & Weaknesses

Strengths:

  • Full control over the PHP runtime and extensions
  • Flexibility to customize the runtime environment as needed
  • Integration with the Serverless Framework simplifies deployment and management

Weaknesses:

  • More complex and time-consuming setup compared to other solutions
  • Requires manual management of PHP runtime updates
  • May require more extensive AWS knowledge

Difficulty Rating

Moderate to Advanced

Deployment Steps

  1. Install the Serverless Framework: Install the Serverless Framework globally on your system using npm:
  2. Configure AWS credentials: Set up your AWS credentials in the Serverless Framework by following the official guide.
  3. Create a PHP project: Set up a new PHP project or use an existing one.
  4. Create a custom PHP runtime layer: Follow the AWS custom runtime documentation to create a custom PHP runtime layer. Package the PHP binary and any required extensions as a Lambda layer.
  5. Create a bootstrap file: In your PHP project, create a bootstrap file that handles Lambda events and communicates with the Lambda Runtime API.
  6. Configure the serverless.yml file: In your project directory, create a serverless.yml configuration file. Configure the custom PHP runtime, Lambda layer, and other settings, such as function handlers and events.
  7. Deploy your PHP application: Deploy your PHP application using the Serverless Framework by running the following command:

Overall Recommendation

Using a custom PHP runtime with the Serverless Framework is suitable for developers who need full control over their runtime environment and extensions. However, it requires more extensive AWS knowledge and time than other solutions like Bref or Laravel Vapor.

When to Use and Avoid This Approach

Use this approach when:

  • You need full control over your PHP runtime and extensions
  • You require a customized runtime environment for specific needs
  • You have experience with AWS Lambda and the Serverless Framework

Avoid this approach when:

  • You’re looking for a faster and more straightforward solution
  • You’re new to AWS Lambda or the Serverless Framework
  • You prefer an out-of-the-box solution like Bref or Laravel Vapor

Deploying PHP on AWS Lambda using Bref and Serverless Framework

Bref is an open-source project that simplifies deploying PHP applications on AWS Lambda. It provides a pre-built custom PHP runtime and integrates with the Serverless Framework, making the deployment process easier and more streamlined.

Strengths & Weaknesses

Strengths:

  • Quick and straightforward setup
  • Pre-built custom PHP runtime with common extensions
  • Seamless integration with the Serverless Framework
  • Active community and support

Weaknesses:

  • Less control over the PHP runtime compared to creating a custom runtime
  • May not cover specific needs or custom extensions

Difficulty Rating

Easy to Moderate

Deployment Steps

  1. Install the Serverless Framework: Install the Serverless Framework globally on your system using npm:
  2. Configure AWS credentials: Set up your AWS credentials in the Serverless Framework by following the official guide.
  3. Install Bref: In your PHP project, install Bref as a dependency using Composer: javascript
  4. Initialize Bref: Run the following command to create a serverless.yml configuration file and a sample index.php file.
  5. Customize the serverless.yml file: Edit the serverless.yml configuration file to define your application’s functions, events, and other settings.
  6. Deploy your PHP application: Deploy your PHP application using the Serverless Framework by running the following command:

Overall Recommendation

Using Bref with the Serverless Framework is an excellent option for developers looking to deploy PHP applications on AWS Lambda quickly and easily. It provides a pre-built custom PHP runtime and simplifies the deployment process, making it suitable for most use cases.

When to Use and Avoid This Approach

Use this approach when:

  • You want a quick and straightforward solution
  • You don’t require full control over the PHP runtime
  • You’re looking for an out-of-the-box solution with community support

Avoid this approach when:

  • You need full control over your PHP runtime and extensions
  • You have specific needs that Bref may not cover
  • You prefer to use Docker containers or other specialized deployment methods

Deploying PHP on AWS Lambda using Docker Containers

Docker containers allow you to package and deploy PHP applications on AWS Lambda using custom container images. This method provides flexibility in configuring the runtime environment and enables you to use the same Docker images across different platforms and stages of your application.

Strengths & Weaknesses

Strengths:

  • Consistency across different platforms and environments
  • Flexibility in configuring the runtime environment
  • Easy integration with your existing Docker-based workflow
  • Allows the use of any PHP version and extensions

Weaknesses:

  • Requires Docker knowledge and experience
  • May have a steeper learning curve for beginners
  • May require more time to set up compared to other solutions

Difficulty Rating

Moderate

Deployment Steps

  1. Install Docker: Ensure that Docker is installed and configured on your system.
  2. Create a Dockerfile: In your PHP project, create a Dockerfile to define the runtime environment, install the required PHP version and extensions, and configure your application.
  3. Build the Docker image: Build the Docker image using the docker build command, and tag it with an appropriate name.
  4. Push the Docker image to a registry: Push the built Docker image to a container registry, such as Docker Hub or Amazon ECR, using the docker push command.
  5. Install the Serverless Framework: Install the Serverless Framework globally on your system using npm.
  6. Configure AWS credentials: Set up your AWS credentials in the Serverless Framework by following the official guide.
  7. Create a serverless.yml file: In your project directory, create a serverless.yml configuration file. Configure the function to use the Docker image from the container registry.
  8. Deploy your PHP application: Deploy your PHP application using the Serverless Framework by running the serverless deploy command.

Overall Recommendation

Using Docker containers to deploy PHP applications on AWS Lambda is an excellent option for developers with Docker experience and require a consistent runtime environment across different platforms. This approach offers flexibility in configuring the PHP runtime but may have a steeper learning curve for beginners.

When to Use and Avoid This Approach

Use this approach when:

  • You’re already familiar with Docker and containerization
  • You need consistency across different platforms and environments
  • You require flexibility in configuring your PHP runtime

Avoid this approach when:

  • You’re new to Docker and containerization
  • You prefer a more straightforward deployment method
  • You want to use a pre-built solution like Bref or Laravel Vapor

Deploying PHP Laravel Applications on AWS Lambda using Laravel Vapor

Laravel Vapor is a serverless deployment platform designed explicitly for Laravel applications. It simplifies deploying, scaling, and managing Laravel applications in a serverless environment on AWS Lambda.

Strengths & Weaknesses

Strengths:

  • Tailored for Laravel applications
  • Easy setup and deployment process
  • Automatic scaling and optimized performance
  • Integration with other Laravel ecosystem tools

Weaknesses:

  • Limited to Laravel applications
  • Paid service with a subscription fee
  • Less control over the runtime environment compared to custom solutions

Difficulty Rating

Easy

Deployment Steps

  1. Install the Vapor CLI: Install the Vapor CLI globally on your system using Composer: javascript
  2. Create a Vapor account: Sign up for a Laravel Vapor account on the official website.
  3. Configure AWS credentials: Set up your AWS credentials in Laravel Vapor by following the official documentation.
  4. Create a new Vapor project: In your Laravel project, run the vapor init command to create a vapor.yml configuration file.
  5. Customize the vapor.yml file: Edit the vapor.yml configuration file to define your application’s environment, resources, and other settings.
  6. Deploy your Laravel application: Deploy your Laravel application using the Vapor CLI by running the following command

Overall Recommendation

Laravel Vapor is an excellent option for developers working with Laravel applications looking to leverage serverless architecture on AWS Lambda. It offers a seamless deployment process and integrates well with the Laravel ecosystem. However, it is limited to Laravel applications and requires a subscription fee.

When to Use and Avoid This Approach

Use this approach when:

  • You’re working with Laravel applications
  • You want an easy and seamless deployment process
  • You’re willing to pay for a subscription service

Avoid this approach when:

  • You’re not working with Laravel applications
  • You need full control over your PHP runtime and extensions
  • You prefer a free or open-source solution like Bref or custom runtimes

Run PHP in AWS Lambda

In this article, we’ve explored different approaches to running PHP applications on AWS Lambda. To recap, we’ve covered the following deployment options:

  1. Custom Runtime with Serverless Framework: This method provides full control over the PHP runtime and extensions but may have a steeper learning curve and requires more time to set up.
  2. Bref with Serverless Framework: A quick and easy-to-use solution with a pre-built custom PHP runtime, suitable for most use cases but may not cover specific needs or custom extensions.
  3. Docker Containers: Offers consistency across different platforms and environments, flexibility in configuring the runtime environment, and easy integration with Docker-based workflows.
  4. Laravel Vapor: A serverless deployment platform tailored for Laravel applications, providing an easy setup and seamless deployment process but limited to Laravel applications and requiring a subscription fee.

Remember that PHP can run in serverless environments, and AWS Lambda is one of the many serverless systems that support PHP. Each deployment option has its strengths and weaknesses, and the best approach for your application depends on your specific needs, experience, and preferences.

In conclusion, running PHP applications on AWS Lambda is a viable option for developers considering serverless architecture. By considering factors such as ease of use, control over the runtime environment, consistency, community support, and cost, you can select the best deployment method for your PHP application in AWS Lambda.

Maximize the Value You Get AWS for Your Applications

Stop running in circles and develop your applications faster and cheaper in AWS. This guide will walk you through ways to maximize AWS to generate real value for your needs. We dive into picking the right services to scale, tighten security and maximize costs.

Download our free guide now and get started with confidence.

Building Web Applications in AWS

This article is part of our series to make AWS easy. We love AWS, but let’s be honest. It isn’t effortless. It’s way too complicated. We’ve created this learning path to help level you up and onboard your PHP app quickly.

Did you find this article helpful?

If you need additional support, I offer a few a few ways to help.

I hope you're enjoying this article.

Here are a few ways that I can provide additional technical support.