Current Location: Home> Latest Articles> Complete Guide to Efficiently Configure PHP and Nginx on CentOS 6.5

Complete Guide to Efficiently Configure PHP and Nginx on CentOS 6.5

gitbox 2025-08-10

Introduction

This guide will walk you through setting up PHP and Nginx on CentOS 6.5, creating a stable and responsive web service. It covers installation, configuration, and optimization processes essential for developers and sysadmins aiming to deploy efficient websites.

Installing Nginx

First, install Nginx on CentOS 6.5. Use the commands below to install the EPEL repository and then Nginx:

sudo yum install epel-release
sudo yum install nginx

Start and Enable Nginx at Boot

After installation, start Nginx and set it to start automatically on system boot:

sudo service nginx start
sudo chkconfig nginx on

Install PHP and Related Modules

Next, install PHP along with PHP-FPM and MySQL support modules to ensure PHP works properly with Nginx:

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

Configure PHP-FPM

Once installed, configure PHP-FPM by editing its configuration file:

<span class="fun">sudo vi /etc/php-fpm.d/www.conf</span>

Modify the user and group to nginx:

user = nginx
group = nginx

Save and close the file.

Start PHP-FPM and Enable it on Boot

sudo service php-fpm start
sudo chkconfig php-fpm on

Configure Nginx to Support PHP

Edit the default Nginx configuration file to add PHP support by running:

<span class="fun">sudo vi /etc/nginx/conf.d/default.conf</span>

Inside the location / {} block, add the following to handle PHP requests:

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Restart Nginx Service

After configuration, restart Nginx to apply changes:

<span class="fun">sudo service nginx restart</span>

Verify PHP Setup

Create a test file in the web root directory to verify the PHP environment. Use this command to create info.php:

<span class="fun">echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php</span>

Then access http://your_server_ip/info.php in your browser to view the PHP info page.

Conclusion

Following these steps allows you to successfully configure PHP and Nginx on CentOS 6.5, creating a stable and high-performance web server environment. Proper configuration not only boosts site performance but also simplifies maintenance and future expansion. We hope this guide helps you quickly set up the server you need.