Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Install MongoDB with PHP on Ubuntu 24.04

How to Install MongoDB with PHP on Ubuntu 24.04

Integrating MongoDB with PHP on Ubuntu 24.04 enables you to build powerful, scalable web applications that leverage the flexibility of NoSQL databases. This guide walks you through the entire process, from preparing your system to testing your setup, ensuring a smooth installation experience. Before you begin, ensure your system is up to date and has a working internet connection, as you’ll need to download packages and repositories. Additionally, having sudo privileges on your Ubuntu machine is essential for executing administrative commands.

What You’ll Need

  • Ubuntu 24.04 installed and running
  • Access to a user account with sudo privileges
  • Basic knowledge of terminal commands
  • Stable internet connection

Step-by-Step

  1. Update Your System: Always start by updating your package list and upgrading existing packages to ensure compatibility.
    sudo apt update && sudo apt upgrade -y
  2. Import MongoDB GPG Key: Add the official MongoDB public key to verify package authenticity.
    wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
  3. Add MongoDB Repository: Create a list file for MongoDB in your sources directory.
    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
  4. Reload Package Database: Refresh your package list to include MongoDB packages.
    sudo apt update
  5. Install MongoDB Packages: Install the latest MongoDB server, shell, and tools.
    sudo apt install -y mongodb-org
  6. Start and Enable MongoDB Service: Enable MongoDB to run on startup and start the service.
    sudo systemctl enable mongod
    sudo systemctl start mongod
  7. Verify MongoDB is Running: Check the status of the MongoDB service.
    sudo systemctl status mongod
  8. Install PHP and Extensions: Ensure PHP is installed along with the MongoDB extension.
    sudo apt install -y php php-cli php-zip php-mbstring php-xml php-curl php-pear
  9. Install PHP MongoDB Extension: Use PECL to install the MongoDB driver for PHP.
    sudo pecl install mongodb
  10. Enable PHP Extension: Add the extension to your PHP configuration.
    echo "extension=mongodb.so" | sudo tee /etc/php/24.04/cli/conf.d/20-mongodb.ini
  11. Restart PHP and Web Server: Restart PHP CLI and your web server (Apache or Nginx) to load the new extension.
    sudo systemctl restart php24.04-fpm
    sudo systemctl restart apache2
  12. Test Your Setup: Create a PHP file to verify MongoDB extension is loaded.
    <?php
    phpinfo();
    ?>

    Check the output for the MongoDB section to confirm successful installation.

Common Mistakes

  • Forgetting to import the MongoDB GPG key, which can cause package verification errors.
  • Using the wrong repository URL or distribution codename, leading to failed package installs.
  • Not enabling the MongoDB service to start on boot, which results in MongoDB not running after reboot.
  • Failing to restart PHP or the web server after installing the extension, preventing PHP from recognizing the new driver.
  • Installing the wrong PHP version or missing the PHP development packages, which can cause extension build failures.

Tips

  • Always verify the MongoDB service status after starting it with sudo systemctl status mongod.
  • Use php -m | grep mongodb to quickly check if the extension is loaded in PHP CLI.
  • Keep your system updated regularly to avoid compatibility issues with MongoDB and PHP.
  • If you encounter issues with PECL, ensure you have the necessary build tools installed: sudo apt install -y build-essential php-dev.
  • Consult the official MongoDB and PHP documentation for any specific version-related instructions or troubleshooting tips.

Troubleshooting

If MongoDB or PHP extension installation does not go as planned, consider the following steps:

  • Check Logs: Review system logs for errors related to MongoDB or PHP extensions. Use journalctl -u mongod or check PHP error logs.
  • Verify Repository and Keys: Ensure the MongoDB repository URL and GPG key are correctly added and that your system recognizes them.
  • Ensure Compatibility: Confirm that your PHP version matches the extension requirements. Use php -v and check the PECL documentation.
  • Reinstall Extensions: Remove and reinstall the PHP MongoDB extension if necessary, using pecl uninstall mongodb and then pecl install mongodb.
  • Restart Services: Always restart PHP and your web server after changes, as services often cache extensions.
  • Seek Help: Use community forums, Stack Overflow, or official documentation if errors persist.

Frequently Asked Questions

1. Can I install MongoDB and PHP on Ubuntu 24.04 without Docker?

Yes, this guide covers native installation without Docker, which is suitable for most development and production environments.

2. How do I upgrade MongoDB on Ubuntu 24.04?

Update the repository to point to the newer version and run sudo apt update followed by sudo apt upgrade -y.

3. Is the PHP MongoDB extension compatible with all PHP versions?

The extension is compatible with PHP 7.0 and above. Always check the specific extension version requirements on PECL.

4. How do I connect my PHP application to MongoDB?

Use the MongoDB PHP library, instantiate a client with new MongoDB\Client(), and specify your database and collection.

5. What security best practices should I follow with MongoDB on Ubuntu?

Enable authentication, configure firewalls, use secure connections (SSL/TLS), and limit user privileges to enhance security.

6. Can I run multiple MongoDB versions on the same Ubuntu system?

It’s generally not recommended due to potential conflicts. Use Docker or separate virtual machines for multiple versions.

Related

How to Install MongoDB with PHP on Ubuntu 24.04
Scroll to top