Over the weekend, I was working on a PHP REST API that is using Mongo DB as it’s database. I haven’t worked with MongoDB before, I have always used MySQL/MariaDB/PostgreSQL DB etc. for almost all my projects. The reason am bringing the MongoDB is because you need PHP MongoDB Module to connect to the database.
By default, AWS Elastic Beanstalk PHP preconfigured environment doesn’t ship with the module, hence the need to install it during deployment. If you are not aware, Elastic Beanstalk applications needs to be stateless. Your application needs to be able to setup everything automatically on its own using EB Extensions.
EB Extensions are used to modify a deployment environment so that you can install missing packages, setup configurations and do other stuff that will help your application run smoothly. All this is done automatically without your input as per the EB extensions. You can learn more about EB Extensions here.
Installing PEAR and PHP PEAR Extensions
In my case, I needed to install the PHP MongoDB Module and Setup Composer. The second bit is straight forward but the first one is where I ran into a lot of difficulties. Almost every resource I found over the internet was working for any one not using PHP 7 and above. This is because commands used for PHP 5 for PEAR (Installing PEAR itself and Installing PEAR Packages) have changed for some reason in Amazon Linux. You need to add a 7 at the end now.
For instance, to instal PEAR use the following command:
sudo yum -y install php7-pear php70-devel gcc
And to install a PEAR Package you need the following command:
sudo pecl7 install mongodb
(Notice the highlighted parts.)
You could connect to your EC2 instance and install packages manually, but It’s not recommended. This is where EB Extensions come into play.
In the root of your project, create a new folder called .ebextensions and add two files: packages.config and php.config. The first one will install all the packages (From yum and other Linux package managers) while the second one will install all PHP PEAR packages.
In the first file – packages.config – you need to add the following content.
packages: yum: php7-pear:  php71-devel:  gcc: 
This tells Elastic Beanstalk to install PHP7-pear, PHP70-devel and GCC compiler. Feel free to add extra packages as you need them. You can learn more about Packages config here.
In the second file – php.config – add the following content:
commands: install_mongo_driver: command: pecl7 install mongodb test: "php -r \"exit(extension_loaded('mongodb') ? 1 : 0);\""
This will first test whether PHP mongodb module exists and if it doesn’t it will install mongodb extension using PEAR. You can learn more about commands configs here.
While this post has primarily focused on MongoDB Module for PHP, it will work with any other PHP Module available on PEAR. You can search for PEAR packages here. Just remember to use pecl7 instead of just pecl during the installation process. In case of further updates, I will be sure to update this post with new information.