APC Info Page

How to Install APC (Alternative PHP Cache) on CentOS 5.6 46


There’s a lot of conflicting information out there on how to install the APC opcode cache on a CentOS 5.6 box. Here’s how I did it:

This tutorial assumes you’re running CentOS 5.6 on a dedicated server, and that you have superuser (root) access. These instructions may also worked in a VPS or shared hosting environment, but if you run into trouble, you should contact your provider’s tech support to see if they have any alternative steps you should take.

First, you need to make sure the following packages are installed:

yum install php-devel pcre-devel

Because you’re going to be compiling a package, you’ll need a C compiler (like gcc) and make. The easiest way to install all the right development tools is with:

yum groupinstall "Development Tools"

Be sure to include those quotes.

Now you’re ready to download and untar the package:

cd /usr/local/src
wget http://pecl.php.net/get/APC-3.1.9.tgz
tar -zxvf APC-3.1.9.tgz

Now it’s time to set it up!

cd APC-3.1.9
phpize
whereis php-config

Take note of where php-config is located (it’s usually in /usr/bin/php-config) as you’ll need it in the next step. If it’s in a different location, use that for the php-config path below:

./configure --enable-apc --enable-apc-mmap --with-apxs --with-php-config=/usr/bin/php-config
make
make install

Next, if you have an /etc/php.d/ directory on your server, create a file called /etc/php.d/apc.ini to store all your configuration settings (if it already exists, you can just edit the existing one). If you don’t have an /etc/php.d/ directory, you can add these configuration settings to your existing php.ini file, which is usually located at /etc/php.ini.

Here’s an example of my apc.ini file, with lots of comments for some guidance. This is a pretty straightforward configuration, so feel free to copy and paste:

; Enable the extension module
extension = apc.so

; Options for the APC module version >= 3.1.3
; See http://www.php.net/manual/en/apc.configuration.php

; This can be set to 0 to disable APC.
apc.enabled=1
; The number of shared memory segments to allocate for the compiler cache.
apc.shm_segments=1
; The size of each shared memory segment, with M/G suffixe
apc.shm_size=64M
; A "hint" about the number of distinct source files that will be included or
; requested on your web server. Set to zero or omit if you're not sure;
apc.num_files_hint=1024
; Just like num_files_hint, a "hint" about the number of distinct user cache
; variables to store.  Set to zero or omit if you're not sure;
apc.user_entries_hint=4096
; The number of seconds a cache entry is allowed to idle in a slot in case this
; cache entry slot is needed by another entry.
apc.ttl=7200
; use the SAPI request start time for TTL
apc.use_request_time=1
; The number of seconds a user cache entry is allowed to idle in a slot in case
; this cache entry slot is needed by another entry.
apc.user_ttl=7200
; The number of seconds that a cache entry may remain on the garbage-collection list.
apc.gc_ttl=3600
; On by default, but can be set to off and used in conjunction with positive
; apc.filters so that files are only cached if matched by a positive filter.
apc.cache_by_default=1
; A comma-separated list of POSIX extended regular expressions.
apc.filters
; The mktemp-style file_mask to pass to the mmap module
apc.mmap_file_mask=/tmp/apc.XXXXXX
; This file_update_protection setting puts a delay on caching brand new files.
apc.file_update_protection=2
; Setting this enables APC for the CLI version of PHP (Mostly for testing and debugging).
apc.enable_cli=0
; Prevents large files from being cached
apc.max_file_size=1M
; Whether to stat the main script file and the fullpath includes.
apc.stat=1
; Vertification with ctime will avoid problems caused by programs such as svn or rsync by making
; sure inodes havn't changed since the last stat. APC will normally only check mtime.
apc.stat_ctime=0
; Whether to canonicalize paths in stat=0 mode or fall back to stat behaviour
apc.canonicalize=0
; With write_lock enabled, only one process at a time will try to compile an
; uncached script while the other processes will run uncached
apc.write_lock=1
; Logs any scripts that were automatically excluded from being cached due to early/late binding issues.
apc.report_autofilter=0
;This setting is deprecated, and replaced with apc.write_lock, so let's set it to zero.
apc.slam_defense=0

The final step is to restart Apache with either:

service httpd restart

or

apachectl restart

To make sure APC is running, create a php file somewhere in your web root that simply contains:

then load the file and scroll down to the APC section to verify that it’s enabled.

For some cool statistics, copy the usr/local/src/APC-3.1.9/apc.php file somewhere in your web root, and then open it in your browser. Edit the file on the server if you’d like to add a password and enable login to see more details. Here’s a peek at what it looks like:

APC Info Page

Congratulations! You’ve just installed APC on CentOS 5.6!

Further Reading

  • brad

    I don’t have /etc/php.d/apc.ini and nano won’t let me create it. what should i do?

    • http://www.stevejenkins.com/ Steve Jenkins

      The only reasons I can think that nano won’t let you create it is because either 1) you’re in a shared hosting environment, or 2) you’re not the superuser (root) when you try to create apc.ini. If shared hosting, which one are you using? If it’s the root issue, just su root and give it a shot.

      • brad

        VPS logged in as root. i don’t have a php.d directory.
        does that mean that php-devel didn’t install?

        • http://www.stevejenkins.com/ Steve Jenkins

          If you have a php.ini file (generally in your web root directory in a shared hosting or VPS environment), you can also add the configuration options in there. I’ve updated the blog post to mention that.

  • http://www.a2zuniforms.com Tim D

    Steve, nice tutorial. I have reimaged my server 4 times trying to get this right. Running centos 5.5 with plesk. I got down to the .config/ line and got this:
    php-config: /usr/bin/php-config /usr/share/man/man1/php-config.1.gz
    [root@u15729373 APC-3.1.9]# ./configure –enable-apc –enable-apc-mmap –with-apxs –with-php-config=/usr/bin/php-config
    checking for egrep… grep -E
    checking for a sed that does not truncate output… /bin/sed
    checking for gcc… no
    checking for cc… no
    checking for cc… no
    checking for cl… no
    configure: error: no acceptable C compiler found in $PATH
    See `config.log’ for more details.
    [root@u15729373 APC-3.1.9]#

    Also I have no make? Can you tell me how to install “make” ?

    Thanks, TD

    • http://www.stevejenkins.com/ Steve Jenkins

      Ahh…. it looks like you don’t have a C compiler. I recommend gcc. Do this to install it:

      yum groupinstall “Development Tools”

      Be sure to include the quotes. That should install everything you’re missing! I’ll add this step to the article.

  • http://www.arvag.net/ Gavrisimo

    Excellent article! I followed every step here and finished it with no problems! :)

    I just have one question, i moved that apc.php to our ‘main’ domain so i have only one entry place for apc statistics, so the question is: do i need to copy apc.php to every domain in order or is only one copy of apc.php enough to see statistics across entire server?

    Thanks.

  • http://www.arvag.net/ Gavrisimo

    Hmm ok i just added apc.php to another domain which has much
    more traffic and I’m seeing different stats. So i guess this does need to be added per domain?

    Sorry for maybe dumb question. :)

    • http://www.stevejenkins.com/ Steve Jenkins

      That’s interesting. As I understand it, you’ll see stats per cache, so you should see the entire server’s stats in one spot. Maybe stuff is just expiring quickly? Try loading both apc.php files side-by-side and see if there are major diffs.

  • http://www.arvag.net/ Gavrisimo

    Well take a look your self:

    (removed for security)

    Both domains are on same server.

    I’ll leave these files on with no authentication for next 10-12h or until your next comment. Second site has barely any traffic atm.

    • http://www.stevejenkins.com/ Steve Jenkins

      Well, yep! Looks like two separate 64M caches. Do your vhosts all share a single php.ini file? You may want to do some Googling about optimizing APC with virtual hosts. You may be able to get away with a smaller cache on your “quieter” site.

  • http://www.arvag.net/ Gavrisimo

    Hmm well then that means i have 64M for every domain i have on server(11 at the moment)?

    I disabled APC at the moment and i will google regarding this issue and also i will contact my hosting support to see if they can help me with this.

  • Rik

    Thanks man, great help!

  • Mathiau

    well

    found this

    yum install pcre-devel

    and it now looks like it worked, not a single guide mentioned pcre-devel :)

    • http://www.stevejenkins.com/ Steve Jenkins

      Well then I’m glad you stumbled on to mine. Glad you got it working. :)

  • Mathiau

    def bookmarking your site!

  • roberto

    I tried to install apc but i got this

    ./configure is not a file o directory

    any help?

  • roberto

    Finally its installed, and I can see the graphic open apc.php but I think its not woring because the only file is cached is apc.php no other file is cached

  • Pingback: How to Install APC Alternative PHP Cache on CentOS 5.6 | Steve Jenkins Blog « myiphonenotes()

  • Jack

    Hi, I got apc.so in modules and added the apc.ini in php.d folder but didn’t work with my apache server, do you know why?
    Thank you!

    • jack

      it works, apache load the apc.so in different folder.

  • http://facebook.com/ksbnok Shashank Shekhar

    Sometimes, once in a while I stumble upon blogs like this…first I read this post (of course my problem was solved after reading this) but I wanted to know more about you… coz somewhere I already knew that u have to be someone different from others… so I read ur bio n my guess was right… u r the man….keep it up Steve..am only 25 yrs at present I might never get chance to meet u but I will always pray for you and your family…am subscribing to your blog…love…

  • http://www.cintanet.org garaa

    hello friend,
    i’m here by looking for how we install apc on linux.
    as im a newbie for linux administration, sometime i will meet an error or crash my site. I hope you can include tutorial how i gonna totally uninstall APC in case after installation my site not working well.
    thank you.

  • http://facebook.com/ksbnok Shashank

    @garaa what problem u facing few days ago I successfully installed APC on linux centos 5.7

  • http://basfile.com Art Tawanghar

    Hi thank you for your great steps in how to install APC, I get to point phpize command no issue, but when I type in phpize in the terminal I get command not found, so I don’t get the ability to continue, what could be wrong, I am on Plesk 10, and php 5.3.3.
    Please help me, I am stuck badly
    Thanks
    ART

  • http://www.psymusic.de Martin

    Dear Steve,
    thanks a lot for the HowTo. At the moment I can not really guess the efficiency of APC, but installation instruction works great!
    Thank you!
    Best regards
    Martin

  • bill

    Thanks Steve, It is a rarity that instructions work as advertised but your did with no problems.

    Thanks again

  • Sergey

    apc.php is not located anywhere on my Debian 6.04 after APC installation. Any idea where to get it?

  • http://watchfairytail.org momo

    Hi Steve,
    I followed the steps and done evrything in it. Everything went right but now when I added the phpinfo file, there is no section about apc.

    Any idea ?

    Thank you

  • Andy

    Don’t work

    All I get in my log is….

    PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/local/lib/php/extensions/no-debug-non-zts-20060613/apc.so’ – /usr/local/lib/php/extensions/no-debug-non-zts-20060613/apc.so: cannot open shared object file: No such file or directory in Unknown on line 0

  • http://www.portalpower.com.br Gol Power

    Very good tutorial, now my site is using apc and I’m happy.

  • Geo

    Thanks Man,

    very clear

  • http://gravatar.com/aiwebsystems aiwebsystems

    Awesome post – worked perfectly after many failed attempts. Much appreciated!

    • http://www.stevejenkins.com/ Steve Jenkins

      Thanks – glad you got it working! Although, I’m hoping that the “many failed attempts” were while trying some other documentation, instead of this one! :)

  • http://gravatar.com/aiwebsystems aiwebsystems

    Indeed they were! Yours was spot on!

    Ryan

    • http://www.stevejenkins.com/ Steve Jenkins

      Phew! :)

  • http://www.mediumdigitalstudio.com uzi

    Worked like a charm minus the httpd restart (/etc/init.d/httpd restart graceful) in my case!

    Uzi

  • Pingback: Error installing apc for php on linux/centos()

  • http://collectables.co.uk Phil Risk

    Every time I enable this it crashes my site with a no response error.

    APC Support disabled
    Version 3.1.9
    APC Debugging Disabled
    MMAP Support Enabled
    MMAP File Mask no value
    Locking type pthread mutex Locks
    Serialization Support broken
    Revision $Revision: 308812 $
    Build Date Mar 25 2013 11:59:36

  • http://gopukrish.wordpress.com gopukrish

    When I check my /var/log/httpd/error_log :

    PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php-zts/modules/apc.so’ – /usr/lib/php-zts/modules/apc.so: undefined symbol: sapi_globals in Unknown on line 0