Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\nInstalling PHP 8 on Debian 12<\/h2>\n\n\n\n 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
Install PHP 8.2 on Debian 12<\/h3>\n\n\n\n The default Debian 12 repositories provide PHP 8.2;<\/p>\n\n\n\n
apt-cache policy php<\/pre>\n\n\n\n\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\nThus, to installing PHP 8 on Debian 12, simply execute the commands below;<\/p>\n\n\n\n
apt update<\/code><\/pre>\n\n\n\napt install php<\/code><\/pre>\n\n\n\n\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\nType y and press enter to proceed with installation.<\/p>\n\n\n\n
php -v<\/code><\/pre>\n\n\n\n\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\nInstall PHP 8.2 Modules<\/h3>\n\n\n\n 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
apt install php-EXTENSION<\/code><\/pre>\n\n\n\nFor example, you can install PHP MySQLi extension using the command below;<\/p>\n\n\n\n
apt install php-mysql<\/code><\/pre>\n\n\n\nInstall PHP 7.x\/8.0\/8.1 on Debian 12<\/h3>\n\n\n\n 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
Install SURY APT Repository<\/h4>\n\n\n\n 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
apt update<\/code><\/pre>\n\n\n\napt -y install apt-transport-https lsb-release ca-certificates curl wget gnupg2<\/code><\/pre>\n\n\n\nwget -qO- https:\/\/packages.sury.org\/php\/apt.gpg | \\\ngpg --dearmor > \/etc\/apt\/trusted.gpg.d\/sury-php-x.x.gpg<\/code><\/pre>\n\n\n\nsh -c 'echo \"deb https:\/\/packages.sury.org\/php\/ $(lsb_release -sc) main\" \\\n> \/etc\/apt\/sources.list.d\/php.list'<\/code><\/pre>\n\n\n\nRe-synchronize your system packages to the latest versions;<\/p>\n\n\n\n
apt update<\/pre>\n\n\n\nInstall PHP 7.x on Debian 12<\/h4>\n\n\n\n PHP 7.x is now available for installation;<\/p>\n\n\n\n
apt-cache policy php7.4<\/code><\/pre>\n\n\n\n\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\nThus, you can install PHP 7.x from SURY repos.<\/p>\n\n\n\n
For example to install PHP 7.4;<\/p>\n\n\n\n
apt install php7.4<\/code><\/pre>\n\n\n\nFor modules, do (replace EXTENSION with respective PHP module);<\/p>\n\n\n\n
apt install php7.4-EXNTENSION<\/code><\/pre>\n\n\n\nInstall PHP 8.0 on Debian 12<\/h4>\n\n\n\n To install PHP 8.0 on Debian 12, run the command below;<\/p>\n\n\n\n
apt install php8.0<\/pre>\n\n\n\nThis command will install PHP 8.0 along with other required package dependencies.<\/p>\n\n\n\n
Install PHP 8.1 on Debian 12<\/h4>\n\n\n\n To install PHP 8.1 on Debian 12, run the command below;<\/p>\n\n\n\n
apt install php8.1<\/pre>\n\n\n\nYou can confirm the installed version;<\/p>\n\n\n\n
php -v<\/code><\/pre>\n\n\n\nDisable SURY repos<\/h4>\n\n\n\n Once you are done, you can disable SURY repos;<\/p>\n\n\n\n
sed -i 's\/^\/#\/' \/etc\/apt\/sources.list.d\/php.list<\/code><\/pre>\n\n\n\nReplace the path to the source list accordingly.<\/p>\n\n\n\n
Testing PHP Execution on Linux<\/h3>\n\n\n\n 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
For example, in apache;<\/p>\n\n\n\n
echo '\n<?php\nphpinfo();\n?>\n' > \/var\/www\/html\/test.php<\/code><\/pre>\n\n\n\nNext, restart Apache;<\/p>\n\n\n\n
systemctl restart apache2<\/code><\/pre>\n\n\n\nIf using Nginx, ensure PHP-FPM is installed and configure Nginx to process PHP by updating your site config;<\/p>\n\n\n\n
For example, we are using the default site config;<\/p>\n\n\n\n
vim \/etc\/nginx\/sites-available\/default<\/code><\/pre>\n\n\n\nNext, configure Nginx to process PHP by updating the location ~ .php$<\/code> section accordingly.<\/p>\n\n\n\nIn the most basic way, this is the config section to update;<\/p>\n\n\n\n
\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\nReplace the following line with the specific address your Nginx is configured to accept FastCGI requests<\/p>\n\n\n\n
unix:\/run\/php\/phpX.X-fpm.sock;<\/code><\/pre>\n\n\n\nYou can get the specific address as follows;<\/p>\n\n\n\n
grep -E '^listen' \/etc\/php\/8.2\/fpm\/pool.d\/www.conf<\/code><\/pre>\n\n\n\nReplace 8.3 on that path with your current PHP version number.<\/p>\n\n\n\n
Sample output;<\/p>\n\n\n\n
listen = \/run\/php\/php8.2-fpm.sock<\/strong>\nlisten.owner = www-data\nlisten.group = www-data<\/code><\/pre>\n\n\n\nSo we will update our Nginx config as follows;<\/p>\n\n\n\n
\nlocation ~ \\.php$ {\n\t\tinclude snippets\/fastcgi-php.conf;\n\t\tfastcgi_pass unix:\/run\/php\/php8.2-fpm.sock;<\/strong>\n\t}\n\n<\/code><\/pre>\n\n\n\nRestart Nginx;<\/p>\n\n\n\n
Then access PHP test page using the address http:\/\/server-IP\/test.php<\/code><\/strong>;<\/p>\n\n\n\nSample PHP page processed by Apache;<\/p>\n\n\n\n <\/figure>\n\n\n\nHow to Set Default PHP Version on Debian<\/h3>\n\n\n\n You can set the default PHP version using the update-alternatives<\/code> command.<\/p>\n\n\n\nIf 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
You can list the paths of the installed PHP versions by running the command update-alternatives --list php<\/code>.<\/p>\n\n\n\nupdate-alternatives --list php<\/code><\/pre>\n\n\n\nSample output;<\/p>\n\n\n\n
\/usr\/bin\/php7.4\n\/usr\/bin\/php8.1\n\/usr\/bin\/php8.2<\/code><\/pre>\n\n\n\nThus, to set the default PHP version, you can use the update-alternatives<\/code> command with the --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\nupdate-alternatives --set php \/usr\/bin\/php8.1<\/code><\/pre>\n\n\n\nYou can also run the command interactively by omitting the path to PHP;<\/p>\n\n\n\n
update-alternatives --config php<\/code><\/pre>\n\n\n\n\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\nTo set a specific version of PHP as the default version, simply choose a number and press ENTER.<\/p>\n\n\n\n
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
\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 to keep the current choice[*], or type selection number: 2\n<\/code><\/pre>\n\n\n\nConfirm the version;<\/p>\n\n\n\n
php -v<\/code><\/pre>\n\n\n\n\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\nThat is all on installing PHP 8 on Debian 12.<\/p>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n Install Debian 12 on VirtualBox<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"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}]}}