Speeding Up PHP with OPcache in Docker
Published on by Paul Redmond
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.
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:
[opcache]opcache.enable=1; 0 means it will check on every request; 0 is irrelevant if opcache.validate_timestamps=0 which is desirable in productionopcache.revalidate_freq=0opcache.validate_timestamps=1opcache.max_accelerated_files=10000opcache.memory_consumption=192opcache.max_wasted_percentage=10opcache.interned_strings_buffer=16opcache.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:
FROM php:7.2-apache-stretchRUN docker-php-ext-install opcacheCOPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.iniCOPY . /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:
├── app├── bootstrap├── config├── database├── docker│ └── php│ └── conf.d├── public├── resources├── routes├── storage├── tests└── 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:
docker build --pull -t opcache-demo -f docker/Dockerfile .docker run --rm -it opcache-demo bash# In a running container:/var/www/html# php -m | grep OPcacheZend 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:
Now that we have an environment-powered INI file, let’s provide some defaults for our project in the Dockerfile:
FROM php:7.2-apache-stretchENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0" \PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \PHP_OPCACHE_MEMORY_CONSUMPTION="192" \PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"RUN docker-php-ext-install opcacheCOPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.iniCOPY . /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:
# Rebuild the image firstdocker build --pull -t opcache-demo -f docker/Dockerfile .docker run --rm -d \-p 8080:80 \-e "PHP_OPCACHE_VALIDATE_TIMESTAMPS=1" \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:
# get the container iddocker psdocker exec -it 6002d83c6d24 bash# In a running container:/var/www/html# php -i | grep validate_timestampsopcache.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]clear_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.
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.