PHP (Hypertext Preprocessor) is a widely-used server-side scripting language known for its efficiency in web development. However, as web applications grow in complexity, performance optimization becomes crucial.

One of the most effective ways to enhance PHP performance is by using Opcache, which caches the compiled bytecode of PHP scripts, reducing the overhead of loading and parsing scripts on each request.

This article will guide you through installing and configuring Opcache to accelerate and optimize PHP performance on a Linux system.

What is Opcache?

Opcache is a built-in opcode caching mechanism for PHP that stores the compiled bytecode of PHP scripts in shared memory.

When a PHP script is executed, Opcache checks if a compiled version is available in memory. If it is, Opcache bypasses the parsing and compilation steps, resulting in faster script execution.

This significantly improves response times and reduces the server load, making it an essential tool for optimizing PHP applications.

Installing Opcache in Linux

Before installation, it’s a good practice to update your package repository to ensure you have the latest package information.

sudo apt update  [On Debian/Ubuntu]
sudo dnf update  [On RHEL/CentOS]

If you’re using a Debian-based system like Ubuntu, you can install Opcache via the php-opcache package.

sudo apt install php-opcache

If you’re on a Red Hat-based system like CentOS, you can install Opcache using the following command:

sudo yum install php-opcache

After installing Opcache, you can verify its installation by running:

php -m | grep opcache
OR
php -i | grep opcache

If you see opcache in the output, it means Opcache is installed successfully.

Check Opcache Installation
Check Opcache Installation

Configuring Opcache in Linux

Once Opcache is installed, you need to configure it in the PHP configuration file (php.ini) at the following locations, depending on your installation.

You can find the location of your php.ini file by running:

php --ini
Find PHP Configuration File
Find PHP Configuration File

To edit the configuration file, use a text editor (e.g., Nano or vim):

sudo vi /etc/php.ini

Add or modify the following Opcache settings:

; Enable Opcache
opcache.enable=1

; Set the memory size for the Opcache (e.g., 128MB)
opcache.memory_consumption=128

; Set the maximum number of scripts to cache
opcache.max_accelerated_files=10000

; Set the cache validity time in seconds
opcache.revalidate_freq=2

; Enable file validation for updated scripts
opcache.validate_timestamps=1

; Set the timeout for a request before it is terminated
opcache.max_file_size=2M

; Enable opcache for the CLI
opcache.enable_cli=1

Explanation of Settings:

  • opcache.memory_consumption: The amount of memory allocated for the Opcache. You can adjust this based on your server’s RAM.
  • opcache.max_accelerated_files: The maximum number of PHP scripts that Opcache will cache.
  • opcache.revalidate_freq: The frequency (in seconds) at which Opcache checks for script updates.
  • opcache.validate_timestamps: If enabled, Opcache checks for changes in scripts and invalidates cached scripts if they have changed.
  • opcache.enable_cli: Enables Opcache for the command-line interface.

After making the changes, save the file and restart your Apache web server for the changes to take effect.

sudo systemctl restart apache2
OR
sudo systemctl restart httpd

If you are using Nginx, use:

sudo systemctl restart nginx

Testing Opcache in Linux

To ensure that Opcache is working correctly, you can create a PHP file (info.php) with the following content in your web server’s document root (e.g., /var/www/html/).

<?php
phpinfo();
?>

Access the file through your web browser:

http://your_server_ip/info.php

Look for the “Opcode Caching” section in the output. If Opcache is installed and configured correctly, you should see information about the Opcache settings.

Check Opcache Settings
Check Opcache Settings

Monitoring Opcache in Linux

To monitor Opcache performance, you can use various tools, such as the opcache-gui, to visualize cache statistics.

git clone https://github.com/amnuts/opcache-gui.git
sudo mv opcache-gui /var/www/html/

Access the GUI in your browser:

http://your-server-ip/opcache-gui
opcache-gui Monitoring Tool
opcache-gui Monitoring Tool

Alternatively, you can also use command-line tools to get detailed information about cache performance. For example, to see the Opcache status from the command line, you can use the following command:

php -i | grep opcache

This command will display Opcache-related information, including memory usage and cache hit rates.

Conclusion

Installing and setting up Opcache is an easy process that can greatly improve your PHP applications by reducing the overhead of script execution and allowing your web server to handle more requests efficiently, resulting in faster response times and an improved user experience.

Similar Posts