How to Install Nginx on CentOS 7 with PHP-FPM

The Nginx Web Server is a good Alternative for the Apache Web Server. While Apache is still the most commonly used web server, Nginx is a Lightweight Web Server design for the high trafficking websites.

In This Tutorial We are going to Learn How to Install Nginx on CentOS 7 with php-fpm enable.

The Nginx Web Server still does not come with the official CentOS 7 repository, But We have several methods available to install nginx on CentOS 7.

One method to install nginx using the repository provides by the Nginx itself, another method is to use fedora epel repository. And also We can install nginx from the source.

In This this tutorial first we are going to install nginx on CentOS 7 using Nginx CentOS 7 repository and configure php-fpm for Nginx Web Server.

How to Install Nginx on CentOS 7 with PHP-FPM

Install Nginx on CentOS 7 using CentOS Nginx repository.

The Preferred method to install nginx on CentOS 7 is to use the Software repository Provides by the Nginx. First, we Create the .repo file and then install nginx using yum install command.

Step One

Create Nginx CentOS Repository for CentOS 7

First, create a file called nginx.repo inside the /etc/yum.repos.d directory.

touch /etc/yum.repos.d/nginx.repo

Then, open the nginx.repo file using a text editor and add the following lines.

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0v

enabled=1

Save the nginx.repo file and run the yum repolist command to make sure that the repository is active.

yum repolist

Step Two

yum install nginx

Now we can install centos nginx using yum command.

yum install nginx

After the installation is finished, start and enable nginx using systemctl command.

systemctl start nginx

systemctl enable nginx

Step Three

Allow HTTP Protocol From the Firewall

Next adds firewall rules to allow http service to the outside using firewall-cmd command.

firewall-cmd --permanent --add-service=http

firewall-cmd --permanent --add-service=https

firewall-cmd --reload

Now if typed IP address of your server on the web browser you will get default nginx welcome page.

CentOS Nginx Default Server Page
  • Location of the Nginx Main Configuration File in CentOS 7 is /etc/nginx/nginx.conf file.
  • Default Document root is /usr/share/nginx/html/ folder.
Step Four

Install PHP-FPM on CentOS 7

Nginx works with php-fpm which stands for PHP FastCGI Process Manager. We can easily install php-fpm on CentOS 7 using yum command.

yum install php-fpm

php-fpm on CentOS 7 run as a service, so we need to start and enable php-fpm using systemctl command.

systemctl start php-fpm

systemctl enable php-fpm

Step Five

Configure PHP-FPM

First open the /etc/php-fpm.d/www.conf file and change value of listen from listen = 127.0.0.1:9000 to listen = /var/run/php-fpm/php-fpm.sock.

listen = /var/run/php-fpm/php-fpm.sock

Next, open the /etc/php.ini file and set the value of cgi.fix_pathinfo to 0 (uncomment the line if it’s been commented).

cgi.fix_pathinfo=0

Now, Restart the centos php-fpm service using systemctl command.

systemctl restart php-fpm
Step Six

Configure Nginx for PHP-FPM

Open /etc/nginx/conf.d/default.conf file and make sure the following configurations are exist in the server block.

server {

listen       80;

server_name  localhost;

root /usr/share/nginx/html;

location / {

root   /usr/share/nginx/html;

index  index.php index.html index.htm;

}

location ~ [^/]\.php(/|$) {

fastcgi_split_path_info ^(.+?\.php)(/.*)$;

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

And restart the centos nginx Web Server.

systemctl restart nginx

Now you can create .php file with phpinfo() function inside the /usr/share/nginx/html folder to test the centos nginx and php-fpm installation.

Create Nginx Virtual Host

Just like Apache Web Server, Nginx Also allows to host multiple website on the same server. So Let's see an example how to configure a virtual host on CentOS 7 Nginx Web Server.

For This Example our domain name is going to be www.example.com and the document root of the example.com going to be /usr/share/nginx/example directory.

So first create the document root using the mkdir command.

mkdir -p /usr/share/nginx/example

Create the configuration file for example.con inside the /etc/nginx/conf.d directory with .conf extension.

touch /etc/nginx/conf.d/example.conf

Then Add the Following php-fpm Configurations.

server {

listen 80;

server_name example.com www.example.com;

root /usr/share/nginx/example;

error_log /var/log/nginx/example.error.log warn;

access_log /var/log/nginx/example.access.log main;

location / {

index index.php index.html index.htm;

}

location ~ [^/]\.php(/|$) {

fastcgi_split_path_info ^(.+?\.php)(/.*)$;

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

Finally, Save the configuration file and restart the centos nginx web server.

systemctl restart nginx

Now we can service our website www.example.com from the /usr/share/nginx/example directory.

So that is how we can install nginx on CentOS 7 using nginx CentOS 7 Repository. Next We Will See How to Install nginx using fedora epel-release.

Install Nginx Web Server on CentOS 7 using epel repository

The Fedora Epel repository also provides nginx for CentOS 7. First, we enable epel release and then install the nginx web server using yum install command.

yum install epel-release

yum install nginx

Configuration is going to be the same as we did before except for the nginx php configuration for the default server block.

For the default site, you should create a configuration file inside the /etc/nginx/default.d directory.

touch /etc/nginx/default.d/default.conf

Then add the following php configuration.

location ~ [^/]\.php(/|$) {

fastcgi_split_path_info ^(.+?\.php)(/.*)$;

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

So This is the end of This Tutorial. We Learned How to Install and Configure Nginx on CentOS 7 Linux with PHP-FPM Enabled.