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.
- Open the terminal and run the command to install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 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:
- Install PHP 8.2:
brew install [email protected]
- Check the PHP version:
php -v
- Start PHP-FPM (FastCGI Process Manager):
brew services start [email protected]
- 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
- Install MongoDB:
brew tap mongodb/brew brew install [email protected]
- Start MongoDB:
brew services start mongodb/brew/mongodb-community
- Check MongoDB status:
brew services list
- 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).
- Install PECL (if not installed):
brew install autoconf pecl install mongodb
- After installation, open the
php.ini
file and add this line:extension=mongodb.so
- Restart PHP-FPM:
brew services restart [email protected]
- 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.
- Install Composer:
brew install composer
- 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.
- Install NGINX:
brew install nginx
- Start NGINX:
brew services start nginx
- By default, NGINX serves from the directory
/usr/local/var/www
. You can navigate to http://localhost:8080 to check if it’s working. - 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; }
- 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.