Ubuntu系统下Apache2配置Laravel虚拟主机遇Composer PHP版本依赖异常问题求助
Hey there! It's super common to hit this kind of mismatch even when your terminal's PHP version checks out—because Apache might be using a completely different PHP binary under the hood. Let's break down the fixes step by step:
1. Confirm which PHP version Apache is actually running
Your CLI PHP version doesn't always align with what Apache loads. Here are two reliable ways to check:
- Create a phpinfo test file: Make a file named
phpinfo.phpin your Laravel project'spublicdirectory with this content:
Visit<?php phpinfo(); ?>http://foo.test/phpinfo.phpand look for the "PHP Version" line at the top of the page—this is the version Apache is using. - Check Apache modules via command line: Run this command to see which PHP module Apache has enabled:
You’ll get output likeapache2ctl -M | grep phpphp7.4_module (shared)—if the version here is below 7.4, that’s exactly the problem.
2. Switch Apache to use the correct PHP version
If Apache is running an older PHP module, swap it out with these commands (replace php7.2 with your currently loaded outdated version):
# Disable the old PHP module sudo a2dismod php7.2 # Enable the required PHP 7.4+ module sudo a2enmod php7.4 # Restart Apache to apply changes sudo systemctl restart apache2
Recheck the phpinfo page after this to confirm the version is now >=7.4.
3. Verify Composer's platform configuration (if applicable)
Occasionally, Composer might be forced to target a specific PHP version even if your system meets the requirement. Open your project's composer.json and look for a platform section:
"config": { "platform": { "php": "7.3.0" } }
If you see this, either update the version to >=7.4.0 or remove the entire platform entry so Composer uses your actual system PHP version. Then run composer install to refresh your dependencies.
4. Double-check directory permissions (for good measure)
While not directly related to the PHP version error, incorrect permissions can cause unexpected side effects. Ensure the Apache user (www-data) has proper access to your project files:
sudo chown -R www-data:www-data /var/www/foo sudo chmod -R 755 /var/www/foo
Give these steps a shot—9 times out of 10, the Apache PHP module mismatch is the root cause here. Let me know if you run into any hiccups!
内容的提问来源于stack exchange,提问作者Álvaro

