How to change php version debian

On your system, if you have installed multiple versions of PHP [eg PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1 and 5.6]. PHP 7.2 is running as default PHP for Apache and CLI. For any requirement, you need to use PHP 5.6. Then you don’t need to remove PHP 7.2. You can simply switch your PHP version to default used for Apache and command line.

We assume you have installed multiple PHP version on your Debian system. Now you need to switch the active PHP version for CLI and Apache2. This tutorial will help you to switch between multiple PHP versions for Apache server and CLI on Debian.

  • Read => How to Install PHP on Debian 11 via PPA
  • Read => How to Install PHP on Debian 10 via PPA

We have installed PHP 8.1, PHP 7.4 and PHP 5.6 on our Debian system. You can use the same for other PHP versions by changes commands accordingly.

Enable PHP 8.1 as Default Version

You need to set PHP 8.1 as your active PHP version for CLI and Apache2 both. You can do this by disabling Apache2 modules for all other installed PHP versions and configure CLI using the update-alternatives command.

  • For Apache:-
    Run the following command to disable Apache module for other PHP versions.
    sudo a2dismod php7.4 php5.6 
    

    Now, enable PHP 8.1 module in Apache server.

    sudo a2enmod php8.1 
    sudo service apache2 restart 
    
  • For Command Line Interface:-
    sudo update-alternatives --set php /usr/bin/php8.1 
    sudo update-alternatives --set phar /usr/bin/phar8.1 
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.1 
    sudo update-alternatives --set phpize /usr/bin/phpize8.1 
    sudo update-alternatives --set php-config /usr/bin/php-config8.1 
    

Note – The phpize8.1 and php-config8.1 command is available in php8.1-dev package. This is more useful for compiling PHP modules using pecl.

Enable PHP 7.4 as Default Version

You need to set PHP 7.4 as your active PHP version for CLI and Apache2 both. You can do this by disabling Apache2 modules for all other installed PHP versions and configure CLI using the update-alternatives command.

  • Apache:-
    Run the following commands to disable Apache module for other PHP versions
    sudo a2dismod php8.1 php5.6 
    

    Now, enable PHP 7.4 module in Apache server.

    sudo a2enmod php7.4 
    sudo service apache2 restart 
    
  • Command Line Interface:-
    sudo update-alternatives --set php /usr/bin/php7.4 
    sudo update-alternatives --set phar /usr/bin/phar7.4 
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4 
    sudo update-alternatives --set phpize /usr/bin/phpize7.4 
    sudo update-alternatives --set php-config /usr/bin/php-config7.4 
    

Note – The phpize7.4 and php-config7.4 command is available in php7.4-dev package. This is more useful for compiling PHP modules using pecl.

Enable PHP 5.6 as Default Version

The developers of PHP are no longer supporting PHP 5.6. It is strongly recommended to upgrade to higher PHP version like 7.4 or 8.1. But still you can install and Use php 5.6 on your Debian system if your application required PHP 5.6. Let’s set PHP 5.6 as your default version for CLI and Apache2 both.

  • Apache:-
    Run the following commands to disable other PHP versions like [php 8.1, php7.4 etc] for Apache and command line.
    sudo a2dismod php7.4 php8.1 php8.0     
    

    Now, enable PHP 5.6 module in Apache server.

    sudo a2enmod php5.6 
    sudo service apache2 restart 
    
  • Command Line:-
    sudo update-alternatives --set php /usr/bin/php5.6 
    sudo update-alternatives --set phar /usr/bin/phar5.6 
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6  
    sudo update-alternatives --set phpize /usr/bin/phpize5.6 
    sudo update-alternatives --set php-config /usr/bin/php-config5.6 
    

Note – The phpize5.6 and php-config5.6 command is available in php5.6-dev package. This is more useful for compiling PHP modules using pecl.

Conclusion

In this tutorial, you have learned about switching PHP versions on Debian system. You can change the default PHP version without uninstalling the other versions.

PHP 8.1 is the latest PHP version released on 2021. In this guide you are going to learn how to install the latest PHP version which is currently 8.1 on your Debian system or server and configure it with Apache and Nginx. You will also learn how to upgrade your PHP version to latest.

This tutorial guides you to configure PHP INI settings, FPM settings, Pools, etc which is more useful for your application to run smooth.

This installation is tested on Google Cloud Platform with a Compute Compute Engine VM Instance. This set up will work on all Linux servers.

Prerequisites

Basic knowledge of using SSH Terminal on Linux.

Getting Started

Make sure your Debian server is having the latest packages by running the following command.

sudo apt update
sudo apt upgrade

This will update the package index and update the installed packages to the latest version.

Download SURY PPA for PHP 8.1

Download the Sury PPA for PHP 8.1 package using wget.

