Skip to content

How to Set Up Apache, MySQL, and PHP on macOS

Advertisement

Setting up a local development environment is important for web developers. It lets them build, test, and experiment with web applications on their own computers. One popular setup is called AMP, which stands for Apache, MySQL, and PHP. This guide will show you how to install and set up these tools on a macOS computer.

We will explain each part of the AMP stack and its role. Then, we will guide you through the installation and setup for each tool. We will focus on macOS-specific instructions. Finally, we will discuss how to configure and secure your setup. By the end of this guide, you will have a working local development environment to build and deploy web applications.

Prerequisites

Before you start, make sure your macOS system is up-to-date and you have administrative privileges. Also, install Homebrew, a package manager for macOS. You can follow the instructions at this link to install Homebrew.

Step 1: Set up Apache

Apache is a web server that lets you serve web pages on your computer. To install and configure Apache on macOS, follow these steps:

1.1. Install Apache

Open Terminal and type the following command to install Apache using Homebrew:

brew install httpd

1.2. Configure Apache

Once Apache is installed, open its configuration file with your text editor:

sudo nano /opt/homebrew/etc/httpd/httpd.conf

Make these changes:

  • Change Listen 8080 to Listen 80 to use the default HTTP port.
  • Uncomment the line “LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so” to enable the URL rewrite module.
  • Change DocumentRoot “/opt/homebrew/var/www” to your desired document root directory.

1.3. Start Apache

Now, start Apache by running this command:

sudo brew services start httpd

Open your web browser and go to http://localhost. You should see the Apache default page.

Step 2: Set up MySQL

MySQL is a popular database system. To install and configure MySQL on macOS, follow these steps:

2.1. Install MySQL

To install MySQL using Homebrew, type this command:

brew install mysql

2.2. Start MySQL

After installing MySQL, start the MySQL service:

brew services start mysql

2.3. Secure MySQL installation

Run the security script to secure your MySQL installation:

mysql_secure_installation

Follow the instructions to set a root password and remove unnecessary features.

Step 3: Set up PHP

PHP is a scripting language used for web development. To install and configure PHP on macOS, follow these steps:

3.1. Install PHP

To install PHP using Homebrew, type this command:

brew install php

3.2. Configure PHP with Apache

Open the Apache configuration file again:

sudo nano /opt/homebrew/etc/httpd/httpd.conf

Add this line with the other LoadModule lines:


LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

Replace DirectoryIndex index.html with:


DirectoryIndex index.php index.html

Add these lines at the end of the file to enable PHP processing:


<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

3.3. Restart Apache

Restart Apache to apply the changes:

sudo brew services restart httpd

3.4. Test PHP

To test PHP, create a file named info.php in your document root directory:

sudo nano /path/to/your/document/root/info.php

Add this code to the file:


<?php
  phpinfo();
?>

Save and close the file. Open your web browser and go to http://localhost/info.php. You should see the PHP information page.

Step 4: Install phpMyAdmin (optional)

phpMyAdmin is a tool for managing MySQL databases. To install phpMyAdmin, follow these steps:

4.1. Install phpMyAdmin

To install phpMyAdmin using Homebrew, type this command:

brew install phpmyadmin

4.2. Configure phpMyAdmin

Create a symbolic link from the phpMyAdmin folder to your document root:

ln -s /opt/homebrew/share/phpmyadmin /path/to/your/document/root/phpmyadmin

4.3. Access phpMyAdmin

Open your web browser and go to http://localhost/phpmyadmin. Log in with your MySQL root credentials to start managing your databases.

Conclusion

In conclusion, this guide has shown you how to install and configure Apache, MySQL, and PHP on macOS. You now have a local development environment where you can build and test web applications. Keep your software updated and follow best practices for a secure and efficient setup

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top