Dominique De Cooman
Publié sur Dominique De Cooman (https://dominiquedecooman.com)

Accueil > Installer Memcache pour Drupal sur CentOS

Dominique De Cooman
Sunday, March 28, 2010 - 15:34
linux [1]
performance [2]
memcached [3]

Dans le terminal :

Installez d’abord libevent :

cd /usr/local/src/
wget <a href="http://monkey.org/~provos/libevent-1.3e.tar.gz
tar">http://monkey.org/~provos/libevent-1.3e.tar.gz
tar</a> zxpfv libevent*
cd libevent*
./configure
make install

Téléchargez et installez Memcached :

cd /usr/local/src/
wget <a href="http://www.danga.com/memcached/dist/memcached-1.2.4.tar.gz
tar">http://www.danga.com/memcached/dist/memcached-1.2.4.tar.gz
tar</a> zxpfv memcached*
cd memcached*
./configure
make install

Démarrez le service pour le tester :

./memcached -u root -d -m 48 -l 10.10.102.6 -p 11211
 
//changer le ip dans votre ip
//changer le utilisateur root dans le bon utilisateur pour memcache
//Pour tester tapez: memcached -u root -vv
//Arrêt le memcached
killall memcached

Si l’erreur « libevent-1.3e.so.1: cannot open shared object file: No such file or directory » apparaît, essayez ceci :

ln -s /usr/local/lib/libevent-1.3e.so.1 /lib64/

Si l’erreur persiste, copiez les fichiers de /usr/local/lib vers /usr/lib.

Le serveur Memcached devrait maintenant fonctionner. Il reste à l’intégrer à PHP. Installez libmemcached :

cd /etc/yum.repos.d/
wget <a href="http://rpms.famillecollet.com/remi-enterprise.repo
wget">http://rpms.famillecollet.com/remi-enterprise.repo
wget</a> <a href="http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
wget">http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarc...</a> <a href="http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm">http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm</a> -Uvh remi-release-5*.rpm epel-release-5*.rpm
 
yum --enablerepo=remi install libmemcached\*

Installez l’extension PECL Memcache :

pecl install memcache 

Vous pouvez aussi la compiler vous-même :

pecl download memcached
tar zxvf memcached-1.0.0.tgz (or whatever version downloads)
cd memcached-1.0.0
phpize
./configure --with-libmemcached-dir=/opt/local
make
make install

Ajoutez ces lignes au fichier php.ini :

extension=memcache.so
memcache.hash_strategy="consistent"

Redémarrez le serveur :

/etc/init.d/httpd restart

Pour vérifier que le module PHP est activé, affichez les informations PHP et recherchez la section des paramètres Memcache.

Dans Drupal, activez le module et ajoutez ceci au fichier de configuration :

$conf = array(
   // The path to wherever memcache.inc is. The easiest is to simply point it
   // to the copy in your module's directory.
   'cache_inc' => './sites/all/modules/memcache/memcache.inc',
   // or
   // 'cache_inc' => './sites/all/modules/memcache/memcache.db.inc',
);

Complément :
Voici une configuration de cache plus adaptée à un site Drupal. J’ai défini six compartiments de cache, puis ajouté un compartiment distinct pour les formulaires dans la mise à jour ci-dessous.

$conf['cache_inc'] = 'sites/all/modules/contrib/memcache/memcache.inc';
$conf['memcache_servers'] = array(
    'localhost:11211' => 'default',
    'localhost:11212' => 'page',
    'localhost:11213' => 'filter',
    'localhost:11214' => 'content',
    'localhost:11215' => 'block',
    'localhost:11216' => 'views',
    'localhost:11217' => 'form',//read update below why the form cache needs its own bin
   );
$conf['memcache_bins'] = array(
     'cache'        => 'default',
 
     'cache_menu'   => 'default',
     'cache_apachesolr'   => 'default',
     'cache_form'   => 'form',//read update below why the form cache needs its own bin
     'cache_glue'   => 'default',
     'cache_rules'   => 'default',
     'cache_mollom'   => 'default',
     'cache_update'   => 'default',
     'cache_usermenu'   => 'default',
     'cache_views'   => 'views',
     'cache_views_data'   => 'views',
     'cache_page'   => 'page',
     'cache_filter' => 'filter',
     'cache_content' => 'content',
     'cache_block'   => 'block',
);

Les compartiments doivent être initialisés différemment. Il faut aussi un script pour les démarrer et les arrêter. Le script suivant démarre les services Memcached à la place de la commande utilisée plus haut. Créez un fichier dans /etc/init.d :

cd /etc/init.d/
vi memcached
//type 'i' to edit
//paste this
#!/bin/bash
#source is the path to your memchached installation (if you have another version adapt)
source="/usr/local/src/memcached-1.2.4/memcached"
 
RETVAL=0
prog="memcached"
desc="Distributed memory caching"
 
start() {
        echo -n $"Starting $desc ($prog): "
 
 
        $source -d -u root -m 64  -p 11211
        $source -d -u root -m 320 -p 11212
        $source -d -u root -m 128 -p 11213
        $source -d -u root -m 320 -p 11214
        $source -d -u root -m 128 -p 11215
        $source -d -u root -m 64 -p 11216
        $source -d -u root -m 64 -p 11217
 
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}
 
stop() {
        echo -n $"Shutting down $desc ($prog): "
        killall $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
        return $RETVAL
}
 
restart() {
        stop
        start
}
 
reload() {
        echo -n $"Reloading $desc ($prog): "
        killproc $prog -HUP
        RETVAL=$?
        echo
        return $RETVAL
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
 condrestart)
        [ -e /var/lock/subsys/$prog ] && restart
        RETVAL=$?
        ;;
  reload)
        reload
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac
 
exit $RETVAL
 
 
//press esc type :wq to save and exit
//make the file executable
chmod u+x memcached
 
//to start
service memcache start
 
//to stop
service memcache stop

Dans admin/settings/memcache, activez l’affichage des statistiques pour surveiller le fonctionnement. Consultez ensuite le rapport Memcached pour voir les statistiques de chaque compartiment. Adaptez la mémoire allouée à vos besoins : une répartition équilibrée donne les meilleures performances.

Mise à jour :
Avec Memcached, les formulaires ont besoin d’un compartiment distinct : http://drupal.org/node/500646 [4]. J’ai corrigé le code ci-dessus en conséquence.

J’ai également rencontré un problème avec Hierarchical Select sous Memcached. Ce correctif permet de le résoudre : http://drupal.org/node/538022#comment-3116204 [5]


URL source: https://dominiquedecooman.com/fr/installer-memcache-pour-drupal-sur-centos

Liens
[1] https://dominiquedecooman.com/fr/blog-topics/linux-0
[2] https://dominiquedecooman.com/fr/blog-topics/performance
[3] https://dominiquedecooman.com/fr/blog-topics/memcached
[4] http://drupal.org/node/500646
[5] http://drupal.org/node/538022#comment-3116204