DEV Community

Roelof Jan Elsinga
Roelof Jan Elsinga

Posted on • Originally published at roelofjanelsinga.com on

5x performance increase: A simple trick to speed up your PHP application

PHP Logo

5x performance increase: A simple trick to speed up your PHP application

Have you been optimizing your PHP application but hit a wall and can't seem to get any more performance?

I've got a little (known) trick you can try! This simple addition increased the performance of one of my PHP application from 16.5 req/sec to 83 req/sec and even works in a Docker container!

What is this trick? OPcache!

What is OPcache?

OPcache is a PHP extension that compiles and caches your PHP scripts, so it can use the cached version of your application when you run the program. This is a huge speed improvement, because PHP is not a compiled language. Normally, PHP compiles your application and then executes it every time you do a request on your application. With OPcache, we can skip the compilation part of the request cycle.

Using OPcache in Docker

I run all of my PHP applications in Docker, so I will focus on how to make this work for a Docker image. You can replicate most of the steps for your own system, so if you're not using Docker, you can still follow along.

The configuration

This is the basic configuration we're using for our OPcache extension. This config file should replace the default opcache.ini at: /usr/local/etc/php/conf.d/opcache.ini.

[opcache]
opcache.enable=1
opcache.revalidate_freq=0
opcache.validate_timestamps=0
opcache.max_accelerated_files=10000
opcache.memory_consumption=192
opcache.max_wasted_percentage=10
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1
Enter fullscreen mode Exit fullscreen mode

This configuration will cache the compiled PHP scripts, even if you've made changes to them. For these changes to show up, you'll have to restart your PHP-FPM daemon. However, if you're running your PHP application in Docker, you won't have to worry about this. You should create a new Docker image when you make changes anyway, so you always start in production fresh after your made your changes.

If you want to use OPcache on your development environment, you'll want to see any changes you make right away, so you can set opcache.enable=0 or let the cache invalidate itself by using: opcache.validate_timestamps=1.

Install OPcache in Docker

Let's install OPcache on a php-fpm:alpine image:

FROM php:8.1-fpm-alpine

# Install build dependencies and the OPcache extension
RUN apk add --no-cache $PHPIZE_DEPS \
    && docker-php-ext-install opcache \
    && apk del $PHPIZE_DEPS

# Copy the opcache.ini into your Docker image    
COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

# Run your application
CMD php-fpm
Enter fullscreen mode Exit fullscreen mode

This is a very minimal Dockerfile and might not even compile, but it does show the steps you'll need to take to enable OPcache in your application.

With this configuration, I've sped up my PHP application by 5x, increasing the requests per second from 16.5 to 83. I'm very satisfied with the results, so I hope it works for you as well.

Top comments (0)