{"id":17188,"date":"2023-06-13T22:07:56","date_gmt":"2023-06-13T19:07:56","guid":{"rendered":"https:\/\/kifarunix.com\/?p=17188"},"modified":"2024-03-10T10:00:00","modified_gmt":"2024-03-10T07:00:00","slug":"how-to-install-php-8-on-debian-12","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-install-php-8-on-debian-12\/","title":{"rendered":"How to Install PHP 8 on Debian 12"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to install PHP 8 on <a href=\"https:\/\/www.debian.org\/News\/2023\/20230610\" target=\"_blank\" rel=\"noreferrer noopener\">Debian 12.<\/a> <em><a href=\"https:\/\/www.php.net\/releases\/8.0\/en.php\" target=\"_blank\" rel=\"noreferrer noopener\">PHP 8.x<\/a> is a major update of the PHP language. It contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#installing-php-8-on-debian-12\">Installing PHP 8 on Debian 12<\/a><ul><li><a href=\"#install-php-8-2-on-debian-12\">Install PHP 8.2 on Debian 12<\/a><\/li><li><a href=\"#install-php-8-2-modules\">Install PHP 8.2 Modules<\/a><\/li><li><a href=\"#install-php-7-x-8-0-8-1-on-debian-12\">Install PHP 7.x\/8.0\/8.1 on Debian 12<\/a><ul><li><a href=\"#install-sury-apt-repository\">Install SURY APT Repository<\/a><\/li><li><a href=\"#install-php-7-x-on-debian-12\">Install PHP 7.x on Debian 12<\/a><\/li><li><a href=\"#install-php-8-0-on-debian-12\">Install PHP 8.0 on Debian 12<\/a><\/li><li><a href=\"#install-php-8-1-on-debian-12\">Install PHP 8.1 on Debian 12<\/a><\/li><li><a href=\"#disable-sury-repos\">Disable SURY repos<\/a><\/li><\/ul><\/li><li><a href=\"#testing-php-execution-on-linux\">Testing PHP Execution on Linux<\/a><\/li><li><a href=\"#how-to-set-default-php-version-on-debian\">How to Set Default PHP Version on Debian<\/a><\/li><li><a href=\"#other-tutorials\">Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"installing-php-8-on-debian-12\">Installing PHP 8 on Debian 12<\/h2>\n\n\n\n<p>Different applications requires different versions of PHP. In Linux, you can be able to install any version of PHP that your application may require.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-php-8-2-on-debian-12\">Install PHP 8.2 on Debian 12<\/h3>\n\n\n\n<p>The default Debian 12 repositories provide PHP 8.2;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apt-cache policy php<\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\nphp:\n  Installed: (none)\n  Candidate: 2:8.2+93\n  Version table:\n     2:8.2+93 500\n        500 http:\/\/deb.debian.org\/debian bookworm\/main amd64 Packages\n<\/code><\/pre>\n\n\n\n<p>Thus, to installing PHP 8 on Debian 12, simply execute the commands below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt update<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install php<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nReading package lists... Done\nBuilding dependency tree... Done\nReading state information... Done\nThe following additional packages will be installed:\n  apache2 apache2-data apache2-utils libapache2-mod-php8.2 php-common php8.2 php8.2-cli php8.2-common php8.2-opcache php8.2-readline\nSuggested packages:\n  apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear\nThe following NEW packages will be installed:\n  apache2 apache2-data apache2-utils libapache2-mod-php8.2 php php-common php8.2 php8.2-cli php8.2-common php8.2-opcache php8.2-readline\n0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 5,047 kB of archives.\nAfter this operation, 23.1 MB of additional disk space will be used.\nDo you want to continue? [Y\/n] y\n<\/code><\/pre>\n\n\n\n<p>Type y and press enter to proceed with installation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -v<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nPHP 8.2.5 (cli) (built: Apr 27 2023 08:13:47) (NTS)\nCopyright (c) The PHP Group\nZend Engine v4.2.5, Copyright (c) Zend Technologies\n    with Zend OPcache v8.2.5, Copyright (c), by Zend Technologies\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-php-8-2-modules\">Install PHP 8.2 Modules<\/h3>\n\n\n\n<p>You can install any PHP 8.2 module by executing the command below, replacing the EXTENSION with specific PHP module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install php-EXTENSION<\/code><\/pre>\n\n\n\n<p>For example, you can install PHP MySQLi extension using the command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install php-mysql<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-php-7-x-8-0-8-1-on-debian-12\">Install PHP 7.x\/8.0\/8.1 on Debian 12<\/h3>\n\n\n\n<p>Similarly, it is possible to literally install any PHP version as long as you have the correct package repositories in place.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"install-sury-apt-repository\">Install SURY APT Repository<\/h4>\n\n\n\n<p>To install PHP 7.x\/8.0\/8.1 on Debian 12, you need to install SURY, third party repositories that provides PHP packages as follows;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt update<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>apt -y install apt-transport-https lsb-release ca-certificates curl wget gnupg2<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>wget -qO- https:\/\/packages.sury.org\/php\/apt.gpg | \\\ngpg --dearmor &gt; \/etc\/apt\/trusted.gpg.d\/sury-php-x.x.gpg<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>sh -c 'echo \"deb https:\/\/packages.sury.org\/php\/ $(lsb_release -sc) main\" \\\n&gt; \/etc\/apt\/sources.list.d\/php.list'<\/code><\/pre>\n\n\n\n<p>Re-synchronize your system packages to the latest versions;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apt update<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"install-php-7-x-on-debian-12\">Install PHP 7.x on Debian 12<\/h4>\n\n\n\n<p>PHP 7.x is now available for installation;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-cache policy php7.4<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\nphp7.4:\n  Installed: (none)\n  Candidate: 1:7.4.33-6+0~20230609.83+debian12~1.gbp32abef\n  Version table:\n     1:7.4.33-6+0~20230609.83+debian12~1.gbp32abef 500\n        500 https:\/\/packages.sury.org\/php bookworm\/main amd64 Packages\n<\/code><\/pre>\n\n\n\n<p>Thus, you can install PHP 7.x from SURY repos.<\/p>\n\n\n\n<p>For example to install PHP 7.4;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install php7.4<\/code><\/pre>\n\n\n\n<p>For modules, do (replace EXTENSION with respective PHP module);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install php7.4-EXNTENSION<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"install-php-8-0-on-debian-12\">Install PHP 8.0 on Debian 12<\/h4>\n\n\n\n<p>To install PHP 8.0 on Debian 12, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apt install php8.0<\/pre>\n\n\n\n<p>This command will install PHP 8.0 along with other required package dependencies.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"install-php-8-1-on-debian-12\">Install PHP 8.1 on Debian 12<\/h4>\n\n\n\n<p>To install PHP 8.1 on Debian 12, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apt install php8.1<\/pre>\n\n\n\n<p>You can confirm the installed version;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -v<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"disable-sury-repos\">Disable SURY repos<\/h4>\n\n\n\n<p>Once you are done, you can disable SURY repos;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed -i 's\/^\/#\/' \/etc\/apt\/sources.list.d\/php.list<\/code><\/pre>\n\n\n\n<p>Replace the path to the source list accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"testing-php-execution-on-linux\">Testing PHP Execution on Linux<\/h3>\n\n\n\n<p>To confirm the execution of PHP, you can create a simple PHP test file and place it in your web server root directory.<\/p>\n\n\n\n<p>For example, in apache;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo '\n&lt;?php\nphpinfo();\n?&gt;\n' &gt; \/var\/www\/html\/test.php<\/code><\/pre>\n\n\n\n<p>Next, restart Apache;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl restart apache2<\/code><\/pre>\n\n\n\n<p>If using Nginx, ensure PHP-FPM is installed and configure Nginx to process PHP by updating your site config;<\/p>\n\n\n\n<p>For example, we are using the default site config;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/nginx\/sites-available\/default<\/code><\/pre>\n\n\n\n<p>Next, configure Nginx to process PHP by updating the <code>location ~ .php$<\/code> section accordingly.<\/p>\n\n\n\n<p>In the most basic way, this is the config section to update;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nlocation ~ \\.php$ {\n\t\tinclude snippets\/fastcgi-php.conf;\n\t\tfastcgi_pass unix:\/run\/php\/phpX.X-fpm.sock;\n\t}\n\n<\/code><\/pre>\n\n\n\n<p>Replace the following line with the specific address your Nginx is configured to accept FastCGI requests<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unix:\/run\/php\/phpX.X-fpm.sock;<\/code><\/pre>\n\n\n\n<p>You can get the specific address as follows;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -E '^listen' \/etc\/php\/8.2\/fpm\/pool.d\/www.conf<\/code><\/pre>\n\n\n\n<p>Replace 8.3 on that path with your current PHP version number.<\/p>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>listen = \/run\/php\/php8.2-fpm.sock<\/strong>\nlisten.owner = www-data\nlisten.group = www-data<\/code><\/pre>\n\n\n\n<p>So we will update our Nginx config as follows;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nlocation ~ \\.php$ {\n\t\tinclude snippets\/fastcgi-php.conf;\n\t\tfastcgi_pass <strong>unix:\/run\/php\/php8.2-fpm.sock;<\/strong>\n\t}\n\n<\/code><\/pre>\n\n\n\n<p>Restart Nginx;<\/p>\n\n\n\n<p>Then access PHP test page using the address <strong><code>http:\/\/server-IP\/test.php<\/code><\/strong>;<\/p>\n\n\n\n<p>Sample PHP page processed by Apache;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1179\" height=\"919\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/php-test-page.png\" alt=\"How to Install PHP 8 on Debian 12\" class=\"wp-image-17202\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/php-test-page.png?v=1686681821 1179w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/php-test-page-768x599.png?v=1686681821 768w\" sizes=\"(max-width: 1179px) 100vw, 1179px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-set-default-php-version-on-debian\">How to Set Default PHP Version on Debian<\/h3>\n\n\n\n<p>You can set the default PHP version using the <code>update-alternatives<\/code> command.<\/p>\n\n\n\n<p>If you have multiple versions of PHP installed, there could be some situations that might make you choose a default version to use.<\/p>\n\n\n\n<p>You can list the paths of the installed PHP versions by running the command <code>update-alternatives --list php<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update-alternatives --list php<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/bin\/php7.4\n\/usr\/bin\/php8.1\n\/usr\/bin\/php8.2<\/code><\/pre>\n\n\n\n<p>Thus, to set the default PHP version, you can use the <code>update-alternatives<\/code> command with the <code>--config<\/code> option followed by the path of the desired PHP version. For example, to set PHP version 8.1 as the default PHP version, run the command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update-alternatives --set php \/usr\/bin\/php8.1<\/code><\/pre>\n\n\n\n<p>You can also run the command interactively by omitting the path to PHP;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update-alternatives --config php<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nThere are 3 choices for the alternative php (providing \/usr\/bin\/php).\n\n  Selection    Path             Priority   Status\n------------------------------------------------------------\n  0            \/usr\/bin\/php8.2   82        auto mode\n  1            \/usr\/bin\/php7.4   74        manual mode\n* 2            \/usr\/bin\/php8.1   81        manual mode\n  3            \/usr\/bin\/php8.2   82        manual mode\n<\/code><\/pre>\n\n\n\n<p>To set a specific version of PHP as the default version, simply choose a number and press ENTER.<\/p>\n\n\n\n<p>For example, in the above prompt, you can enter 2 and press ENTER to set PHP 8.1 as the default version;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nThere are 3 choices for the alternative php (providing \/usr\/bin\/php).\n\n  Selection    Path             Priority   Status\n------------------------------------------------------------\n  0            \/usr\/bin\/php8.2   82        auto mode\n  1            \/usr\/bin\/php7.4   74        manual mode\n* 2            \/usr\/bin\/php8.1   81        manual mode\n  3            \/usr\/bin\/php8.2   82        manual mode\n\nPress <enter> to keep the current choice[*], or type selection number: 2\n<\/code><\/pre>\n\n\n\n<p>Confirm the version;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -v<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\nPHP 8.1.20 (cli) (built: Jun  9 2023 07:44:56) (NTS)\nCopyright (c) The PHP Group\nZend Engine v4.1.20, Copyright (c) Zend Technologies\n    with Zend OPcache v8.1.20, Copyright (c), by Zend Technologies\n<\/code><\/pre>\n\n\n\n<p>That is all on installing PHP 8 on Debian 12.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-tutorials\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-debian-12-on-virtualbox\/\">Install Debian 12 on VirtualBox<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to install PHP 8 on Debian 12. PHP 8.x is a major update of the PHP language. It<\/p>\n","protected":false},"author":10,"featured_media":17208,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,299,342,938],"tags":[6859,6872,6874,6871,6873],"class_list":["post-17188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-database","category-php","category-phpmyadmin","tag-debian-12","tag-debian-12-php-8","tag-install-php-8-on-debian-12","tag-install-php-on-debian-12","tag-php-7-4-debian-12","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/17188"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=17188"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/17188\/revisions"}],"predecessor-version":[{"id":20786,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/17188\/revisions\/20786"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/17208"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=17188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=17188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=17188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}