29-11-2011 : Remettre dans le débat politique les principes du Conseil National de la Résistance
17-02-2012 : Pétition pour une protection de l’apiculture et des consommateurs face au lobby des OGM
Logo de mon site
Logo de mon site
Faire un don

Luxpopuli / eZ Publish / Dossiers techniques / eZ publish : optimiser l'environnement / Alternative PHP Cache (APC) -- Cache PHP Alternatif





Right menu

Logo du site ez.no  Logo XHTML 1.O du W3C  Logo XHTML 1.O du W3C  Site francophone officiel de Firefox
zero papier grâce aux catalogues et promos en ligne de bonial

Alternative PHP Cache (APC) -- Cache PHP Alternatif

Date de publication: le vendredi 13 juillet 2007 à 03h34
Dernière modification: par Pascal BOYER le dimanche 26 septembre 2010 à 20h05
« Article précédent: Configuring Apache and PHP -- Configurer Apache et PHP
» Article suivant: Conclusion and Resources -- Conclusions et Ressources

APC is an Open Source PHP accelerator for caching intermediate code from scripts. Intermediate code is the internal memory structures produced during compilation that are fed into the executor. APC increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. APC stores and executes compiled PHP scripts from shared memory. 
APC est un accélérateur PHP Open Source permettant de mettre en cache le code intermédiaire des scripts. Le code intermédiaire correspond aux structures de la mémoire interne (???) produites par les compilations introduites dans l'exécuteur. APC augmente les performances des scripts PHP en les mettant en cache lorsqu'ils sont à l'état compilés permettant ainsi que le surcoût (???) dû à la compilation soit presque complètement éliminé. APC stocke et exécute les scripts PHP compilés depuis la mémoire partagée.

 

 

Alternative PHP Cache

Obtaining APC / Télécharger APC

