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.
[well]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.[/well]
Once you’ve SSH’d to your instance, run the following to install required libraries:
sudo yum -y install php-pear sudo yum -y install php56-devel sudo yum -y install gcc sudo yum -y install openssl-devel sudo pecl install mongodb
Then, we’ll need to add the extension to our php.ini file. Open your editor with the following command:
sudo vi /etc/php.ini
Now, we’ll want to insert extension=mongodb.so into the file at the right place. We’re looking for the area under the following header:
[syntax type=”html|php|js|css”];;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;[/syntax]
Once you’ve added extension=mongodb.so to the file – just put it on its own new line – save it and exit.
Finally, restart the web server.
sudo service httpd restart