PHP can be very resource intensive at times. If you’re not already using some sort of PHP caching extension, like eAccelerator, APC or XCache, you’re probably well aware of the resource usage associated with PHP and web/command-line apps that rely on it. Fortunately, these extensions provide caching for increased performance and efficiency, which translates to less load on your server. In this article, we’ll focus on installing and configuring eAccelerator.
You will need a PHP version earlier than 5.4.x installed for eAccelerator to build properly. 5.2.x and 5.3.x should work with no issues. You will also need build/compilation tools and libraries installed on your server (gcc, make, etc.).
Downloading sources, building and installing
At the time of this posting, the latest version of eAccelerator is 0.9.6.1. Though it doesn’t look like a new version will be released any time in the very near future, should a new version come out, you can simply modify the version numbers below and all the commands should work the same:
wget "http://softlayer.dl.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%220.127.116.11/eaccelerator-0.9.6.1.zip" unzip eaccelerator-0.9.6.1.zip cd eaccelerator-0.9.6.1 phpize ./configure ; make ; make install
If you received no errors during the build and the install worked properly, proceed to configuration.
[eAccelerator] extension="eaccelerator.so" eaccelerator.shm_size="32" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="86400" eaccelerator.shm_prune_period="3600" eaccelerator.shm_only="1" eaccelerator.compress="1" eaccelerator.compress_level="9"
Now, keep in mind these settings are suggestions. They should be modified to suit your needs. 32MB of SHM (Shared Memory) should be enough for most lower-load sites, but 64MB or even more might be necessary in other places. This particular setup limits caching to memory only, which should introduce great amounts of speed. You can also enable disk caching with a slight performance decrease.
NOTE: The default maximum SHM size on most Linux distros is 32M. Do NOT increase this value without modifying the kernel sysctl (see below) unless you don’t like your webserver or PHP app to work right!
To raise the maximum SHM size on Linux, just run the following command (in bytes so assuming desired MB = M, do: M * 1024 * 1024):
sysctl -w kernel.shmmax=134217728
This assumes you want to raise it to 128MB maximum SHM size. Now, to make that change permanent, just add the following line to /etc/sysctl.conf:
kernel.shmmax = 134217728
The final step in this setup is to apply your new configuration. Assuming you’re running custom-built Apache, you can run the following commands to restart it:
/usr/local/apache/bin/httpd -k stop /usr/local/apache/bin/httpd -k start
This should conclude your setup. Once you’re done, you should notice a dramatic decrease in the amount of processing power it takes to run your intensive PHP apps. And, since everyone loves graphic proof, some images follow below.