How to Switch PHP Versions in Apache2 on Ubuntu: From PHP 5.3 to 5.2


2 views

When running Apache2 on Ubuntu, the system may default to a newer PHP version (like 5.3.2) while certain legacy applications (particularly CRMs) strictly require older versions (like 5.2.8). This mismatch often appears in phpinfo() output despite manual PHP compilation.

  1. Disable current PHP module:
    sudo a2dismod php5
    
  2. Install PHP 5.2 CGI:
    sudo apt-get install php5-cgi
    
  3. Configure Apache to use specific PHP:
    Add to /etc/apache2/mods-available/fastcgi.conf:

    FastCgiServer /usr/bin/php-cgi
    AddHandler php-fastcgi .php
    Action php-fastcgi /php-fastcgi
    Alias /php-fastcgi /usr/bin/php-cgi
    
  4. Enable required modules:
    sudo a2enmod fastcgi actions alias
    

For compiled PHP 5.2.8 in /etc/php-5.2.8:

sudo update-alternatives --install /usr/bin/php php /etc/php-5.2.8/bin/php 52
sudo update-alternatives --set php /etc/php-5.2.8/bin/php

Create version_test.php:

<?php
phpinfo();
?>

Key checks:

  • Apache error logs (/var/log/apache2/error.log)
  • Module loading order (apache2ctl -M)
  • PHP CLI version (php -v) vs Apache version

For CRM installations that check versions during setup:

  • Modify version checks in install.php (temporary)
  • Set custom php_value directives in .htaccess
  • Consider Docker containers for isolated environments

When working with legacy CRM systems or specific PHP applications, you might encounter version compatibility issues. The problem occurs when Apache2 serves pages using a different PHP version than what your application requires. Here's how to properly configure Apache2 to use your desired PHP version.

First, verify which PHP version Apache is currently using:

<?php
phpinfo();
?>

Save this as test.php in your web root and access it via browser. The output will show the active PHP version and loaded configuration file.

For Ubuntu 10.04, you'll need to manually compile PHP 5.2.8 since it's not available in standard repositories:

wget http://museum.php.net/php5/php-5.2.8.tar.gz
tar -xzvf php-5.2.8.tar.gz
cd php-5.2.8
./configure --prefix=/etc/php-5.2.8 --with-apxs2=/usr/bin/apxs2 --with-mysql
make
sudo make install

Edit your Apache configuration to point to the correct PHP installation:

# In /etc/apache2/mods-available/php5.conf
LoadModule php5_module /etc/php-5.2.8/libphp5.so

# Update PHP ini path
PHPIniDir "/etc/php-5.2.8"

After changing versions, ensure required modules are available:

# For common modules
sudo apt-get install php5.2-mysql php5.2-gd php5.2-curl

Or compile them manually with your PHP installation using:

./configure --with-mysql --with-gd --with-curl

After making changes, restart Apache:

sudo /etc/init.d/apache2 restart

Verify the change by checking both CLI and Apache PHP versions:

php -v
# And through your test.php script

If you encounter problems:

  1. Check Apache error logs: /var/log/apache2/error.log
  2. Verify module loading: apache2ctl -M | grep php
  3. Ensure proper file permissions on your PHP installation

For more flexibility, consider using PHP-FPM to run multiple PHP versions simultaneously:

sudo apt-get install php5.2-fpm
# Then configure Apache virtual hosts to use specific FPM pools

This allows different applications to run on different PHP versions without affecting the entire server.