Laravel Octane

Full speed ahead.

Demo

A demo of how Laravel Octane together with FrankenPHP can supercharge your applications performance.

Laravel Octane uses performant servers like FrankenPHP to boost your applications performance by booting it once into memory. This once only boot into memory allows requests to be served much quicker, as unlike with FPM which requires a re-boot of the application on every request.

The example for Laravel Octane involves LaraPi, it was first setup using FPM and because of the processor in the Raspberry Pi, each request was taking multiple seconds, so, after some research, Octane with FrankenPHP was implemented and request times reduced to the milliseconds.

Response Time Comparison

PHP FPM (seconds)

FrankenPHP (milliseconds)

Let me explain a bit on how I use it in LaraPi.

Show Your Working

A quick walkthrough of how this works.

My assumptions, at the very least, are that you have a working Laravel project, Docker will also be used in this guide.

First we need to install Octane, you can do this with the following commands.

composer require laravel/octane
php artisan octane:install

You will also need a compatible PHP server, in this example that will be FrankenPHP, Docker can be used to run this server, the following is an Dockerfile for this.

1FROM dunglas/frankenphp
2 
3# Install only essential system packages and PHP extensions
4RUN apt-get update && apt-get install -y \
5 git \
6 curl \
7 unzip \
8 libonig-dev \
9 libzip-dev \
10 libssl-dev && \
11 install-php-extensions \
12 pcntl \
13 pdo_mysql \
14 mbstring \
15 bcmath \
16 zip \
17 intl \
18 pcntl && \
19 pecl install redis && \
20 docker-php-ext-enable redis && \
21 apt-get clean && rm -rf /var/lib/apt/lists/*
22 
23# Set working directory
24USER www-data
25ENV HOME=/app
26WORKDIR /app
27 
28EXPOSE 9000
29 
30# Run Laravel Octane via FrankenPHP
31ENTRYPOINT ["php", "artisan", "octane:frankenphp", "--host=0.0.0.0", "--port=9000"]

You will need to set an environment variable to tell Laravel Octane to use FrankenPHP, this can be done in your .env file by adding OCTANE_SERVER=frankenphp

Nginx will be used to proxy requests to FrankenPHP, so we need to define some Nginx configuration, create a default.conf file with the following.

1resolver 127.0.0.11 valid=10s;
2server_tokens off;
3
4server {
5 listen 80 default_server;
6 location / {
7 proxy_pass http://app:9000;
8 include includes/proxy_defaults.conf;
9 }
10}

We can now run our application under FrankenPHP, once this is done, we can use Nginx to proxy requests to it and serve users, all we need to do is run the following Docker Compose stack.

1services:
2 app:
3 build:
4 dockerfile: frankenphp.Dockerfile
5 context: .
6 image: larapi/frankenphp
7 pull_policy: always
8 restart: unless-stopped
9 working_dir: /app
10 depends_on:
11 - mariadb
12 volumes:
13 - ./:/app
14 networks:
15 - larapi
16 
17 mariadb:
18 image: mariadb:lts
19 pull_policy: always
20 restart: unless-stopped
21 environment:
22 MARIADB_DATABASE: larapi
23 MARIADB_ROOT_PASSWORD: larapi
24 MARIADB_PASSWORD: larapi
25 MARIADB_USER: larapi
26 volumes:
27 - mariadb-data:/var/lib/mysql
28 command: --max_allowed_packet=64M
29 networks:
30 - larapi
31 
32 nginx:
33 image: nginx
34 pull_policy: always
35 restart: unless-stopped
36 ports:
37 - "80:80"
38 volumes:
39 - default.conf:/etc/nginx/conf.d/default.conf
40 - nginx-logs:/var/logs/nginx
41 networks:
42 - larapi
43 
44volumes:
45 mariadb-data:
46 driver: local
47 nginx-logs:
48 driver: local
49 
50networks:
51 larapi:
52 driver: bridge

This stack will run the bare minimum (Nginx, FrankenPHP and MariaDB), bring it up with the following command.

docker compose up

You should now be able to go to http://localhost in your browser and see your application.

An important note to remember is that because of the way FrankenPHP works, only boot once, so if you make any changes to your applications code, you will need to restart the FrankenPHP Docker container.

Resource Links

A few links to supporting resources.