Skip to content

Instantly share code, notes, and snippets.

@rashkopetrov
Last active December 3, 2019 14:28
Show Gist options
  • Save rashkopetrov/1023d78e67e54ba849a49d7abc9a3ca7 to your computer and use it in GitHub Desktop.
Save rashkopetrov/1023d78e67e54ba849a49d7abc9a3ca7 to your computer and use it in GitHub Desktop.
Install MongoDB and make it work with PHP 7.0

Install MongoDB

  1. Import the public key used by the package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
  1. Create a list file for MongoDB.
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
  1. Reload local package database.
sudo apt-get update
  1. Install the MongoDB packages.
sudo apt-get install -y mongodb-org

Install PHP Driver

  1. Install PHP pear
sudo apt-get install php-pear
  1. Install MondoDB Driver
sudo pecl install mongodb

Configure PHP

  1. Create mongo.ini file in /etc/php/7.0/mods-available with the following content:
; configuration for php mongodb module
; priority=20
extension=mongodb.so
  1. Create symlinks
sudo ln -s /etc/php/7.0/mods-available/mongo.ini /etc/php/7.0/cli/conf.d/20-mongo.ini
sudo ln -s /etc/php/7.0/mods-available/mongo.ini /etc/php/7.0/fpm/conf.d/20-mongo.ini
  1. Restart Nginx and PHP
sudo service php7.0-fpm restart && sudo service nginx restart
  1. test
php -i | grep 'Mongo'
<?php
// http://php.net/manual/en/class.mongodb-driver-manager.php
$manager = new MongoDB\Driver\Manager();
echo '<pre>';
var_dump($manager);
echo '</pre>';
exit;
@Jul10l1r4
Copy link

Thanks!!!

@ThomasSquall
Copy link

If you'd like to test a wrapper that simplify the usage here's repository of mine that can help you :)

https://github.com/ThomasSquall/PHP7MongoDriver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment