Speeding Up PHP with OPcache in Docker

Tutorials

December 4th, 2018

docker.png

If you’re on Docker for Mac or Docker for Windows, you might see some noticeable slowness and time to the first byte (TTFB) depending on your application’s setup. One of the most important things you can do to improve performance is enabling the OPCache module (regardless of the development environment). There are other things like volume caching (if possible), but OPcache is a win that you want in any environment you’re running PHP applications.

OPcache Settings

When you enable the OPCache module, you need to consider a few things so that your configuration is development-friendly, yet, can be ready for production if you plan on using Docker in production.

Here’s the rough configuration you’ll end up with in development:

1[opcache]
2opcache.enable=1
3; 0 means it will check on every request
4; 0 is irrelevant if opcache.validate_timestamps=0 which is desirable in production
5opcache.revalidate_freq=0
6opcache.validate_timestamps=1
7opcache.max_accelerated_files=10000
8opcache.memory_consumption=192
9opcache.max_wasted_percentage=10
10opcache.interned_strings_buffer=16
11opcache.fast_shutdown=1

Note that we’ve hard-coded these values, which isn’t very flexible between environments. We’ll come back and make it more flexible in a minute!

The most important setting for development is the opcache.validate_timestamps=1 which allows us to make changes to our code. If you’re using a Docker volume, it means that OPcache will respect file timestamps and your changes will reflect immediately. In a production environment that’s not ideal, and that’s where our dynamic configuration will come into play shortly.

You shouldn’t copy/paste these settings verbatim without understanding what they do. The configuration primarily comes from the article Best Zend OpCache Settings/Tuning/Config by Steve Corona and is an excellent resource on understanding each of these values. Another excellent resource on performance (including OPcache) is Scaling Laravel by Chris Fidao.

Copying INI Settings in the Dockerfile

Here’s a rough Dockerfile for installing the OPcache module and copying in an INI file to configure OPCache:

1FROM php:7.2-apache-stretch
2
3RUN docker-php-ext-install opcache
4
5COPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
6COPY . /var/www/html

I’m not showing a complete working example of a PHP application. Laravel’s document root needs to be /var/www/html/public, but I am merely trying to demonstrate setting up OPcache in this article. For a complete example you should check out my course Docker for PHP Developers.

The Dockerfile assumes the following folder structure for organizing your Docker files:

1├── app
2├── bootstrap
3├── config
4├── database
5├── docker
6│   └── php
7│   └── conf.d
8├── public
9├── resources
10├── routes
11├── storage
12├── tests
13└── vendor

Note that in a real project you’d probably have a base image instead of having all this in your project, but I’m showing this so you can follow along with the OPcache-specific configuration.

Building the Dockerfile

Here’s the build command you can run to experiment with configuring OPcache:

1docker build --pull -t opcache-demo -f docker/Dockerfile .
2docker run --rm -it opcache-demo bash
3# In a running container:
4/var/www/html# php -m | grep OPcache
5Zend OPcache

Flexible Configuration with Environment

We have OPcache enabled, but if we want to make this configuration flexible we can use environment variables to configure INI settings:

1[opcache]
2
3opcache.enable=1
4opcache.revalidate_freq=0
5opcache.validate_timestamps=${PHP_OPCACHE_VALIDATE_TIMESTAMPS}
6opcache.max_accelerated_files=${PHP_OPCACHE_MAX_ACCELERATED_FILES}
7opcache.memory_consumption=${PHP_OPCACHE_MEMORY_CONSUMPTION}
8opcache.max_wasted_percentage=${PHP_OPCACHE_MAX_WASTED_PERCENTAGE}
9opcache.interned_strings_buffer=16
10
11opcache.fast_shutdown=1

Now that we have an environment-powered INI file, let’s provide some defaults for our project in the Dockerfile:

1FROM php:7.2-apache-stretch
2
3ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0" \
4 PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \
5 PHP_OPCACHE_MEMORY_CONSUMPTION="192" \
6 PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"
7
8RUN docker-php-ext-install opcache
9
10COPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
11COPY . /var/www/html

Note that by default we’ll disable timestamps, so we need to override this environment value in development. In this post, we’re not going to cover using Docker Compose to set environment, but this is the rough command you can run to make sure timestamps are validated in development:

1# Rebuild the image first
2docker build --pull -t opcache-demo -f docker/Dockerfile .
3
4docker run --rm -d \
5 -p 8080:80 \
6 -e "PHP_OPCACHE_VALIDATE_TIMESTAMPS=1" \
7 opcache-demo

With the Apache container running in the background, you can validate that the OPcache timestamp setting is 1 by verifying in the container:

1# get the container id
2docker ps
3docker exec -it 6002d83c6d24 bash
4
5# In a running container:
6/var/www/html# php -i | grep validate_timestamps
7opcache.validate_timestamps => On => On

As you can see, our configuration is now powered dynamically by environment variables! In Docker, your code will be cached with OPCache by default and will not update due to timestamps validation being disabled. Please note that if you’re using Nginx + PHP-FPM, you’ll need to either ensure that clear_env = no is in your FPM pool (probably www):

1[www]
2clear_env = no

You can also manually add environment variables to the pool if you don’t want to keep the entire environment available to PHP.

Learn More

While the ideas presented in this article aren’t exclusive to Docker, the extra bit of help from OPcache in development is helpful, without sacrificing the ability to update your code.

If you want to learn more about developing PHP applications with Docker and PHP, including Laravel, check out my course Docker for PHP Developers.


The links included are affiliate links which means if you decide to buy Laravel News gets a little kickback to help run this site.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.