How to store php sessions using memcached and unix socket

7,123

Memcached config.d

A file path is all that's needed for Memcached's unix socket.

MEMCACHED_ARGS="-s /tmp/memcached.sock -a 666"

You can confirm that Memcached is operating normally by running the stats command through the unix socket.

bash $> echo stats | nc -U /tmp/memcached.sock
# returns list of server statistics

Configure PHP's session to use Memcached

Edit php.ini, or /etc/php/conf.d/memcached.ini, and add session support.

extension=memcached.so
session.save_handler="memcached"
session.save_path="/tmp/memcached.sock"

Restart services and verify php logs

Share:
7,123

Related videos on Youtube

laertiades
Author by

laertiades

Fresh out of hibernation

Updated on September 18, 2022

Comments

  • laertiades
    laertiades over 1 year

    I am running Arch linux with systemd, nginx, and php with php-fpm. I am trying (and failing) to configure memcached to store sessions using a unix socket. I have memcached installed and active, however, I am unable to disable networking. I added the following line to /etc/conf.d/memcached

    MEMCACHED_ARGS="-s unix:///tmp/memcached.sock -a 666"
    

    I also tried:

    MEMCACHED_ARGS="-s /tmp/memcached.sock -a 666"
    MEMCACHED_ARGS="-s unix:/tmp/memcached.sock -a 666"
    

    when I restart memcached I always get:

    memcached.service - Memcached Daemon
              Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled)
              Active: active (running) since Sat 2013-01-19 17:41:15 PST; 5min ago
            Main PID: 773 (memcached)
              CGroup: name=systemd:/system/memcached.service
                      └─773 /usr/bin/memcached -l 127.0.0.1
    

    when I run php script with sessions php error log shows (not surprisingly):

    [19-Jan-2013 16:46:45 America/Los_Angeles] PHP Warning:  Unknown: Failed to write session data (memcached). Please verify that the current setting of session.save_path is correct (unix:/tmp/memcached.sock) in Unknown on line 0
    

    I also installed the php-memcached package but I don't what it does or how to get it going. I uncommented the following line in /etc/php/conf.d/memcached.ini:

    extension=memcached.so
    

    but that didn't change anything.

    Any insights or suggestions would be greatly appreciated.

  • laertiades
    laertiades over 10 years
    info here is good. Thanks. I also had to put memcached config info into service file because config file wasn't getting read. Furthermore I had to install php-memcached package per pontikis.net/blog/install-memcached-php-archlinux
  • Josh
    Josh about 10 years
    Instead of making the file world-writable, you could also run memcached as the web server user (if possible).