Mac中Apache虚拟主机指向ServerRoot而非Laravel指定目录问题
Hey there, let's work through this step by step since you've got a few interconnected issues going on with your Apache setup.
1. Confirm Which Apache Instance You're Running
First, your whereis httpd and /usr/sbin/httpd -V output confirms you're using the system-default Apache (not a brew/XAMPP version). Critical details from that output:
- Server config file:
/private/etc/apache2/httpd.conf - Module path:
libexec/apache2/(not the/usr/localpaths you were editing earlier)
All your configuration changes need to target this system Apache, not the /usr/local instance you were modifying before.
2. Fix Virtual Hosts Not Loading
Step 1: Enable Required Modules & Vhosts Include
Edit /private/etc/apache2/httpd.conf and uncomment these lines (remove the # at the start):
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so Include /private/etc/apache2/extra/httpd-vhosts.conf
Step 2: Update Virtual Host Configuration
Open /private/etc/apache2/extra/httpd-vhosts.conf and update your VirtualHost block to include directory permissions (Apache requires explicit access to your files):
<VirtualHost *:80> DocumentRoot "/usr/local/var/www/mydomain.com/public_html" ServerName dev.mydomain ServerAlias www.dev.mydomain # Add this Directory block to grant access <Directory "/usr/local/var/www/mydomain.com/public_html"> AllowOverride All Require all granted </Directory> ErrorLog "/private/var/log/apache2/dev.mydomain/error_log" CustomLog "/private/var/log/apache2/dev.mydomain/access_log" common </VirtualHost>
Step 3: Prepare Log Directory & Validate Config
- Create the log directory if it doesn't exist:
sudo mkdir -p /private/var/log/apache2/dev.mydomain - Test your config for syntax errors:
sudo apachectl -t - If no errors, restart Apache:
sudo apachectl restart - Verify virtual hosts are loaded:
sudo apachectl -S
You should now see dev.mydomain listed in the output.
3. Fix PHP Files Showing as Plain Text
System Apache doesn't enable PHP by default. Edit /private/etc/apache2/httpd.conf to:
- Uncomment the PHP module line (match your PHP version; this is for PHP 7):
LoadModule php7_module libexec/apache2/libphp7.so - Add or ensure this handler exists to map .php files to the PHP processor:
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
Restart Apache again with sudo apachectl restart and test a PHP file.
4. Resolve mod_mpm_prefork.so Load Error
This error comes from modifying the wrong Apache's config (the /usr/local/etc/httpd/httpd.conf instance). To fix it:
- If you don't need the
/usr/localApache instance, stop it (e.g., if installed via Homebrew):brew services stop httpd - If you want to use the Homebrew Apache instead, update its
httpd.confto use the correct module path. For Homebrew, modules are typically in/usr/local/opt/httpd/lib/httpd/modules/, so change the line to:LoadModule mpm_prefork_module /usr/local/opt/httpd/lib/httpd/modules/mod_mpm_prefork.so
内容的提问来源于stack exchange,提问作者Dan

