How To Install Nginx With PHP-FastCGI on CentOS 6

Install Nginx With PHP-FastCGI on CentOS 6

In this tutorial, we will show you how to install Nginx With PHP-FastCGI on CentOS 6. For those of you who didn’t know, Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or a reverse proxy.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple. I will show you the step-by-step installation of Nginx with fast-CGI on a CentOS 6 server.

Prerequisites

  • A server running one of the following operating systems: CentOS or RHEL-based.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection.
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Nginx With PHP-FastCGI on CentOS 6

Step 1. Before proceeding, update your operating system to make sure all existing packages are up to date. Use this command to update the server packages:

sudo yum upgrade
sudo yum update
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php74

Step 2. Installing Nginx on Fedora 37.

By default, the Nginx package comes in the default CentOS repository. Now run the following command below to install Nginx to your CentOS system:

sudo yum install nginx

After the installation is complete, start the Nginx service and enable it to start automatically on boot by running the following commands:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3. Installing PHP-FastCGI.

Install PHP-FastCGI by running the following command:

sudo yum install php-fpm

Step 4. Configure Nginx to Use PHP-FastCGI.

First, we need to open the Nginx default configuration file in a text editor. In CentOS, the Nginx configuration files are located in the /etc/nginx directory:

nano /etc/nginx/nginx.conf

Add the following lines:

server {
    listen 80;
    server_name your-domain.com;

    root /var/www/html;
    index index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

To apply the new configuration, restart Nginx with the following command:

sudo systemctl restart nginx

Step 5. Test the Installation.

To test the installation, create a PHP file in the web root directory /var/www/html. You can do this by running the following command:

nano /var/www/html/info.php

Inside the file, add the following PHP code:

<?php
phpinfo();
?>

Save and close the file, then open your web browser and navigate to your server’s IP address or domain name.

Congratulations! You have successfully installed Nginx and PHP-FastCGI. Thanks for using this tutorial for installing Nginx and PHP-FastCGI on CentOS 6 systems. For additional help or useful information, we recommend you check the official Nginx website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!