Easy Steps To Upgrade PHP Version on Debian 11 – OrcaCore

Posted on

Easy Steps To Upgrade PHP Version on Debian 11 - OrcaCore

Easy Steps To Upgrade PHP Version on Debian 11 – OrcaCore

In this guide, we will demonstrate How To Upgrade PHP Version on Debian 11. The default PHP version available on Debian 11 is PHP 7.4. If you’re looking to update your PHP version to a newer release, this guide, brought to you by OrcaCore, will walk you through the necessary steps.

Before you begin, ensure you are logged into your server as a non-root user with sudo privileges. You can refer to our guide on Initial Server Setup with Debian 11 if you haven’t already configured this.

Let’s dive into the steps to Upgrade PHP Version on Debian 11:

Step 1. What Is the Default PHP Version for Debian 11?

Debian 11 comes with PHP 7.4 as the default version. You can confirm this by running the following command in your terminal:

php -v

The output should resemble the following:

Default PHP Version for Debian 11

Step 2. Remove PHP 7.4 From Debian 11

Before installing a newer PHP version, you need to remove the existing one. Use the following command to remove PHP 7.4:

sudo apt purge php7.*

After uninstalling the packages, clean up any residual dependencies with these commands:

# sudo apt autoclean
# sudo apt autoremove

Step 3. How To Add PHP PPA Repository in Debian?

To install PHP 8.x on Debian 11, you’ll typically use the Ondřej Surý PPA repository. First, install the necessary packages to enable adding and using external repositories:

sudo apt -y install lsb-release apt-transport-https ca-certificates

Next, add the PPA repository using the following commands:

# sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
# sudo echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list

Once the PPA repository is added, you’re ready to proceed with the installation.

Step 4. How To Udate PHP Version on Debian 11?

Update your local package index to include the newly added repository:

sudo apt update

Now, install the desired PHP version. As of this writing, PHP 8.1 is a widely used and stable option. Install it using:

sudo apt install php8.1

Verify the installation by checking the PHP version:

php -v

The output should now reflect the updated PHP version:

Udate PHP Version on Debian 11

Install common PHP extensions. These are commonly required by many PHP applications:

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-intl -y

Conclusion

You have successfully learned how to Upgrade PHP Version on Debian 11 by adding the PPA repository and installing the latest PHP version. This upgrade enhances security, improves performance, and ensures compatibility with modern web applications. Keep your server updated for optimal functionality and security. Remember the steps to Upgrade PHP Version on Debian 11 for future use.

Don’t forget to follow us on Facebook, Instagram, and YouTube for more helpful guides and tutorials.

You might also find these articles interesting:

How To Upgrade PHP Version on Ubuntu 20.04

How To Upgrade PHP Version on Centos 7

Upgrade Bash in Debian

Upgrade Debian Kernel Without Network Connection

FAQs

How do I switch between multiple PHP versions?

If you have multiple PHP versions installed, you can switch with the command below and select the preferred PHP version from the list:
sudo update-alternatives --config php

Will upgrading PHP break my existing applications?

It can if your applications depend on deprecated features. Always test in a staging environment before upgrading.

Can I downgrade PHP if needed?

Yes, remove the current PHP version and install the older one:
sudo apt remove --purge php8.1
sudo apt install php7.4

Alternative Solutions to Upgrade PHP Version on Debian 11

While the PPA method described above is common, here are two alternative approaches to Upgrade PHP Version on Debian 11: using Docker and compiling from source.

1. Using Docker

Docker provides a containerization solution that allows you to run applications in isolated environments. This is a particularly good choice if you want to use a different PHP version for specific applications without affecting the system-wide PHP installation.

Explanation:

Docker uses images that contain everything needed to run an application, including the operating system, runtime, libraries, and application code. You can pull a pre-built PHP image from Docker Hub, which contains a specific PHP version, and then run your application within that container. This isolates your application from the host system’s PHP version.

