Setting Up Apache and PHP on macOS with Homebrew: A Step-by-Step Guide

Installing Apache and PHP using Homebrew on macOS and virtual hosts configuration for multiple projects.

Prerequisites

Make sure you have Homebrew installed on your macOS. If not install it by running the following command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 1: Install Apache

To install Apache using Homebrew, open the terminal and run:

brew install httpd
Once the installation is complete, start the Apache service:
brew services start httpd 
To verify that Apache is running, check its status with:
brew services list

Visit http://localhost:8080 in your browser. You should see the “It works!” page, indicating that Apache is successfully installed.

Apache to listen for traffic on the port 8080. However, as HTTP traffic goes to port 80 by default, we want to listen on that port instead. We need to edit Apache configuration file.

nano /opt/homebrew/etc/httpd/httpd.conf
find the line Listen 8080 and change it as:
Listen 80

Step 2: Install PHP

Next, let’s install PHP. Run the following command:

brew install php 
Once PHP is installed, we need to configure Apache to use the PHP module. Open the Apache configuration file:
nano /opt/homebrew/etc/httpd/httpd.conf
Add/edit the following lines:
ServerName localhost
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
 —
# PHP settings
Include /opt/homebrew/etc/httpd/extra/httpd-php.conf
 —
The rest of our PHP configuration goes in the file /opt/homebrew/etc/httpd/extra/httpd-php.conf:
<IfModule php_module>
  <FilesMatch \.php$>
    SetHandler application/x-httpd-php
  </FilesMatch>

  <IfModule dir_module>
    DirectoryIndex index.html index.php
  </IfModule>
</IfModule>

Step 3: Set Up Virtual Hosts

Now, let’s set up virtual hosts to manage multiple projects easily.
First, enable the virtual hosts configuration in the main Apache configuration file by adding the following line:

nano /opt/homebrew/etc/httpd/httpd.conf
add:
Include /opt/homebrew/etc/httpd/httpd-vhosts.conf

Next, create a new configuration file for your virtual hosts:

nano /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf 
Add the following configuration, replacing mypage.com and the document root with desired values:
<VirtualHost *:80>
   ServerAdmin webmaster@mypage.com
   DocumentRoot "/path/to/your/project/mypage"
   ServerName mypage.com
   ErrorLog "/opt/homebrew/var/log/httpd/mypage-error_log"
   CustomLog "/opt/homebrew/var/log/httpd/mypage-access_log" common
</VirtualHost>

Don’t forget to create the directory for your project if it doesn’t exist:

mkdir -p /path/to/your/project/mypage

Step 4: Update Hosts File

To access your virtual host via a custom domain, we need to update your hosts file. Open the file with:

sudo nano /etc/hosts

Add the following line:

127.0.0.1 mypage.com

Step 5: Restart Apache

After making these changes, restart Apache for the configuration to take effect:

brew services restart httpd

Step 6: Test Your Setup

Now you should be able to access your virtual host by navigating to http://mypage.com:8080 in your browser. You can also create an info.php file in your document root to test if PHP is working:

<?php phpinfo(); ?> 
Save this file in your document root and access it via http://mypage.com:8080/info.php. You should see the PHP configuration page.

Conclusion

Congratulations! You’ve successfully installed Apache and PHP on macOS using Homebrew and configured virtual hosts for your projects. This setup allows you to easily manage multiple projects without any conflicts. Happy coding!