In this tutorial, we will show you how to install and enable Alternative PHP Cache (APC) on CentOS. For those of you who didn’t know, The Alternative PHP Cache (APC) is a free and open-source opcode caching solution for PHP. It is designed to cache and reuse compiled bytecode, thereby reducing the overhead of parsing and compiling PHP scripts on each request. APC not only caches opcodes but also user-defined function calls, class definitions, and file metadata, resulting in significant performance improvements for PHP applications.
One of the primary benefits of using APC is its ability to improve application performance by reducing server load and decreasing response times. By caching frequently accessed PHP scripts and data, APC minimizes the need for repetitive compilation, leading to faster execution and more efficient resource utilization. Additionally, APC provides a robust set of configuration options, allowing developers to fine-tune the caching behavior to suit their specific application requirements.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the Alternative PHP Cache (APC) on the CentOS system.
Prerequisites
- A server running one of the following operating systems: CentOS Linux or RHEL-based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Alternative PHP Cache (APC) on CentOS
Step 1. First, let’s start by ensuring your system is up-to-date.
sudo dnf clean all sudo dnf update sudo dnf install php-pear php-devel httpd-devel pcre-devel gcc make
Step 2. Installing the Required Packages.
To install APC on CentOS, you need to install the required packages using the following command below:
sudo yum install httpd php php-devel php-pear
Step 3. Installing Alternative PHP Cache (APC) on CentOS.
To install APC, run the following command:
sudo pecl install apc
Step 3. Configuring APC.
You can put your configuration in php.ini
file but I prefer to have a separate file like the above for configuration. Values mentioned below are for demonstration purposes, different values for APC can be set depending on the number of PHP pages, size of memory in the server, number of page hits e.t.c
nano /etc/php.d/apc.ini
Add the following lines to the configuration file:
; Enable the extension module extension = apc.so ; Options for the APC module version >= 3.1.3 ; See http://www.php.net/manual/en/apc.configuration.php ; This can be set to 0 to disable APC. apc.enabled=1 ; The number of shared memory segments to allocate for the compiler cache. apc.shm_segments=1 ; The size of each shared memory segment, with M/G suffixe apc.shm_size=512M ; A "hint" about the number of distinct source files that will be included or ; requested on your web server. Set to zero or omit if you're not sure; apc.num_files_hint=1024 ; Just like num_files_hint, a "hint" about the number of distinct user cache ; variables to store. Set to zero or omit if you're not sure; apc.user_entries_hint=4096 ; The number of seconds a cache entry is allowed to idle in a slot in case this ; cache entry slot is needed by another entry. apc.ttl=7200 ; use the SAPI request start time for TTL apc.use_request_time=1 ; The number of seconds a user cache entry is allowed to idle in a slot in case ; this cache entry slot is needed by another entry. apc.user_ttl=7200 ; The number of seconds that a cache entry may remain on the garbage-collection list. apc.gc_ttl=3600 ; On by default, but can be set to off and used in conjunction with positive ; apc.filters so that files are only cached if matched by a positive filter. apc.cache_by_default=1 ; A comma-separated list of POSIX extended regular expressions. apc.filters ; The mktemp-style file_mask to pass to the mmap module apc.mmap_file_mask=/tmp/apc.XXXXXX ; This file_update_protection setting puts a delay on caching brand new files. apc.file_update_protection=2 ; Setting this enables APC for the CLI version of PHP (Mostly for testing and debugging). apc.enable_cli=0 ; Prevents large files from being cached apc.max_file_size=1M ; Whether to stat the main script file and the fullpath includes. apc.stat=1 ; Vertification with ctime will avoid problems caused by programs such as svn or rsync by making ; sure inodes havn't changed since the last stat. APC will normally only check mtime. apc.stat_ctime=0 ; Whether to canonicalize paths in stat=0 mode or fall back to stat behaviour apc.canonicalize=0 ; With write_lock enabled, only one process at a time will try to compile an ; uncached script while the other processes will run uncached apc.write_lock=1 ; Logs any scripts that were automatically excluded from being cached due to early/late binding issues. apc.report_autofilter=0 ;This setting is deprecated, and replaced with apc.write_lock, so let's set it to zero. apc.slam_defense=0
After making the changes, you need to restart the Apache web server for the changes to take effect. You can do this by running the following command:
sudo systemctl restart httpd
APC provides a web interface with detailed information on the cache (memory usage, hits, and misses cache entries). By default, it is not accessible so you need to copy the file /usr/share/php/apc.php
to somewhere you can browse. Now from the browser, you can go to http://your-domain.com/apc.php
. I prefer to wait for a day to see the APC performance so we can have a clear idea of how well our configuration did.
Congratulations! You have successfully installed Alternative PHP Cache (APC). Thanks for using this tutorial for installing Alternative PHP Cache (APC) on the CentOS system. For additional help or useful information, we recommend you check the official PHP website.