This short tutorial will explain how to install the latest PHP 8 on Debian 10,
PHP 8 comes with a lot of new features and improvements, and some of the deprecated extensions which you should take a look at before moving to PHP 8. For example, xmlrpc moved to PECL, sury repository doesn’t provide these extensions.
Install Dependency
Install dependencies
sudo apt install apt-transport-https lsb-release ca-certificates wget curl -y
Install Sury Repository
We’ll use 3rd party repository to install PHP 8, even the maintener is the one who maintenance official PHP repository on Debian since PHP 5. Install Sury PHP repository for Debian 10
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
Install PHP GPG key of Sury
curl -sSL -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
Update Repository Metadata
sudo apt update
Install PHP 8
Time to install the PHP 8 on our Debian 10, you can use multiple version of Debian on 1 server, we’ll talk about that later
sudo apt install php8.0 php8.0-fpm php8.0-curl php8.0-mysqlnd php8.0-zip php8.0-mbstring php8.0-bcmath -y
Check PHP 8 FPM
sudo systemctl status php8.0-fpm
check current installed PHP 8 version
$ php8.0 -v PHP 8.0.0 (cli) (built: Nov 30 2020 20:40:07) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies
How to use PHP 8.0 with Nginx
to switch between PHP version, on nginx you only need to change the PHP Handler, let say the current server block has PHP 7.3
location ~ \.php(?:$|/) { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param HTTPS on; fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice fastcgi_pass unix:/run/php/php7.3-fpm.sock; }
change the fastcgi_pass to PHP 8
location ~ \.php(?:$|/) { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param HTTPS on; fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice fastcgi_pass unix:/run/php/php8.0-fpm.sock; }
validate the configuration and restart nginx
sudo nginx -t && sudo systemctl restart nginx