Part 4. How to install PHP on Debian 11?

Introduction

This page is part of a full tutorial on how to install a web werver on a virtual machine. This fourth part of the tutorial explains how to install PHP on our server. It assume virtual machine is created and Debian 11 is already installed with Apache. If not, please go to the home page and follow the guide.

Before installing PHP, update your system:

sudo apt update && sudo apt -y upgrade

Install PHP

PHP can easily be installed with synaptic. Run the following commands on your server terminal to install PHP:

sudo apt -y install php php-common

Check you PHP version with php -v:

student@debianwebserver:~$ php -v
PHP 7.4.33 (cli) (built: Nov  8 2022 11:40:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

PHP extensions

Here is the most common PHP extensions:

The following command install all the extensions listed previously. You can add or remove extensions according to your beeds.

sudo apt -y install php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath php-intl

Note that if you want to install a CMS like Wordpress or Prestashop, you will need additional extensions. Please refer to the installation recommendation for these CMS.

Using PHP with Apache

Diagram explaining the integration of PHP in Apache

To make PHP working with our Apache web server, we need to install libapache2_mod_php. Once installed, Apache can send requests to the PHP interpreter.

sudo apt -y install libapache2-mod-php

Check installation

To check the installation, let's create a PHP file that display the PHP configuration:

echo "<?php phpinfo() ?>" > ~/www/phpinfo.php

The file should be placed in the server root directory. In your browser, enter the IP of the server followed by /phpinfo.php. A page similar to this one should be displayed:

Web page displaying the PHP configuration

Permissions

PHP is working, but PHP script can't write files or folder in the www folder.

The following command displays the owner and group owner of the www folder. The www folder belongs to the user student and group student:

student@debianwebserver:~$ ls -l www/
total 176
-rw-r--r-- 1 student student     39 Dec  7 18:00 index.html

There are several ways to solve this problem. I choose the change the group owner of the folder for www-data. www-data is the group used by our Apache server. The following command keep the current user as the owner of the folder. But it set the group to www-data:

sudo chown -R username:www-data ~/www

-R stands for Recursive.

The group owner of the folder has changed for www-data :

student@debianwebserver:~$ ls -l www
total 176
-rw-r--r-- 1 student www-data     39 Dec  7 18:00 index.html

As you can see above, the group have read permission, but not write and execute permissions. Let's give these permissions to the group on the www directory:

sudo chmod -R g+wx ~/www/

The last problem is that new files will not hinerit from theses permissions. Fortunaltly, you can do this with the following command where we use s flag for this purpose.

sudo chmod -R g+s ~/www/

PHP is ready. Let's now install MariaDB (MySQL alternative).

See also


Last update : 12/13/2022