Current Location: Home> Latest Articles> How to Install PHP on Linux: Complete Guide and Common Issues Solution

How to Install PHP on Linux: Complete Guide and Common Issues Solution

gitbox 2025-07-15

The Importance of Installing PHP on Linux

PHP (Hypertext Preprocessor) is a widely used open-source scripting language, particularly suited for web development. For developers aiming to build dynamic websites, installing PHP on Linux is a critical step. This article will provide a detailed guide on how to install PHP in a Linux environment, along with common methods and steps.

Pre-installation Preparation

Before starting the installation of PHP, make sure your Linux system is up to date. Open the terminal and run the following commands:

<span class="fun">sudo apt update && sudo apt upgrade</span>

This will ensure that all software packages are up to date and minimize dependency-related issues.

Installing PHP Using Package Managers

The easiest method to install PHP on Linux is through package managers. For example, Ubuntu and Debian users can use APT, while CentOS users can use YUM.

Installing PHP on Ubuntu/Debian

On Ubuntu or Debian systems, you can easily install PHP using the following command:

<span class="fun">sudo apt install php libapache2-mod-php php-mysql</span>

This command installs PHP, the Apache module, and the MySQL extension to better support database operations.

Installing PHP on CentOS

If you're using CentOS, you can install PHP using the following command:

<span class="fun">sudo yum install php php-mysqlnd php-fpm</span>

Similarly, this command installs PHP and the MySQL extension, ensuring your development environment is complete.

Verifying PHP Installation

Once the installation is complete, the best way to verify if PHP was successfully installed is by creating a PHP info file. Create a file named info.php in the root directory of your web server with the following content:

<span class="fun">phpinfo();</span>

Then, visit http://your-server-ip/info.php in your browser. If everything is correct, you will see the PHP configuration page.

Common Issues and Solutions

PHP Not Displaying Correctly

If you see the source code when viewing info.php in your browser, it might indicate that PHP is not properly configured in Apache or Nginx. Make sure you've installed the necessary modules and restart the web server:

<span class="fun">sudo systemctl restart apache2  # For Apache</span>
<span class="fun">sudo systemctl restart nginx      # For Nginx</span>

Additional PHP Extensions Required

Depending on your project needs, you may also need to install additional PHP extensions. You can install common extensions using the following command:

<span class="fun">sudo apt install php-curl php-xml php-gd php-mbstring</span>

Conclusion

The steps to install PHP on Linux are relatively simple, and can be completed through the package manager. After installation, make sure to verify PHP and install any additional extensions required for your project. This will lay the foundation for a stable and efficient web development environment.

We hope this article helps and makes your PHP development journey smoother!