Skip to content
Follow
Ubuntu Linux

How to Install MongoDB with PHP on Ubuntu 24.04

Richard
Written by
Richard
Mar 14, 2025 Updated Jul 13, 2026 4 min read
How to Install MongoDB with PHP on Ubuntu 24.04
How to Install MongoDB with PHP on Ubuntu 24.04

Installing MongoDB with PHP on Ubuntu 24.04 involves setting up the MongoDB database, the PHP MongoDB driver, and configuring your web server.

MongoDB is a powerful NoSQL database that stores data like documents, making it great for modern web apps. The PHP MongoDB driver acts as a translator, allowing your PHP code to talk to MongoDB.

This guide focuses on Ubuntu 24.04 LTS, the latest Long Term Support version. By following these steps, you’ll build a solid foundation for PHP projects that need to work with MongoDB, whether you use Apache or Nginx as your web server.

⚡ Quick Answer

Install MongoDB, your chosen web server (Apache or Nginx), and PHP. Then, install the MongoDB PHP driver using `pecl install mongodb` and enable it in your PHP configuration files. Finally, restart your web server and PHP.

Install MongoDB

To use MongoDB, PHP, and a web server, you must first install MongoDB. If you haven’t installed it yet, read the post below to learn how to do that.

How to install MongoDB on Ubuntu

After installing MongoDB, continue below.

Apache

📝Good to Know
A PHP application requires a web server. To run MongoDB, PHP, and Apache, refer to the post below for instructions on how to install Apache on Ubuntu.

How to install Apache on Ubuntu

After installing Apache, continue below.

Install Nginx

If you prefer to use Nginx instead of Apache, install Nginx on Ubuntu. If you haven’t done so, read the post below to learn how.

How to install Nginx on Ubuntu

After installing Nginx, continue below.

Install PHP

You need to install specific PHP modules to help PHP connect with your MongoDB database on Ubuntu 24.04. For Apache, run ‘sudo apt install php php-common php-mongodb php-pear php-dev’. This ensures PHP has the tools it needs to work with MongoDB.

You install specific PHP modules to use MongoDB with PHP and Apache. These modules, such as `php-mongodb`, allow your PHP code to communicate with the MongoDB database, enabling you to store, retrieve, and manage your data effectively.

Apache:

🐧Bash / Shell
sudo apt install php php-common php-mongodb php-pear php-dev

If you plan to use MongoDB with PHP and [Nginx], then install the PHP modules below.

Nginx:

🐧Bash / Shell
sudo apt install php-fpm php-common php-mongodb php-pear php-dev

After installing the PHP, continue below.

Install MongoDB PHP drivers

To make your PHP applications talk to MongoDB on Ubuntu 24.04, you must install the MongoDB PHP drivers. Run the command ‘sudo pecl install mongodb’ and accept the default settings when prompted. This step connects your PHP code to the MongoDB database.

This will glue MongoDB and PHP together.

🐧Bash / Shell
sudo pecl install mongodb

During the installation, accept the default prompts for all the choices.

💻Code
Enable developer flags? (yes/no) [no] : 
Enable code coverage? (yes/no) [no] :
Use system libraries for libbson, libmongoc, and libmongocrypt? (yes/no) [no] :
Enable client-side encryption? (auto/yes/no) [auto] :
Enable Snappy for compression? (auto/yes/no) [auto] :
Enable zlib for compression? (auto/system/bundled/no) [auto] :
Enable zstd for compression? (auto/yes/no) [auto] :
Enable SASL for Kerberos authentication? (auto/cyrus/no) [auto] :
Enable crypto and TLS? (auto/openssl/libressl/darwin/no) [auto] :
Use system crypto profile (OpenSSL only)? (yes/no) [no] :
Use bundled or system utf8proc for SCRAM-SHA-256 SASLprep? (bundled/system) [bundled] :

After building and installing the MongoDB PHP drivers, execute the command below to add it to the PHP configuration file.

This assumes you are running PHP 8.3.

Apache: Run:

🐧Bash / Shell
sudo -s
sudo echo "extension=mongodb.so" >> /etc/php/8.3/apache2/php.ini
sudo echo "extension=mongodb.so" >> /etc/php/8.3/cli/php.ini

Nginx: Run:

🐧Bash / Shell
sudo -s
sudo echo "extension=mongodb.so" >> /etc/php/8.3/fpm/php.ini
sudo echo "extension=mongodb.so" >> /etc/php/8.3/cli/php.ini
⚠️Warning
Ensure you update the PHP version number in the above command installed on your systems.

After setting everything up, restart the web server and PHP. Then, run the PHP test page.

You should see MongoDB PHP module installed with PHP.

MongoDB PHP test page
MongoDB PHP test page

That should do it!

Conclusion:

In summary, integrating MongoDB with PHP and a web server like Apache or Nginx allows developers to build modern, scalable web applications. Here are the key takeaways:

  • Flexible Data Storage: MongoDB’s JSON-like data model provides a dynamic schema, facilitating easier changes and scalability.
  • Seamless Integration: Works well with popular PHP frameworks like Laravel, Symfony, and CodeIgniter.
  • Installation Process: Follow the steps to install MongoDB, Apache/Nginx, and the required PHP modules.
  • PHP Drivers: Ensure the MongoDB PHP drivers are installed and configured correctly to enable communication between MongoDB and PHP.
  • Testing: Always verify the installation by checking the PHP test page for the MongoDB module.

By following the installation steps for MongoDB on Ubuntu 24.04, you can significantly enhance your web applications with MongoDB's powerful features.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Install MongoDB 8 on Ubuntu 24.04
Ubuntu Linux How to Install MongoDB 8 on Ubuntu 24.04
How to Install PHP on Ubuntu Linux
Ubuntu Linux How to Install PHP on Ubuntu Linux
How to Install Apache on Ubuntu Linux
Ubuntu Linux How to Install Apache on Ubuntu Linux
How to Install Nginx on Ubuntu Linux
Ubuntu Linux How to Install Nginx on Ubuntu Linux

No comments yet — be the first to share your thoughts!

Leave a Comment

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