Home News Update How-To Guides Detail article

How to set up PHP8.2, MongoDB, and NGINX on macOS

This easy-to-follow guide walks you through installing PHP 8.2, MongoDB, and NGINX on macOS, offering tips for seamless setup and configuration.
How to set up PHP8.2, MongoDB, and NGINX on macOS

Setting up PHP 8.2-FPM, MongoDB, and NGINX on macOS

Follow the steps below to set up a complete development environment for PHP 8.2-FPM, MongoDB, and NGINX on macOS.

1. Install Homebrew

Homebrew is a popular package manager on macOS, helping you install software easily.

  1. Open the terminal and run the command to install Homebrew:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. After installation, check if Homebrew is installed successfully:
    brew --version
2. Install PHP 8.2-FPM

You can install PHP 8.2 with FPM via Homebrew:

  1. Install PHP 8.2:
    brew install [email protected]
  2. Check the PHP version:
    php -v
  3. Start PHP-FPM (FastCGI Process Manager):
    brew services start [email protected]
  4. If you need to configure PHP, you can edit the PHP config file:
    nano /opt/homebrew/etc/php/8.2/php.ini
3. Install MongoDB
  1. Install MongoDB:
    brew tap mongodb/brew
    brew install [email protected]
  2. Start MongoDB:
    brew services start mongodb/brew/mongodb-community
  3. Check MongoDB status:
    brew services list
  4. To connect to MongoDB, use the MongoDB shell:
    mongosh
4. Install MongoDB PHP Extension

To connect PHP with MongoDB, you need to install the MongoDB PHP extension (MongoDB driver).

  1. Install PECL (if not installed):
    brew install autoconf
    pecl install mongodb
  2. After installation, open the php.ini file and add this line:
    extension=mongodb.so
  3. Restart PHP-FPM:
    brew services restart [email protected]
  4. Check if the MongoDB extension is installed:
    php -m | grep mongodb
5. Install Composer and MongoDB PHP Library

Composer is a dependency manager for PHP. You will need it to install the MongoDB PHP library.

  1. Install Composer:
    brew install composer
  2. Create a PHP project and install the MongoDB PHP library:
    composer require mongodb/mongodb
6. Install and Configure NGINX

NGINX is a powerful web server that can be used to serve your PHP 8.2-FPM application.

  1. Install NGINX:
    brew install nginx
  2. Start NGINX:
    brew services start nginx
  3. By default, NGINX serves from the directory /usr/local/var/www. You can navigate to http://localhost:8080 to check if it’s working.
  4. To configure NGINX for PHP 8.2-FPM, edit the NGINX configuration file:
    nano /usr/local/etc/nginx/nginx.conf

    Add the following configuration inside the server block to enable PHP processing:

    
    location ~ \.php$ {
        root           /usr/local/var/www;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
                    
  5. Restart NGINX after making changes:
    brew services restart nginx
7. Running PHP with MongoDB

Once everything is set up, you can test connecting MongoDB with PHP 8.2 using a PHP file like this:


<?php
require 'vendor/autoload.php'; // Ensure Composer is installed

$client = new MongoDB\Client("mongodb://localhost:27017");

$collection = $client->test->users;
$result = $collection->insertOne(['name' => 'John Doe', 'email' => '[email protected]']);

echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>
        

Run the PHP file:

php test.php
Summary

- Use Homebrew to install PHP 8.2, MongoDB, Composer, and NGINX.

- Install the MongoDB PHP driver via PECL.

- Use Composer to manage the MongoDB PHP library.

- Configure NGINX and PHP-FPM for serving PHP applications.

- Utilize an IDE like VSCode or PHPStorm for easier development.