Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Set Up a LAMP Server on macOS

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 sudo for some commands).
  • An internet connection to download packages.
  • Basic familiarity with the Terminal app.

Step-by-Step

  1. 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, run brew doctor to ensure everything is set up correctly.
  2. 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.
  3. 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:
    - Set Listen 8080 to avoid conflicts with the built-in Apache on port 80 (or stop the built-in Apache with sudo apachectl stop and use port 80).
    - Uncomment the line LoadModule php_module if present (we'll add PHP later, so this may not exist yet).
    - Set DocumentRoot to a folder you'll use for projects, e.g., /Users/yourname/Sites. Create that folder and set permissions: mkdir ~/Sites.
  4. 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.
  5. Install PHP. Homebrew no longer includes PHP in the core, but you can use a tap:
    brew tap shivammathur/php
    brew 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.
  6. 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 ensure DirectoryIndex index.php index.html is set.
  7. Restart Apache to load PHP:
    brew services restart httpd
  8. Test PHP. Create a file ~/Sites/info.php with <?php phpinfo(); ?>. Visit http://localhost:8080/info.php to see the PHP info page. If it works, PHP is running.
  9. Install MySQL. Use MariaDB (a drop-in replacement) or MySQL. For MySQL:
    brew install mysql
    Then start it:
    brew services start mysql
  10. 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 like root.
  11. Install phpMyAdmin (optional but helpful) to manage databases via a web interface:
    brew install phpmyadmin
    Then configure Apache to serve it. Add to httpd.conf an 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 running which php.
  • Permissions on the Sites folder: Apache runs as user _www. If you get 403 errors, make sure your ~/Sites folder has execute permissions for others: chmod 755 ~/Sites.

Tips

  • Use a virtual host for each project. In httpd.conf, uncomment Include httpd-vhosts.conf and edit /usr/local/etc/httpd/extra/httpd-vhosts.conf to 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.test by editing /etc/hosts and 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 in httpd.conf or port already in use. Run apachectl configtest to validate the config.
  • PHP not executing: If you see raw PHP code in the browser, the module isn't loaded. Verify the LoadModule line is uncommented and the path is correct. Also ensure FilesMatch directive is present.
  • MySQL connection fails: If you get "Access denied", run mysql -u root -p and enter the password you set. If you forgot it, stop MySQL (brew services stop mysql) and restart with mysqld_safe --skip-grant-tables to reset.
  • 403 Forbidden: Check folder permissions and the Require directives in the Directory block. Make sure your DocumentRoot and Directory paths 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.