How do I install PECL 7 modules on Elastic Beanstalk environments running on PHP with Amazon Linux 1 stacks?

Last updated: 2020-07-08

I want to install Redis, Memcached, or other modules for PECL 7 on an AWS Elastic Beanstalk environment running on PHP 7.3 using Amazon Linux 1 stacks.

Short description

To install PECL 7 modules, you must:

  1. Install any dependencies.
  2. Create a Linux configuration file (.ebextensions) to install and run the PECL 7 modules.

Important: The following PHP extensions are already included on the release of the PHP platform on Amazon Linux 2:

  • php-pecl-redis (for communication with the Redis key-value store)
  • php-pecl-memcached (for the Memcached caching daemon)

Note: You can install PECL PHP extensions on any PHP environment that has PEAR support. PECL is installed by default on the Elastic Beanstalk PHP stack and is provided by the php-pear RPM. For more information, see redis on the PECL website.

Resolution

Install the Redis extension

1.    In the .ebextensions directory in the root of the source bundle of your application, create a .ebextension file based on the following example:

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

The .ebextension does the following:

Installs the Redis extension (01_install_redis)
Removes the entry that the pecl7 command creates on the /etc/php.ini file to load the extension (02_rmfromphpini)
Creates a configuration file that loads the Redis module (03_createconf), which requires that the JSON module (installed by default) loads before the Redis module

2.    Deploy the new version of your application to your Elastic Beanstalk environment.

3.    Connect to your instance using SSH, and then run the following command:

php -m | egrep '(redis)'

You receive the following output:

$ php -m | egrep '(redis)'
redis

Install the Memcached extension

1.    In the .ebextensions directory in the root of the source bundle of your application, create a .ebextension file based on the following example:

packages:
  yum:
    libmemcached-devel: []

The preceding .ebextension installs libmemcached-devel, which is a prerequisite for installing the Memcached extension.

2.    In the .ebextensions directory in the root of the source bundle of your application, create a .ebextension file based on the following example:

commands:
  01_install_memcached:
    command:  /usr/bin/yes 'no'| /usr/bin/pecl7 install memcached
    test: '! /usr/bin/pecl7 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'

The .ebextension does the following:

Installs the Memcached extension (01_install_memcached)
Removes the entry that the pecl7 command creates in the /etc/php.ini file to load the extension (02_rmfromphpini)
Creates a configuration file that loads the Memcached extension (03_createconf)

3.    Deploy the new version of your application to your Elastic Beanstalk environment.

4.    Connect to your instance using SSH, and then run the following command:

php -m | egrep '(memcached)'

You receive the following output:

$ php -m | egrep '(memcached)'
memcached

Did this article help?


Do you need billing or technical support?