How to Fix Apache Using Old PHP Version After PHP 5.4 Upgrade on Ubuntu


2 views

After upgrading PHP from 5.3.2 to 5.4 on Ubuntu, you might notice this common scenario:

$ php -v
PHP 5.4.0 (cli)

But when checking through Apache:

<?php phpinfo(); ?>

Still shows PHP 5.3.2. This happens because:

Ubuntu maintains separate PHP packages for CLI and Apache:

  • php5-cli for command line
  • libapache2-mod-php5 for Apache module

First, verify installed PHP packages:

$ dpkg -l | grep php5

Then install/upgrade the Apache module:

$ sudo apt-get install libapache2-mod-php5
$ sudo apt-get --reinstall install php5

Enable the module and restart Apache:

$ sudo a2enmod php5
$ sudo service apache2 restart

For better performance, consider PHP-FPM:

$ sudo apt-get install php5-fpm
$ sudo a2enmod actions fastcgi alias

Then configure virtual host:

<VirtualHost *:80>
    SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost"
</VirtualHost>

Create test file:

<?php
header('Content-Type: text/plain');
echo 'PHP Version: ' . phpversion() . "\n";
echo 'Loaded Config: ' . php_ini_loaded_file() . "\n";
?>

Check Apache error log if issues persist:

$ tail -f /var/log/apache2/error.log

When you run php -v in terminal, it shows PHP 5.4 while Apache serves pages using PHP 5.3.2. This occurs because:

# CLI uses different PHP binary than Apache module
which php
/usr/local/bin/php  # New 5.4 installation

# While Apache loads:
locate libphp5.so
/usr/lib/apache2/modules/libphp5.so  # Old 5.3 module

First verify which PHP module Apache is actually using:

apache2ctl -M | grep php
# Output may show:
php5_module (shared)

For Ubuntu 10.04, you'll need to:

sudo apt-get remove libapache2-mod-php5
sudo apt-get install apache2 libapache2-mod-php5.4
# Or compile from source:
./configure --with-apxs2=/usr/bin/apxs2
make
sudo make install

If you prefer FastCGI approach:

sudo apt-get install libapache2-mod-fcgid
sudo a2enmod actions fcgid alias

# Add to Apache config:
<FilesMatch \.php$>
    SetHandler fcgid-script
</FilesMatch>
Action fcgid-script /usr/local/php54/bin/php-cgi

Create test.php in your webroot:

<?php
phpinfo();
?>

Then restart Apache and check:

sudo service apache2 restart
curl -I http://localhost/test.php | grep X-Powered-By