PHP OPcache, short for Opcode Cache, is a powerful performance optimization tool for PHP applications. It stores precompiled script bytecode in shared memory, reducing the need for PHP to load and parse scripts on each request. This significantly improves the execution speed of PHP applications by eliminating redundant compilation processes.
In this tutorial, we will guide you through installing and configuring PHP OPcache on Ubuntu 22.04.
Step 1 – Install PHP OPcache for Apache
PHP OPcache is included in the PHP core starting from PHP 5.5. You can install it with Apache and other packages using the following command.
apt update -y apt-get install apache2 libapache2-mod-php php php-cli php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml -y
Once the installation is completed, you can verify the PHP version using the following command.
php -version
Output.
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
Step 2 – Configure PHP OPcache
To optimize PHP OPcache for your environment, you’ll need to modify the configuration settings. The configuration file for OPcache is usually located at /etc/php/{PHP_VERSION}/apache2/php.ini. Replace {PHP_VERSION} with your installed PHP version (e.g., 8.1).
nano /etc/php/8.1/apache2/php.ini
Adjust the following settings based on your application’s requirements:
opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=200
Next, restart the Apache service to apply the changes.
systemctl restart apache2
You can verify that OPcache is enabled and check its status by creating a PHP script with the following content:
php -i | grep opcache
Output.
opcache.max_file_size => 0 => 0 opcache.max_wasted_percentage => 5 => 5 opcache.memory_consumption => 128 => 128 opcache.opt_debug_level => 0 => 0 opcache.optimization_level => 0x7FFEBFFF => 0x7FFEBFFF opcache.preferred_memory_model => no value => no value opcache.preload => no value => no value opcache.preload_user => no value => no value opcache.protect_memory => Off => Off opcache.record_warnings => Off => Off opcache.restrict_api => no value => no value opcache.revalidate_freq => 2 => 2 opcache.revalidate_path => Off => Off opcache.save_comments => On => On opcache.use_cwd => On => On opcache.validate_permission => Off => Off opcache.validate_root => Off => Off opcache.validate_timestamps => On => On
Step 3 – Install PHP OPcache for NGINX
You can install the PHP OPcache with NGINX by running the following command.
apt-get install nginx php-opcache php-fpm -y
For NGINX, the configuration file for PHP OPcache is usually located at /etc/php/{PHP_VERSION}/fpm/php.ini. You can edit it with the following command.
nano /etc/php/8.1/fpm/php.ini
Adjust the following settings based on your application’s requirements:
opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=3000 opcache.revalidate_freq=200
Save and close the file, then restart NGINX and PHP-FPM services to apply the changes.
systemctl restart nginx systemctl restart php8.1-fpm
Conclusion
Congratulations! You have successfully installed and configured PHP OPcache on your Ubuntu 22.04 server. OPcache plays a crucial role in enhancing the performance of PHP applications by reducing the overhead of script compilation. Remember to fine-tune the configuration settings based on your application’s requirements and monitor the server’s performance for further optimization opportunities. You can now try to deploy PHP OPcache on dedicated server hosting from Atlantic.Net!