Current Location: Home> Latest Articles> How to Install and Configure PHP on CentOS - Complete Step-by-Step Guide

How to Install and Configure PHP on CentOS - Complete Step-by-Step Guide

gitbox 2025-06-30

System Preparation

Before installing PHP, ensure that your CentOS system is up to date. Use the following command to update the system:

sudo yum update

Install Required Dependencies

In order to install PHP successfully, you'll need to install some necessary dependencies. Run the following command to install these packages:

sudo yum install -y gcc libxml2-devel bzip2-devel curl-devel libpng-devel freetype-devel gmp-devel

Enable EPEL and Remi Repositories

CentOS's default repositories may not contain the latest version of PHP, so you'll need to enable the EPEL (Extra Packages for Enterprise Linux) and Remi repositories. Use the following commands to enable them:

sudo yum install epel-release
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

Select PHP Version

The Remi repository offers multiple PHP versions. Use the following command to enable the repository for the PHP version you want to install. For example, to install PHP 8.1:

sudo yum --enablerepo=remi-php81 install php php-cli php-common php-mysqlnd php-fpm

Install PHP

Once you've selected the appropriate PHP version, you can begin installing PHP and the necessary modules. Use the following command to install PHP:

sudo yum install php php-cli php-common php-mysqlnd php-fpm

Verify Installation

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

php -v

If the installation was successful, you'll see the PHP version information.

Configure PHP

After installing PHP on CentOS, you may need to configure PHP to meet your specific needs. Typically, you can edit the php.ini file to adjust settings. Use the following command to open and edit the file:

sudo vi /etc/php.ini

Install Common PHP Extensions

Depending on your project requirements, you may need to install some PHP extensions. Use the following command to install commonly used extensions:

sudo yum install php-gd php-xml php-mbstring php-zip php-curl

Restart Web Server

After installation and configuration, don't forget to restart your web server to apply the changes. Use the following command to restart Apache or Nginx:

sudo systemctl restart httpd

Or

sudo systemctl restart nginx

Conclusion

By following these steps, you've successfully installed PHP on your CentOS system. Mastering this process will not only help you set up and maintain websites but also enhance your development skills. Regular updates and maintenance of your PHP environment will be an important part of your future work, ensuring its security and stability.