Howto: XCache in a Lighttpd Chroot on Debian

Posted 1137 days ago - Debian, Development

PHP Logo Whether you're pressed for resources on a virtual/dedicated server, or simply looking for ways to improve web application performance, XCache is guaranteed to produce the desired result. Within minutes of installing XCache: page load times were cut in half, PHP/MySQL RAM consumption was under control, and overall PHP rendering/output performance dramatically improved. In this post I'll go over the process of installing, configuring, and enabling XCache in a php5-cgi+Lighttpd chroot jail on a Debian Lenny web server.

What is XCache?

Hopefully you're already aware, but just in case... :

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 under Windows, for thread-safe and non-thread-safe versions of PHP. This relatively new opcode caching software has been developed by mOo, one of developers of Lighttpd, to overcome some of the limitations of the existing solutions at that time; such as being able to use it with new PHP versions as they arrive.

Assumptions / Conditions

This tutorial assumes Lighttpd (>= 1.4.23-3+b2) is already setup and running in a chroot environment with php5/fastcgi enabled and working. Also assumes a minimum of 64 megabytes of RAM is available. If you're looking for a Lighttpd chroot setup guide, I highly recommend this one.

Install XCache

Login to your Debian server and obtain root (sudo, su, whatever) and run the following:

[184][vps ~]:# aptitude install php5-xcache

Put XCache in the Lighttpd chroot:

[185][vps ~]:# cp -avr /usr/share/xcache /chroot/usr/share/
[186][vps ~]:# cp -avr /usr/lib/php5/20060613+lfs/xcache.so /chroot/usr/lib/php5/20060613+lfs/
[187][vps ~]:# l2chroot /usr/lib/php5/20060613+lfs/xcache.so
[188][vps ~]:# cat /etc/php5/conf.d/xcache.ini > /chroot/etc/php5/conf.d/xcache.ini
[189][vps ~]:# vi /etc/lighttpd/lighttpd.conf

Create an alias to access the XCache Administration pages:

$HTTP["host"] =~ "some-domain.tld" {
	alias.url = ( "/xcache-admin/" => "/usr/share/xcache/admin/" )
}

Save changes to lighttpd.conf and quit (:wq!). See this page for the l2chroot script.

Setup XCache

Generate a password using the "md5sum" command and create/edit xcache.ini settings file.

[190][vps ~]:# echo -n "your_desired_password" | md5sum
c889e0b89df36eaa47f3a71675d1e9f4
[191][vps ~]:# vi /chroot/etc/php5/conf.d/xcache.ini

The following settings work for my purposes. You may need to consult the XCache docs and adjust according to your environment. Plenty of information out there - use the goog.

[xcache-common]
extension = xcache.so

[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "xcadmin"
xcache.admin.pass = "c889e0b89df36eaa47f3a71675d1e9f4"

[xcache]
xcache.shm_scheme = "mmap"
xcache.size = 96M
xcache.count = 8
xcache.slots = 8K
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 16M
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	= ""

Finalize & Test

If a /chroot/dev/zero special device doesn't exist, go ahead and create one, now. Finally, restart Lighttpd and visit the XCache Administration page (the alias created above).

[192][vps ~]:# mknod /chroot/dev/zero c 3 4
[193][vps ~]:# /etc/inti.d/lighttpd restart

A page resembling the screen-shot below should appear after logging in (click for full-size).

XCache 1.2.2 Administration Page

The Lighttpd wiki suggests setting 1 "max-procs" and several "PHP_FCGI_CHILDREN" in 10-fastcgi.conf. For some reason that particular setup only lasts 20 to 30 minutes before crashing. Here's the setup used at the time the screen-shot above was taken which happens to work for me (so-far).

server.modules += ( "mod_fastcgi" )
fastcgi.server = ( ".php" => ((
	"bin-path" => "/usr/bin/php5-cgi",
	"socket" => "/var/tmp/lighttpd/php5-cgi.socket",
	"max-procs" => 2,
	"idle-timeout" => 20,
	"bin-environment" => (
		"PHP_FCGI_CHILDREN" => "5",
		"PHP_FCGI_MAX_REQUESTS" => "5000"
	),
	"bin-copy-environment" => ( "PATH", "SHELL", "USER" ),
	"broken-scriptfilename" => "enable"
)) )

Result is 2 parent php5-cgi processes with 5 children each:

/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi
	 \_ /usr/bin/php5-cgi

Enjoy! Comments are open...

Word Count: 904

Tags: , , , , , , ,

Click Here to Submit a Comment

Permalink / Last Modified:

Support Nullamatix.com:

See Also:

  • 12/10/2009 -- 529 Attacks in 9 Days: id1.txt, RFI, & More
    Excerpt: "Long time Nullamatix readers know how much I love reviewing log files. Logs can provide detailed incite into not only the overall health of a system, but information one can use to mitigate the risks of automated attacks. In this post, I'll go over a couple ..."
  • 08/14/2009 -- Howto: Tail Lighttpd Logs with Style using Sed
    Excerpt: "Although extremely useful, http server access logs are a mess in their raw form. If you're interested in watching your http server logs in real time with formatting rules and pretty colors, this post is for you. Teh Scr1pt Create a new file to put the l33t ..."
  • 01/10/2010 -- Solution: chown: invalid user: www-data:www-data
    Excerpt: "Ran into this issue after getting rid of the www-data user and group. The solution is simple and doesn't involve adding the account/group, assuming the objective is to run Lighttpd as a different user (not www-data). If the intention is to run Lighttpd with ..."
  • 08/31/2009 -- Howto: Insert Bash Command Output Into MySQL
    Excerpt: "A BlogStorm reader emailed me today, Hello, I am replying to your post on http://www.blogstorm.co.uk/how-to-scrape-pages-with-coldfusion/ Wanted to see your experience in page scraping, may need your help on a project. HOw much did you do beyond the ..."

Leave a Reply