Skip to main content

How to Install PHP on CentOS

In this tutorial, we will guide you through the step-by-step process of installing PHP on the CentOS operating system. PHP is a popular server-side scripting language used for web development, and installing it on CentOS is a relatively straightforward process.

Prerequisites

Before we begin, make sure you have the following:

  • A CentOS operating system installed on your server or virtual machine.
  • Access to the root account or a user account with sudo privileges.

Step 1: Update System Packages

First, let's ensure that all system packages are up to date. Open a terminal or SSH into your CentOS server and run the following command:

sudo yum update

This command will update your system with the latest package versions and security patches. You may be prompted to enter your password.

Step 2: Install PHP

To install PHP on CentOS, we will use the yum package manager. Run the following command in the terminal:

sudo yum install php

This command will download and install PHP along with its dependencies. You may be prompted to confirm the installation by typing 'y' and pressing Enter.

Step 3: Verify PHP Installation

Once the installation is complete, we can verify that PHP is installed correctly. We can do this by creating a simple PHP test file.

Create a new file called info.php in your web server's document root directory (usually /var/www/html/) using your preferred text editor. Add the following lines of code to the file:

<?php
phpinfo();
?>

Save the file and exit the text editor.

Now, open a web browser and navigate to http://your_server_ip/info.php. You should see a page displaying detailed information about your PHP installation. This page provides useful information about your PHP version, extensions, and configuration settings.

Step 4: Additional PHP Modules

By default, the php package we installed includes a basic set of modules. Depending on your project requirements, you may need to install additional PHP modules.

To search for available modules, use the yum search command followed by the keyword php. For example, to search for the MySQL module, run the following command:

sudo yum search php | grep mysql

This command will display a list of PHP modules related to MySQL. To install a specific module, use the yum install command as we did in Step 2.

Step 5: Restart Web Server

After installing PHP and any additional modules, it is necessary to restart your web server for the changes to take effect.

If you are using Apache, run the following command:

sudo systemctl restart httpd

If you are using Nginx, run the following command:

sudo systemctl restart nginx

Conclusion

Congratulations! You have successfully installed PHP on CentOS. You can now start developing web applications using PHP on your CentOS server. Remember to regularly update your PHP installation to benefit from the latest security patches and features.

Feel free to explore additional PHP modules to enhance your development capabilities. If you encounter any issues during the installation process, consult the official documentation or seek assistance from relevant online communities.

Enjoy coding with PHP on CentOS!