Book Image

PHP and MongoDB Web Development Beginner's Guide

Book Image

PHP and MongoDB Web Development Beginner's Guide

Overview of this book

With the rise of Web 2.0, the need for a highly scalable database, capable of storing diverse user-generated content is increasing. MongoDB, an open-source, non-relational database has stepped up to meet this demand and is being used in some of the most popular websites in the world. MongoDB is one of the NoSQL databases which is gaining popularity for developing PHP Web 2.0 applications.PHP and MongoDB Web Development Beginner’s Guide is a fast-paced, hands-on guide to get started with web application development using PHP and MongoDB. The book follows a “Code first, explain later” approach, using practical examples in PHP to demonstrate unique features of MongoDB. It does not overwhelm you with information (or starve you of it), but gives you enough to get a solid practical grasp on the concepts.The book starts by introducing the underlying concepts of MongoDB. Each chapter contains practical examples in PHP that teache specific features of the database.The book teaches you to build a blogging application, handle user sessions and authentication, and perform aggregation with MapReduce. You will learn unique MongoDB features and solve interesting problems like real-time analytics, location-aware web apps etc. You will be guided to use MongoDB alongside MySQL to build a diverse data back-end. With its concise coverage of concepts and numerous practical examples, PHP and MongoDB Web Development Beginner’s Guide is the right choice for the PHP developer to get started with learning MongoDB.
Table of Contents (17 chapters)
PHP and MongoDB Web Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Time for action - installing PHP driver for MongoDB on Windows


Let's try installing the driver on a Windows machine running PHP 5.2 on Apache:

  1. 1. Download the ZIP archive http://downloads.mongodb.org/mongo-latest-php5.2vc6ts.zip on your machine and extract it.

  2. 2. Copy the php_mongo.dll file from the extracted folder to the PHP extension directory; this is usually the folder name ext inside your PHP installation.

  3. 3. Open the php.ini file inside your PHP installation and add the following line:

    extension=php_mongo.dll
    
  4. 4. Save the file and close it. Restart the Apache web server.

  5. 5. Open up your text editor and add the following code to a new file:

    <?php
    phpinfo();
    
  6. 6. Save the file as phpinfo.php inside the DocumentRoot of the Apache web server (the htdocs folder).

  7. 7. Execute the phpinfo.php script in your browser (http://localhost/phpinfo.php). Scroll down to find the section mongo to see all the MongoDB driver-specific information.

Congratulations! You have successfully installed the PHP driver for MongoDB.

What just happened?

In step 1, we download the ZIP file containing the DLL file php_mongo.dll for the PHP-MongoDB driver for PHP 5.2 (for the PHP 5.3 specific version, download http://downloads.mongodb.org/mongo-latest-php5.3vc6ts.zip instead). In step 2, we copy the php_mongo.dll file to the PHP extensions directory. If the installation directory of PHP on your machine is C:\php, the extension directory should be C:\php\ext. Then we edit the php.ini file (located under C:\php as well) to add the line extension=php_mongo.dll to it and restart Apache for the changes to take effect. Next we create and execute a one-line PHP script to invoke the phpinfo() method. If we are able to see the MongoDB driver specific information in the phpinfo() output, listed under section mongo, this means the driver was installed without a glitch.

Note

If you are running PHP on IIS, you should download the thread-safe VC9 version of the driver instead. Get it from the URL http://downloads.mongodb.org/mongo-latest-php5.3vc9ts.zip.

Installing the PHP-MongoDB driver on Unix

In a Unix-based system, the PHP driver for MongoDB can be installed using the pecl (PECL - PHP Extension Community Islam) program. You need to have it installed on your machine, which can be done by using the following command:

sudo pecl install mongo

When the installation is finished, edit the php.ini file (usually found at /etc/php.ini) to add the line:

extension=mongo.so

and then restart Apache.

In case you don't have pecl installed on your machine, you can download the driver source code from GitHub, build it, and install it manually:

$ tar zxvf mongodb-mongdb-php-driver-<commit_id>.tar.gz
$ cd mongodb-mongodb-php-driver-<commit_id>
$ phpize
$ ./configure
$ sudo make install

Check out the Mongo driver installation page http://www.php.net/manual/en/mongo.installation.php on the PHP official website to get operating system specific detailed information.