How to Set Up a LAMP Server on macOS
In this article
Setting up a LAMP (Linux, Apache, MySQL, PHP) stack on macOS gives you a local web server for developing PHP applications, testing WordPress themes, or learning server administration. While macOS ships with Apache and PHP, they are often outdated or disabled. This guide walks you through installing and configuring a modern LAMP stack using Homebrew, the popular package manager for macOS.
What You'll Need
- A Mac running macOS Catalina (10.15) or later (the steps use Homebrew, which supports current macOS versions).
- Administrative access (you'll use
sudofor some commands). - An internet connection to download packages.
- Basic familiarity with the Terminal app.
Step-by-Step
- Install Homebrew (if not already installed). Open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Follow the prompts. After installation, runbrew doctorto ensure everything is set up correctly. - Install Apache. macOS includes a built-in Apache, but it's older and controlled by the system. Instead, install the newer version from Homebrew:
brew install httpd
This installs Apache 2.4 with the latest security patches. - Configure Apache. The Homebrew Apache configuration file is at
/usr/local/etc/httpd/httpd.conf(on Apple Silicon, it's/opt/homebrew/etc/httpd/httpd.conf). Open it in a text editor:nano /usr/local/etc/httpd/httpd.conf
Make these changes:
- SetListen 8080to avoid conflicts with the built-in Apache on port 80 (or stop the built-in Apache withsudo apachectl stopand use port 80).
- Uncomment the lineLoadModule php_moduleif present (we'll add PHP later, so this may not exist yet).
- SetDocumentRootto a folder you'll use for projects, e.g.,/Users/yourname/Sites. Create that folder and set permissions:mkdir ~/Sites. - Start Apache. Run:
brew services start httpd
This starts Apache and keeps it running in the background. Verify by visiting http://localhost:8080 (or http://localhost if you used port 80). You should see a default page. - Install PHP. Homebrew no longer includes PHP in the core, but you can use a tap:
brew tap shivammathur/phpbrew install shivammathur/php/php@8.3
This installs PHP 8.3, the current stable version. After installation, add the PHP binary to your PATH if needed. The installer will show the exact path. - Connect PHP to Apache. Edit the Apache config again (
httpd.conf) and add the following lines (adjust the path if on Apple Silicon):LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so<FilesMatch \.php$>SetHandler application/x-httpd-php</FilesMatch>
Also ensureDirectoryIndex index.php index.htmlis set. - Restart Apache to load PHP:
brew services restart httpd - Test PHP. Create a file
~/Sites/info.phpwith<?php phpinfo(); ?>. Visit http://localhost:8080/info.php to see the PHP info page. If it works, PHP is running. - Install MySQL. Use MariaDB (a drop-in replacement) or MySQL. For MySQL:
brew install mysql
Then start it:brew services start mysql - Secure MySQL. Run the security script to set a root password and remove insecure defaults:
mysql_secure_installation
Follow the prompts. For local development, you can set a simple root password likeroot. - Install phpMyAdmin (optional but helpful) to manage databases via a web interface:
brew install phpmyadmin
Then configure Apache to serve it. Add tohttpd.confan alias:Alias /phpmyadmin /usr/local/share/phpmyadmin
And grant access:<Directory /usr/local/share/phpmyadmin>Require all granted</Directory>
Restart Apache. Access it at http://localhost:8080/phpmyadmin.
Common Mistakes
- Port conflict: The built-in Apache may already be running on port 80. Using port 8080 avoids this, or stop the built-in service with
sudo apachectl stop. - Wrong PHP module path: On Apple Silicon, Homebrew installs to
/opt/homebrew, not/usr/local. Double-check the path from the PHP install output. - Forgetting to restart Apache after config changes. Always run
brew services restart httpd. - Using the system PHP: macOS includes an old PHP in
/usr/bin/php. Ensure your PATH uses the Homebrew version by runningwhich php. - Permissions on the Sites folder: Apache runs as user
_www. If you get 403 errors, make sure your~/Sitesfolder has execute permissions for others:chmod 755 ~/Sites.
Tips
- Use a virtual host for each project. In
httpd.conf, uncommentInclude httpd-vhosts.confand edit/usr/local/etc/httpd/extra/httpd-vhosts.confto define<VirtualHost *:8080>entries. - Keep everything up to date with
brew update && brew upgrade. - Use a database GUI like Sequel Ace or TablePlus for easier MySQL management, instead of the command line.
- Set up a local domain like
mysite.testby editing/etc/hostsand using Apache virtual hosts. - Enable HTTPS locally with a self-signed certificate to test secure connections.
Troubleshooting
- Apache won't start: Check the error log at
/usr/local/var/log/httpd/error_log. Common causes are syntax errors inhttpd.confor port already in use. Runapachectl configtestto validate the config. - PHP not executing: If you see raw PHP code in the browser, the module isn't loaded. Verify the
LoadModuleline is uncommented and the path is correct. Also ensureFilesMatchdirective is present. - MySQL connection fails: If you get "Access denied", run
mysql -u root -pand enter the password you set. If you forgot it, stop MySQL (brew services stop mysql) and restart withmysqld_safe --skip-grant-tablesto reset. - 403 Forbidden: Check folder permissions and the
Requiredirectives in the Directory block. Make sure yourDocumentRootandDirectorypaths match. - Port 8080 not working: Some firewalls or VPNs may block non-standard ports. Try a different port like 8888, or use port 80 after stopping the system Apache.
Frequently Asked Questions
Do I need to disable the built-in Apache?
Not necessarily. If you use port 8080 for Homebrew Apache, both can coexist. However, the system Apache may interfere with port 80 if you try to use it. It's safer to stop the built-in one with sudo apachectl stop.
Can I use MAMP or XAMPP instead?
Yes, those are one-click installers that provide the same stack. This guide is for those who prefer manual control and the Homebrew ecosystem, which makes updating components easier.
How do I change the document root?
Edit httpd.conf and change the DocumentRoot and corresponding <Directory> directive. Then restart Apache. For example, set it to /Users/yourname/Sites.
How do I install a specific PHP version?
Use the tap: brew install shivammathur/php/php@7.4 for example. You can have multiple versions installed and switch between them using brew unlink php@8.3 && brew link php@7.4.
Why is my PHP script showing a blank page?
This usually means a PHP error is suppressed. Check the Apache error log at /usr/local/var/log/httpd/error_log. You can also enable error display in PHP by setting display_errors = On in /usr/local/etc/php/8.3/php.ini.
Is it safe to use this setup for production?
No. This is a local development environment. For production, you'd need a Linux server with hardened security, proper firewall, and performance tuning. Use this only for testing and development.