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.
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
After installation, start Nginx and set it to start automatically on system boot:
sudo service nginx start
sudo chkconfig nginx on
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>
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.
sudo service php-fpm start
sudo chkconfig php-fpm on
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;
}
After configuration, restart Nginx to apply changes:
<span class="fun">sudo service nginx restart</span>
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.
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.