How to install PHP 8.2 with Apache on Debian 11 Linux

Debian 11 is an excellent server OS to run the LAMP stack. PHP (Hypertext Preprocessor) is a widely-used open source general-purpose scripting language especially suited for web development. However, Debian 11 comes with nixcbz[failed to get contents] - contact @ [email protected] with the url. Here is how to install PHP 8.2 with Apache server on Debian 11 using the CLI or over ssh based session.

Installing PHP 8.2 with Apache on Debian 11 Linux

The procedure to install nixcbz[failed to get contents] - contact @ [email protected] with the url is as follows on Debian Linux version 11:

  1. First, nixcbz[failed to get contents] - contact @ [email protected] with the url box using the apt command or apt-get command. For example:
    $ sudo apt update
    $ sudo apt upgrade

    nixcbz[failed to get contents] - contact @ [email protected] with the url box if a new Linux kernel is installed using the reboot or shutdown command:
    $ sudo reboot
  2. Next, install the nixcbz[failed to get contents] - contact @ [email protected] with the url 2.4.xx as follows:
    $ sudo apt install apache2
    Installing Apache 2 on Debian 11 Linux using APT GET

    Installing Apache 2 on Debian 11 Linux using APT (click to enlarge)

    Verify that the Apache web server started and enabled at boot time using the systemctl command. For example:
    $ systemctl status apache2
    # Is the service enabled? #
    $ systemctl is-enabled apache2
    # Is the Apache responding to requests?
    $ systemctl is-enabled apache2

    Testing Debian 11 Apache 2 installation

    Testing the Apache 2 server installation (click to enlarge)

  3. You need to remove PHP version 7.4. It is possible to use both 7.4 and 8.2 same time, but it complicates configuration and is beyond the scope of this tutorial. Type the following command to list installed php versions:
    $ apt list --installed php\*
    You may see a list as follows:

    php-common/stable,now 2:76 all [installed]
    php-igbinary/stable,now 3.2.1+2.0.8-2 amd64 [installed]
    php-imagick/stable,now 3.4.4+php8.0+3.4.4-2+deb11u2 amd64 [installed]
    php-msgpack/stable,now 2.1.2+0.5.7-2 amd64 [installed]
    php-redis/stable,now 5.3.2+4.3.0-2+deb11u1 amd64 [installed]
    php7.4-cli/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-common/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-curl/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-fpm/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-gd/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-intl/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-json/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-mbstring/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-mysql/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-opcache/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-phpdbg/stable-security,now 7.4.33-1+deb11u3 amd64 [installed,automatic]
    php7.4-readline/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-xml/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-xmlrpc/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]
    php7.4-zip/stable-security,now 7.4.33-1+deb11u3 amd64 [installed]

    Remove those package names (run all commands as the root user):
    # mkdir /root/old.config/
    # copy PHP config files if needed later #
    # cp -avr /etc/php/* /root/old.config/
    # old_pkgs="$(dpkg --list | grep -E '^ii.*php' | awk '/^ii/{ print $2}')"
    # echo "Removing ${old_pkgs}"
    # apt --purge --remove $old_pkgs

  4. Enable https://deb.sury.org/ repo for PHP 8.2 as follows:
    $ sudo apt install lsb-release ca-certificates curl
    Use the nixcbz[failed to get contents] - contact @ [email protected] with the url to verify package. For example:
    $ sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
    Install the repo. Here is nixcbz[failed to get contents] - contact @ [email protected] with the url:
    $ 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'
    Refresh APT repo to install PHP 8.2. For example:
    $ sudo apt update
  5. Here is how to search and list PHP 8.2 packages:
    $ apt-cache search php8.2 | more
    $ apt-cache search php8.2 | grep gd
    $ apt-cache search php8.2 | grep -E 'gd|msyql|redis

    Outputs:

    php8.2-gd - GD module for PHP
    php8.2-gd-dbgsym - debug symbols for php8.2-gd
    php8.2-redis - PHP extension for interfacing with Redis
    php8.2-redis-dbgsym - debug symbols for php8.2-redis
  6. First, install PHP 8.2 Apache module named mod_php as follows:
    $ sudo apt install libapache2-mod-php8.2
    Installing PHP version 8.2 for Apache 2 on Debian 11 using APT GET

    Installing PHP version 8.2 for Apache 2 on Debian 11 (click to enlarge)

    Verify PHP version, type:
    $ /usr/bin/php -v

    PHP 8.2.4 (cli) (built: Mar 16 2023 14:37:38) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.2.4, Copyright (c) Zend Technologies
        with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies
    
  7. Open TCP ports # 80 and 443 using the ufw command on Debian 11:
    $ sudo ufw allow in "WWW Full"

    Related
    Also, check all our complete firewall tutorials for nixcbz[failed to get contents] - contact @ [email protected] with the url, nixcbz[failed to get contents] - contact @ [email protected] with the url, nixcbz[failed to get contents] - contact @ [email protected] with the url, nixcbz[failed to get contents] - contact @ [email protected] with the url, Ubuntu Linux version nixcbz[failed to get contents] - contact @ [email protected] with the url/nixcbz[failed to get contents] - contact @ [email protected] with the url/nixcbz[failed to get contents] - contact @ [email protected] with the url, and nixcbz[failed to get contents] - contact @ [email protected] with the url.

  8. To test your PHP installation create a new file named test.php in /var/www/html as follows:
    $ sudo vim /var/www/html
    Append the following code:

    <?php
    	phpinfo();
    ?>
    nixcbz[failed to get contents] - contact @ [email protected] with the url by pressing Esc+:+x and hit the [Enter] key. Fire a web browser and type url:
    http://your-ip/test.php
    http://1.2.3.4/test.php
    https://my-website-name-com/test.php

    PHP 8.2 running on Debian Linux 11 with Apache 2.4 server

    PHP 8.2 running on Debian Linux 11 with Apache 2.4 server (click to enlarge)

  9. You can now install the PHP extension of your choice. For example, I need PHP GD for graphics, and for MySQL database, I need MySQL extension. So here is how to install them:
    $ apt-cache search php8.2 | grep -E 'gd|mysql'
    $ sudo apt install php8.2-gd php8.2-mysql
  10. You can now create a Virtual Host for your website. For example, create a new DocumentRoot for your project:
    $ sudo mkdir -vp /var/www/cyberciti.biz/public_html
    nixcbz[failed to get contents] - contact @ [email protected] with the url, run:
    $ sudo useradd -d /var/www/cyberciti.biz/ -s /usr/sbin/nologin -c 'www.cyberciti.biz user' cbzuser
    $ sudo passwd -l cbzuser

    Create a new PHP/HTML file for test:
    $ sudo vim /var/www/cyberciti.biz/public_html/index.php
    Add some code

    <?php
     echo "This is a test file for www.cyberciti.biz";
    ?>

    Set up permission using the chmod command and chown command:
    $ sudo chown -R cbzuser:cbzuser /var/www/cyberciti.biz/
    $ sudo chmod 0444 /var/www/cyberciti.biz/
    $ sudo find /var/www/cyberciti.biz/ -type d -print0 | sudo xargs -0 -I {} chmod 0445 "{}"

    Create a new virtual host Apache config:
    $ sudo vim /etc/apache2/sites-available/www.cyberciti.biz.conf
    Append the following config:

    <Directory /var/www/cyberciti.biz/public_html>
            Require all granted
    </Directory>
    <VirtualHost *:80>
            ServerName cyberciti.biz
            ServerAlias www.cyberciti.biz
            ServerAdmin [email protected]
            DocumentRoot /var/www/cyberciti.biz/public_html
    	ErrorLog ${APACHE_LOG_DIR}/www.cyberciti.biz_error.log
    	CustomLog ${APACHE_LOG_DIR}/www.cyberciti.biz_access.log combined
    </VirtualHost>

    Enable the virtual host site:
    $ sudo a2ensite www.cyberciti.biz
    nixcbz[failed to get contents] - contact @ [email protected] with the url:
    $ sudo systemctl reload apache2
    That is all. Fire a web browser and type url:
    http://your-domain-name/index.php
    http://www.cyberciti.biz/index.php

  11. Optionally you may want to install MySQL/MariaDB server too. Here is how to install MariaDB 11:
    $ sudo apt install mariadb-server
    Secure it, run
    $ sudo mysql_secure_installation
    You can now log into mariadb server to create a new database and users as per your needs, type:
    $ sudo mariadb

Summing up

In this quick tutorial, you created a LAMP stack on Debian 11 using PHP version 8.2 to run web apps written in PHP. Do check the following resources:

About the author: Vivek Gite is Editor-in-Chief and the man behind nixCraft and OpensourceFlare ✨. He creates and maintains content on both sites as accurately as possible. Since 2000 Vivek has written over 7k+ posts that have been read many times. He is a die-hard fan of FLOSS and a full-time Linux desktop user since 1996. OpensourceFlare provides in-depth guides about Linux, BSD, programming, and other IT topics for Patreon subscribers without any ads or tracking. Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or weekly email newsletter.

0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. All comments must be on the topic, and offtopic comments will be automatically removed.