XCache is an alternative yet fast and stable PHP accelerator and opcode cacher that is similar to the famous eAccelerator and APC (Alternative PHP Cache). XCache extension works by caching the compiled bytecode (opcode or intermediate code) form of PHP scripts to avoid the overhead to parse and compile PHP source code on every page request, and thus reduces server loads, boosts the performance of PHP scripts and increases the speed of PHP code from 2-10 times.

XCache supports all of the latest PHP cvs branches such as PHP 4.3, PHP 4.4, PHP 5.1, PHP 5.2, PHP 5.3, PHP 5.4 and PHP 5.5 on Linux server including CentOS, Red Hat Enterprise Linux (RHEL), Ubuntu and Debian, which commonly used on cPanel and Plesk based web hosting server, and ThreadSafe/Windows web server. XCache overcomes a lot of problems that exist on opcachers or accelerators such as unable to work with new PHP versions. Beside, XCache also been benchmarked to be faster than other opcode cachers.

This is a simple tutorial and guide on how to install XCache PHP cacher by manually compiling from source code package. If you’re using WordPress or using XCache to speed up and improve WordPress blog performance, you should first ensure that PHP is upgraded to latest version (PHP 5.2.1 or later), as PHP 4 may cause some instability issue such as unable to post comment, partial page loading and etc. Anyway, PHP5 is always recommended if possible.

And as with all PHP accelerator, XCache won’t work with PHP suEXEC turns on. PHP suEXEC must be disabled for XCache to work properly.

Currently, XCache maintains 2 branches, i.e. version 1.0.3 and version 1.2.0 is on stable version 1.3.0 which supports PHP 5.33.2.0 which supports PHP 5.1 to 5.6, and it’s constantly been updated. All version is stable release, with version 1.0.x recommended for PHP 4.3.x and 4.4.x, and version 1.3.x recommended for most version of PHP including 5.1.x and 5.2.x, except PHP 5.0.x series. If you’re using PHP4, try both version of XCache to pick the one most suitable for your application. To check which version of PHP you’re usinng, issue command php -v or setup a PHP file with the following content, then call the file from web browser:

<?php
phpinfo();
?>

Compiling and Installing XCache

  1. Change to user local source directory such as /usr/local/src by using following command:
    cd /usr/local/src
  2. Retrieve and download the source package of the version of XCache you choose to install (example below used XCache 3.2.0):
    wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz 

    Updated version of XCache can be found at http://xcache.lighttpd.net/wiki/ReleaseArchive.

  3. Untar, unzip, unpack or extract the archive with one of the following commands:
    tar -zxf xcache-*.tar.gz
    gzip -dc xcache-*.tar.gz | tar -x
  4. Go into the xcache directory with source content:
    cd xcache
  5. Trigger PHP building environment:
    phpize
  6. Optional but recommended practice is to build XCache outside of source directory:
    mkdir ../xcache-build
    cd ../xcache-build
  7. Run the configure with the option that you want, the basic is as below which will enable the XCache by default:
    ../xcache/configure --enable-xcache

    You can list out all available options by using ../xcache/configure –help command, which will display the following:

    –enable-xcache Include XCACHE support.
    –enable-xcache-optimizer XCACHE: (N/A)
    –enable-xcache-coverager XCACHE: Enable code coverage dumper
    –enable-xcache-assembler XCACHE: (N/A)
    –enable-xcache-disassembler XCACHE: Enable opcode to php variable dumper
    –enable-xcache-encoder XCACHE: (N/A)
    –enable-xcache-decoder XCACHE: (N/A)
    –enable-xcache-test XCACHE: Enable self test – FOR DEVELOPERS ONLY!!

  8. Compile the source with following command:
    make

    Ensure that no error occurred.

  9. If you haven’t already using root account, su to root user.
  10. Install XCache binary with the following command:
    make install

    Ensure that no error occurred, and note down the extension installation path of the xcache.so.

  11. Next, XCache need to be configured and enabled in PHP.INI file. Locate the correct location of php.ini configuration file by using phpinfo() function (see sample listed above to check version of PHP). In web hosting environment, normally it’s located in /etc or /usr/local/lib. If the install of PHP parses the /etc/php.d/ folder, then simply create a file in that folder named “xcache.ini” or copy over the sample xcache.ini came with source package.

    Change back to xcache source unpack directory, and run the following command with root user:

    cat xcache.ini >> /etc/php.ini 
    $EDITOR /etc/php.ini

    Note: change to the path of php.ini accordingly

    This will append the default XCache configuration to the end of php.ini. Alternatively, you can copy and paste the following text into the php.ini file yourself too:

    [xcache-common]
    ;; install as zend extension (recommended)
    zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-xxx/xcache.so
    ;; install as zend extension with thread-safe
    ; zend_extension_ts = /usr/local/lib/php/extensions/non-debug-zts-xxx/xcache.so
    ;; install as PHP extension (extension_dir must be set to the full path to xcache.so)
    ; extension = xcache.so
     
    [xcache.admin]
    xcache.admin.user = "admin"
    xcache.admin.pass = md5($your_password)
    
    [xcache]
    xcache.shm_scheme =        "mmap"
    xcache.size  =               16M
    xcache.count =                 1
    xcache.slots =                8K
    xcache.ttl   =                 0
    xcache.gc_interval =        0
    
    xcache.var_size  =            0M
    xcache.var_count =             1
    xcache.var_slots =            8K
    xcache.var_ttl   =             0
    xcache.var_maxttl   =          0
    xcache.var_gc_interval =     300
    
    xcache.test =                Off
    xcache.readonly_protection = Off
    xcache.mmap_path =    "/dev/zero"
    xcache.coredump_directory =   ""
    xcache.cacher =               On
    xcache.stat   =               On
    xcache.optimizer =           Off
    
    [xcache.coverager]
    xcache.coverager =          Off
    xcache.coveragedump_directory = ""
  12. You can configure XCache to your preference. One setting that must be changed is under the [xcache-common], where you must specify the correct installation path of the XCache extension module. You can also opt to run XCache as PHP extension instead of extension for Zend module. In this case, comment and uncomment the line accordingly. Other important options include "xcache.size" which specify amount the memory allocated to XCache, "xcache.count" to take advantage of multi-core processors or multiple processors. And if you plan to use XCache web administration interface, input the "xcache.admin.user" and md5 checksum value of "xcache.admin.pass". Use any hash value tools to generate the correct value, or use the following command to generate the MD5 for your password:
    echo -n "password" | md5sum

    Note: Explanation and definition of all XCache INI settings can be found here.

  13. Restart the Apache httpd web server. The following commands should work:
    /etc/init.d/apache* restart

    Or,

    /etc/init.d/httpd* restart
  14. Ensure the XCache is running by calling PHP file with phpinfo() (see sample above). You should see a new XCache Support block with opcode cache enabled.
  15. If you wish to set up XCache administration page, configure an alias in httpd.conf of Apache to point to the admin subdirectory within XCache. To do so, add the following line to httpd.conf (assuming your xcache source is extracted at /usr/local/src/, and you want to access admin page from /xcache-admin/ URL):
    Alias /xcache-admin/ /usr/local/src/xcache/admin/

    Then restart Apache web server. For lighttpd, add the following line instead:

    alias.url += ("/xcache-admin/" => "/usr/share/xcache/admin/")

    Alternatively, you can also copy the whole "admin" directory from the "xcache" source folder to web document-root or sub-directory of your domain which is web accessible. However, this method is not recommended as you may forget to update the admin page when XCache package is updated.

    Note: Ensure that open_basedir protection, if enabled, is excluded for the directory contains admin-page.