Skip to main content

Laravel on CloudFlow

Learn how to run a default Laravel app at the edge for low latency and high availability. You can use our repo as a template, or perform the steps yourself using the Kubernetes dashboard or kubectl commands.

note

Before starting, create a new CloudFlow Project and then delete the default Deployment and ingress-upstream Service to prepare the project for your new deployment.

Option 1 - Copy Our GitHub Repo

workflow status

  1. Make a new repo from our template: in your browser visit https://github.com/section/laravel-template and select Use this template (don't clone, don't fork, but use the template). Choose yourself as an owner, give it a name of your choice, and make it be Public (not Private).
  2. In your new GitHub repo, under Settings > Secrets > Actions, use New repository secret to add these two:
  3. Make a simple change to the HTML text in resources/views/welcome.blade.php and watch your changes go live.

Every time you push to the repo your project will be built and deployed to CloudFlow automatically using GitHub Actions.

Option 2 - Step by Step

Following are step-by-step instructions to deploy a default Laravel app to the edge on CloudFlow. We'll Dockerize it, push it to GitHub Packages, and deploy it on CloudFlow.

Prerequisites

  • You need Docker, PHP, and Composer installed so that you can build a Docker image.

Create the Laravel App

Create a Laravel app via the Composer create-project command:

composer create-project laravel/laravel laravel

After the Laravel app has been created, test it locally using the Laravel's Artisan CLI serve command:

cd laravel

php artisan serve

Access it by running curl http://localhost:8000 in your terminal or by visiting http://localhost:8000 in your browser. You should get the default Laravel welcome page.

Dockerize It

Let's build the container image that we'll deploy to CloudFlow. First, make a Dockerfile in your directory with the following content.

Dockerfile
FROM lorisleiva/laravel-docker:8.1

COPY . .

RUN composer install

CMD php artisan serve --host=0.0.0.0

Create a .dockerignore file from the .gitignore file:

cp .gitignore .dockerignore

Build and tag the Docker image.

docker build . -t ghcr.io/YOUR_GITHUB_USERNAME/laravel:main

Push It

Push it to GitHub Packages. This makes it available to CloudFlow.

docker push ghcr.io/YOUR_GITHUB_USERNAME/laravel:main

Be sure to make it public. To see your packages and make this change, visit https://github.com/YOUR_GITHUB_USERNAME?tab=packages.

Deploy It

Next, create a CloudFlow deployment for the Laravel app with a laravel-deployment.yaml file, substituting YOUR_GITHUB_USERNAME and the environment variables accordingly. This will direct CloudFlow to distribute the container you've pushed to GitHub Packages.

laravel-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: laravel
labels:
app: laravel
spec:
replicas: 1
selector:
matchLabels:
app: laravel
template:
metadata:
labels:
app: laravel
spec:
containers:
- name: laravel
image: ghcr.io/YOUR_GITHUB_USERNAME/laravel:main
imagePullPolicy: Always
resources:
requests:
memory: "200Mi"
cpu: "200m"
limits:
memory: "200Mi"
cpu: "200m"
ports:
- containerPort: 80
env:
- name: APP_NAME
value: "Laravel"
- name: APP_ENV
value: "production"
- name: APP_KEY
value: "YOUR_APP_KEY"
- name: APP_DEBUG
value: "false"
- name: APP_URL
value: "https://YOUR.DOMAIN.COM"

- name: LOG_CHANNEL
value: "stack"
- name: LOG_DEPRECATIONS_CHANNEL
value: "null"
- name: LOG_LEVEL
value: "debug"

- name: BROADCAST_DRIVER
value: "log"
- name: CACHE_DRIVER
value: "file"
- name: FILESYSTEM_DISK
value: "local"
- name: QUEUE_CONNECTION
value: "sync"
- name: SESSION_DRIVER
value: "cookie"
- name: SESSION_LIFETIME
value: "120"

- name: MAIL_MAILER
value: "smtp"
- name: MAIL_HOST
value: "mailhog"
- name: MAIL_PORT
value: "1025"
- name: MAIL_USERNAME
value: "null"
- name: MAIL_PASSWORD
value: "null"
- name: MAIL_ENCRYPTION
value: "null"
- name: MAIL_FROM_ADDRESS
value: "hello@example.com"
- name: MAIL_FROM_NAME
value: "Laravel"

Apply this deployment resource to your Project with either the Kubernetes dashboard or kubectl apply -f laravel-deployment.yaml.

tip

For a production Laravel app, use Kubernetes Secrets as the values for private environment variables.

Expose It

Expose it on the internet, mapping the container's port 8000.

ingress-upstream.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: ingress-upstream
name: ingress-upstream
spec:
ports:
- name: 80-8080
port: 80
protocol: TCP
targetPort: 8000
selector:
app: laravel
sessionAffinity: None
type: ClusterIP

Apply this service resource to your Project with either the Kubernetes dashboard or kubectl apply -f ingress-upstream.yaml.

See the pods running on CloudFlow's network with either the Kubernetes dashboard or kubectl get pods -o wide. The -o wide switch shows where your app is running according to the default AEE location optimization strategy. Your app will be optimally deployed according to traffic. In lieu of significant traffic, your deployment will be made to default locations.

Finally, follow the instructions that configure DNS and TLS.

See What You've Built

See the Laravel app you've built by visiting the https://YOUR.DOMAIN.COM, substituting YOUR.DOMAIN.COM according to your DNS and HTTPS configuration.