APC is available as an extension for PHP from the PECL repository. You can download the latest version from the APC home page (http://pecl.php.net/package/APC). 
APC est disponible dans le dépôt PECL en tant qu'extension de PHP. Vous pouvez en télécharger la dernière version depuis la page d'accueil du site du projet APC: http://pecl.php.net/package/APC

Installing APC / Installer APC

Before you start installing APC, make sure that the development tools listed in the APC prerequisites (such as autoconf and libtool) are installed on your system. Also, you may require access to the root account. 
Avant d'installer APC, assurez-vous que les outils de développement listés dans les prérequis de APC sont bien présents sur votre système (en particulier autoconf et libtool). Vous devez aussi être en mesure d'accéder au compte root de votre machine.

:
Pensez à jeter un oeil aux commentaires  laissés au bas des articles originaux !

The basic commands you must execute to compile and install APC from source are: 
Voici la liste des commandes à lancer pour compiler puis installer les sources de APC:

shell> cd /usr/local/src
shell> gunzip < /PATH/TO/APC-x.x.x.tgz | tar xvf -
shell> cd APC-x.x.x
shell> phpize
shell> ./configure --enable-apc-mmap
shell> make
shell> su -
shell> make install

These commands are interpreted as follows: 
Ces commandes sont interprétées de la manière suivante:

1. Choose the directory under which you want to unpack the distribution, and then change location into it. In the following example, we unpack the distribution under   /usr/local/src  
Choisissez le répertoire dans lequel vous souhaitez désarchiver la distribution et placez-vous dans ce répertoire. Dans l'exemple qui suit, nous désarchivons la distribution dans le répertoire /usr/local/src.

shell> cd /usr/local/src

2. Obtain an APC distribution from http://pecl.php.net/package/APC 
Téléchargez la distribution de APC sur cette page: http://pecl.php.net/package/APC

3. Unpack the source file, which creates the installation directory. This example shows how to unpack the distribution using gunzip: 
Désarchivez le fichier contenant les sources de APC pour créer le répertoire d'installation. L'exemple ci-dessous montre comment désarchiver la distribution en utilisant la commande gunzip :

shell> gunzip < /CHEMIN/VERS/APC-x.x.x.tgz | tar xvf -

The tar command creates a directory called "APX-x.x.x" (where "x.x.x" is the APC version number). If you are using GNU tar, no separate invocation of gunzip is necessary. Instead, you can use the following command to uncompress and extract the distribution: 
La commande tar  crée un répertoire nommé APC-x.x.x (où x.x.x représente le numéro de version de APC). Si vous utilisez GNU tar il n'est pas nécessaire d'invoquer séparément la commande gunzip. A la place, vous pouvez utiliser la commande ci-dessous pour décompresser et extraire la distribution:

shell> tar zxvf /CHEMIN/VERS/APC-x.x.x.tgz

4. Change location into the installation directory: 
Placez-vous dans le répertoire d'installation:

shell> cd APC-x.x.x

5. Run   phpize  .   phpize   is a script that is included with the PHP distribution, and is by default located in   /usr/local/php/bin   (assuming you installed PHP in   /usr/local/php  ). 
Exécutez la commande phpize qui est un script inclus dans la distribution de PHP et placé par défaut dans le répertoire /usr/local/php/bin (en supposant que vous ayiez installé PHP dans le répertoire /usr/local/php).

shell> /usr/local/php/bin/phpize

The output from running   phpize   should look like the following: 
La sortie de la commande phpize doit ressembler à ceci:

Configuring for:
PHP Api Version: 20020918
Zend Module Api No: 20020429 Zend Extension Api No: 20050606

6. Run the configuration script with the following parameters: 
Exécutez le script de configuration de APC avec les paramètres suivants:

shell> ./configure --enable-apc-mmap

The   --enable-apc-mmap   option tells the APC configuration script to use mmap instead of the default IPC shared memory support. 
L'option --enable-apc-mmap indique au script de configuration de APC d'utiliser mmap au lieu du support de mémoire partagée IPC par défaut.

The   configure   script will take several minutes to run, as it tests for the availability of packages and prerequisites on your system and builds the   Makefiles   that will later be used to compile APC. 
Le script configure prendra plusieurs minutes pour s'exécuter: il test la disponibilité, sur votre système, de certains packages et de tout ce qui est nécessaire puis crée le fichier Makefiles qui sera utilisé ultérieurement pour compiler APC.

7. Now you can build the APC components by running the command: 
Vous pouvez maintenant construire les composants de APC en lançant la commande make:

shell > make

8. Switch to the root user to execute the following commands: 
Passez en mode superutilisateur avec la commande suivante:

shell> su -

If you don't have access to the root account, ask your system administrator for help. 
Si vous n'avez pas accès au compte root, demandez de l'aide à l'administrateur de votre système.

9. Next, install the package by running: 
Ensuite, installez le package avec la commande suivante:

shell> make install

10. When the installation is done, make install should print the path to the extension. 
Edit your   php.ini   and set the path to the   apc.so   extension: 
Lorsque l'installation est terminée, la routine make install devrait afficher le chemin vers l'extension.
Editez alors votre fichier php.ini et indiquez le chemin vers l'extension apc.so comme ceci:

extension="/path/to/extension/apc.so"

Now you can restart your web server (using the command   /usr/local/apache/bin apachectl restart  ) and create a small test PHP file in your document root (by default   /usr/local/apache/htdocs  ). The file should contain just the following line: 
Vous pouvez à présent redémarrer votre serveur web avec la commande /usr/local/apache/bin apachectl restart puis créer, à la racine de votre serveur (par défaut /usr/local/apache/htdocs), un petit fichier PHP de test contenant cette simple ligne:

<?php phpinfo(); ?>

Put the   test.php   file in the Apache documents directory (  /usr/local/apache/htdocs  ) and access it in the web browser via the URL http://localhost/test.php. APC should be listed in the extensions list. If APC is not shown, check the Apache error log (  /usr/local/apache/logs/error.log  ) for possible errors. 
Nommez ce fichier test.php, placez-le à la racine de votre serveur Apache (par défaut /usr/local/apache/htdocs) puis accédez à ce fichier à l'aide de votre navigateur en tapant l'URI http://localhost/test.php. APC doit alors faire partie de la liste des extensions. Dans le cas contraire, consultez les fichiers de log d'Apache (placés par défaut dans /usr/local/apache/logs/error.log) pour y rechercher de possibles erreurs.

PECL installer / Installeur PECL

Another way to install APC is to use the PECL ("PHP Extension Community Library") installer. Run sudo peclinstall APC from the shell prompt. 
Une autre façon d'installer APC consiste à utliser l'installeur de PECL  ("PHP Extension Community Library"). Pour cela il suffit de lancer la commande:

sudo peclinstall APC

APC configuration / Configuration de APC

Add the following lines to your   php.ini   file (located by default in   /usr/local/lib/php.ini  ): 
Ajoutez les lignes suivantes à votre fichier php.ini (placé par défaut dans /usr/local/lib/php.ini):

; Load APC extension
 extension=/usr/local/lib/php/extensions/no-debug-non-zts-20020429/apc.so
 
; This can be set to 0 to disable APC. This is
; primarily useful when APC is statically compiled
; into PHP, since there is no other way to disable
; it (when compiled as a DSO, the zend_extension
; line can just be commented-out).
; (Default: 1)
apc.enabled = 1
 
 
; The size of each shared memory segment in MB.
; By default, some systems (including most BSD
; variants) have very low limits on the size of a
; shared memory segment.
; (Default: 30)
apc.shm_size=128 

APC GUI / L'Interface Graphique Utilisateur de APC

APC comes with useful graphical monitoring tool. It provides information about the APC state, cached files, amount of used and free memory, etc. Using this APC application, you can also clear all the caches without restarting Apache. This tool is also useful for tuning memory usage. 
APC installe un outil graphique de surveillance bien utile qui fournit des informations sur l'état de APC, sur les fichiers mis en cache, sur la quantité de mémoire libre et utilisée, etc... Par le biais de cette application vous pouvez également vider tous les caches sans avoir à redémarrer Apache. Cet outil est aussi très pratique pour régler l'usage de la mémoire.

To enable the APC application, copy   apc.php   from the   APC-x.x.x   directory to the Apache document directory (  /usr/local/apache/htdocs  ). Access the application from a web browser via the URL http://localhost/apc.php. 
Pour accéder à l'application APC faites une copie du fichier apc.php, placé dans le répertoire APC-x.x.x, dans le répertoire racine d'Apache  ( /usr/local/apache/htdocs) puis tapez l'URI http://localhost/apc.php dans votre navigateur.

The screenshot below shows the APC application: 
La capture d'écran ci-dessous montre l'application APC:

 

 

L'application APC

A propos de l'usage de APC, je souhaite ajouter cette référence: Benchmark eAccelerator VS apc  

Je vous invite à lire l'ensemble du test et particulièrement la page de conclusion qui se termine ainsi:

Conclusion

The tests we did showed us there can be an important performance gain by using a PHP accelerator. Using a PHP accelerator seems essential for a high-traffic site. 
Les tests que nous avons fait nous montrent que d'importants gains de performances peuvent être réalisés en utilisant un accélerateur PHP et qu'un accélérateur peut même s'avérer essentiel pour les sites à fort trafic.

But PHP accelerators are no panacea : if your code is slow, it will stay slow even with apc, eAccelerator or Zend. 
Mais les accélérateurs de PHP ne sont pas la panacée: si votre code est lent, il restera lent quand bien même vous utiliseriez APC, eAccelerator ou Zend.

Choosing an accelerator depends on your criteria : 
Le choix d'un accelerateur dépend de vos critères:

Each tool has advantages and drawbacks, the only way to know which one is the best at handling your code is to test both. 
Chaque outil présente des avantages et des inconvénients, le seul moyen pour savoir celui qui gèrera le mieux votre code est donc de les tester.

If, as I did, you choose eAccelerator (in order not to spend a dime on it, and to be able to change your files whenever you want), I would advise you not to get too much tied to eAccelerator, you might have to switch to apc in the future... 
Si, comme je l'ai fait, vous choisissez eAccelerator (afin de ne pas dépenser un 1/10 de dollar (???) et d'être capable de modifier vos fichiers chaque fois que vous le désirez) j'attire votre attention pour que vous ne vous attachiez pas trop à eAccelerator car vous risqueriez de devoir basculer vers APC dans le future... (l'article datant du 30 Mai 2006 !)

Friday 04 August 2006 10:41:00

Commentaires