Yii2 and MongoDb configuration with PHP7.0-fpm and NGiNX

Author: Rafal Marguzewicz
Categories: Yii2

peclList to do:
– Install MongoDB on Debian/Linux
– Install requires MongoDB PHP Extension
– Install MongoDB Extension for Yii 2
– Yii2 file configuration to use MongoDB

Install MongoDB on Debian/Linux

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.4 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl enable mongod.service
sudo systemctl start mongod

Check service MongoDB

sudo systemctl status mongod

Install requires MongoDB PHP Extension

sudo apt-get install libcurl4-openssl-dev pkg-config libssl-dev libsslcommon2-dev
sudo pecl install mongodb

add extension=mongodb.so in fpm and cli:

sudo echo "extension=mongodb.so" >> /etc/php/7.0/fpm/conf.d/30-mongodb.ini
sudo echo "extension=mongodb.so" >> /etc/php/7.0/cli/conf.d/30-mongodb.ini

Restart services:

sudo systemctl restart php7.0-fpm
sudo systemctl reload nginx

Install MongoDB Extension for Yii 2

composer require --prefer-dist yiisoft/yii2-mongodb

Yii2 file configuration

return [
    'components' => [
        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',

and in code example:
$collection = Yii::$app->mongodb->getCollection('test');

tnx for reading,

Your email address will not be published. Required fields are marked *

Name *
Email *

Read previous post:
NGINX phpMyAdmin configuration on localhost

If NGINX and MariaDb/MySQL are installed, can start install and configuration phpMyAadmin (PMA). This article is for Linux system. If...