How to Install PHP 8.2 on Debian 11

How to Install PHP 8.2 on Debian 11

Are you trying to figure out how to install PHP 8.2 on Debian 11 so you can enhance your programming skills? Then you’ve come to the right place. For we’ll tackle everything, you need to know about this programming language.

PHP is a popular web development language. It is open source and free language generally used for web development on Linux platforms. This article will discuss all the necessary steps to install PHP 8.2 on Debian 11. 

Note: PHP is not available by default on the Debian repository. So you need to add the Ondřej Surý repository. 

It is the most up-to-date repository for PHP. We will start this guide with a brief introduction to PHP. To be followed with the basic steps for the installation. And we will wrap everything with how we can use PHP alongside Nginx and Apache.

To follow the steps discussed in this article, the following are the prerequisites:

  • You should have a system with Debian installed and running.
  • You should have root access to your system or an account with sudo privileges.

Introduction to PHP

PHP stands for hypertext preprocessor. It is one of the most used server-side programming languages. The program runs on the server and sends the output to the client for display on the browser. There is a large community of developers for PHP. 

You can use PHP to create websites for e-commerce, manage databases, and track sessions.

Note: PHP is a scripting language, and the PHP programs are interpreted at the server end.

How to Install PHP 8.2 on Debian 11

Following are the necessary steps to install PHP 8.2 on Debian 11.

Update your system

The very first step is to update your system. Open your Terminal and type the following command:

$ apt update


There may be an upgrade available. So, upgrade your system and reboot with the help of the following command:

$ apt upgrade -y && reboot


Add the ppa repository

Now download the CPG key with the help of the following commands:

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


Add SURY to the repository using the following command:

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


You must see the following output:

$ deb https://packages.sury.org/php/ buster main


Install PHP

Now, install the PHP with the help of the following command:

$ apt install php8.2 –y


Afterward, verify if you’ve installed PHP by typing the following command on your Terminal:

$ php –v


Install extensions

You can install PHP extensions to provide more functionality for PHP. To do so, input the following command:

$ apt install php8.2-<extension>


Some extensions you may want to install are cli, zip, MySQL, bz2, curl, mbstring, intl, and common. So, you can install these modules with the help of the following command:

$ sudo apt install php8.2-{cli,zip,mysql,bz2,curl,mbstring,intl,common}


You may want to see the list of PHP extensions installed on your computer. For this purpose, type the following command:

$ php –m


Using PHP with Nginx

For creating dynamic pages with PHP, you should use Nginx or Apache web server. Here, we are going to talk about using PHP with Nginx. For this purpose, you will need FastCGI Process Manager (PHP-FPM). PHP-FPM is a daemon that listens for incoming requests and runs in a separate process. 

Nginx behaves like a reverse proxy. It forwards the incoming request to PHP-FPM, which executes it. Follow the steps below for using PHP with Nginx.

Install Nginx, and PHP-FPM

Use the following command to install Nginx and PHP-FPM:

$ sudo apt install nginx php8.2-fpm


Configuration of Nginx

Now, we will edit the Nginx configuration file using our favorite editor:

$ sudo vim /etc/nginx/nginx.conf


Now, add the following block inside the HTTP block:

server {
listen 80;
server_name mysite.example.com;
root /var/www/mysite;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
}

To verify if the configuration is OK, use the following command:

$ sudo nginx –t


Using PHP with Apache

The Apache web server runs the PHP server code using the mod_php module. This module helps in running the PHP code within the PHP process. To use this, follow the steps below.

Installation of Apache and PHP

Install Apache and PHP using the following command:

$ sudo apt install apache2 libapache2-mod-php8.2


Enable mod_php module

Now, we will enable the mod_php module. This allows apache to process PHP code. For this purpose, use the following command:

$ sudo a2enmod php8.2


Restart the Apache web server

Finally, you should restart the apache web server using the following command:

$ sudo systemctl restart apache2


Testing PHP on an Apache web server

We will discuss how we can test PHP on the Apache web server. You must open ports 80 and 443 in your firewall. Now follow the steps below.

Create a test page

Create a test page in apache root using the following command:

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


Now, add the sample PHP code as follows:

<?PHP
phpinfo();
?>


Now, save the file and close the editor. To apply these changes, use the following command to restart the apache server:

$ sudo systemctl restart apache2


Browse the page

After creating a test page and restarting the apache web server, we will open the page on the browser. Open your favorite browser and point to the following location:

http://localhost/test.php


Or, you may provide the IP address if you use a different computer:

http://your-server/test.php


You should see the following page:

PHP Server window

In this article, we have discussed how to install PHP 8.2 on Debian 11. It is a web development language that is free and open source.

We provided the basic steps for the installation of PHP and extensions. We also discussed how we could use PHP with Nginx and apache. Finally, we have also discussed how to test the PHP with apache by creating a test page and browsing the page. We hope you have enjoyed the article.

If this guide helped you, please share it. 

Related Posts