How to Install MongoDB with PHP on Ubuntu 24.04
You install MongoDB with PHP on Ubuntu 24.04 by setting up the MongoDB database, the PHP MongoDB driver, and configuring your web server.
MongoDB is a leading NoSQL document database known for its flexible schema, which uses a JSON-like format that’s perfect for dynamic web applications. The PHP MongoDB driver allows your PHP scripts to communicate directly with your MongoDB instance.
This guide focuses on Ubuntu 24.04 LTS, ensuring you get a robust backend for projects using PHP frameworks. You’ll learn to integrate MongoDB with your Apache or Nginx web server effectively.
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
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
Now, you are ready to install PHP to support MongoDB.
Install the following PHP modules if you plan to use MongoDB with PHP and [Apache].
Apache:
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:
sudo apt install php-fpm php-common php-mongodb php-pear php-dev
After installing the PHP, continue below.
Install MongoDB PHP drivers
Now that you have installed MongoDB, Apache|Nginx and PHP, run the command below to install MongoDB PHP drivers.
This will glue MongoDB and PHP together.
sudo pecl install mongodb
During the installation, accept the default prompts for all the choices.
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:
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:
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
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.

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 these steps, you can leverage the power of MongoDB to enhance your web applications significantly.
Was this guide helpful?
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.
No comments yet — be the first to share your thoughts!