Configuring PHP and Apache on macOS

Verifying System Components

macOS includes pre-installed Apache and PHP environments, though they're inactive by default. To verify your Apache installation:

apachectl -version
For PHP version information:

php -v

Apache Service Management

Control Apache through these commands:

  • Start: sudo apachectl start
  • Stop: sudo apachectl stop
  • Restart: sudo apachectl restart
After starting, navigate to http://localhost - the default "It works!" page confirms successful activation.

Enabling PHP Support

Activate PHP by modifying Apache's configuration:

sudo vim /etc/apache2/httpd.conf
Locate and uncomment the PHP module line by removing the # prefix:

LoadModule php7_module libexec/apache2/libphp7.so
Note: Adjust the module name according to your PHP version.

Testing PHP Configuration

Apache's default document root is /Library/WebServer/Documents. Create a test file:

<?php
phpinfo();
?>
Save this as index.php in the document root, then remove any existing index.html file. Access http://localhost to view the PHP information page.

Customizing Document Root

To use a custom web directory (e.g., /Users/username/Sites):

  1. Edit the Apache configuration:
sudo vim /etc/apache2/httpd.conf
  1. Modify these two directives:
DocumentRoot "/Users/username/Sites"
<Directory "/Users/username/Sites">
  1. Restart Apache and test with your index.php file

Resolving Permission Issues

If encountering "403 Forbidden" errors after changing directories:

  1. Edit the Apache configuration again
  2. Locate the root directory section and replace:
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>
With:

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all
</Directory>
  1. Additionally, ensure AllowOverride All is set for your custom directory section
  2. Restart Apache for changes to take effect

Tags: macOS apache PHP web development Server Configuration

Posted on Fri, 12 Jun 2026 18:21:07 +0000 by Yesideez