17 C
Texas
angeloma
Senior Writer and partner

Configuring PHP OPCache with Apache and Nginx

Hello, friends. We already know that the faster our website loads the more satisfied visitors will be. That is why we have to make some adjustments to the server to achieve this goal. That is why we prepared this post so you can configure PHP OPCache with Apache and Nginx. So your PHP applications or websites will load faster.

What is OPCache?

OPcache is a PHP extension that improves the performance of a web page by storing the bytecode of a precompiled script in shared memory and eliminates the obligation to constantly load and parse scripts.

This extension is included in PHP 5.5.0 and later, and is “ available in PECL for PHP versions 5.2, 5.3 and 5.4.

In short, we can make use of it to speed up our websites without the need to install anything additional. Of course, for extreme situations, there are other solutions but for personal websites and small projects, OPCache is ideal.

- Advertisement -

So, in this post, I will show you how to configure PHP OPCache with Apache and Nginx so you can get the most out of it.

Configuring PHP OPCache – Apache

Note: For this post, we will use Ubuntu 20.04 as an example but you can also do it using Debian 10 or any derivative,

Before you start, update the whole system

sudo apt update
sudo apt upgrade

If you don’t have Apache and PHP installed you can do it with the following command:

sudo apt install apache2 libapache2-mod-php php php-cli php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'php7.4-opcache' instead of 'php-opcache'
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils fontconfig-config fonts-dejavu-core libapache2-mod-php7.4 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
  libfontconfig1 libgd3 libgdbm-compat4 libjansson4 libjbig0 libjpeg-turbo8 libjpeg8 liblua5.2-0 libmagic-mgc libmagic1 libonig5 libperl5.30 libtiff5 libwebp6 libx11-6
  libx11-data libxau6 libxcb1 libxdmcp6 libxpm4 libxslt1.1 libzip5 perl perl-modules-5.30 php-common php7.4 php7.4-cli php7.4-common php7.4-curl php7.4-gd php7.4-json
  php7.4-mbstring php7.4-mysql php7.4-readline php7.4-xml php7.4-zip
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser php-pear libgd-tools file perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl
  make libb-debug-perl liblocale-codes-perl
Recommended packages:
  ssl-cert
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils fontconfig-config fonts-dejavu-core libapache2-mod-php libapache2-mod-php7.4 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap libfontconfig1 libgd3 libgdbm-compat4 libjansson4 libjbig0 libjpeg-turbo8 libjpeg8 liblua5.2-0 libmagic-mgc libmagic1
  libonig5 libperl5.30 libtiff5 libwebp6 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxpm4 libxslt1.1 libzip5 perl perl-modules-5.30 php php-cli php-common
  php-curl php-gd php-mbstring php-mysql php-xml php-zip php7.4 php7.4-cli php7.4-common php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-mysql php7.4-opcache
  php7.4-readline php7.4-xml php7.4-zip
0 upgraded, 57 newly installed, 0 to remove and 29 not upgraded.
Need to get 16.7 MB of archives.
After this operation, 91.0 MB of additional disk space will be used.
Do you want to continue? [Y/n]

As you can notice in this command is included the php-opcache package which is where the extension is.

You can check the version of PHP installed and you can even create a test file.

php -version
PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Now it is necessary to enable the extension and this can be done by modifying some parameters from the PHP configuration file.

sudo nano /etc/php/7.4/apache2/php.ini

And in the file uncomment the following lines:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=2000
opcache.revalidate_freq=30

What we are doing is, enable it, tell it in Mb how much memory it can consume; the number of files to accelerate, and the time in seconds that it will look for files.

Save the changes and close the editor. To apply the changes, restart Apache.

sudo systemctl restart apache2

Now check if OPCache is enabled

php -i | grep opcache

The output will look something like this:

.
.
.
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => Off => Off
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.file_cache => no value => no value
opcache.file_cache_consistency_checks => 1 => 1
opcache.file_cache_only => 0 => 0
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
.
.
.

So, PHP OPCache is now configured.

Configuring PHP OPCache – Nginx

In the case of Nginx, the process is quite similar. Remember that it is not a good idea to have Apache and Nginx running together. So I will assume in this part of the post that you don’t have Apache.

So, install Nginx, PHP, and some modules from the official repositories of the distribution

sudo apt install nginx php php-fpm php-cli php-opcache php-mysql php-zip php-gd php-mbstring php-curl php-xml

Again, the php-opcache package is present as it cannot be missing in the installation.

Then edit the php-fpm configuration file which is where we will have to enable the extension

sudo nano /etc/php/7.4/fpm/php.ini

And uncomment the following lines

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200

Save the changes and close the editor.

After that, restart nginx and php-fpm.

sudo systemctl restart nginx php7.4-fpm

PHP OPCache will now be configured with Apache and Nginx. Enjoy it.

Conclusion

In this post, we have shown you how with a PHP extension you can improve the performance of your web applications. This will improve the load times and make your users happy.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

2 COMMENTS

  1. I think that you wanted to say "Configuring opcache – nginx" instead of "Configuring opcache – apache" in the second section.

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article