Apache and the command line run different PHP versions
php -v shows a different version than phpinfo() in the browserShort answer
Apache and the php command are two separate things. a2enmod changes which PHP module Apache loads. php -v reports the command line program, which update-alternatives controls. Two PHP modules enabled at once also conflict in Apache.
Fix it fast
1Find out which cause you have
Compare the two answers: what the command line runs, and which PHP modules Apache has enabled.
php -v ls /etc/apache2/mods-enabled | grep -i php2Match what you see, then jump to its fix
What does the error look like?
php -v shows a different version than phpinfo() in the browsera2enmod php8.3 succeeds but php -v still shows the old version
Cause 1 of 2
php -v reports the CLI, not Apache
1Check
The php manual says the CLI is a different SAPI from the others, and php -v tells you which one you run. a2enmod only links a module in mods-enabled, so it cannot change the CLI.
update-alternatives --display php2Fix
Point the php command at the version you want. --set picks it without a prompt.
sudo update-alternatives --set php /usr/bin/php8.33Verify
php -v now matches the version Apache serves.
php -vConfirmed by manpages.ubuntu.com
Cause 2 of 2
Two PHP modules are enabled in Apache
1Check
Apache can load only one mod_php. The listing from the first step shows every enabled PHP version.
ls /etc/apache2/mods-enabled | grep -i '^php'2Fix
Disable the old module, enable the one you want, and restart Apache.
sudo a2dismod php8.1
sudo a2enmod php8.3
sudo systemctl restart apache23Verify
Create a file with phpinfo() and open it in the browser; the version at the top is the one you enabled.
ls /etc/apache2/mods-enabled | grep -i '^php'Confirmed by manpages.ubuntu.com
How do I stop it coming back?
- After a PHP upgrade, check both php -v and phpinfo() from the web.
- Keep one PHP version per server, or move to PHP-FPM pools if you need several.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Composer reports a different PHP version than php -v on Ubuntu
You are running 8.1.2.
Composer takes its PHP version from the interpreter that runs it, unless config.platform fakes one.
3 causes, each with a fixServers
docker-php-ext-install mbstring fails: oniguruma not found
The docker-php-ext-install step exits with a non-zero code
mbstring needs the Oniguruma library to build its regex functions, and the PHP build finds it through pkg-config.
3 causes, each with a fixServers
Laravel returns 500 Internal Server Error on Apache
/var/www/html/.htaccess: RewriteCond: bad flag delimiters
Apache and Laravel report different 500s.
5 causes, each with a fixServers
nginx gives up waiting for the app and returns 504
upstream timed out (110: Connection timed out) while reading response header from upstream
nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.
5 causes, each with a fixServers
nginx and PHP-FPM show File not found or Primary script unknown
File not found.
PHP-FPM received a script path it could not turn into a file.
4 causes, each with a fix