You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级MacOS High Sierra后PHP无法正常运行的问题求助

Fixing PHP Showing as Plain Text After MacOS High Sierra Upgrade

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:
    brew --prefix php@7.2
    
    Append /lib/httpd/modules/libphp7.so to 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.php file in your server root (default is /Library/WebServer/Documents/) with this content:
    <?php phpinfo(); ?>
    
  • Visit http://localhost/phpinfo.php in 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.

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_profile or ~/.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 server directive 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:36:55