How do you get php working on Mac OS X?
When upgrading to a new macOS release like Lion, enabling "Web Sharing" in System Preferences is often the first step developers take. However, this only activates the HTTP server; it does not automatically configure the underlying PHP runtime or ensure the correct modules are loaded. If you add an info.php file containing <?php phpinfo(); ?> to your web root and receive plain text output instead of a formatted information table, it indicates that the request is being handled by the shell or a basic CGI wrapper without PHP processing enabled.
The core issue usually lies in how the server interprets file extensions. When you enable Web Sharing via System Preferences, macOS defaults to using the built-in Apache server with specific configurations that may not include PHP. In many default setups, only index.html and static files are served by default. If your info.php is treated as a text file, the server lacks the necessary .htaccess rules or configuration directives in the httpd.conf file to invoke the PHP interpreter for that specific extension.
To resolve this, you must explicitly configure Apache to recognize and process PHP files. This typically involves editing the Apache configuration file located at /etc/apache2/httpd.conf. You need to ensure that the PHP module is loaded. Look for a line containing LoadModule php5_module libexec/apache2/libphp5.so (note: on newer versions of macOS, this path and module name might differ depending on whether you are using Apple's built-in PHP or a Homebrew installation). If the line is commented out with a semicolon, remove it to activate the module.
Beyond loading the module, you must ensure that Apache knows to process files ending in .php. Add the following lines inside the <VirtualHost *:80> block or the main server configuration:
AddType application/x-httpd-php .php
DirectoryIndex index.php index.html
The AddType directive tells Apache to treat .php files as PHP scripts rather than plain text. The DirectoryIndex change ensures that if you visit your domain root, it serves an index.php file if one exists, which is standard practice for dynamic sites.
Another critical step involves checking the PHP error reporting settings. If PHP is running but encountering a fatal error during initializationâsuch as missing required extensions or configuration file syntax errorsâit might silently fail or throw a 500 Internal Server Error that looks like plain text if not caught properly. To debug this, ensure that php.ini is correctly linked from the web root or located in a standard path like /etc/php.ini.
If you are using Homebrew to install PHP alongside the system version, remember that they run as separate entities. You might need to install the Apache module for your specific Homebrew version of PHP using brew install php@7.4 (or the current version) and then configure Apache to use it via LoadModule.
One common pitfall is assuming that enabling Web Sharing grants full administrator privileges or access to all system libraries. The "Web Sharing" pane in macOS allows you to share specific directories, but it does not automatically inject development frameworks into your local environment. If you are building a Laravel application or any modern PHP stack, relying solely on the default Mac configuration is insufficient for production-ready performance or security.
For robust setups, consider running a dedicated PHP server or configuring Nginx with PHP-FPM if the native Apache setup feels restrictive. However, for simple development and testing on macOS, ensuring the httpd.conf is correctly populated with the PHP load module and MIME types remains the most direct path to getting your phpinfo() output rendered correctly as a formatted HTML page rather than raw code.