How to Install PHP 8 on Debian 12: A Step-by-Step Guide

With the constant evolution of web technologies, keeping your server’s software up to date is crucial for security, performance, and compatibility. PHP, being one of the most popular scripting languages for web development, releases new versions offering improvements and new features regularly. If you’re running a Debian 12 system and looking to upgrade or install PHP 8, you’ve come to the right place. This guide will walk you through the process step by step.

Before You Begin

Before diving into the installation process, ensure your Debian system is up to date. This step is crucial for security and compatibility reasons. Open your terminal and execute the following commands:

sudo apt update sudo apt upgrade -y

This process updates the list of available packages and their versions and then upgrades the installed packages to their latest versions.

Installing Required Software

To add new repositories securely and manage your packages effectively, you’ll need some essential tools. If not already installed, you can get them by running:

sudo apt install -y software-properties-common lsb-release ca-certificates apt-transport-https curl

These tools will help you add new repositories, import their GPG keys, and ensure secure communication over HTTPS.

Adding the PHP Repository

Debian’s default repositories might not always provide the latest PHP versions. To get the latest PHP 8 version, you can add a third-party repository maintained by Ondřej Surý, a reputable Debian developer known for keeping PHP packages up to date. Execute the following commands to add his PHP repository:

sudo curl -fsSL https://packages.sury.org/php/apt.gpg | sudo gpg --dearmor -o /usr/share/keyrings/php.gpg echo "deb [signed-by=/usr/share/keyrings/php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

Installing PHP 8

With the repository added, it’s time to install PHP 8. First, update your package list to include the newly added repository:

sudo apt update

Now, install PHP 8 by choosing the specific version you need. For PHP 8.0, use:

sudo apt install -y php8.1

If you’re interested in PHP 8.1 or any other available version, adjust the version number in the command accordingly.

Verifying the Installation

After installation, it’s a good practice to verify that PHP is correctly installed and check its version. You can do this by running:

php -v

This command will display the PHP version installed, confirming the successful installation.

SSL Setup with Letsencrypt

Certbot is a tool that automates the process of obtaining and renewing SSL certificates from Let’s Encrypt. To install Certbot, run the following command:

First, install snap

sudo apt update
sudo apt install snapd -y

Run this command on the command line on the machine to install Certbot.

sudo snap install --classic certbot

Execute the following instructions on the command line on the machine to ensure that the certbot command can be run.

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Run this command to get a certificate and have Certbot edit your Apache configuration automatically to serve it, turning on HTTPS access in a single step.

sudo certbot --apache

Automatic certificate renewal

This command will simulate the certificate renewal process to make sure everything is configured correctly. If there are no errors, Certbot will automatically renew your SSL certificate when it is due to expire.

To renew the Certificate automatically you need to set a cronjob via crontab. The commands are given below.

sudo crontab -e
0 3 * * * sudo certbot renew >/dev/null 2>&1

Conclusion

Congratulations! You’ve successfully installed PHP 8 on your Debian 12 system. This upgrade not only enhances your system’s capabilities with the latest PHP features but also ensures you’re up to date with security and performance improvements.

Remember, managing server software and configurations requires careful attention to detail and understanding the implications of each action. Always back up your data before making significant changes, and consider testing configurations in a development environment first.

By following this guide, you’re ensuring your web development environment remains robust, secure, and on the cutting edge. Happy coding!

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply