PHP is an open-source programming language used for web development, created by Rasmus Lerdorf. It is an HTML-embedded scripting language for creating dynamic web sites.
PHP 7.3 is the default version of PHP for installation on Debian 9 Stretch server.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related PHP queries.
In this context, we shall look into how to install PHP 7.3 or 7.2 on Debian 9 system.
Before we begin this installation procedure, you need Debian 9 server with a non-root user account and make sure that you have full root access.
Then follow the steps given below.
1. Start by updating the apt package list and install the dependencies necessary to add a new repository over HTTPS:
$ sudo apt-get update
$ sudo apt-get upgrade
2. Let's execute the following commands to install the required packages first on your system:
$ sudo apt install ca-certificates apt-transport-https
$ wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
$ echo "deb https://packages.sury.org/php/ stretch main" | sudo tee /etc/apt/sources.list.d/php.list
Once your system is fully up to date, you can proceed to the next step.
Now you can install latest version of PHP on your server.
Run the following commands to install PHP 7.3 on Debian 9:
$ sudo apt install php7.3
We can extend the core functionality of PHP by installing additional extensions. PHP extensions are available as packages and can be easily install by following commands :
$ sudo apt install php7.3-cli php7.3-common php7.3-curl php7.3-mbstring php7.3-mysql php7.3-xml
Same way, you can install other versions of PHP on your server.
Run the following commands to install PHP 7.2 on Debian 9:
$ sudo apt install php7.2
To install php 7.2 modules, run following commands :
$ sudo apt install php7.2-cli php7.2-common php7.2-curl php7.2-mbstring php7.2-mysql php7.2-xml
You can verify php version by running the following command which will print the PHP version:
$ php -v
Then, you will get the following output:
PHP 7.3.3-1+0~20190307202245.32+stretch~1.gbp32ebb2 (cli) (built: Mar 7 2019 20:22:46) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.3, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.3-1+0~20190307202245.32+stretch~1.gbp32ebb2, Copyright (c) 1999-2018, by Zend Technologies
If you are using Apache web server to install PHP then you need to install Apache PHP modules by following commands :
$ sudo apt install php7.3 libapache2-mod-php
After that, you need to restart Apache service to enable the php modules :
$ sudo systemctl restart apache2
Since Nginx does not contain native PHP processing like some other web servers, so we will need to install fpm ("fastCGI process manager") which will handle PHP files:
$ sudo apt install php7.3-fpm
Once installation is complete the fpm service will start automatically. You can also check status by executing below command :
$ sudo systemctl status php7.3-fpm
Now edit your website nginx server block and add following lines to so Nginx can process PHP files :
server {
#... other code
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
#... other code
}
To take effect of new configuration need to restart Nginx service by following command :
$ sudo systemctl restart nginx
Both Apache and Nginx do not support processing of PHP files by default when the browser requests the PHP page. Therefore, we need to install the PHP package on the server to support PHP files.
You can install a below package with Apache webserver to support PHP. This package provides the PHP modules for Apache 2 web server. Change PHP version, if required
$ sudo apt install -y apache2 libapache2-mod-php7.4
Nginx does not have a PHP modules package to support PHP. But, we can use the PHP FastCGI Process Manager to handle PHP requests. Change PHP version, if required
$ sudo apt install -y php7.4-fpm
Once you have installed FastCGI manager, add socket details in Nginx virtual host:
server {
# other codes
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
This article covers how to install PHP 7.3 on Debian 9 (Stretch) and Debian 8 (Jessie). With PHP 7.3 comes a number of bug fixes, new functionalities and features as well as a number of deprecations.
To Set Default PHP Version on Debian ?
You can set the default PHP version with the below command incase your system has multiple PHP versions. Change php7.3 with the version you want to set as the default PHP version:
$ sudo update-alternatives --set php /usr/bin/php7.3