Website Vidya
Linux & ServersStep-by-Step Guide

How to Enable Imagick PHP Extension: 6 Easy Steps

The Imagick PHP extension is a powerful tool that allows developers to manipulate images using the ImageMagick library. ImageMagick is a robust software suite that supports a wide range of image formats and provides extensive functionality ...

RR
Rajesh RayLead Systems Educator
Published:
8 min read
How to Enable Imagick PHP Extension: 6 Easy Steps
AI Executive SummaryCORE CIS AI

8 min read

The Imagick PHP extension is a powerful tool that allows developers to manipulate images using the ImageMagick library. ImageMagick is a robust software suite that supports a wide range of image formats and provides extensive functionality ...

Key Highlights
  • Introduction to OPcache
  • Prerequisites
  • How to Install OPcache PHP Extension on Linux
Grounded in full engineering runbookCORE CIS Ai Engine

PHP is one of the most widely used server-side scripting languages for web development. To enhance the performance of PHP applications, various optimization techniques and tools are available. One such tool is OPcache, a PHP extension that improves performance by storing precompiled script bytecode in shared memory, thereby reducing the need for PHP to load and parse scripts on each request.

In this article, we will provide a comprehensive guide on how to install OPcache PHP extension on Linux. We will cover the installation process on different Linux distributions, configuration, and verification steps to ensure that OPcache is working correctly.


Introduction to OPcache

OPcache is a Zend Engine extension that caches precompiled PHP scripts in shared memory. This caching mechanism significantly reduces the overhead of parsing and compiling PHP scripts on each request, leading to faster execution times and improved performance for PHP applications.

OPcache is included by default in PHP 5.5.0 and later versions. However, it may not be enabled by default, so you may need to install and configure it manually.

Advertisement

Prerequisites

Before proceeding with the installation of OPcache, ensure that you have the following prerequisites in place:

  • A Linux-based operating system (e.g., Ubuntu, Debian, CentOS, RHEL, Fedora, or Arch Linux).
  • PHP is installed on your system. You can check the PHP version by running the following command:
  php -v
  • Administrative privileges (root or sudo access) to install packages and modify configuration files.

How to Install OPcache PHP Extension on Linux

The installation process for OPcache varies depending on the Linux distribution you are using. Below, we provide step-by-step instructions for installing OPcache on popular Linux distributions.

Installing OPcache on Ubuntu/Debian

Ubuntu and Debian-based distributions use the apt package manager to install software packages. Follow these steps to install OPcache:

Update the Package List:

Before installing any new packages, it is a good practice to update the package list:

Advertisement
sudo apt update

Install the OPcache Extension:

Use the following command to install the OPcache extension:

sudo apt install php-opcache

This command will install the OPcache extension for the default PHP version on your system.

Enable the OPcache Extension:

After installation, the OPcache extension should be enabled automatically. However, you can verify this by checking the PHP configuration file:

Advertisement
sudo nano /etc/php/<php_version>/apache2/php.ini

Replace <php_version> with your installed PHP version (e.g., 7.4, 8.0, etc.). Look for the following line:

zend_extension=opcache.so

If this line is not present, add it to enable OPcache.

Restart the Web Server:

After enabling OPcache, restart the Apache or Nginx web server to apply the changes:

sudo systemctl restart apache2

or

sudo systemctl restart nginx

Installing OPcache on CentOS/RHEL

CentOS and RHEL-based distributions use the yum or dnf package manager. Follow these steps to install OPcache:

Update the Package List:

Update the package list to ensure you have the latest versions of the packages:

sudo yum update

Install the OPcache Extension:

Use the following command to install the OPcache extension:

sudo yum install php-opcache

This command will install the OPcache extension for the default PHP version on your system.

Enable the OPcache Extension:

After installation, the OPcache extension should be enabled automatically. You can verify this by checking the PHP configuration file:

sudo nano /etc/php.d/opcache.ini

Ensure that the following line is present:

zend_extension=opcache.so

Restart the Web Server:

