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

2 minute read
1

I want to install Redis, Memcached, or other modules for PECL 7 on an AWS Elastic Beanstalk environment running on PHP 8.1 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

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 uses only files that end 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.

AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago