Installing PHP 5.2 and PHP 5.3 on the Same Linux System

Recently, while upgrading from PHP 5.2 to PHP 5.3, I needed to install both PHP 5.2 and PHP 5.3 on the same Linux machine. Through exploration, I summarized a solution that simplifies installation and configuration, and makes it convenient to switch between the two versions.

The test Linux system is Fedora 14, initially installed with Apache 2.0.64 and PHP 5.2.6, installed in the following paths:

/usr/local/apache2
/usr/local/php5

Now we want to install PHP 5.3 while preserving the existing PHP 5.2. Ideally, both versions should be able to share the existing Apache 2. Considering that PHP also needs to run from the command line (without going through Apache), this involves system environment variables, so we'll start by preparing the environment variables.

1. Create a soft link to avoid modifying environment variables when switching PHP versions

This way, we only need to modify the soft link when switching versions.

# mkdir -p /usr/php/
# ln -s /usr/local/php5 /usr/php/default

Then add to system environment variables:

# vi /etc/profiles

Add a line at the beginning of the file:

PHP_HOME=/usr/php/default

Find the PATH= line and add PHP_HOME to it, for example:

PATH=$JAVA_HOME/bin:$PATH:$PHP_HOME/bin:$MYSQL_HOME/bin

Exit the command line window and reopen it to make the environment variables take effect, then execute:

$ php -version

If it outputs something like PHP 5.2.6 (cli), the soft link and environment variables are set up correctly.

2. Backup the PHP 5.2 module for Apache

Find the libphp5.so file in /usr/local/apache2/modules, which should be the module compiled during the existing PHP 5.2 installation. Rename it as a backup to facilitate compiling a new PHP module when installing PHP 5.3 below.

# mv libphp5.so libphp526.so

3. Compile and install PHP 5.3

Specific configurations can be chosen as needed, just make sure the installation path is different from the existing PHP 5.2 path. For example, if the existing PHP 5.2 path is /usr/local/php5, then when compiling PHP 5.3:

./configure --prefix=/usr/local/php53 --with-apxs2=/usr/local/apache2/bin/apxs

After compilation and installation, many files will be generated in /usr/local/php53, and a new libphp5.so will be generated in /usr/local/apache2/modules. Comparing file sizes, it should be much larger than the backed up libphp526.so.

4. Configure Apache2 to support different PHP versions

First, stop the apache service.

Edit httpd.conf and find:

LoadModule php5_module modules/libphp5.so

Delete this line and add the following section:

<IfDefine php53>
LoadModule php5_module modules/libphp5.so
</IfDefine>
<IfDefine php526>
LoadModule php5_module modules/libphp526.so
</IfDefine>

Then start apache with the -D parameter, for example:

/usr/local/apache2/bin/apachectl -D php53 -k start

Apache will call the <IfDefine php53> section in httpd.conf and load the new PHP 5.3 libphp5.so. Similarly, if:

/usr/local/apache2/bin/apachectl -D php526 -k start

Apache will call the <IfDefine php526> section in httpd.conf and load the backup PHP 5.2 libphp526.so.

Open a phpinfo() information page in your browser to check the PHP information. You should see information for different PHP versions.

5. Update the PHP path soft link

To install PHP extensions and let the command line execute the new PHP 5.3 version, we just need to update the soft link for the PHP path.

# rm -f /usr/php/default;
# ln -s /usr/local/php53 /usr/php/default

Then execute in the command line:

$ php -version

If it outputs something like PHP 5.3.6 (cli), the soft link and environment variables now support the new version.

Then when compiling and installing PHP extensions, you can directly use phpize without specifying the absolute path for PHP 5.3.

6. Switching PHP versions

To summarize the above operations, to use PHP 5.3, point /usr/php/default to /usr/local/php53, and start Apache with:

/usr/local/apache2/bin/apachectl -D php53 -k start

To use PHP 5.2, point /usr/php/default to /usr/local/php5, and start Apache with:

/usr/local/apache2/bin/apachectl -D php526 -k start

Original Link: https://www.snowpeak.fun/en/article/detail/install_php_52_and_php_53_in_same_linux/