How to Install PHP on Debian 12?
PHP can run on various platforms, including Linux, Windows, and Mac OS. PHP can interact with many databases and web servers, such as MySQL, PostgreSQL, Apache, and Nginx.
Installing PHP on Debian 12 serves several purposes, especially if you’re setting up a web server or developing web applications. It allows users to create web pages dynamically by embedding PHP scripts within HTML.
To use PHP on the Debian 12 system, first install it. This article will demonstrate different methods to install PHP on Debian 12 and configure it for optimal performance.
Let’s begin with the installation.
How to Install PHP on Debian 12?
The Debian 12 contains a PHP 8.3 package in default repositories. To install PHP on Debian 12, follow the below methods:
- Method 1: Using the Default Repository
- Method 2: Using the PHP Official Website
- Method 3: Using a Third-Party Repository
Let’s begin with the easiest one.
Method 1: Install PHP on Debian 12 Using the Default Repository
This method is easy and convenient, but you may need a different version of PHP. To install PHP on Debian 12 using the default repository, follow the below steps:
Step 1: Update Debian Packages
Before installation, the Debian system requires to be up-to-date with the latest software updates as well as security patches:
sudo apt update sudo apt upgrade
Step 2: Install PHP
Now, install PHP on Debian 12 from the default repository with the following command:
sudo apt install php
Note: Users can install the specific PHP version that suits their needs. For instance, installing PHP 8.2 version:
sudo apt install php8.2
Step 3: Verify PHP Installation
Users can confirm the installation and check the PHP version with the command, via the “version” utility:
php --version
Switch Between PHP Versions (if needed)
If you have multiple PHP versions installed, use the “update-alternatives” command to switch between them:
sudo update-alternatives --set php /usr/bin/php<version_number>
Note: Use up-to-date as well as supported versions of PHP for the best performance and security.
Remove/Uninstall PHP
To remove/uninstall PHP on Debian 12, utilize the “remove” option along with the “apt” command:
sudo apt autoremove php
You have successfully installed PHP on Debian 12 using the default repository. You can now use PHP to create dynamic web applications or run scripts from the command line.
Method 2: Install PHP on Debian 12 Using the PHP Official Website
This method allows users to compile PHP from the source and customize it according to their needs. To install PHP on Debian 12 using the tar.gz file, you need to follow these steps:
Step 1: Install Dependencies for PHP
Users are required to install some dependent packages for PHP, and some libraries. For this, execute the below commands to install them:
sudo apt install cmake build-essential autoconf pkg-config bison libxml2-dev re2c libsqlite3-dev
Step 2: Download the Latest PHP Version
Next, users need to download the latest version of PHP from the official website. On-time of this guide, the latest version is 8.3.3:
wget https://www.php.net/distributions/php-8.3.3.tar.gz
Note: Visit the official website and choose the PHP version users want to install.
Step 3: Extract the tar.gz File
Then, users need to extract the tar.gz file. For this, use the tar command by specifying the downloaded package name:
tar -xvzf php-8.3.3.tar.gz
Step 4: Configure PHP
Now, you need to configure PHP with the options. For this, utilize the “./configure” command in the extracted directory:
cd php-8.3.3 ./configure
Step 5: Install PHP
To install its dependencies, use the “make install” command with the “sudo” privileges:
sudo make install
Step 6: Add Path Environment
Now, users need to add the PHP path to the environment variable “PATH”:
sudo nano ~/.bashrc
After that, add the script line at the end of the “.bashrc” file below:
export PATH="$PATH:/usr/local/bin/php"
Step 7: Verify PHP Installation
Users can verify that PHP is installed by running the “version” utility. It visualizes the PHP version and some additional information:
php --version
Remove/Uninstall php
To remove/uninstall php from Debian 12, use the “rm” command by specifying the path:
sudo rm -rf /usr/local/bin/php
That is all from this section.
Method 3: Install PHP on Debian 12 Using a Third-Party Repository
Now let’s add the SURY PHP PPA repository, which provides up-to-date PHP packages. This repository includes PHP 8.1, 7.3, 7.2, and 5.6 versions. To install PHP on Debian 12 via the third-party repository, follow the below steps:
Step 1: Install Dependencies
Users need to install the required packages before installing the PHP package on Debian 12 from a third-party repository:
sudo apt install apt-transport-https lsb-release ca-certificates wget
Step 2: Add the SURY PHP PPA Repository
To access up-to-date PHP packages, add a third-party (SURY PHP) repository. For instance, add the repository by executing the below commands:
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
Now, users need to update the Debian packages to install the PHP package from the added repository:
sudo apt update
Step 4: Install PHP
Finally, install php8.3 (the latest one at the time of writing) using the apt command:
sudo apt install php8.3
Step 5: Verify PHP Installation
Users can confirm the installation and check the PHP version with the command, via the “version” utility:
php --version
Switch Between PHP Versions (if Needed)
If users have several PHP versions installed, switch between them via the “update-alternatives” command as below:
sudo update-alternatives --config php
That is all from the guide.
How to Run a PHP Script on Debian 12?
To run a PHP script on Debian 12, install the PHP interpreter and a web server such as Apache or Nginx. For this, use the following commands to install them:
sudo apt update sudo apt install php libapache2-mod-php sudo systemctl restart apache2
Alternatively, use the PHP built-in web server for testing purposes:
php -S localhost:8000
Once you have the web server running, create a PHP file with the .php extension and place it in the web server’s document root, usually /var/www/html for Apache or /usr/share/nginx/html for Nginx.
To run a PHP script on Debian 12 from the web server or command line, follow these steps:
Step 1: Create a PHP File
First, ensure you have PHP installed on Debian 12. After that, use any text editor to write the PHP code, such as Nano, vim, or gedit. For instance, create a file called “linuxgenie.php” with the following content:
sudo nano linuxgenie.php
<?php echo "Linuxgenie is empowering linux users"; ?>
Save the .php file by pressing the “CTRL+C” and exit via the “CTRL+X” keys:
Step 2: Run PHP Script
To run the script, open your web browser and navigate to the URL of the file, such as http://localhost/linuxgenie.php or http://localhost:8000/linuxgenie.php as below:
For the Terminal, type the “php” by specifying the file name such as “linuxgenie.php” and run it:
php linuxgenie.php
It displays the output of the script.
Conclusion
One of the easiest ways to install PHP on Debian 12 is to use the official PHP website, which provides pre-compiled packages for various Linux distributions. To install PHP on Debian 12, update system packages, install the dependencies, add the official PHP repository to the sources list, and import the GPG key. Finally, install the PHP version, configure PHP settings, and test your installation.
To install PHP on Debian 12, use the default package repositories or a third-party repository. The default repositories contain PHP 8.3 packages, but not other versions like PHP 8.2, 7.3, 7.2, or 5.6. If users need a different version, add the “Sury PHP repository”, for up-to-date packages.