skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. I solved this by adding memchache directly into installation

    packages:
      yum:
          libmemcached-devel: []
           memcached
    commands:
      01_rmfromphpini:
        command: /bin/sed -i -e '/extension="memcached.so"/d' /etc/php.ini
      02_createconf:
        command: /bin/echo 'extension="memcached.so"' > /etc/php.d/41-memcached.ini
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search