Home » Blogging » Alternative PHP Cache (APC) with W3 Total Cache for WordPress

Alternative PHP Cache (APC) with W3 Total Cache for WordPress

PHP is a very popular language for dynamic websites such as those using WordPress. Parsing PHP scripts, converting them to opcode, and converting them again to machine code can be quite taxing on the CPU. This in turn affects the site speed and performance. In my previous posts (this and this), I showed how to check site loading speed and described a few ways to improve it. In this post, I am going to describe how to install and use alternative PHP cache (APC) to cache PHP files / codes. This results in faster execution time, reduction of CPU load, and better site speed. For those who self-host their WordPress site or whose Hosting provider offers APC, I am also going to explain how to make APC work with W3 Total Cache, the best WordPress cache plugin.

Recommended Reading:

How Alternative PHP Cache Works

APC caches frequently access PHP files and codes in opcode format, which is the intermediate code from the parsed and compiled PHP scripts. Cached opcodes are in shared memory and served when needed, reducing the need for parsing and compiling PHP codes again. Unlike other caching systems such as memcached APC stored both the opcode and the data. Memcached stores only the data, which is accessed by applications through TCP. Therefore normally (there are exceptions), if you have APC installed and working, you do not need memcached.

Increase your RAM Memory

Step 1: Install APC in Ubuntu

Unlike previous releases of Ubuntu, installation of APC in Lucid Lynx (10.04) and later is extremely simple. It is available through the repository. Although you may compile and install the latest release of APC (Ubuntu repository is normally a bit outdated), I recommend going with the repository installation route for compatibility reasons. You can install APC using the following command:

sudo apt-get install php-apc

During the installation process, you will be asked a series of questions regarding activation of experimental modules. I recommend that you do not enable experimental modules. You can hit enter to choose the [defaults]:

Enable internal debugging in APC [no] :
Enable per request file info about files used from the APC cache [no] :
Enable spin locks (EXPERIMENTAL) [no] :
Enable memory protection (EXPERIMENTAL) [no] :
Enable pthread mutexes (default) [yes] :
Enable pthread read/write locks (EXPERIMENTAL) [no] :

Restart Apache:

sudo service apache2 reload

Step 2: Verify APC Installation

You can verify if APC is installed and running using the following command:

php -r "phpinfo();" | grep apc

The output should look something similar to the one below. Do not worry about details in the output yet. It will vary depending on your APC settings, which are discussed later in this post.

Additional .ini files parsed => /etc/php5/cli/conf.d/apc.ini,
apc
MMAP File Mask => /tmp/apc.XXXXXX
apc.cache_by_default => On => On
apc.canonicalize => On => On
apc.coredump_unmap => Off => Off
apc.enable_cli => Off => Off
apc.enabled => On => On
apc.file_md5 => Off => Off
apc.file_update_protection => 2 => 2
apc.filters => no value => no value
apc.gc_ttl => 0 => 0
apc.include_once_override => Off => Off
apc.lazy_classes => Off => Off
apc.lazy_functions => Off => Off
apc.max_file_size => 10M => 10M
apc.mmap_file_mask => /tmp/apc.XXXXXX => /tmp/apc.XXXXXX
apc.num_files_hint => 4096 => 4096
apc.preload_path => no value => no value
apc.report_autofilter => Off => Off
apc.rfc1867 => Off => Off
apc.rfc1867_freq => 0 => 0
apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS
apc.rfc1867_prefix => upload_ => upload_
apc.rfc1867_ttl => 3600 => 3600
apc.shm_segments => 1 => 1
apc.shm_size => 128 => 128
apc.stat => On => On
apc.stat_ctime => Off => Off
apc.ttl => 7200 => 7200
apc.use_request_time => On => On
apc.user_entries_hint => 4096 => 4096
apc.user_ttl => 7200 => 7200
apc.write_lock => On => On

Step 3: Check APC Web Interface

APC comes with a great web interface that provides a wealth of information. It is not enabled by default. To enable it you will have to copy a file to the root of your website:

sudo cp /usr/share/doc/php-apc/apc.php.gz /var/www
sudo gzip -d /var/www/apc.php.gz
sudo vim /var/www/apc.php

We are not done yet. For security reasons, I highly recommend you to edit apc.php file and change the username and password from their defaults (apc and password):

defaults('ADMIN_USERNAME','apc'); // Admin Username
defaults('ADMIN_PASSWORD','password'); // Admin Password - CHANGE THIS TO ENABLE!!!

Also, ensure USE_AUTHENTICATION is set to 1:

defaults('USE_AUTHENTICATION',1);

Now you can go to your domainname.com/apc.php (if you have one), 192.168.0.13/apc.php (if that the network IP of your server on which you have APC running), or localhost/apc.php if you are on the machine running APC. Below is a screenshot of the very informative web interface.

APC Web Interface
APC Web Interface

Get 20% OFF with IPVanish VPN:

♦   Hide your browsing and streaming activity: No logs and no tracking
♦   Access geo-restricted content from anywhere
♦   Encrypt and anonymize: Kodi, Plex, Downloads, Personal Info
♦   Windows, Mac, Linux, Android, iOS, Router, and more.
♦   Money back guarantee - Sign Up Now

About the author

Anand

Anand is a self-learned computer enthusiast, a part-time blogger, and a Food Scientist by career. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs on htpcbeginner.com and smarthomebeginner.com.

Join the other 110,000 followers