Proudly Hosting over 100,000 Fast Websites since 2010

How to Change Default PHP Version in Ubuntu: A Step-by-Step Guide

How to Change Default PHP Version in Ubuntu

Ubuntu is a popular operating system used for web servers, and it comes with a default PHP version installed. However, as new PHP versions are released with improved features and security patches, it becomes necessary to update the default PHP version on your server. This guide will show you how to change the default PHP version in Ubuntu.

Checking Current PHP Version:

Before installing a new PHP version, it’s important to check the current version installed on your server. To do this, open a terminal and run the following command:

php -v 

This will display the current PHP version installed on your server. Make a note of the version number, as you’ll need it later.

Installing New PHP Versions:

Ubuntu has a built-in tool called “add-apt-repository” that allows you to add a new PHP version repository to your system. To add a new PHP repository, run the following command:

bash
sudo add-apt-repository ppa:ondrej/php

 

This command adds the “ondrej/php” repository, which contains the latest PHP versions. After running the command, you’ll need to update your system’s package list:

sql
sudo apt-get update

 

Once the package list is updated, you can install a new PHP version by running the following command:

csharp

sudo apt-get install phpX.X 

Replace "X.X" with the version number you want to install. For example, to install PHP 7.4, you would run:
csharp

sudo apt-get install php7.4

 

After the installation is complete, you can verify that the new PHP version is installed by running the “php -v” command again.

Configuring Apache to Use New PHP Version:

If you’re using Apache as your web server, you’ll need to configure it to use the new PHP version. First, disable the old PHP module by running the following command:

sudo a2dismod phpX.X

 

Replace “X.X” with the version number of the old PHP module you want to disable. For example, to disable PHP 7.2, you would run:

sudo a2dismod php7.2

 

Next, enable the new PHP module by running the following command:

sudo a2enmod phpX.X

 

Replace “X.X” with the version number of the new PHP module you want to enable. For example, to enable PHP 7.4, you would run:

sudo a2enmod php7.4

 

Finally, restart the Apache server to apply the changes:

sudo service apache2 restart

Configuring CLI to Use New PHP Version:

If you're using the command-line interface (CLI) to run PHP scripts, you'll need to update the "alternatives" to use the new PHP version. To do this, run the following command:
Python
sudo update-alternatives --set php /usr/bin/phpX.X

 

Replace “X.X” with the version number of the new PHP version you want to set as default. For example, to set PHP 7.4 as the default version, you would run:

Python
sudo update-alternatives --set php /usr/bin/php7.4

Testing New PHP Version:

After changing the default PHP version, it’s important to test that everything is working correctly. Create a new PHP file with the following code:

php
<?php phpinfo(); ?>

 

Save the file as “test.php” in your web server’s document root.

Configuring CLI to Use New PHP Version:

In addition to configuring Apache to use the new PHP version, you also need to configure the command line interface (CLI) to use the new version. The CLI is used when running PHP scripts from the terminal.

Updating Alternatives:

To change the default PHP version for the CLI, you need to update the alternatives. Alternatives is a system tool that allows you to manage symbolic links that determine which version of a particular command or program is run when multiple versions are installed on a system.

You can update the PHP alternatives by running the following command:

Python
sudo update-alternatives --set php /usr/bin/php{version number}

 

Replace {version number} with the version number of the PHP version you want to use. For example, to set PHP 7.4 as the default version, you would run:

python

sudo update-alternatives --set php /usr/bin/php7.4

Setting New PHP Version as Default:

After updating the alternatives, you can check if the new PHP version is set as the default for the CLI by running:

 

php -v

 

This will display the version number of the PHP version currently used by the CLI.

If the version number displayed is the new version you installed, then the configuration was successful. If not, you can set the new PHP version as the default for the CLI by running the following command:

sql

sudo update-alternatives --config php

 

This command will display a list of available PHP versions. Use the arrow keys to select the version you want to use as the default and press Enter.

Testing New PHP Version:

After configuring Apache and the CLI to use the new PHP version, you need to test that everything is working as expected.

Creating PHP Test File:

Create a new PHP file in the Apache document root directory. You can use any text editor to create the file. For example, to create a file called “test.php”, run the following command:

css
sudo nano /var/www/html/test.php

 

Add the following PHP code to the file:

php
<?php phpinfo(); ?>

 

This code will display information about the PHP version and configuration when accessed from a web browser.

Checking PHP Version on Test File:

Open a web browser and navigate tohttp://localhost/test.php. This will display the PHP information page. Look for the “PHP Version” section to verify that the new PHP version is being used.

Alternatively, you can run the following command in the terminal to check the PHP version:

CSS

php /var/www/html/test.php

 

This will display the PHP information in the terminal.

Removing Old PHP Versions:

After verifying that the new PHP version is working correctly, you can remove the old versions to free up disk space and avoid conflicts.

Uninstalling old PHP Versions:

To uninstall an old PHP version, run the following command:

CSharp
sudo apt-get remove php{version number}

 

Replace {version number} with the version number of the PHP version you want to remove. For example, to remove PHP 7.2, you would run:

csharp
sudo apt-get remove php7.2

 

Updating Alternatives:

After uninstalling the old PHP version, you need to update the alternatives to remove the old version from the list of available PHP versions. Run the following command:

sql

sudo update-alternatives --remove php /usr/bin/php{version number}

 

Replace {version number} with the version number of the PHP version you want to remove. For example, to remove PHP 7.2, you would run:

sql

sudo update-alternatives --remove

Conclusion

Changing the default PHP version in Ubuntu is a straightforward process that can be accomplished in a few simple steps. By installing the latest PHP version, you can take advantage of new features and performance improvements, while also ensuring compatibility with the latest web applications. we hope that now you have a clear understanding of how to change the default PHP version on your Ubuntu system.

Remember to always back up your files and configurations before making any changes to your system to avoid any potential issues. With the latest PHP version installed, you can continue to develop and deploy your web applications with confidence.

 

Facebook
Twitter
LinkedIn
Reddit

Leave a Reply

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