Switching PHP Versions on Ubuntu Using Command Line and System Alternatives

Introduction

When managing multiple projects or environments, developers often need to switch between different versions of PHP. This is especially relevant when working with an operating system like Ubuntu where multiple PHP versions can coexist. In this tutorial, we will guide you through switching between PHP versions on the command line and configure Apache to use the desired version.

Understanding update-alternatives

The update-alternatives tool in Ubuntu helps manage symbolic links that determine default commands when multiple program versions are installed. It provides a convenient way to switch versions without manually editing configuration files or restarting services like Apache.

Step 1: Install Multiple PHP Versions

First, ensure you have more than one PHP version installed on your system. You can install specific PHP versions using Ubuntu’s package manager:

sudo apt-get update
sudo apt-get install php5.6
sudo apt-get install php7.1

Repeat these steps for any additional PHP versions you need.

Step 2: Configure update-alternatives

After installing the required PHP versions, register them with the alternatives system:

sudo update-alternatives --install /usr/bin/php php /usr/bin/php5.6 56
sudo update-alternatives --install /usr/bin/php php /usr/bin/php7.1 71

Here, 56 and 71 are priority numbers; higher values take precedence.

Step 3: Switch PHP Versions

To switch between installed PHP versions, use:

sudo update-alternatives --config php

This command will list all available PHP versions. You can select the desired version by entering its selection number.

Verifying the Change

Verify the change with:

php -v

The output should reflect the selected PHP version.

Configuring Apache to Use a Specific PHP Version

To configure Apache to use a specific PHP version, follow these steps:

  1. Disable Current PHP Module:

    sudo a2dismod php7.1  # or whichever version is currently active
    
  2. Enable Desired PHP Module:

    sudo a2enmod php5.6  # replace with your desired version
    
  3. Restart Apache:

    sudo service apache2 restart
    

Verifying Apache Configuration

Check the active PHP version used by Apache using:

  • A PHP info file: Create info.php in /var/www/html/ containing <?php phpinfo(); ?>. Access it via your browser to see the PHP configuration.

  • CLI verification: Run php -v from the terminal.

Best Practices

  • Consistency: Ensure both command line and Apache use the same PHP version using update-alternatives.
  • Documentation: Keep track of which versions are installed and their respective paths for troubleshooting.
  • Security Updates: Regularly update your PHP installations to patch vulnerabilities.

By following these steps, you can efficiently manage multiple PHP versions on Ubuntu, ensuring compatibility across different projects or requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *