The Linux Blog UNIX, LINUX, BSD, OSX

11Dec/090

How to install XCache for Centos / Redhat / Fedora – Php5

How do I install Xcache opcode cacher for PHP 5 under RHEL / CentOS version 5.0 server?

XCache is a open-source opcode cacher, which means that it accelerates the performance of PHP on servers. It optimizes performance by removing the compilation time of PHP scripts by caching the compiled state of PHP scripts into the shm (RAM) and uses the compiled version straight from the RAM. This will increase the rate of page generation time by up to 5 times as it also optimizes many other aspects of php scripts and reduce serverload.

Step # 1: Download xcache source code

Use wget command to download latest stable release:
# cd /opt
# wget http://xcache.lighttpd.net/pub/Releases/1.2.1/xcache-1.2.1.tar.gz

Step # 2: Untar tar ball

Use tar command:
# tar -zxvf xcache-1.2.1.tar.gz
# cd xcache-1.2.1

Step # 2: Compile and install xcahce

Use phpize command to prepare xcache as a PHP extension for compiling:
# phpize
Configure, compile and install xcache:
# ./configure --enable-xcache
# make
# make install

Default xcache.so installation location

  • 64 bit PHP module installed at /usr/lib64/php/modules/xcache.so
  • 32 bit PHP module installed at /usr/lib/php/modules/xcache.so

Step # 3: Create xcache.ini file

Under RHEL / CentOS, you place php modules configuration at /etc/php.d/ directory:
# cd /etc/php.d/
Create xcache.ini file:
# vi xcache.ini
Append configuration directives:

[xcache-common]
; change me - 64 bit php => /usr/lib64/php/modules/xcache.so
; 32 bit php => /usr/lib/php/modules/xcache.so
zend_extension = /usr/lib64/php/modules/xcache.so

[xcache.admin]
xcache.admin.auth = On
xcache.admin.user = "mOo"
; xcache.admin.pass = md5($your_password)
xcache.admin.pass = ""

[xcache]
xcache.shm_scheme =        "mmap"
xcache.size  =               32M
xcache.count =                 1
xcache.slots =                8K
xcache.ttl   =              3600
xcache.gc_interval =         300

; Same as aboves but for variable cache
; If you don't know for sure that you need this, you probably don't
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

; N/A for /dev/zero
xcache.readonly_protection = Off

xcache.mmap_path =    "/dev/zero"

xcache.cacher =               On
xcache.stat   =               On

Save and close the file.

Alternatively, you can also copy default xcache.ini to /etc/php.d/
# cp xcache.ini /etc/php.d/
# vi /etc/php.d/xcache.ini

Restart your Apache web server:
# service httpd restart

If you are using Lighttpd web server, enter:
# service lighttpd restart

Step # 4: Make sure xcache is working

Type the following command for verification:
$ php -v
Output:

PHP 5.1.6 (cli) (built: Nov 20 2007 11:11:52)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
    with XCache v1.2.1, Copyright (c) 2005-2007, by mOo
10Mar/090

How to install Xcache module for Apache

XCache is a fast, stable PHP opcode cacher that has been tested and is now running on production servers under high load. It is tested (on linux) and supported on all of the latest PHP cvs branches such as PHP_4_3 PHP_4_4 PHP_5_0 PHP_5_1 PHP_5_2 HEAD(6.x). ThreadSafe/Windows is also supported. It overcomes a lot of problems that has been with other competing opcachers such as being able to be used with new PHP versions. See Introduction for more information.

You don't have to check the following list yourself, the configure script will do for you, unless you have problem with configure/make.

Check version with cli

$ php-cgi -v
PHP 4.4.3-dev (cgi-fcgi) (built: Mar 10 2006 18:46:02)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

or setup a file with:

Check version with phpinfo

<?php
phpinfo();
?>

and request it from your browser.

* Get the php works with webserver without XCache first!
* common build tools: c compiler, make, libtool (required by php build env)
* php building env installed. if you've install php yourself, make sure you have do "make install". for some os distro, there is "php-devel" package. check it out with:

Check phpize

$ which phpize
/usr/local/bin/phpize
(or)
/usr/bin/phpize

you output may be vary from this, depending on your installtion of php. if it's not found, you should find it yourself

* m4
* indent (optional)

Building:

~ $ cd ~/src/xcache
~/src/xcache $ ls
(you XCache source is here)

~/src/xcache $ phpize
(generating configure .... everytime you upgrade php, or update to a new XCache, you have to run phpize again)

(it is suggested to build outside of the source directory, so make an build directory first and enter it)
~/src/xcache $ mkdir ../xcache-build
~/src/xcache $ cd ../xcache-build
~/src/xcache-build $ ../xcache/configure --help
......
--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!!
......

(run configure with options you selected now)
~/src/xcache-build $ ../xcache/configure --enable-xcache --enable-xcache-coverager
(many output here, if you have problem, read the error message twice)
(and search inside config.log, and check Pre-requirement in this page above)

~/src/xcache-build $ make
(many output here again, check if it success or error out.)

NOTE: It's always better not to enable unnecessary modules for production server unless you're not the maintainer of the server. Play with it locally.

WARNING: If you're using something like /opt/php/bin/phpize which isn't the 1st one found in $PATH, remember to configure --with-php-config=/opt/php/bin/php-config, exactly the same directory as phpiz.

Special path

~/src/xcache-build $ PATH="/opt/php/bin/:$PATH" ../xcache/configure \
--with-php-config=/opt/php/bin/php-config \
--enable-xcache \
--enable-xcache-coverager
(line is broken up for readability)
~/src/xcache-build $ make

Installing

~/src/xcache-build $ su
Password:
(input your root password here. whenever u see a red # in code listing in this wiki, it means you need to be root to do that)

~/src/xcache-build # make install
(many output here, and you can see where the XCache extension is installed into, remember the extension path)

You have to modify php.ini to make XCache enable in your php!