升级MacOS High Sierra后PHP无法正常运行的问题求助
Hey there, let's work through this PHP parsing issue you're facing after upgrading to MacOS High Sierra. I've helped several developers troubleshoot similar problems, so let's break this down step by step:
1. Verify Apache is Loading the Correct PHP Module
The most common culprit here is that Apache isn't linked to your Homebrew-installed PHP 7.2 anymore. Here's how to fix it:
- Open Apache's main config file with a text editor (you'll need sudo privileges):
sudo nano /etc/apache2/httpd.conf - Look for a line starting with
LoadModule php7_module. If it doesn't exist, add this line (replace the path if your Homebrew PHP location differs):LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so - To confirm the correct path for your PHP module, run this command in terminal:
Appendbrew --prefix php@7.2/lib/httpd/modules/libphp7.soto the output to get the full module path. - Also, make sure this line is present to tell Apache which files to treat as PHP:
AddType application/x-httpd-php .php
2. Check Virtual Host Configurations (If Used)
If you're using virtual hosts, each host block needs to recognize PHP files:
- Open your virtual hosts config file:
sudo nano /etc/apache2/extra/httpd-vhosts.conf - For each
<VirtualHost>block, add these lines (or ensure they're inherited from the main config):AddHandler php7-script .php AddType text/html .php
3. Validate PHP Module Loading
After updating configs, test if Apache is parsing PHP correctly:
- Restart Apache to apply changes:
sudo apachectl restart - Create a
phpinfo.phpfile in your server root (default is/Library/WebServer/Documents/) with this content:<?php phpinfo(); ?> - Visit
http://localhost/phpinfo.phpin your browser. If you see the PHP info page, the module is loaded properly. If it still shows plain text, move to the next step.
4. Fix Path-Related Errors
You mentioned path errors when adding configurations—this is likely because your config paths don't match where Homebrew installed PHP:
- Run this command to get all critical paths for your PHP 7.2 installation:
brew info php@7.2 - Update any config files (Apache, PHP.ini) with the correct paths from the output.
- Also, ensure Homebrew's PHP is in your system PATH by adding this to your
~/.bash_profileor~/.zshrc:export PATH="/usr/local/opt/php@7.2/bin:$PATH" - Reload your shell config to apply changes:
source ~/.bash_profile # or source ~/.zshrc if using Zsh
5. If Using Nginx Instead of Apache
If you're running Nginx instead of the default Apache, the setup is different:
- Open your Nginx server config file (usually in
/usr/local/etc/nginx/servers/). - Add this location block inside your
serverdirective to handle PHP files:location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } - Restart Nginx:
sudo nginx -s reload - Make sure PHP-FPM is running:
brew services start php@7.2
The root issue here is that upgrading MacOS often resets or breaks the link between your web server and PHP. Reinstalling PHP is a good first step, but you need to ensure the server is using the new PHP installation by updating its configuration files.
内容的提问来源于stack exchange,提问作者Roy Granit

