Current Location: Home> Latest Articles> Complete Guide to PHP Environment Setup on CentOS: Installation to Debugging

Complete Guide to PHP Environment Setup on CentOS: Installation to Debugging

gitbox 2025-06-28

Preparation

Before you start configuring the PHP environment, make sure your CentOS system is updated to the latest version. Use the following command to update your system:

sudo yum update

Install PHP and Dependencies

Installing PHP on CentOS is a simple process. First, you need to install some necessary dependencies:

sudo yum install epel-release sudo yum install httpd

Next, install PHP and its related modules:

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

Install PHP Extensions

Depending on your project requirements, you might need to install some common PHP extensions. Here are commands to install a few common ones:

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

Configure Apache to Support PHP

Next, you need to configure the Apache server to support PHP. First, ensure that the Apache service is running:

sudo systemctl start httpd

Then, enable Apache to start automatically on boot:

sudo systemctl enable httpd

Next, edit the Apache configuration file to properly handle PHP files:

sudo nano /etc/httpd/conf/httpd.conf

Ensure PHP Modules are Correctly Loaded

In the configuration file, make sure the following lines are not commented out:

LoadModule php_module modules/libphp.so
AddHandler php-script .php
AddType application/x-httpd-php .php

Test the PHP Environment

After completing the configuration, you can create a simple test file to verify that PHP is working properly. Go to Apache's root directory (usually /var/www/html) and create a file named info.php:

sudo nano /var/www/html/info.php

Add the following code to the file:

<?php phpinfo(); ?>

After saving the file, visit http://your_server_ip/info.php in your browser. If you see the PHP info page, PHP has been successfully configured.

Security Considerations

To keep your PHP environment secure, regularly update your PHP version and extensions. Additionally, consider using SELinux and a firewall to further enhance your server's security.

By following these steps, you can successfully configure and maintain a stable, secure PHP development environment.