Knowledge

Improving PHP performance

#Hosting

The performance and speed of PHP can be improved in different ways. Find out the multiple pathways to better performance.

Published by Mark van Eijk on October 10, 2024
Updated on November 21, 2024 · 1 minute read

  1. Enable OPcache
  2. Enable OPcache JIT compiler

Enable OPcache

Using the OPcache PHP extension is one of the most significant (and easy) things you can do to improve PHP performance. When a PHP script runs, the server translates the script into a form the computer can understand (called "opcode") every time someone visits the website. This process takes time. With the OPcache extension enabled thee translated version (opcode) is stored in memory, so the server doesn’t have to re-translate the script every time. Instead, it reuses the stored version, making the website load faster.

# Install OPcache extension (change PHP version accordingly)
sudo apt install php8.4-opache

# Enable it in php.ini
opcache.enable = 1
opcache.enable_cli = 1
opcache.max_accelerated_files = 50000
opcache.interned_strings_buffer = 64
opcache.memory_consumption = 256
opcache.save_comments = 1
opcache.validate_timestamps = 1

If you change opcache.validate_timestamps to 0 you can improve performance even more because OPcache does not need to check the file for modifications everytime. But then you will need to make sure you reload the PHP process every time you make changes in your code.

Enable OPcache JIT compiler

After enabling OPcache, you should also consider enabling the OPcache JIT compiler. It enhances performance by translating frequently executed PHP code into machine code at runtime, meaning running even more efficient on your server.

Enable JIT by adding these lines to the OPcache config:

opcache.jit=on
opcache.jit_buffer_size=128M

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

Related articles

How to get top processes with highest memory usage

The performance and speed of PHP can be improved in different ways. Find out the multiple pathways to better performance.

Read more →

How to get top processes with highest CPU usage

The performance and speed of PHP can be improved in different ways. Find out the multiple pathways to better performance.

Read more →