Current Location: Home> Latest Articles> PHP Installation and Configuration Guide for Linux Environment

PHP Installation and Configuration Guide for Linux Environment

gitbox 2025-06-29

Why Choose Linux for PHP Installation?

Linux is an open-source operating system, widely appreciated by developers for its stability and security. Installing PHP on Linux not only optimizes system resources but also provides access to a wealth of community support for easier application development and deployment. Moreover, many popular web servers like Apache and Nginx run seamlessly on Linux.

Prepare Your Linux Environment

Before installing PHP, make sure your Linux system is properly configured. Update your system using the following commands:

sudo apt update && sudo apt upgrade

Install Apache or Nginx

PHP is typically used in conjunction with a web server, so you need to install one first. Here we use Apache as an example:

sudo apt install apache2

Verify Apache Installation

After installation, you can verify Apache is running by visiting http://localhost. If successful, you'll see Apache's default page.

Install PHP

Next, install PHP using the following command:

sudo apt install php libapache2-mod-php

Install Specific PHP Extensions

Depending on your project's needs, you might need additional PHP extensions. For example, if you need MySQL support, you can use the following command:

sudo apt install php-mysql

Test PHP Installation

To ensure PHP is installed correctly, create a test file. In Apache's document root directory, create a file named info.php:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Then, visit http://localhost/info.php to view the PHP information page. If you see the PHP configuration details, the installation was successful.

Conclusion

By following these steps, you'll be able to successfully install and configure PHP in a Linux environment. Following this guide step-by-step will not only ensure PHP is installed correctly but also prepare you for future development and deployment tasks.