MongoDB + PHP

MongoDB with PHP

Hey Guys !

Its Arsalan Peerzada Here .. In this Blog we try to learn how to connect MongoDB with PHP using WAMP, because WAMP (Windows Apache MySQL PHP) is more dedicated to windows as compare to XAMPP or other Servers.

So lets Start with the Downloading ….

MongoDB : https://www.mongodb.org/downloads

Windows 32-bit or Windows 64-bit 2008 R2+   , In .Zip format.

pic1

WAMP : http://www.wampserver.com/en/#download-wrapper

Windows 32-bit or Windows 64-bit.

pic2

pic3

This picture will appear , but don’t worry it is not that much dangerous as is look like …

Open WAMPServer Installation Wizard. With This icon

logo

Click on Next >

pic4

Select “I accept the agreement” and then click Next.

pic5

Select the default path and Click Next >

pic6

Select both and Click Next.

pic7

Click Install

pic8

After Installation…!

Choose Browser of your own choice or Just click Open

pic9

If you are not sure , leave the default values ..

pic10

Now Click Select Launch and then Click Finish  …

pic11

You’ll see then WAMP icon in lower-right corner

Red Icon = All Services Stop .

Orange Icon = Partial Services are running .

Green Icon = All Services are running.

Open it ,

pic12

If Icon is Red , Orange or Green . Do not Worry we’ll configure it later.

Now Click Localhost , WAMPServer Page will Open .

If not ,

Go to

C:\wamp\bin\apache\apache2.4.9\conf

Open httpd and httpd.conf respectively and search Listen then change Every :80  to :8080

Go to

C:\wamp\bin\apache\apache2.4.9\conf\extra

Open httpd-vhosts then change every :80 to :8080

Now your LocalHost will be LocalHost:8080

MongoDB Continue:

Now go to

C:\wamp\bin\

Create a new folder , Copy the name of MongoDB zip file and rename the folder with it.

pic13

like

Extract the MongoDB zip file into this folder.

Now Create Folder , names conf , data , logs

pic14

In conf folder create a note pad file and save it in mongodb.conf and write this content in it.

# mongodb.conf

# data directory

dbpath=C:\wamp\bin\mongodb-win32-x86_64-2008plus-3.0.7\data\db

# log directory

logpath=C:\wamp\bin\mongodb-win32-x86_64-2008plus-3.0.7\logs\mongodb.log

logappend=true

# run on localhost for development

bind_ip = 127.0.0.1

# using port

port = 27017

 

Note :- adjust your mongodb version in the file.

 

Open the windows command prompt as administrator privilege and locate it to

C:\wamp\bin\mongodb-win32-x86_64-2008plus-3.0.7\bin.

 

 

Note :- adjust your mongodb version

 

Install MONGODB :

Execute this command to install Mongo DB as a windows service.

Command to execute:

mongod.exe –install –config C:\wamp\bin\mongodb-win32-x86_64-2008plus-3.0.1\conf\mongodb.conf –logpath C:\wamp\bin\mongodb-win32-x86_64-2008plus-3.0.1\logs\mongodb.log

 

Note :- adjust your mongodb versions

 

Set Environment Path:

Now set the Windows Environment Path

Right click on My Computer.
Click Advanced system settings.
Click Environment Variables button.
Click PATH on Variable column, then append this path.

 

;C:\wamp\bin\mongodb\mongodb-win32-x86_64-2008plus-3.0.1\bin;C:\wamp\bin\php\php5.5.12

Note :- adjust your mongodb and PHP versions 

Check MONGODB:

Now Open Command Prompt with Administrator and type mongod , you’ll see this

pic15

Check your MongoDB , Now Open another Command Prompt with administrator and type mongo

And some other commands to check your MongoDB database is working or not.

pic16

NOW ADD THE PHP Extension:

first of all you have to download the latest mongo dll files from

https://s3.amazonaws.com/drivers.mongodb.org/php/index.html

unzip the downloaded file and you will see a bunch of files like this:

pic17

now it is time to decide which dll file you have to use.
open your phpinfo() and search for word compiler. you will see sth like this:
pic18
so now according to my compiler version (MSVC11) I have to choose file with vc11 word inside their name. according to my PHP version which is 5.5.12 I have to choose file with word 5.5 inside their name.
now search for word thread safety in your phpinfo
pic19
and if it was enabled, it means that you have to choose file that doesn’t have word nts inside their name but if your thread safety was disabled then you need to choose file with word nts inside their name.
now according to your installation architecture you may choose files with x86_64 inside their name.
after choosing the appropriate dll file, rename it to

php_mongo.dll

and copy paste it to your php extensions directory which is mostly located at

C:\wamp\bin\php\php5.5.12\ext

pic20

now open your php.ini file and add this at the end of it.

Edit your php.ini file to add the new extension.

pic21

wampmanager -> PHP -> php.ini

find extensions then add this line below other extension lines,

extension=php_mongo.dll

To use mongo with scripts run from the command line (PHP CLI) edit

C:\wamp\bin\php\phpx.y.z\php.ini

and add the extension line again in there.

 

Copy all dll files from

C:\wamp\bin\php\php5.5.12

to

C:\wamp\bin\apache\apache2.4.9\bin

Restart your system and Open WAMP

Run localhost:8080 by :-

pic22

and then click on the phpinfo() link on the homepage.

You should now see a section entitled ‘mongo’ with some configuration options listed. If you do the PHP MONGO extension is active.

pic23

Starting WAMP with MONGODB:

Now Open Notepad++ in c:\wamp\www

with MyPHP.php

write this code

<?PHP

 

$m = newMongoClient();

 

echo “MongoDB Successfully Running”;

 

?>

Now open your browser type  LocalHost:8080\MyPhP.php

 

If this code work then your MongoDB+WAMP is running.

 

Conclusion:

Today we learn how to Connect MongoDB with WAMP.

Now we are able to work with famous NoSQL MongoDB with PHP