Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Apache and the command line run different PHP versions

Errorphp -v shows a different version than phpinfo() in the browser

Short 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.

  • Applies to Ubuntu 24.04, Apache 2.4, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 php
  2. 2Match what you see, then jump to its fix

    1. 1phpinfo() in the browser shows the new version but php -v shows the old onephp -v reports the CLI, not ApacheFix
    2. 2More than one phpX.Y.load file in mods-enabledTwo PHP modules are enabled in ApacheFix

What does the error look like?

  • php -v shows a different version than phpinfo() in the browser
  • a2enmod 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 php

2Fix

Point the php command at the version you want. --set picks it without a prompt.

sudo update-alternatives --set php /usr/bin/php8.3

3Verify

php -v now matches the version Apache serves.

php -v

Confirmed by manpages.ubuntu.com

Did this fix it?

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 apache2

3Verify

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.