JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Switching PHP versions on Ubuntu

Author:JIYIK Last Updated:2025/04/13 Views:

Different tasks may require running multiple versions of PHP.

You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods.

We can switch PHP versions on Ubuntu using the command line with just a few commands.

This article explains the complete process of switching PHP versions on Ubuntu.


How to switch PHP versions on Ubuntu

First, let's check the version of PHP installed on our system. Open Ubuntu bash and insert the following command:

$ php -v

The output will show the version of PHP used on our system.

Output:

PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

The output shows that only PHP 7.4.3 is installed on our system. Install another version of PHP first and then try to switch.

Insert the following command to install PHP 5.6:

#add php repository; this repository might not install earlier or later versions of PHP.
$ sudo add-apt-repository -y ppa:ondrej/php
#update sudo
$ sudo apt update
#install php 5.6
$ sudo apt install php5.6

The output of each command should not retrieve errors. Otherwise, PHP 5.6 will not be installed.

If you check the PHP version again, the output is still the same.

To switch PHP version from 7.4.3 to 5.6, insert the following command in bash.

To disable PHP 7.4.3:

$ sudo a2dismod php7.4

Output:

Module php7.4 already disabled

If the output is different than above, you may need to install apache mod and run the above command to disable PHP 7.4. Run this command to install apache mod.

$ sudo apt-get install libapache2-mod-php7.4

After disabling PHP 7.4, run the following command to enable PHP 5.6:

$ sudo a2enmod php5.6

Output:

Considering dependency mpm_prefork for php5.6:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php5.6:
Module php5.6 already enabled

You may need to restart apache between running these commands.

$ sudo service apache2 restart

PHP switches from 7.4 to 5.6. If you need to set PHP 5.6 as the default version, use the following command.

#This command will directly set PHP 5.6 as default. Manual Switching
$ sudo update-alternatives --set php /usr/bin/php5.6
#This command will give you the option to select a default PHP version. Interactive Switching
$ sudo update-alternatives --config php

Output:

There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php7.4   74        auto mode
* 1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.0   70        manual mode
  3            /usr/bin/php7.4   74        manual mode

Press <enter> to keep the current choice[*], or type selection number:

You can select any version by simply entering the corresponding number. The selected version will be set as default.

You may also need to set other PHP extensions as default. For example:

$ sudo update-alternatives --config phar

The command will also give you phara list of versions and you can choose the default as above. It will help if you restart apache after you are done setting the defaults.

$ sudo service apache2 restart

To switch back to PHP 7.4, you have to run these commands in reverse.

$ sudo a2dismod php5.6
$ sudo a2enmod php7.4
$ sudo service apache2 restart

The output will show that PHP 5.6 is disabled and 7.4 is enabled. Restart apache if needed.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Displays PHP configuration information on localhost

Publish Date:2025/04/13 Views:107 Category:PHP

phpinfo() is a built-in function in PHP which outputs all the information of PHP configuration on the local host. We have to phpinfo() create a PHP file with a simple function call. Sometimes, the file may not work properly and output a 404

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial