WebFaction
Community site: login faq
4
1

I want to use APC-based caching for my PHP 5.3 application, now that I have more memory. How do I do that?

asked 17 Dec '11, 03:11

neeravk's gravatar image

neeravk
1.5k3411
accept rate: 41%

edited 17 Dec '11, 03:14


Admin note: this procedure will not produce a working APC installation on our servers, since a) APC doesn't work with php-cgi, and b) you can't load custom PECL extensions with php-fastcgi. If you need APC, then see the second answer for instructions on how to build a Nginx + php-fpm stack.


You can do that by installing the APC PECL extension :). Here is the list of commands to do that:

# Procedure for installing APC for PHP5.3 on a PHP+CGI server
# Create an install directory
mkdir ~/pecl_install
cd ~/pecl_install
# Download the last version of APC (as seen at http://pecl.php.net/package/APC)
wget http://pecl.php.net/get/APC-3.1.9.tgz
# Unpack and get inside the extracted dir
tar xzvf APC-3.1.9.tgz
cd APC-3.1.9
# Generate the configuration script
phpize53
# Configure for php53 and make
./configure --with-php-config=/usr/local/bin/php53-config
make
# Create a folder where the custom PHP extensions would live
mkdir ~/php53-exts
# Copy the compiled extension to the custom folder
cp modules/apc.so ~/php53-exts/
# (Optional) Link the default server extensions inside the new folder, so they are still accessible
ln -s /usr/local/lib/php53/extensions/no-debug-non-zts-20090626/* ~/php53-exts/

# Customize the php.ini file of the desired web application, so that it loads the new extension
# MAKE SURE to replace 'htdocs' with the specific application you are enabling this for
echo "extension_dir = $HOME/php53-exts" >> ~/webapps/htdocs/php.ini
echo "extension = apc.so" >> ~/webapps/htdocs/php.ini
permanent link

answered 17 Dec '11, 03:13

neeravk's gravatar image

neeravk
1.5k3411
accept rate: 41%

edited 16 Jan '15, 20:47

seanf's gravatar image

seanf ♦♦
11.8k21333

Script executed successfully but APC is not listed in phpinfo(), any idea what can be wrong?

(30 Dec '11, 07:40) Naz Naz's gravatar image

Hi,

It's hard to say without looking at your app. Could you open a ticket, so we can take a look?

(30 Dec '11, 07:59) todork todork's gravatar image

for some reason "extension = apc.so" was not included inside php.ini, thanks for pointing it out.

(30 Dec '11, 08:38) Naz Naz's gravatar image

I've got APC running and it seems to be functioning but when I browse to the apc.php file to see the stats nothing seems to be displayed?

(15 Feb '13, 13:55) peterhough peterhough's gravatar image
1

If you are not using fastCGI, APC will not work at all (everything is re-initialized on each request).

If you are using FastCGI, then APC will work, but each thread will have its own separate cache, making APC caching inefficient and much less effective (everything may need to be cached up to six times before it's really cached, and this also bloats memory consumption). This is unfortunately just how APC works, as per this bug report, and this stackoverflow discussion.

This guide shows how to install a PECL extension such as APC. The actual operation of APC itself is up to the APC maintainers, and in their words, "this behaviour is the intended one as of now".

(18 Feb '13, 21:22) ryans ♦♦ ryans's gravatar image

Thanks for your comment ryans. However, looking at the bug report "It works fine if you use spawnfcgi or php-fpm". Is using a process manager possible or would I need to build my own PHP worker as a custom app?

(22 Feb '13, 05:04) peterhough peterhough's gravatar image
showing 5 of 6 show 1 more comments

In response to my comments on the answer above I thought I'd clarify how I've got APC working with PHP-FPM. I'm by no means an expert at this but managed to fumble my way through.

Using the guide posted on question 930 as a base I installed nginx as a custom webapp and a custom PHP stack. The only issue I encountered was that I also needed to install libmcrypt in my home directory.

The PHP configure line I used tries to match a standard Webfaction PHP install:

./configure --enable-fpm --prefix=/home/username/phpstack/php-5.4.12 --with-curl=/usr --with-pdo-pgsql --with-pgsql --with-pdo-mysql --with-mysql --enable-bcmath --enable-calendar --enable-exif --enable-ftp --with-gd --with-gettext --with-gmp --with-imap --with-imap-ssl --enable-intl --with-ldap --enable-mbstring --with-mcrypt=/home/username/phpstack/libmcrypt-2.5.8 --with-mhash --with-mysqli --with-openssl --with-pspell --enable-soap --enable-sockets --with-tidy --with-xmlrpc --with-xsl --enable-zip --with-zlib-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-kerberos --with-mysql-sock=/var/lib/mysql/mysql.sock --with-iconv --with-regex

With this set up I've a custom nginx webapp running a custom install of PHP with FPM enabled. APC is caching my files and the stats display correctly.

Similar to how the Webfaction php worker script works I also need to installed a cronjob to restart nginx & PHP if the server went down. Both nginx and PHP need to be configured to create PID files and you'll also init scripts for both nginx and php. The init scripts need to fail if the PID exists so the nginx one will need modifying to do this (use the PHP one as an example).

The script to do run on cron might look like the following:

#!/bin/sh
nginx_PID=/home/username/webapps/nginx/logs/nginx.pid
php_PID=/home/username/phpstack/php-5.4.12/var/run/php-fpm.pid

if [ -r $nginx_PID ] ; then if [ ! -f /proc/$(cat $nginx_PID)/status ] ; then echo "NGINX process not running - removing stale PID" rm $nginx_PID fi fi

if [ -r $php_PID ] ; then if [ ! -f /proc/$(cat $php_PID)/status ] ; then echo "PHP process not running - removing stale PID" rm $php_PID fi fi

/home/username/phpstack/bin/php-fpm start /home/username/phpstack/bin/nginx start

I hope that helps other fumble their way through this set-up.
Comments welcome.

Pete.

permanent link

answered 11 Apr '13, 04:23

peterhough's gravatar image

peterhough
2314
accept rate: 0%

edited 27 Jan '15, 01:09

seanf's gravatar image

seanf ♦♦
11.8k21333

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×251
×9

question asked: 17 Dec '11, 03:11

question was seen: 16,146 times

last updated: 28 Jan '15, 02:16

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM