Hướng dẫn · 18/09/2026

How to Install PHP, MySQL, and Apache on macOS with Homebrew

Homebrew keeps development tools separate from the components bundled with macOS and makes upgrades easier to control. This guide builds a local PHP, MySQL, and Apache stack for both Apple Silicon and Intel Macs.

Hướng dẫn cài đặt PHP, MySQL và Apache trên macOS bằng Homebrew

Homebrew keeps development tools separate from the components bundled with macOS and makes upgrades easier to control. This guide builds a local PHP, MySQL, and Apache stack for both Apple Silicon and Intel Macs.

Prepare Homebrew and command-line tools

xcode-select --install
brew --version
brew update

Install Homebrew from brew.sh if needed. Use brew --prefix instead of hardcoding /opt/homebrew or /usr/local.

Install the stack

brew install php mysql httpd
php -v
mysql --version
$(brew --prefix httpd)/bin/httpd -v

Homebrew distributes Apache as httpd. Its default configuration uses port 8080 and serves files from $(brew --prefix)/var/www.

Start services

brew services start php
brew services start mysql
brew services start httpd
brew services list

Open http://localhost:8080 to verify Apache.

Initialize MySQL

mysql_secure_installation
mysql -u root -p

Create a dedicated database user for the application:

CREATE DATABASE demo CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'demo'@'localhost' IDENTIFIED BY 'local_password';
GRANT ALL PRIVILEGES ON demo.* TO 'demo'@'localhost';
FLUSH PRIVILEGES;

Connect PHP to Apache

Edit $(brew --prefix)/etc/httpd/httpd.conf. Locate the installed module instead of copying a version-specific path:

brew --prefix php
find "$(brew --prefix php)" -name 'libphp.so'

If available, load that module and configure PHP files:

<IfModule php_module>
    DirectoryIndex index.php index.html
    AddType application/x-httpd-php .php
</IfModule>

Some Homebrew PHP versions may favor PHP-FPM. If libphp.so is unavailable, configure proxy_fcgi and FPM rather than pasting an obsolete module path.

Create a virtual host

<VirtualHost *:8080>
    ServerName demo.test
    DocumentRoot "/Users/yourname/Sites/demo/public"
    <Directory "/Users/yourname/Sites/demo/public">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Enable virtual hosts, add 127.0.0.1 demo.test to /etc/hosts, then validate and restart:

$(brew --prefix httpd)/bin/httpd -t
brew services restart httpd

Verify PHP and MySQL support

php -m | grep -E 'mysqli|pdo_mysql'
php --ini
echo '<?php phpinfo();' > "$(brew --prefix)/var/www/info.php"

Remove the information page after testing because it exposes detailed configuration.

Common problems

  • Find port conflicts with lsof -nP -iTCP:8080 -sTCP:LISTEN.
  • If PHP downloads as text, inspect the module or FPM configuration and Apache logs.
  • If the CLI uses the wrong PHP, inspect which php and your PATH.
  • If MySQL fails, inspect the service status and formula data logs.

Completion checklist

  1. Apache serves the local site on the expected port.
  2. The CLI and web server use the intended PHP version.
  3. The application connects with a dedicated MySQL user.
  4. The virtual host points to the public directory.
  5. No real password or phpinfo() page is committed.

References

Discussion

Comments 0

Sign in to comment

You need an account to join the discussion and reply to other readers.

Sign inRegister

No comments yet. Be the first to share your thoughts.