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

Last updated: 2021-03-22

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

Short description

To install PECL 7 modules, you must create a configuration file (.ebextension) that includes commands to:

  • Install dependencies
  • Install the PECL module
  • Create or modify configuration files

Note: For Amazon Linux 1 stacks, see How do I install PECL 7 modules on Elastic Beanstalk environments running on PHP with Amazon Linux 1 stacks?

Resolution

1.    Set up the .ebextensions directory in the root of the source bundle of your application. For example:

|-- .ebextensions
|        |-- redis/memcached.config
|        |-- other .config files
|-- other application files

For more information, see Advanced environment customization with configuration files (.ebextensions).

Note: All configuration files are stored in the .ebextensions folder. The Elastic Beanstalk environment only uses files ending with the .config extension.

2.    In the .ebextensions directory, create a configuration file that includes the following commands for your module.

For Redis modules:

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

The Redis configuration file does the following:

  • Installs the Redis module and tests for installation success (01_install_redis)
  • Removes the Redis extension from the /etc/php.ini file (02_remove_extension)
  • Creates a configuration file called 41-redis.ini at /etc/php.d/ (03_create_conf)

For Memcached modules:

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_remove_extension:
    command: /bin/sed -i -e '/extension="memcached.so"/d' /etc/php.ini
  03_create_conf:
    command: /bin/echo 'extension="memcached.so"' > /etc/php.d/41-memcached.ini

The Memcached configuration file does the following:

  • Installs the Memcached module and tests for installation success (01_install_memcached)
  • Removes the Redis extension from the /etc/php.ini file (02_remove_extension)
  • Creates a configuration file called 41-memcached.ini at /etc/php.d/ (03_create_conf)

Note: Always test a new .ebextension before adding to a production environment.


Did this article help?


Do you need billing or technical support?