sudo apt -y install lsb-release apt-transport-https ca-certificates wget
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg //packages.sury.org/php/apt.gpg

Once you have downloaded the PPA you can add it to the server packages.

echo "deb //packages.sury.org/php/ $[lsb_release -sc] main" | sudo tee /etc/apt/sources.list.d/php.list

Now you can proceed the installation of PHP 8 by updating the system.

sudo apt update

Once you have added the Surry PPA you can install PHP 8.1.

Install PHP 8.1 for Apache

Execute the following command to install PHP 8.1

sudo apt install php8.1

After the installation has completed, you can confirm the installation using the following command

php -v

Install PHP 8.1 FPM for Nginx

For Nginx you need to install FPM. Execute the following command to install PHP 8.1 FPM

sudo apt install php8.1-fpm

After the installation has completed, confirm that PHP 8.1 FPM has installed correctly with this command

php-fpm8.1 -v

Install PHP 8.1 Extensions

Installing PHP extensions are simple with the following syntax.

sudo apt install php8.1-extension_name

Now, install some commonly used php-extensions with the following command.

sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-redis php8.1-intl -y

Configure PHP 8.1

Now we configure PHP for Web Applications by changing some values in php.ini file.

For PHP 8.1 with Apache the php.ini location will be in following directory.

sudo nano /etc/php/8.1/apache2/php.ini

For PHP 8.1 FPM with Nginx the php.ini location will be in following directory.

sudo nano /etc/php/8.1/fpm/php.ini

Hit F6 for search inside the editor and update the following values for better performance.

upload_max_filesize = 32M 
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000

Once you have modified your PHP settings you need to restart your Apache for the changes to take effect.

For users with Nginx to who use PHP-FPM, you need to restart PHP-FPM.

sudo service php8.1-fpm restart

Configure PHP 8.1 FPM Pools

PHP-FPM allows you to configure the user and group that the service will run under. You can modify these with these commands

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

If you want to change the username name you can change the following lines by replacing the www-data with your username.

user = username 
group = username 
listen.owner = username
listen.group = username

Hit CTRL+X and Y to save the configuration and check if the configuration is correct and restart PHP.

Restart PHP 8.1 FPM

Once you have updated your PHP FPM settings you need to restart it to apply the changes.

sudo php-fpm8.1 -t 
sudo service php8.1-fpm restart

Now you are having PHP 8.1 Installed and configured.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

Upgrade to PHP 8.1 for Apache

Once you have installed PHP 8.1 you need to upgrade to the latest installed version of PHP.

You need to tell Apache to use the PHP 8.1version we installed right now. Disable the old PHP module [below I have mentioned php7.4, you need to use your current php version used by Apache] and enable the new PHP module using the following command.

Replace the current enabled PHP version with your version.

sudo a2dismod php7.4
sudo a2enmod php8.1

Restart Apache for the changes to take effect.

sudo service apache2 restart

Upgrade PHP 8.1 for Nginx

For Nginx you need to update the PHP-FPM socket in your Nginx configration located inside the sites-available directory. This will be located inside the location block location ~ .php$

Edit your configuration…

sudo nano /etc/nginx/sites-available/your.conf

The line you need to modify will look like this…

fastcgi_pass unix:/run/php/php7.4-fpm.sock; 

You need to replace the old PHP version with the new version.

fastcgi_pass unix:/run/php/php8.1-fpm.sock; 

Test your configration.

sudo nginx -t

Save the file and exit the editor and restart Nginx for the changes to take effect.

sudo service nginx restart

Conclusion

Now you have learned how to install PHP 8.1 on your Debian server for Apache and Nginx and also how to upgrade to latest version.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

How do I change my current version of PHP?

Log in to your one.com control panel. Scroll down to the Advanced settings tile and select PHP and database settings. Scroll down to Update PHP version. Select the PHP version you want to switch to and click Update.

How do I upgrade to PHP 7?

User Contributed Notes 1 note.
Login to the server via SSH as root. ... .
Check your current version of PHP. ... .
To install PHP 7.3, we must first add the repository: ... .
Then run an update: ... .
After completing the update, we need to install PHP 7.3. ... .
Then install the required PHP packages according to your current installation:.

Can I downgrade my PHP version?

If you'd like to downgrade your PHP installation you can do so without interfering with the path to your installation and can simply overwrite older files. This process will overwrite your current PHP.

How do I download PHP on Debian?

Install PHP 7.4 on Debian 10 / Debian 9.
Step 1: Update system. Ensure your system is updated: sudo apt update sudo apt upgrade -y && sudo reboot..
Step 2: Add SURY PHP PPA repository. Download and store PPA repository in a file on your Debian Server/Desktop. ... .
Step 3: Install PHP 7.4 on Debian 10 / Debian 9..

Chủ Đề