PHP Accelerators and cachers will increase the performance of your PHP applications, by caching the compiled form of php scripts . A PHP accelerator typically reduces server load and increases the speed of your PHP scripts between 1 to 10 times.
We have presently 3 good php accelerators from opensource i.e eAccelerator,APC,xcache and now we will see these three php accelerators how to install and configure in debian and ubuntu machines
eAccelerator
eAccelerator is a free open-source PHP accelerator, optimizer, encoder and dynamic content cache. It increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. It also optimizes scripts to speed up their execution. eAccelerator typically reduces server load and increases the speed of your PHP code by 1-10 times.
Project Home page
Latest stable version:0.9.5
Install eAccelerator in debian
First you need to download php4-dev package which we need to compile eAccelerator
#apt-get install php4-dev
If you are using php5 install the following package
#apt-get install php5-dev
Download eAccelerator source package from here
#wget http://bart.eaccelerator.net/source/0.9.5/eaccelerator-0.9.5.tar.bz2
Extract the downloaded file using the following command
#tar jxvf eaccelerator-0.9.5.tar.bz2
#cd eaccelerator-0.9.5
Running phpize creates config file which is needed in make.
#phpize
#./configure –enable-eaccelerator=shared –with-php-config=/usr/bin/php-config
#make
#make install
This will complete the installation.
Now you need to Edit the file /etc/php4/apache2/php.ini for php4 users and add following lines to install php extension
#vi /etc/php4/apache2/php.ini
extension=”eaccelerator.so”
eaccelerator.shm_size=”16″
eaccelerator.cache_dir=”/tmp/eaccelerator”
eaccelerator.enable=”1″
eaccelerator.optimizer=”1″
eaccelerator.check_mtime=”1″
eaccelerator.debug=”0″
eaccelerator.filter=””
eaccelerator.shm_max=”0″
eaccelerator.shm_ttl=”0″
eaccelerator.shm_prune_period=”0″
eaccelerator.shm_only=”0″
eaccelerator.compress=”1″
eaccelerator.compress_level=”9″
If you want to install as Zend extension you need to enter the following lines in /etc/php4/apache2/php.ini file
zend_extension=”/usr/lib/php4/eaccelerator.so”
eaccelerator.shm_size=”16″
eaccelerator.cache_dir=”/tmp/eaccelerator”
eaccelerator.enable=”1″
eaccelerator.optimizer=”1″
eaccelerator.check_mtime=”1″
eaccelerator.debug=”0″
eaccelerator.filter=””
eaccelerator.shm_max=”0″
eaccelerator.shm_ttl=”0″
eaccelerator.shm_prune_period=”0″
eaccelerator.shm_only=”0″
eaccelerator.compress=”1″
eaccelerator.compress_level=”9″
If you use a thread safe build of PHP you must use “zend_extension_ts” instead of “zend_extension”.
If you are using PHP5 you need to edit the /etc/php5/apache2/php.ini file
Creating cache directory
One last very important step is creating the cache directory that you configured in the previous step. The default value is /tmp/eaccelerator It’s easy because it’s writable to everyone.
A safe bet is making it world writable. A safer and cleaner way would be to change the owner of the directory to the same user PHP runs as (most of the time the same user as Apache or Lighttpd) and set 0644 permissions
#mkdir /tmp/eaccelerator
#chmod 0777 /tmp/eaccelerator
Restart apache and you’re finished with the eAccelerator configuration
#/etc/init.d/apache2 restart
Testing Your eaccelerator installation
Create test.php file with following lines on it
<?php
eaccelerator();
?>
If you have eAccelerator info shown on that page, accelerator is working fine.
or you can use the following command
#php -v
Since eAccelerator first stores compiled PHP pages in shared memory, then on disk when that runs out, the size of the shared memory pool will likely have the largest impact on performance.
Install eAccelerator in Ubuntu
First you need to download php4-dev package which we need to compile eAccelerator
sudo apt-get install php4-dev
If you are using php5 install the following package
sudo apt-get install php5-dev
Download eAccelerator source package from here http://sourceforge.net/project/showfiles.php?group_id=122249
wget http://bart.eaccelerator.net/source/0.9.5/eaccelerator-0.9.5.tar.bz2
Extract the downloaded file using the following command
sudo tar jxvf eaccelerator-0.9.5.tar.bz2
cd eaccelerator-0.9.5
Running phpize creates config file which is needed in make.
sudo phpize
sudo ./configure –enable-eaccelerator=shared –with-php-config=/usr/bin/php-config
sudo make
sudo make install
This will complete the installation.
Now you need to Edit the file /etc/php4/apache2/php.ini for php4 users and add following lines to install php extension
sudo vi /etc/php4/apache2/php.ini
extension=”eaccelerator.so”
eaccelerator.shm_size=”16″
eaccelerator.cache_dir=”/tmp/eaccelerator”
eaccelerator.enable=”1″
eaccelerator.optimizer=”1″
eaccelerator.check_mtime=”1″
eaccelerator.debug=”0″
eaccelerator.filter=””
eaccelerator.shm_max=”0″
eaccelerator.shm_ttl=”0″
eaccelerator.shm_prune_period=”0″
eaccelerator.shm_only=”0″
eaccelerator.compress=”1″
eaccelerator.compress_level=”9″
If you want to install as Zend extension you need to enter the following lines in /etc/php4/apache2/php.ini file
zend_extension=”/usr/lib/php4/eaccelerator.so”
eaccelerator.shm_size=”16″
eaccelerator.cache_dir=”/tmp/eaccelerator”
eaccelerator.enable=”1″
eaccelerator.optimizer=”1″
eaccelerator.check_mtime=”1″
eaccelerator.debug=”0″
eaccelerator.filter=””
eaccelerator.shm_max=”0″
eaccelerator.shm_ttl=”0″
eaccelerator.shm_prune_period=”0″
eaccelerator.shm_only=”0″
eaccelerator.compress=”1″
eaccelerator.compress_level=”9″
If you use a thread safe build of PHP you must use “zend_extension_ts” instead of “zend_extension”.
If you are using PHP5 you need to edit the /etc/php5/apache2/php.ini file
Creating cache directory
One last very important step is creating the cache directory that you configured in the previous step. The default value is /tmp/eaccelerator It’s easy because it’s writable to everyone.
A safe bet is making it world writable. A safer and cleaner way would be to change the owner of the directory to the same user PHP runs as (most of the time the same user as Apache or Lighttpd) and set 0644 permissions
sudo mkdir /tmp/eaccelerator
sudo chmod 0777 /tmp/eaccelerator
Restart apache and you’re finished with the eAccelerator configuration
#/etc/init.d/apache2 restart
Testing Your eaccelerator installation
Create test.php file with following lines on it
<?php
eaccelerator();
?>
If you have eAccelerator info shown on that page, accelerator is working fine.
or you can use the following command
#php -v
Alternative PHP Cache (APC)
The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. It was conceived of to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.
Project Home page
http://pecl.php.net/package/APC
Latest stable version: 3.0.12p2
Install Alternative PHP Cache (APC) in debian
Install all the required packages
#apt-get install apache2-threaded-dev php4-dev php4-pear make gcc g++
If you are using php5 use the following
#apt-get install apache2-threaded-dev php5-dev php5-pear make gcc g++
Preparing for pecl
I am using Apache2 and PHP4 on my server.If you are using Apache2, the apxs is named differently, and pecl will fail if you do not work around this. You had to provide an apxs which is just a symbolic link.
#ln -s /usr/bin/apxs2 /usr/bin/apxs
Install APC Using pecl
#pecl install apc
If you are using apache2 you need to make sure you have created symbolic link for apxs2
Configuring APC
Now you need to edit your php.ini file and add the extension=apc.so
#vi /etc/php4/apache2/php.ini
If you are using php5 edit the following file
#vi /etc/php5/apache2/php.ini
add the following line
extension=apc.so
save the file and exit
Restart apache server using the following server
#/etc/init.d/apache2 restart
Install Alternative PHP Cache (APC) in Ubuntu
Install all the required packages
sudo apt-get install apache2-threaded-dev php4-dev php4-pear make gcc g++
If you are using php5 use the following
sudo apt-get install apache2-threaded-dev php5-dev php5-pear make gcc g++
Preparing for pecl
I am using Apache2 and PHP4 on my server.If you are using Apache2, the apxs is named differently, and pecl will fail if you do not work around this. You had to provide an apxs which is just a symbolic link.
ln -s /usr/bin/apxs2 /usr/bin/apxs
Install APC Using pecl
sudo pecl install apc
If you are using apache2 you need to make sure you have created symbolic link for apxs2
Configuring APC
Now you need to edit your php.ini file and add the extension=apc.so
sudo vi /etc/php4/apache2/php.ini
If you are using php5 edit the following file
sudo vi /etc/php5/apache2/php.ini
add the following line
extension=apc.so
save the file and exit
Restart apache server using the following server
sudo /etc/init.d/apache2 restart
XCache
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.
Project Home page
http://trac.lighttpd.net/xcache/
Latest Stable Version
1.0.3-rc1
Install xcache in debian sarge
Preparing your system
#apt-get install gcc g++ make
Download xcache source code using the following command
#wget http://210.51.190.228/pub/XCache/rc/1.0.3-rc1/xcache-1.0.3-rc1.tar.gz
Extract the file using the following command
#tar xzvf xcache-1.0.3-rc1.tar.gz
Now you should be having xcache directory
Now you need to go inside xcache directory
#cd xcache
Run the following command
#phpize
Configuring for:
PHP Api Version: 20020918
Zend Module Api No: 20020429
Zend Extension Api No: 20021010
#./configure –enable-xcache
#make
#make install
#cat xcache.ini >> /etc/php4/apache2/php.ini
Now you need to configure the following settings in /etc/php4/apache2/php.ini file
zend_extension = /usr/lib/php4/20050606/xcache.so
xcache.size = 64M
xcache.cacher = On
restart your apache server using the following command
#/etc/init.d/apache2 restart
Install xcache in debian etch
If you are using debian etch you need to use the following comamnd for php4 users
#apt-get install php4-xcache
Now you need to configure the following settings in /etc/php4/apache2/php.ini file
zend_extension = /usr/lib/php4/20050606/xcache.so
xcache.size = 64M
xcache.cacher = On
restart your apache server using the following command
#/etc/init.d/apache2 restart
For php5 users
#apt-get install php5-xcache
Now you need to configure the following settings in /etc/php5/apache2/php.ini file
zend_extension = /usr/lib/php5/20050606/xcache.so
xcache.size = 64M
xcache.cacher = On
restart your apache server using the following command
#/etc/init.d/apache2 restart
In your write-up, you state the following:
If you have eAccelerator info shown on that page, accelerator is working fine.
or you can use the following command
#php -v
Neither of these commands works, at least not with version 0.9.5. The only way to tell is by using phpinfo(); and scrolling down to the eaccelerator section. Hope this helps somebody else out! Thanks for the guide!
It should be noted that pecl is not available in Debian stable (Sarge). It is part of the php-pear in testing (Etch) and unstable (Sid).
xcache isn’t packaged in Debian Etch, it’s only in testing and unstable according to the Debian site.
Thanks Scott. Your feedback was indeed helpful!
Thanks Scott. I install APC (Ubuntu).
1 thing wanna know – why do we need to install apache2-threaded-dev?
Reason why im asking is because i install LAMP with apache2 preforked.
And when i did your step to install APC (ubuntu) – it removed my apache2-preforked to apache2-threaded.
Thanks in adv.
Faisal
Ubuntu Gutsy or Hardy
sudo apt-get install php5-xcache
vi /etc/php5/apache2/conf.d/xcache.ini
modify settings as desired. for the web interface:
cp -a /usr/share/xcache/admin /var/www/xcache-web
edit username/password in xcache.ini as above, point browser to http://localhost/xcache-web/
Thanks for the helpful tutorial. Is there a typo in “Install Alternative PHP Cache (APC) in Ubuntu”
If you are using php5 use the following
sudo apt-get install apache2-threaded-dev php5-dev php5-pear make gcc g++
should be: sudo apt-get install apache2-threaded-dev php-dev php5-pear make gcc g++
Sorry, there was a typo in my correction!
February 9th, 2009 at 1:11 pm
Thanks for the helpful tutorial. Is there a typo in “Install Alternative PHP Cache (APC) in Ubuntu”
If you are using php5 use the following
sudo apt-get install apache2-threaded-dev php5-dev php5-pear make gcc g++
should be: sudo apt-get install apache2-threaded-dev php5-dev php-pear make gcc g++
Thank you, this little tutorials has me very helped.