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):
- Edit the Apache configuration:
sudo vim /etc/apache2/httpd.conf
- Modify these two directives:
DocumentRoot "/Users/username/Sites"
<Directory "/Users/username/Sites">
- Restart Apache and test with your
index.php file
Resolving Permission Issues
If encountering "403 Forbidden" errors after changing directories:
- Edit the Apache configuration again
- 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>
- Additionally, ensure
AllowOverride All is set for your custom directory section
- Restart Apache for changes to take effect