How To Install PHP on Debian Based Linux

Here is an article on how to install PHP on a Debian-based system. These instructions will guide you through installing the PHP packages, configuring the web server, and testing the installation.

Step 1: Update the System

Before starting the installation process, it’s important to update the system’s package repository. This ensures that you install the latest available version of PHP.

Open a terminal window.

Run the following command to update the list of available packages:
sudo apt update

Optionally, you can upgrade existing packages by running:
sudo apt upgrade

Step 2: Install PHP

Debian’s official repositories contain the PHP packages. The default version in the Debian repository may not always be the latest, but it will be stable.

To install the default version of PHP (which might be PHP 7.4 or 8.x, depending on your Debian version), run:
sudo apt install php

This will install the latest stable PHP version available from the default Debian repositories.

If you want to install a specific version of PHP (e.g., PHP 8.0), you can use the following command:
sudo apt install php8.0

To install other PHP versions, replace php8.0 with the version you want (e.g., php7.4, php8.1, etc.).

Step 3: Install PHP Modules (Optional)

Depending on your use case, you might need additional PHP modules to enable specific functionality, such as interacting with a database or handling various types of content.

To install commonly used PHP modules, run:

sudo apt install php-cli php-mysql php-xml php-mbstring php-curl php-zip php-json php-common

PHP Module Descriptions:

  • php-cli: Command-line interface for PHP.
  • php-mysql: MySQL database driver for PHP.
  • php-xml: XML handling in PHP.
  • php-mbstring: Multi-byte string handling for PHP (needed for UTF-8 support).
  • php-curl: PHP extension for handling HTTP requests via cURL.
  • php-zip: For working with zip files in PHP.
  • php-json: JSON support for encoding and decoding JSON objects.
  • php-common: Common files for PHP.

You can install additional PHP modules as needed. To search for available PHP modules, use the following command:

apt search php-

This will list all available PHP packages and modules. You can install any of them using sudo apt install <module-name>.

Step 4: Install PHP with Apache

If you want to run PHP on a web server (e.g., Apache), you’ll need the PHP Apache module. If Apache is already installed, you can simply install the PHP module for Apache by running:

sudo apt install libapache2-mod-php

Once installed, restart Apache to enable the PHP module:

sudo systemctl restart apache2

This will allow Apache to interpret PHP files and serve them correctly.

Step 5: Install PHP with Nginx (Optional)

If you are using Nginx as your web server, the process is slightly different because Nginx does not have a built-in PHP module like Apache does. Instead, you will need PHP-FPM (FastCGI Process Manager) to handle PHP requests.

First, install the necessary PHP-FPM package:
sudo apt install php-fpm

After that, you’ll need to configure Nginx to work with PHP. For example, in your Nginx server block configuration, you’ll need to add the following lines:
server {

    listen 80;

    server_name example.com;

    root /var/www/html;

    index index.php index.html index.htm;

    location ~ \.php$ {

        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;  # Replace with your PHP version

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;

    }

}

After configuring Nginx, restart both Nginx and PHP-FPM:
sudo systemctl restart nginx

sudo systemctl restart php8.0-fpm  # Replace with your PHP version if needed

Step 6: Check PHP Version

After PHP has been installed, you can verify that the correct version is installed by running:

php -v

This will display the PHP version and some details about the PHP configuration. Example output:

PHP 8.0.8 (cli) (built: Jul  6 2020 00:32:47) ( NTS )

Step 7: Test PHP in a Web Browser

To make sure PHP is working with your web server (Apache or Nginx), create a simple PHP script to test it.

For Apache:

Navigate to the web root directory (usually /var/www/html/ for Apache):
sudo nano /var/www/html/info.php

Add the following code to the info.php file:
<?php

phpinfo();

?>

  1. Save and exit (Ctrl + X, then Y, and hit Enter).
  2. Now, open a web browser and go to http://localhost/info.php or http://your_server_ip/info.php (replace your_server_ip with the actual IP if accessing remotely).
    • If PHP is working correctly, you’ll see a page with detailed information about your PHP configuration.

Step 8: Secure PHP (Optional)

For production environments, it is essential to secure PHP by disabling some dangerous functions and limiting access to sensitive areas.

Edit the PHP configuration file:
sudo nano /etc/php/8.0/apache2/php.ini  # Adjust the path if you’re using a different PHP version

Look for the following lines and adjust them as needed:

Disable dangerous PHP functions:
disable_functions = exec, shell_exec, system, passthru, proc_open, popen

Disable file uploads if you do not need them:
file_uploads = Off

Change the session save path for better security (optional):
session.save_path = “/var/lib/php/sessions”

After making changes to the php.ini file, restart Apache (or PHP-FPM if you’re using Nginx) to apply the changes:
sudo systemctl restart apache2

Step 9: Manage PHP Versions (Optional)

If you need to manage multiple versions of PHP on your system, you can use the update-alternatives tool to configure the default PHP version for the command line.

To configure the default PHP version, use:
sudo update-alternatives –config php

This will display a list of installed PHP versions. Enter the number corresponding to the version you want to use as the default.

Step 10: Troubleshooting

PHP Errors: If you encounter PHP errors, check Apache’s or Nginx’s error logs to get more details:

For Apache:
sudo tail -f /var/log/apache2/error.log

 For Nginx:
sudo tail -f /var/log/nginx/error.log

PHP-FPM Errors (if using Nginx):
sudo tail -f /var/log/php8.0-fpm.log

Conclusion

You have successfully installed PHP on your Debian system. Whether you’re using Apache or Nginx as your web server, PHP should now be fully integrated and functioning. You can now use PHP to create dynamic websites, install CMSs like WordPress, or run web applications on your Debian server.