Search code examples
phpamazon-web-servicesmemcachedebextensionsamazon-linux-2

Installing memcached on Amazon Linux


I am trying to use memcached for PHP on a Amazon Linux EC2 instance. In my PHP script when I call $mc = new Memcached(); the following error gets thrown in /var/log/php-fpm/www-error.log:

[06-Sep-2020 00:49:44 UTC] PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/app/current/memcached.php:8
Stack trace: #0 {main}
  thrown in /var/app/current/memcached.php on line 8

Calling php -m I do not see memcached listed, which I assume means it is not installed.

So, first I tried:

sudo yum install memcached
sudo yum install php70-pecl-memcached

along with edits to /etc/php.ini to include

session.save_handler = memcached
session.save_path = "127.0.0.1:11211"

as outlined here but no luck.

So next, I tried the suggestion as outlined by AWS support here for Amazon Linux EC2 v1. Looking through cfn-init-cmd.log, the package libmemcached is installed successfull, however, the script fails when it tries to install memcached as it is unable to find /use/bin/pecl7 as per the command 01_install_memcached. For reference, my full script in .ebextensions/memcache.config was:

packages:
  yum:
    libmemcached-devel: []

commands:
  01_install_memcached:
    command:  /usr/bin/yes 'no'| /usr/bin/pecl7 install memcached
    test: '! /usr/bin/pecl info memcached'
  02_rmfromphpini:
    command: /bin/sed -i -e '/extension="memcached.so"/d' /etc/php.ini
  03_createconf:
    command: /bin/echo 'extension="memcached.so"' > /etc/php-7.3.d/41-memcached.ini
    test: '/usr/bin/pecl7 info memcached'

Any ideas on how to resolve?


Solution

  • So this slightly revised .ebextension script worked. I'm no AWS/Linux/PHP expert, so there is probably a more correct way to approach this.

    packages:
      yum:
        libmemcached-devel: []
    
    commands:
      01_install_memcached:
        command:  /usr/bin/yes 'no'| /usr/bin/pecl install memcached
        test: '! /usr/bin/pecl info memcached'
      02_rmfromphpini:
        command: /bin/sed -i -e '/extension="memcached.so"/d' /etc/php.ini
      03_createconf:
        command: /bin/echo 'extension="memcached.so"' > /etc/php.d/41-memcached.ini