Quick Setup PHP
This is a quick guide on how to install PHP. The following assumes you are using Debian 11 and have a user account with sudo
privileges.
PHP versions on the Debian repo
As of writing, the Debian repos only have version 7.4. If you require a newer (8.0 - 8.1) or earlier (5.6-7.3) version of PHP, you can add the Sury Debian repo using the following commands:
sudo apt-get update
sudo apt-get -y install apt-transport-https lsb-release ca-certificates curl
sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt-get update
To install the version of your choice, append the version number you require to php
e.g. php8.1
, php7.3
. It is also the same for installing extensions e.g. php8.1-xml
, php7.3-xml
sudo apt install php8.1 php8.1-xml
Installing PHP with NGINX support
First update the repos and upgrade any packages before installing PHP:
sudo apt update
sudo apt upgrade
With NGINX you will need the PHP FastCGI Process Manager in order to handle PHP files as it doesn't have support built-in:
sudo apt install php php-fpm
Once installed, the PHP-FPM service will be started automatically. From there, you need to edit your server block in NGINX to process PHP files. Add the following code snippet into your server block:
server {
# ...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
Reload the NGINX service with sudo service nginx reload
to apply the changes. Then scroll down to Verify PHP Processing.
Installing PHP Extensions
You may need a few extensions to install in order to make your PHP program work. Here is a shorthand example to install multiple extensions:
sudo apt install php-{common,mbstring,gd,mysql,xml,curl,fpm}
Verify PHP Processing
To verify that PHP Processing is working, create a PHP file in the /var/www/html/
directory to display information about the current PHP configuration.
Using your favourite editor make a file called test.php
and enter the following contents:
<?php
phpinfo();
Then go to http://<IP-of-server>/test.php
and it should display the your PHP configuration as shown below.
If configuration page doesn't display, ensure the PHP version for the socket is correct in the NGINX server block and double check that the php-fpm.service
is enabled and is active.
You have successfully installed PHP as well as enabling PHP processing with NGINX.