Introduction:
Developing and testing websites locally on MacOS with Intel hardware requires a reliable LEMP (Linux, Nginx, MySQL, PHP) or LAMP (Linux, Apache, MySQL, PHP) stack. This guide focuses on setting up these stacks with the latest PHP 8.2, ensuring compatibility with modern web applications like WordPress.
Step 1: Choosing Your Stack – LEMP or LAMP
- LEMP vs. LAMP: Decide between Nginx (LEMP) for performance or Apache (LAMP) for compatibility.
- Software Requirements: Both stacks require MySQL and PHP 8.2.
Step 2: Installing Homebrew
- What is Homebrew?: A package manager for MacOS to simplify software installations.
- Installation: Run the following in Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Verify Installation: Enter
brew doctor
in Terminal.
Step 3: Installing Nginx/Apache, MySQL, and PHP 8.2
- Install Nginx/Apache:
- For Nginx:
brew install nginx
- For Apache:
brew install httpd
- For Nginx:
- Installing MySQL and Setting Up Database
- Install MySQL:
- Open Terminal and run the command:
brew install mysql
- This will download and install the latest version of MySQL.
- Open Terminal and run the command:
- Starting MySQL Service:
- Start the MySQL service by running:
brew services start mysql
- This command ensures that MySQL starts automatically upon system boot.
- Start the MySQL service by running:
- Securing MySQL Installation:
- Run the secure installation script:
mysql_secure_installation
- This script will guide you through setting a root password and securing your MySQL installation.
- Run the secure installation script:
- Creating a New Database for WordPress:
- Log into MySQL with the command:
mysql -u root -p
(you will be prompted for the root password you set earlier). - Create a new database for WordPress:sqlCopy code
CREATE DATABASE wordpress_db;
Replacewordpress_db
with your preferred database name.
- Log into MySQL with the command:
- Creating a New MySQL User:
- Create a new user and grant privileges to the WordPress database:sqlCopy code
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost'; FLUSH PRIVILEGES;
Replacewordpress_user
andpassword
with your desired username and password.
- Create a new user and grant privileges to the WordPress database:sqlCopy code
- Exiting MySQL:
- Type
exit
to leave the MySQL command line.
- Type
- Install MySQL:
- Installing and Configuring PHP 8.2
- Install PHP 8.2:
- Open Terminal and run the command:
brew install php@8.2
- This will install PHP version 8.2 on your system.
- Open Terminal and run the command:
- Linking PHP 8.2:
- To ensure that PHP 8.2 is the default version, you may need to link it:
brew link php@8.2 --force --overwrite
- To ensure that PHP 8.2 is the default version, you may need to link it:
- Configuring Apache to Use PHP 8.2
- Edit Apache Configuration:
- Open the Apache configuration file in a text editor. The file is typically located at
/usr/local/etc/httpd/httpd.conf
. - Look for the line that loads the PHP module and update it to point to the PHP 8.2 module. It should look something like this:bashCopy code
LoadModule php_module /usr/local/opt/php@8.2/lib/httpd/modules/libphp.so
- If this line is commented out (preceded by a
#
), remove the#
to enable the module.
- Open the Apache configuration file in a text editor. The file is typically located at
- Restart Apache:
- Apply the changes by restarting Apache:
brew services restart httpd
- Apply the changes by restarting Apache:
- Edit Apache Configuration:
- Configuring Nginx to Use PHP 8.2
- PHP-FPM Configuration:
- PHP-FPM (FastCGI Process Manager) is used with Nginx to handle PHP processing.
- Start PHP-FPM for PHP 8.2:
brew services start php@8.2
- Edit Nginx Configuration:
- Open the Nginx configuration file, typically located at
/usr/local/etc/nginx/nginx.conf
. - In the
server
block, ensure that thelocation ~ \.php$
block is configured to pass PHP requests to PHP-FPM. It should look something like this:rubyCopy codelocation ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
- Make sure the
fastcgi_pass
directive points to the correct port for PHP-FPM (default is usually 9000).
- Open the Nginx configuration file, typically located at
- Restart Nginx:
- Apply the changes by restarting Nginx:
brew services restart nginx
- Apply the changes by restarting Nginx:
- PHP-FPM Configuration:
- Install PHP 8.2:
Step 4: Configuring the Stack
- Starting Services: Use
brew services start
with the service name (nginx, httpd, mysql). - Configuring PHP: Edit the
php.ini
file as needed, located in/usr/local/etc/php/8.2/
.
Step 5: Downloading and Installing WordPress
- Downloading WordPress: Get the latest version from WordPress.org.
- Installing WordPress: Place the extracted WordPress files in the web server’s root directory.
- Creating a Database: Use MySQL to create a new database for WordPress.
Step 6: Running the WordPress Installation
- Access WordPress: Navigate to
http://localhost
in your browser. - Complete the Installation: Follow the WordPress setup wizard using your database details.
Conclusion:
Your MacOS (Intel hardware) now hosts a fully functional LEMP or LAMP stack with PHP 8.2, ideal for local web development and testing. This setup provides a solid foundation for working on WordPress and other PHP-based projects.