Guide to Setup Apache, PHP and MySQL on MacOS with Homebrew

  sonic0002        2025-03-20 11:22:24       2,855        0          English  简体中文  繁体中文  ภาษาไทย  Tiếng Việt 

This guide is to set up a workable Apache, PHP and MySQL on MacOS with Homebrew to help develop websites locally. There are other solutions available like MAMP which is similar to WAMP on Windows, this guide serves the purpose of setting up everything ourselves and know how each part works and how they work together as whole solution.

1. Make Apache Work

Normally on MacOS, there is built in Apache installed and can already use. To find the Apache, can just try to run below command.

brew services restart httpd

Below output can be found

Stopping `httpd`... (might take a while)
==> Successfully stopped `httpd` (label: homebrew.mxcl.httpd)
==> Successfully started `httpd` (label: homebrew.mxcl.httpd)

To confirm it's running, can issue below command

ps -ef | grep httpd

Should see below similar output

  501 13800     1   0 11:43PM ??         0:01.71 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13805 13800   0 11:43PM ??         0:00.02 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13806 13800   0 11:43PM ??         0:00.01 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13807 13800   0 11:43PM ??         0:00.01 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13808 13800   0 11:43PM ??         0:00.02 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13809 13800   0 11:43PM ??         0:00.01 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13818 13800   0 11:43PM ??         0:00.05 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13820 13800   0 11:43PM ??         0:00.12 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 13821 13800   0 11:43PM ??         0:00.01 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 26586 11613   0  9:52AM ttys018    0:00.01 grep httpd

If httpd is running, you should see a few processes are showing. 

Next we would need to find where the configuration is located so that we could later make changes to it to use PHP. Run below command.

httpd -V | grep SERVER_CONFIG_FILE

The output should tell the location.

pike6@MacBook-Pro www % httpd -V | grep SERVER_CONFIG_FILE
 -D SERVER_CONFIG_FILE="/opt/homebrew/etc/httpd/httpd.conf"

In this case, it's at /opt/homebrew/etc/httpd/httpd.conf

2. Install PHP

In this guide, we would use PHP 8.3, to install PHP, can run below command

brew install [email protected]

This will install PHP 8.3 on your machine, once it's installed, can run below command to add the php command to PATH.

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc  
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

Next run source command to make the change take effect.

source ~/.zshrc

Verify the php version is 8.3

php --version

Output

PHP 8.3.17 (cli) (built: Feb 11 2025 22:03:03) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.17, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.17, Copyright (c), by Zend Technologies

3. Install MySQL

The command to install MySQL is quite straightforward, 

brew install mysql

Once installed, can run below command to start mysql service.

brew services start mysql

To access the databases, issue below command

mysql -u root -p

When prompting password, just type ENTER, you should be able to login to mysql shell. 

If you wanna secure your mysql access, can install below to set root password(recommended setp but not ncessary for lcoal environment).

mysql_secure_installation

4. Update Apache Configurations

If we wamma make the Apache, PHP and MySQL working together, we need to make changes to the http configuration in Step 1. Below changes are needed to the /opt/homebrew/etc/httpd/httpd.conf.

  • Add below line to enable php module
    LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so​
  • Update DocumentRoot and Directory to your website's root directory
    DocumentRoot "/Users/pike6/work/project/website/www"
    <Directory "/Users/pike6/work/project/website/www">
       AllowOverride All
       Require all granted
    </Directory​

    • (Optional) If you wanna set up more than one website on your local, you may need to enable virtual hosts. This can be done by uncommenting
      Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf​
    • Theerafter edit /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf, add some entries like below depends on your need.
      <VirtualHost *:80>
          ServerAdmin [email protected]
          DocumentRoot "/Users/pike6/work/project/website/www"
          ServerName website1.localhost
          ErrorLog "/opt/homebrew/var/log/httpd/www-error.log"
          CustomLog "/opt/homebrew/var/log/httpd/www-access.log" common
      </VirtualHost>
      
      <VirtualHost *:80>
          ServerAdmin [email protected]
          DocumentRoot "/Users/pike6/work/project/website/website2"
          ServerName website2.localhost
          ErrorLog "/opt/homebrew/var/log/httpd/website2-error.log"
          CustomLog "/opt/homebrew/var/log/httpd/website2-access.log" common
      </VirtualHost>​
    • Edit /etc/hosts, add below address mapping so that the new ServerName above can be mapped to 127.0.0.1
      127.0.0.1       website1.localhost
      127.0.0.1       website2.localhost​
    • In /opt/homebrew/etc/httpd/httpd.conf, add below for every new website.
      <Directory "/Users/pike6/work/project/website/website1">
          AllowOverride All
          Require all granted
      </Directory>
      
      <Directory "/Users/pike6/work/project/website/website2">
          AllowOverride All
          Require all granted
      </Directory>​
  • Depends on your need, you may need to uncomment below as you may have some rewrite engine enable in your .htaccess.
    LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so​

Now with above steps, restart httpd service and access your site with website1.locahost. It should work.

Bonus 1

If you also want phpMyAdmin, can run below 

brew install phpmyadmin

Post installation, add below to httpd conf /opt/homebrew/etc/httpd/httpd.conf.

Alias /phpmyadmin /opt/homebrew/share/phpmyadmin
<Directory /opt/homebrew/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>

Restart httpd service

brew services restart httpd

Bonus 2

If you don't find your site working after starting httpd service, you can first check whether the service starts and listens at the port you specified. 

lsof -i :80

If port is not listening, it means the service is not started successfully. You can check whether the correct user permission or httpd ownership is given. For example, you can check the owner of /opt/homebrew/opt/httpd/bin/httpd, if it's not your username, you may not be able to start it by just using brew, you may need sudo brew. Or you can change the owner to your username if only you are owning the machine.

PHP  APACHE  MYSQL  PHPMYADMIN  GUIDE  MACOS  HTTPD  MULTIPLE WEBSITES 

           

  RELATED


  0 COMMENT


No comment for this article.



  PROGRAMMER HUMOR

Correct way to get a job


  SUPPORT US