Skip to main content

How to Install PHP on Debian

PHP is a widely-used open-source scripting language that is especially suited for web development. It allows developers to create dynamic web pages and applications. Installing PHP on Debian is a straightforward process that can be completed in a few easy steps. In this tutorial, we will guide you through the installation process and provide examples along the way.

Step 1: Update Package Lists

Before installing any software, it's important to make sure your system is up to date. Open a terminal and run the following command to update the package lists:

sudo apt update

This command will fetch the latest information about available software packages from the Debian repositories.

Step 2: Install PHP

Once the package lists are updated, you can proceed with the installation of PHP. Run the following command to install PHP along with some commonly used extensions:

sudo apt install php php-cli php-fpm php-mysql php-curl php-gd php-xml php-mbstring

The above command will install PHP and its dependencies. It includes extensions for MySQL, cURL, GD, XML, and Multibyte String, which are commonly required for web development.

Step 3: Verify PHP Installation

After the installation is complete, you can verify that PHP is installed correctly by running the following command:

php -v

This command will display the PHP version installed on your system, along with other information about the PHP configuration.

Example output:

PHP 7.4.3 (cli) (built: Mar 26 2021 20:24:47) ( NTS )

Step 4: Test PHP with a Web Page

To ensure PHP is working as expected, let's create a simple PHP web page and test it on a web browser.

Create a new file named index.php in your preferred text editor and add the following PHP code:

<?php
phpinfo();
?>

Save the file and move it to the web server's document root directory. The default document root for Apache web server on Debian is /var/www/html/.

Restart Apache to apply the changes:

sudo service apache2 restart

Now, open a web browser and navigate to http://localhost/index.php or http://your_server_ip/index.php. You should see a page displaying detailed information about your PHP installation.

Step 5: Watch Out for File Permissions

When working with PHP and web servers, it's crucial to pay attention to file permissions. By default, the web server runs under a specific user, such as www-data. Make sure the PHP files you create are readable by the web server user.

To change the ownership of the PHP file, you can use the following command:

sudo chown www-data:www-data /var/www/html/index.php

This command sets the owner and group of the index.php file to www-data, which is the default user and group used by the Apache web server on Debian.

Conclusion

Congratulations! You have successfully installed PHP on Debian and verified its functionality. You can now start developing dynamic web pages and applications using PHP. Remember to keep your PHP files secure by setting appropriate file permissions. Enjoy coding with PHP!