Steps:

  1. Install Docker: If you don’t have Docker installed, you’ll need to install it first. Follow the official Docker documentation for Debian: https://docs.docker.com/engine/install/debian/

  2. Pull a PHP Docker Image: Choose a PHP image from Docker Hub that matches your desired PHP version and configuration. For example, to pull PHP 8.1 with Apache:

    docker pull php:8.1-apache
  3. Create a Dockerfile (Optional): If you need a custom PHP configuration or specific extensions, you can create a Dockerfile in your application directory:

    FROM php:8.1-apache
    
    # Install extensions
    RUN apt-get update && apt-get install -y 
        php8.1-mysql 
        php8.1-gd 
        php8.1-mbstring 
        php8.1-zip 
        && docker-php-ext-enable gd mbstring zip
    
    # Copy your application code
    COPY . /var/www/html/
  4. Build the Docker Image (If using Dockerfile):

    docker build -t my-php-app .
  5. Run the Docker Container:

    docker run -d -p 8080:80 -v /path/to/your/app:/var/www/html my-php-app
    • -d: Run the container in detached mode (in the background).
    • -p 8080:80: Map port 8080 on your host to port 80 inside the container. Access your application through http://localhost:8080.
    • -v /path/to/your/app:/var/www/html: Mount your application directory to the web server’s document root inside the container. This allows you to modify your code on the host machine, and the changes will be reflected inside the container.

Benefits:

  • Isolation: Keeps your application isolated from the host system.
  • Version Control: Easy to switch between different PHP versions for different applications.
  • Reproducibility: Ensures consistent environments across different machines.

2. Compiling From Source

Compiling PHP from source gives you the most control over the installation process. However, it’s also the most complex and time-consuming method.

Explanation:

This method involves downloading the PHP source code, configuring the build options, compiling the code, and installing the resulting binaries. It allows you to customize the PHP installation to your exact needs.

Steps:

  1. Install Dependencies: Before you begin, you’ll need to install the necessary build tools and libraries.

    sudo apt-get update
    sudo apt-get install -y build-essential libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libcurl4-openssl-dev libbz2-dev libssl-dev
  2. Download the PHP Source Code: Visit the official PHP website (https://www.php.net/downloads) and download the source code for the desired PHP version (e.g., PHP 8.1).

  3. Extract the Source Code:

    tar -xvf php-8.1.x.tar.gz
    cd php-8.1.x
  4. Configure the Build: The ./configure script allows you to specify various build options, such as the installation directory and the extensions you want to include.

    ./configure --prefix=/usr/local/php81 --with-config-file-path=/usr/local/php81/etc --with-mysqli --with-pdo-mysql --with-curl --with-gd --with-jpeg --with-png --with-freetype --enable-mbstring --enable-zip --with-openssl
    • --prefix: Specifies the installation directory.
    • --with-config-file-path: Specifies the location of the php.ini configuration file.
    • --with-*: Enables specific extensions.
    • --enable-*: Enables other features.
  5. Compile the Code:

    make
  6. Install the Binaries:

    sudo make install
  7. Configure PHP:

    • Copy the php.ini-development or php.ini-production file to the configuration directory and rename it to php.ini.
    • Edit the php.ini file to customize your PHP settings.
  8. Add PHP to the System Path: Add the PHP installation directory to your system’s PATH environment variable. Edit your .bashrc or .zshrc file and add the following line:

    export PATH="/usr/local/php81/bin:$PATH"

    Then, source the file:

    source ~/.bashrc
  9. Verify the Installation:

    php -v

Benefits:

  • Full Control: Complete control over the installation process and configuration.
  • Customization: Ability to enable or disable specific features and extensions.

Drawbacks:

  • Complexity: Requires more technical expertise.
  • Time-Consuming: Takes longer to compile and install.
  • Maintenance: Requires manual updates and security patching.

Both Docker and compiling from source offer viable alternatives to using a PPA when you Upgrade PHP Version on Debian 11. Docker is excellent for application isolation and easy version management, while compiling from source provides ultimate control and customization. Choose the method that best suits your needs and technical expertise.

Leave a Reply

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