How to Install PHP on Debian 12

PHP is the programming language that is popular in web development,also it is open source, and can be used for setting up the HTML pages. PHP, also known as the Hypertext Preprocessor and was created by Rasmus Lerdorf in 1994. 

PHP can be run on different platforms including the Linux distributions and this blog will demonstrate the installation of the PHP on Debian with the following outline:

  1. What is the PHP
  2. What are the Installation Methods of PHP on Debian
  3. Method 1: Install PHP on Debian Using the Default Repository
  4. Method 2: Install PHP on Debian Using the PPA Repository
  5. How to Test the PHP Script on Debian 
  6. How to Uninstall the PHP on Debian 

Let us start the post with an understanding of PHP. 

What is PHP?

PHP is the programming language that is used for web development and this programming language is used to write the web scripts on the server side because of it, dynamic web pages are developed. It also helps web developers to integrate static web pages with dynamic websites.

It also contains a wide range of libraries that help web developers in handling databases.  One of the most important features of PHP is that it is open source and also has an active community. This helps the beginners understand the usage of the PHP programming language. 

What are the Methods to Install PHP on Debian?

Two different installation methods for PHP on Debian 12 can be used which are:

  • Using the default repository
  • Using the PPA repository

Method 1: Install PHP on Debian Using the Default Repository

Debian 12 comes with hundreds of packages of different software that can be installed with the help of the package manager. Following the steps mentioned below, users will be able to install the PHP package on Debian 12 from its repository with the apt package manager. 

Step 1: Launch the Terminal

First open and launch the terminal of Debian 12 as shown below:

Step 2: Update the Packages

After launching the terminal, update all the packages so that the updated version of the PHP will be installed on the computer:

$ sudo apt update

Step 3: Install the PHP on Debian

Now install the package of the PHP with the execution of the command:

$ sudo apt install php libapache2-mod-php

Type the “y” and press the ENTER key to proceed with the installation:

Step 4: Confirm the Installation 

To confirm the installation, display the version of installed PHP:

$ php -v

PHP has successfully been installed on Debian 12 using the default repository package. 

Method 2: Install PHP on Debian Using the PPA Repository

Another method to install PHP on Debian 12 is by using the PPA repository. For this purpose, run all the commands explained in the next steps.

Step 1: Add the Repository

First, run the wget command to download the GPG key of PHP by running the command:

$ sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Step 2: Download the Repository of PHP

After downloading the GPG keys, install the PHP repository to the Debian 12 with the command:

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

Step 3: Update the Repository 

Now update the default repository of Debian 12 to add the new repository:

$ sudo apt update

Step 4: Upgrade the Packages

Run the upgrade to update the packages whose updates are downloaded with the command:

$ sudo apt upgrade -y

Step 5: Install the PHP

Now run the installation command of the PHP as shown:

$ sudo apt install php8.2 -y

Step 6: Verify the Installation

Display the version of the installed PHP with the following command:

$ php -v

The package of the PHP programming language has been successfully installed.

How to Test the PHP Script on Debian?

To test the PHP installation, create a simple PHP script using the text editor and run it on Debian 12. For example, use the nano text editor to create a new file with the my_php file with the following command:

$ sudo nano /var/www/html/my_php.php

Now type the simple PHP script to display a message:

<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>

        <?php
            echo "Welcome to ItsLinuxFoss";
        ?>

    </body>
</html>

Exit the nano text editor after saving the file with the CTRL+S shortcut key. Then restart the apache server or the installed server with the systemclt command:

$ sudo systemctl restart apache2

Now find the ip address of the computer by running the command:

$ ip a

Open the web browser and go to the following URL:

10.0.2.15/my_php.php

The web page has been displayed with the PHP programming language.

How to Uninstall the PHP on Debian?

To uninstall the PHP on Debian 12 with all its configuration files, run the command:

$ sudo apt purge php8.2 -y

Now remove all the unused dependencies of the PHP using the command:

$ sudo apt autoremove -y

Finally restart the apache2 server with the following command:

$ sudo systemctl restart apache2

Now confirm the uninstallation of the version of PHP by displaying its version:

$ php -v

PHP has successfully been uninstalled on Debian. 

Conclusion

To install the PHP on Debian 12, run the command “sudo apt install php libapache2-mod-php -y” in its terminal. Another installation method is by downloading the third party repository. This blog explained both the installation method for the PHP with the step by step guide. A simple PHP script is also used to test the installation of the PHP on Debian 12.

Join 10 Select Businesses Growing with Us! 500M+ Views Across Our Blogs & YouTube Channels – Be Part of Our Success. Click here to learn more. Click here if you are interested. ×