{"id":16963,"date":"2025-11-21T12:18:01","date_gmt":"2025-11-21T06:48:01","guid":{"rendered":"https:\/\/www.skynats.com\/?p=16963"},"modified":"2025-11-24T12:37:56","modified_gmt":"2025-11-24T07:07:56","slug":"how-to-install-and-configure-php-mongodb-driver-on-ubuntu","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/how-to-install-and-configure-php-mongodb-driver-on-ubuntu\/","title":{"rendered":"How to Install\u00a0 and configure\u00a0 PHP MongoDB Driver on Ubuntu\u00a0"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>Establish a connection to a MongoDB instance from a PHP script using MongoDB\\Driver\\Manager.<\/li>\n\n\n\n<li>Supports both local and cloud-based MongoDB server<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-small-font-size\" id=\"h-1-install-prerequisites\"><strong>1. Install Prerequisites<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before installing the PHP MongoDB driver on Ubuntu, ensure that you have the necessary tools and dependencies:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>PHP<\/strong> PHP 7.4 or above<\/li>\n\n\n\n<li><strong>PHP-dev<\/strong> and other necessary development libraries.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Run the following commands to install the required dependencies:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt update\napt install php php-cli php-dev php-pear libssl-dev pkg-config<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This installs PHP, the PHP development tools, and the required libraries for compiling extensions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-small-font-size\"><strong>2. Download MongoDB PHP Driver (1.7.4)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can download the MongoDB PHP driver 1.7.4 directly from the PECL website or use wget to download it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/pecl.php.net\/get\/mongodb-1.7.4.tgz<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will download the .tgz archive file of the MongoDB driver.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-small-font-size\"><strong>3. Extract the Driver<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once the file is downloaded, you need to extract it to a directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar -xvzf mongodb-1.7.4.tgz\ncd mongodb-1.7.4<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>4. Prepare for Installation<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before installing the driver, you need to prepare the PHP extension using phpize. This command will set up the environment for compiling the driver:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>phpize<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command prepares the necessary files for compilation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>5. Configure the MongoDB Driver<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, run the .\/configure command to check your system and set up the MongoDB extension for your PHP version:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/configure<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If everything goes well, the .\/configure step will complete without errors. If you encounter any issues, make sure all dependencies were installed properly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>6. Compile and Install the Driver<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, compile the extension using the make command, then install it with make install:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>make\nsudo make install<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This step compiles and installs the MongoDB driver extension for PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-small-font-size\"><strong>7. Enable the MongoDB Extension<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After installation, you need to enable the MongoDB extension in PHP. You do this by editing your php.ini file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Find the location of your php.ini file. You can do this by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php --ini<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It will show the path to the loaded php.ini file (for example: \/etc\/php\/7.4\/cli\/php.ini).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. Open the php.ini file for editing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim  \/etc\/php\/7.4\/cli\/php.ini <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Add the following line at the end of the php.ini file to load the MongoDB extension:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extension=mongodb.so<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>8. Restart PHP or Web Server<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After updating php.ini, you need to restart PHP (or your web server if you&#8217;re running PHP-FPM or <a href=\"https:\/\/httpd.apache.org\/\" target=\"_blank\" rel=\"noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-secondary-color\">Apache<\/mark><\/a>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re using <strong>PHP-FPM<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart php8.2-fpm  # Replace 8.2 with your PHP version<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re using Apache with mod_php:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart apache2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>9. Verify the Installation<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To ensure the MongoDB extension is correctly installed, you can run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -m | grep mongodb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the installation was successful, this will output mongodb.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re facing issues or want a seamless, optimized setup, our expert team is here to assist. With our comprehensive <a href=\"https:\/\/www.skynats.com\/server-management\/\">Server Management Services<\/a>, we handle everything from package installation to database configuration and performance tuning\u2014ensuring your environment runs smoothly and securely. Whether you&#8217;re installing the PHP MongoDB Driver on Ubuntu for the first time or need advanced troubleshooting, our certified engineers are available 24\/7 to support your server needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Install Prerequisites Before installing the PHP MongoDB driver on Ubuntu, ensure that you have the necessary tools and dependencies: Run the following commands to install the required dependencies: This installs PHP, the PHP development tools, and the required libraries for compiling extensions. 2. Download MongoDB PHP Driver (1.7.4) You can download the MongoDB PHP [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1175,302],"class_list":["post-16963","post","type-post","status-publish","format-standard","hentry","category-blog","tag-php-mongodb-driver-on-ubuntu","tag-server-management-services"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/16963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=16963"}],"version-history":[{"count":3,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/16963\/revisions"}],"predecessor-version":[{"id":16966,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/16963\/revisions\/16966"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=16963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=16963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=16963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}