Restart the Apache or Nginx web server to apply the changes:

sudo systemctl restart httpd

or

sudo systemctl restart nginx

Installing OPcache on Fedora

Fedora uses the dnf package manager. Follow these steps to install OPcache:

Update the Package List:
Update the package list to ensure you have the latest versions of the packages:

sudo dnf update

Install the OPcache Extension:
Use the following command to install the OPcache extension:

sudo dnf install php-opcache

Enable the OPcache Extension:

After installation, the OPcache extension should be enabled automatically. You can verify this by checking the PHP configuration file:

sudo nano /etc/php.d/opcache.ini

Ensure that the following line is present:

zend_extension=opcache.so

Restart the Web Server:

Restart the Apache or Nginx web server to apply the changes:

sudo systemctl restart httpd

or

sudo systemctl restart nginx

Installing OPcache on Arch Linux

Arch Linux uses the pacman package manager. Follow these steps to install OPcache:

Update the Package List:

Update the package list to ensure you have the latest versions of the packages:

sudo pacman -Syu

Install the OPcache Extension:

Use the following command to install the OPcache extension:

sudo pacman -S php-opcache

Enable the OPcache Extension:

After installation, the OPcache extension should be enabled automatically. You can verify this by checking the PHP configuration file:

sudo nano /etc/php/conf.d/opcache.ini

Ensure that the following line is present:

zend_extension=opcache.so

Restart the Web Server:

Restart the Apache or Nginx web server to apply the changes:

sudo systemctl restart httpd

or

sudo systemctl restart nginx

Configuring OPcache

After installing OPcache, you may want to configure it to suit your specific needs. The configuration settings for OPcache are located in the php.ini file or a dedicated configuration file (e.g., opcache.ini).

Here are some common configuration options:

  • Enable OPcache:
opcache.enable=1
  • Set the Memory Size:
    The opcache.memory_consumption directive determines the amount of memory allocated for OPcache. The default is 64MB, but you can increase it if needed:
opcache.memory_consumption=128
  • Set the Maximum Number of Cached Files:
    The opcache.max_accelerated_files directive sets the maximum number of files that can be cached. The default is 2000, but you can increase it:
opcache.max_accelerated_files=4000
  • Enable File Validation:
    The opcache.validate_timestamps directive determines whether OPcache should check for updated scripts. Set it to 0 in production environments to improve performance:
opcache.validate_timestamps=0
  • Set the Revalidation Frequency:
    If opcache.validate_timestamps is enabled, you can set the frequency (in seconds) at which OPcache checks for updated scripts:
opcache.revalidate_freq=60

After making changes to the configuration, restart your web server to apply the changes.


Verifying OPcache Installation

To verify that OPcache is installed and enabled, you can create a PHP info file:

Create a PHP Info File:

Create a file named info.php in your web server's document root:

sudo nano /var/www/html/info.php

Add the Following Code:

<?php
phpinfo();
?>

Access the PHP Info Page:

Open a web browser and navigate to http://your_server_ip/info.php. Look for the "OPcache" section in the output. If OPcache is enabled, you should see details about its configuration.


Troubleshooting Common Issues

  • OPcache Not Enabled:
    If OPcache is not enabled, ensure that the zend_extension=opcache.so line is present in your PHP configuration file.
  • Memory Allocation Errors:
    If you encounter memory allocation errors, consider increasing the opcache.memory_consumption value.
  • File Validation Issues:
    If you experience issues with file validation, ensure that opcache.validate_timestamps is set correctly for your environment.

Conclusion

OPcache is a powerful tool for improving the performance of PHP applications by caching precompiled script bytecode. In this article, we have provided a detailed guide on how to install OPcache PHP extension on Linux across various distributions, including Ubuntu, Debian, CentOS, RHEL, Fedora, and Arch Linux. We have also covered configuration, verification, and troubleshooting steps to ensure that OPcache is working correctly.

By following this guide, you can easily install and configure OPcache on your Linux server, leading to faster PHP application performance and a better user experience.

RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Linux & Servers•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout