How to install the PHP MongoDB driver on Amazon EC2

I recently found myself with the need to connect to a Mongo DB instance from a PHP application running on Amazon EC2.  The PHP docs led me to this MongoDB extension, which seemed to be a good fit for my needs.  Still, the matter of installation was a little bit less clear, considering that the EC2 instance was pretty barebones and lacked a fair number of the “standard” libraries that would enable this single line installation to work.

After a bit of finagling, I got everything to work via the steps I’ve outlined below.

As a quick note, my Amazon EC2 instance was running a standard Amazon Linux AMI, and a LAMP web server had been installed via these well-documented steps.

Once you’ve SSH’d to your instance, run the following to install required libraries:

Then, we’ll need to add the extension to our php.ini file. Open your editor with the following command:

Now, we’ll want to insert into the file at the right place. We’re looking for the area under the following header:

; Dynamic Extensions ;

Once you’ve added to the file – just put it on its own new line – save it and exit.

Finally, restart the web server.