Greenwebpage Community Blog Tutorials How to Install PHP on Debian 12
Tutorials

How to Install PHP on Debian 12

Linux, Windows, and Mac OS are among the many platforms on which PHP can run. PHP is compatible with a variety of databases and web servers, including MySQL, PostgreSQL, Apache, and NGINX. If you wish to set up a web server or start building online apps, installing PHP on Debian 12 offers several benefits. It enables users to dynamically generate web content by melding PHP scripts into HTML documents. In this article, I’ll show you how to install and properly configure PHP on Debian 12.

Let’s start with the most straightforward method.

How to Install PHP on Debian 12?

This section focuses on different ways of installing PHP on Debian 12. Through its default repositories, Debian 12 offers a package containing PHP 8.4. The following methods detail installing PHP on Debian 12:

Let’s begin with the easiest one.

Method 1: Using the Default Repository

Even though this method is stress-free, there is a potential chance that you may require a distinct PHP version. To install PHP on your Debian 12, within the given repository, follow the guidelines below:

Step 1: Update Debian Packages

Like all operating systems, software updates as well as security patches need to be regularly updated.

sudo apt update
sudo apt upgrade

Step 2: Install PHP

Now, for installing PHP on Debian 12, use the apt repository and execute the following command:

sudo apt install php

sudo apt install php

Note: Users have the freedom to choose the specific PHP version they want to install, for example, one may choose to install a specific PHP version:

sudo apt install php<version_name>

Step 3: Confirm PHP Installation

The installation can be checked and the version confirmed by the command with the “version” utility:

php –version

Note: Always ensure the versions of PHP being used are up-to-date and supported for maximum performance as well as security.

Remove/Uninstall PHP

When using Debian 12, you can uninstall PHP by executing the “autoremove” command with “apt”:

sudo apt autoremove php

That is all from this section.

Method 2: Using the PHP Official Website

This method lets users compile PHP from its source code and modify it to better suit their demands. Be “tar.gz” file accessible, to install PHP on Debian 12 follow these guidelines:

Step 1: Install Dependencies for PHP

Depending on what methods you want to use, some of you might prefer to compile PHP manually, and for that, I have provided the complete packages needed to do so.

sudo apt install cmake build-essential autoconf pkg-config bison libxml2-dev re2c libsqlite3-dev

Step 2: Download the Latest PHP Version

Start by downloading the appropriate version of PHP you are trying to download by visiting the official page for PHP releases:

wget https://www.php.net/distributions/php-8.4.7.tar.gz

Note: Make sure to double-check the official website to see if any newer versions have been released.

Step 3: Extract the tar.gz File

Next, the users have to do the extraction of a certain file tar.gz. For this purpose, type the tar command and the name of the package that was downloaded:

tar -xvzf php-8.4.7.tar.gz

Step 4: Configure PHP

Then, you have to set the options for PHP that need to be configured. For this purpose, execute the “./configure” command within the directory extracted previously:

cd php-8.4.7
./configure

Step 5: Install PHP

To install, also set the dependencies for PHP. With those dependencies, grant PHP permissions and give the installation:

sudo make install

Step 6: Add Path Environment

Now, the variable “PATH” needs to be defined where PHP has to be specified in the environment variable:

sudo nano ~/.bashrc

Thereafter, add the line of script found at the last section of the “.bashrc” script beneath:

export PATH=”$PATH:/usr/local/bin/php”

Step 7: Verify PHP Installation

It allows visualizing the version of PHP alongside its other details that are purposely hidden until the command “version” is run:

/usr/local/bin/php -v

Remove/Uninstall php

To delete/uninstall PHP from Debian 12, simply invoke the command “rm” with the following path specified:

sudo rm -rf /usr/local/bin/php

That is all from this section.

Method 3: Using a Sury PHP Repository

Let’s now enable the Sury PHP PPA repository, which contains PHP packages because it adds support for PHP 8.4, 7.3, 7.2, and 5.6 versions. For installing PHP on Debian 12 through the Sury PHP repository, use the following steps:

Step 1: Install Required Packages

For users to be able to install the PHP package from a third-party repository on Debian 12, they need to install the inadequate packages first:

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

Step 2: Add the PPA Repository

Earlier Steps, explained how to install a package or add a repository and also access for Sury PHP repository to get the latest packages for PHP, do the following commands to add the repository:

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

sudo wget -qO – https://packages.sury.org/php/apt.gpg | sudo gpg –no-default-keyring –keyring gnupg-ring:/etc/apt/trusted.gpg.d/debian-php-8.gpg –import

sudo chmod 644 /etc/apt/trusted.gpg.d/debian-php-8.gpg

Step 3: Update Debian Packages

Update the Debian packages first to be able to administer and install PHP packages from the repository added earlier:

sudo apt update

Step 4: Install PHP

At last, execute the command to install PHP 8.4, which is the latest version at the moment of writing, through the apt command:


sudo apt install php8.4


Step 5: Verify PHP Installation

Using the command of the version utility, verify the installation and use it to display the version of PHP:

php –version

How to Switch Between PHP Versions (if Needed)

In the case that users have multiple installed PHP versions, you can change the desired version through the command update-alternatives as below:

sudo update-alternatives –config php

That is all of the steps from this guide.

How to Run/Execute a PHP Script on Debian 12?

You need to procure a PHP interpreter along with a web server such as Apache or Nginx to execute a PHP script on Debian 12. For this purpose, both can be obtained by using the following commands:

sudo apt update
sudo apt install php libapache2-mod-php
sudo systemctl restart apache2

As a different route, you may utilize the built-in PHP web server for testing purposes:

php -S localhost:8000

To execute a PHP script on Debian 12 through a web server or from the command line, follow the next steps:

Step 1: Create a PHP File

To begin with, make sure you have PHP on the Debian 12 system. Then, enter any text editor of your choice, such as Nano, Vim, or gedit, to begin writing the code.

sudo nano greenwebpage.php

For example, write the following in a file “greenwebpage.php”:

<?php

echo “Green Webpage is the best hosting provider website”;

?>

Press the combination of keys “CTRL+C” to save the .php document and to exit, or opt for the “CTRL+X” keys:

Step 2: Run PHP Script

To run, type into the Terminal php greenwebpage.php. This will run the file called greenwebpage.php.

php greenwebpage.php

The output of the script will be printed.

Conclusion

Using the official PHP website is one of the most efficient methods of installing PHP on Debian 12. To install PHP on this platform, first check that system packages are updated and then install the necessary dependencies. From there, you can add the official PHP repository to the sources list, import the GPG key, install the required version of PHP, and finally set the configurations and run the necessary tests to check for successful installation.

Another method is to utilize the default package repositories, which would streamline the installation process, or even a third-party repository. The default ones do have PHP 8.4, but lack packages for PHP 8.2, 7.3, 7.2, and even 5.6. If users require any of those, they can just add the “Sury PHP repository,” which is sure to have current packages.

Frequently Asked Questions

Run sudo apt update && sudo apt install php to install the default PHP version available in the Debian repositories.
Use the command php -v to display the current PHP version installed on your system.
Add the SURY repository, then run sudo apt install php8.2. This lets you install newer or alternative versions.
The main php.ini file is typically located at /etc/php//apache2/php.ini for Apache or /etc/php//cli/php.ini for CLI.
Use sudo apt install php- (e.g., php-mysql, php-curl, php-gd) to install needed extensions.

Exit mobile version