DebianDebian Based

How To Install PHP on Debian 12

Install PHP on Debian 12

In this tutorial, we will show you how to install PHP on Debian 12. For those of you who didn’t know, PHP, the widely used scripting language for web development, plays a crucial role in creating dynamic and interactive websites. When it comes to deploying PHP applications, Debian 12, a robust and reliable Linux distribution, provides a stable foundation.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the PHP programming language on a Debian 12 (Bookworm).

Prerequisites

  • A server running one of the following operating systems: Debian 12 (Bookworm).
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for PHP.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install PHP on Debian 12 Bookworm

Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt commands in the terminal:

sudo apt update
sudo apt install apt-transport-https lsb-release ca-certificates

This command will refresh the repository, allowing you to install the latest versions of software packages.

Step 2. Installing PHP on Debian 12.

Now that your system is up to date, it’s time to install PHP on Debian 12. First, add the Ondřej Surý PHP repository to Debian 12 by executing the following commands in the terminal:

wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

These commands will enable your system to fetch PHP packages from the repository.

Next, update the package lists once again and install PHP and the necessary extensions by running the command:

sudo apt update
sudo apt install php8.2

Note: Replace php8.2 with the desired PHP version, such as php8.0, depending on your requirements.

For modules, do (replace EXTENSION with respective PHP module):

sudo apt install php8.2-EXNTENSION

To ensure that PHP is installed correctly, let’s run a simple test:

sudo nano /var/www/html/info.php

Add the following content to the file:

<?php
phpinfo();
?>

Save the file and exit the text editor, then open a web browser and navigate to http://your_server_IP/info.php. You should see a PHP information page displaying detailed information about your PHP installation.

Step 3. Customizing PHP Configuration.

PHP’s default configuration may not always align with your project’s requirements. Let’s explore how you can customize the PHP settings to fine-tune your environment.

  • Overview of PHP Configuration:

The PHP configuration is managed through the php.ini file. This file contains various directives that control PHP’s behavior:

  • Modifying PHP Settings:

Locate the php.ini file:

sudo nano /etc/php/8.1/apache2/php.ini

Within this file, you can modify settings such as memory_limit, error_reporting, and display_errors, among others. For example, to increase the memory limit to 256MB, find the memory_limit directive and update it as follows:

memory_limit = 256M

Save the changes and restart the web server for the new configuration to take effect:

sudo systemctl restart apache2

Step 4. Troubleshooting Tips.

Even with careful installation, issues may arise. Here are some common troubleshooting tips.

  • Verify that the PHP package is installed correctly by running:
php -v
  • Check the web server logs for any error messages that may indicate misconfigurations or other issues:
sudo tail -f /var/log/apache2/error.log
  • Ensure that the required PHP extensions for your application are installed. You can search for available PHP extensions using the command:
apt search php | grep ^php

Congratulations! You have successfully installed PHP. Thanks for using this tutorial for installing the latest version of PHP programming language on Debian 12 Bookworm. For additional help or useful information, we recommend you check the official PHP website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!
Back to top button