Introduction:
For web developers, setting up a local development environment on macOS is essential. In this article, we will walk you through the process of installing and configuring PHP, MySQL, and Apache, the fundamental components for web development. Whether you’re a beginner or an experienced developer, this step-by-step guide will help you establish a smooth and robust environment on your Mac.
Step 1: Install Homebrew
Homebrew simplifies the installation process by acting as a package manager for macOS. To install Homebrew, open Terminal and execute the following command:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install PHP
Once Homebrew is installed, you can proceed to install PHP by typing the following command in Terminal:brew install php
After the installation completes, verify the PHP version by running php -v in Terminal.
Step 3: Configure Apache
macOS comes pre-installed with Apache, which serves as the web server. To configure Apache, open Terminal and enter the following command to open the Apache configuration file:sudo nano /etc/apache2/httpd.conf
Inside the file, make the following changes:
- Uncomment the line
LoadModule php_module libexec/apache2/libphp.soby removing the#symbol at the beginning. - Find the line that says
DocumentRoot "/Library/WebServer/Documents"and change it toDocumentRoot "/Users/YourUsername/Sites". Replace “YourUsername” with your actual username. - Scroll down and locate the
<Directory "/Library/WebServer/Documents">block. Change it to<Directory "/Users/YourUsername/Sites">. - Save the changes and exit the text editor.
Step 4: Start Apache
To start the Apache server, enter the following command in Terminal:sudo apachectl start
You can verify if Apache is running by visiting http://localhost in your web browser. If you see the default Apache page, it means Apache is successfully running.
Step 5: Install MySQL
Next, we need to install MySQL, a popular relational database management system. Use Homebrew to install MySQL by executing the following command in Terminal:brew install mysql
Follow the instructions displayed in Terminal to set up a secure password for the root user.
Step 6: Configure MySQL
To configure MySQL, open Terminal and enter the following command:sudo nano /usr/local/etc/my.cnf
Inside the file, locate the [mysqld] section and add the following line:sql_mode=NO_ENGINE_SUBSTITUTION
Save the changes and exit the text editor.
Step 7: Start MySQL
Start the MySQL server by entering the following command in Terminal:brew services start mysql
You can verify if MySQL is running by typing brew services list and checking for the “mysql” service.
Step 8: Test PHP and MySQL
To ensure PHP and MySQL are functioning correctly, create a test PHP file that connects to the database. Open Terminal and enter the following command to create the file:nano ~/Sites/test.php
Inside the file, add the following code:<?php $mysqli = new mysqli("localhost", "root", "your_password", "your_database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; } else { echo "Connected to MySQL successfully!"; } ?>
Remember to replace “your_password” and “your

Leave a Reply