How to install PHP APC on LNMP

by Ruchira on May 13, 2013

php-apcNote : This is a part of http://www.ruchirablog.com/nginx-autoinstaller/ guide.

APC Alternative PHP Cache, is a nice little caching add-on to PHP which can increase the performance of PHP significantly. APC works by optimizing the PHP compiler and storing the final compiled code in the memory. So this saves the burden of having to retrieve the source file from the disk and compile it every time when a visitor requests a page.

So how to install APC on LNMP 0.9 ? Log in to the terminal and just follow the instructions below

Download the APC installation files from the source

wget -c wget http://pecl.php.net/get/APC-3.1.13.tgz

Unpack the installer

tar zxvf APC-3.1.13.tgz
cd APC-3.1.13/

And start installing

/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-apc-mmap --enable-apc --enable-apc-filehits
make -s
make -s install

After that we need to edit the php.ini file and add apc.so to it

nano /usr/local/php/etc/php.ini

And add the below code to the top of that file

[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 32
apc.optimization = 1
apc.num_files_hint = 0
apc.ttl = 0
apc.gc_ttl = 3600
apc.cache_by_default = on
apc.mmap_file_mask =/tmp/apc.XXXXXX

It’s done, Now you need to restart php-fpm to start running APC

/etc/init.d/php-fpm restart

Congratulations, now you have APC successfully installed. See the code we added to the php.ini file “apc.shm_size = 32″ this is the place that you have to mention the memory limit for APC to operate. 32 means 32mb in this case. 32mb is enough for most cases like wordpress blogs. If you have enough memory on your server you can set this to higher limit such as 128mb. Just change that 32 to 128 and restart php-fpm or lnmp and you are done.

APC statistics

APC comes with a web based page to monitor the APC starts and you can find this on APC-3.1.9/ directory and its called apc.php . Just move apc.php to your domain root directory so you can access it from the browser.

I'm Ruchira Sahan and all posts on this blog are completely my thoughts and writings. I love DIY and Technology. So feel free to contact me for anything about this blog and don't forget to add a comment if this blog helped you! Thanks
Ruchira
View all posts by Ruchira

Last 5 posts by Ruchira

  • http://ekoice.info ekoice

    all i got on running the apc.php is:

    No cache info available. APC does not appear to be running.

    i followed your guide, using nginx 1.4.1 and php 5.3.25

    what should i do? thanks!

    • http://www.ruchirablog.com Ruchira

      See the phpinfo.php page to check if php-apc is loaded

Previous post:

Next post: