OPcache is a PHP interpreter module for Apache. It boosts performance by storing precompiled scripts in a shared memory pool. The module is typically used to speed up and enhance the performance of PHP web applications such as WordPress, Drupal, and custom PHP sites. With OPcache, there is no need to load and parse PHP scripts for each request.
With that out of the way, let focus on how to install and enable PHP Opcache module on Ubuntu 20.04.
Firstly, ensure that the package lists are refreshed as follows
$ sudo apt update
This ensures the repositories and package lists are up to date. Once complete, head over to the next step.
First install Apache, PHP, and the required PHP extensions on your system. Execute the commands:
$ sudo apt-get install apache2 libapache2-mod-php php php-opcache php-mysql php-mbstring php-cli php-zip php-gd php-curl php-xml -y
After the installation, verify the PHP version as shown:
$ php -v
In this step, we will configure Opcache. First, edit the php.ini file to enable the Opcache service. Run:
$ sudo nano /etc/php/7.4/apache2/php.ini
Next, uncomment the following lines:
opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=200
Next, save the file and exit. To effect the changes made, reload the webserver.
$ sudo systemctl restart apache2
To confirm or check that the OPcache module has been loaded, execute the command:
$ php -i | grep opcache
The output is as shown below:
You can choose to install PHP OPcache with the Nginx web server instead of Apache. Follow the steps given below:
Start off by installing Nginx, PHP, and the required PHP extensions as shown:
$ sudo apt-get install nginx php php-fpm php-cli php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml -y
To confirm the installation was successful, check the PHP version installed as follows:
$ php -V
In this step, we will configure Opcache. First, edit the php.ini file to enable the Opcache service. Run:
$ sudo vim /etc/php/7.4/fpm/php.ini
Yet again, uncomment the following lines as we saw earlier with Apache.
opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=200
Next, save the file and exit. Finally, reload the web server and PHP-FPM service to apply the changes.
$ sudo systemctl restart nginx php7.4-fpm
To verify the PHP Opcache installation, execute the command:
$ php -i | grep opcache
PHP Opcache is now successfully installed on Ubuntu 20.04:
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
2024 was the GenAI year. With new and more performant LLMs and a higher number…
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…