How to Install MongoDB with PHP on Ubuntu 24.04

The article guides users through installing and configuring MongoDB with PHP on Apache or Nginx on Ubuntu 24.04, emphasizing its flexibility and scalability for modern applications. It details the installation process, necessary PHP modules, driver integration, and verifying setup functionality, encouraging developers to leverage MongoDB for dynamic data requirements.

This article explains how to install and configure MongoDB with PHP, Apache or Nginx web server on Ubuntu 24.04.

MongoDB is a popular NoSQL database that stores data in a flexible, JSON-like format, allowing for dynamic schemas and easy scalability. MongoDB offers a more flexible way to store data, which is advantageous for applications with changing data requirements.

MongoDB works seamlessly with various frameworks and libraries, making it ideal for building modern applications using PHP frameworks such as Laravel, Symfony, or CodeIgniter.

Integrating MongoDB with PHP and a web server allows you to create powerful, flexible, and scalable web applications that address diverse use cases.

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.

Comments

Leave a Reply

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