Install the APC PHP extension on Debian
APC is the acronym for Alternative PHP Cache. This module enhance the performances of your PHP applications by caching the "compiled" PHP op-code.
This howto is tested on:
- Debian 6.0 Squeeze
- Debian 7.0 Wheezy
- Ubuntu 13.10 Saucy Salamander
Warning
This post is incompatible with these how-to:
Prerequisites
This howto needs :
- a working PHP setup, as described by Install PHP-FPM on Debian.
Installation from Debian repository
Install the software:
if [ -n "$(command apt-cache pkgnames php5-apc)" \
-a -n "$(command dpkg --status "php5-common" | command grep "dotdeb")" ]; then
# Check for dot-deb packages.
command apt-get -y install php5-apc
elif [ -n "$(command apt-cache pkgnames php-apc)" ]; then
command apt-get -y install php-apc
fi
Reload the configuration:
test -e '/etc/init.d/php5-fpm' && command service 'php5-fpm' 'restart'
test -e '/etc/init.d/apache2' && command service 'apache2' 'force-reload'
Configuration
Detect PHP extension configuration path:
MODS_CONF_PATH='/etc/php5/conf.d'
test -d '/etc/php5/mods-available' \
&& MODS_CONF_PATH='/etc/php5/mods-available'
The APC default configuration is sufficient. Make sure the APC extension is enabled, and turn on the upload progress status option:
echo '; APC Configuration
; priority=50
apc.enabled = 1
; Memory allocated to APC. Use Munin or APC Info to see if more is needed.
apc.shm_size="96M"
; rfc1867 allow file upload progression display.
apc.rfc1867 = on' \
> "${MODS_CONF_PATH}/apc-config.ini"
command type -f 'php5enmod' &>'/dev/null' && command php5enmod 'apc-config/50'
If needed, apply PHP-FPM specific settings:
if [ -e '/etc/init.d/php5-fpm' ]; then
echo '; Enable APC ressources sharing.
; priority=50
apc.mmap_file_mask=/apc.shm.XXXXXX' \
> "${MODS_CONF_PATH}/apc-fpm.ini"
command type -f 'php5enmod' &>'/dev/null' && command php5enmod 'apc-fpm/50'
fi
Note : If sharing APC data between many PHP applications is a (security) issue, the solution is to create a FPM pool by site. The main downside is the raise of server configuration complexity.
Reload the configuration:
test -e '/etc/init.d/php5-fpm' && command service 'php5-fpm' 'restart'
test -e '/etc/init.d/apache2' && command service 'apache2' 'force-reload'
Manual installation
If the Debian package is not available, install the extension manually.
Detect PHP extension configuration path:
MODS_CONF_PATH='/etc/php5/conf.d'
test -d '/etc/php5/mods-available' \
&& MODS_CONF_PATH='/etc/php5/mods-available'
Environment preparation
Install the needed software:
command apt-get install php5-dev make php-pear apache2-prefork-dev libpcre3-dev
Update PEAR:
command pear channel-update pear.php.net
command pear upgrade PEAR
Update PECL:
command pecl channel-update pecl.php.net
command pecl upgrade
Software installation
Install the extension:
command pecl install 'apc' --with-apxs='/usr/bin/apxs2'
Enable the extension:
echo "; configuration for php APC module
; priority=20
extension=apc.so" > "${MODS_CONF_PATH}/apc.ini"
test -n "$(command -v php5enmod)" && command php5enmod 'apc'
Reload the configuration:
test -e '/etc/init.d/php5-fpm' && command service 'php5-fpm' 'restart'
test -e '/etc/init.d/apache2' && command service 'apache2' 'force-reload'
You can now configure APC.
Finalization
The manual installation recommends:
- The setup of a email alert on new PEAR upgrades availability, as described by Setup a email alert for PEAR upgrades.
Thanks
- Thanks to Alternative PHP Cache (en) developers.