How to Install Nginx with PHP-FPM on CentOS 7

Installing Nginx with PHP-FPM on different operating systems can be a daunting task for many. But with the right guidance and steps, it can be done easily. In this article, we will discuss how to install Nginx with PHP-FPM on CentOS systems. We will also provide a step-by-step guide to help you get started quickly and easily. So if you are looking for an easy way to install Nginx with PHP-FPM, then this article is for you!

What is Nginx and why we need PHP-FPM ?

Nginx is a powerful web server that can be used to serve static and dynamic content. It is highly optimized for performance, scalability, and security. In combination with PHP-FPM, it provides an efficient way to process dynamic requests and deliver content quickly. By configuring Nginx with PHP-FPM, you can easily create a reliable web server environment that can handle high traffic volumes and deliver content efficiently. With the right configuration settings, Nginx can provide an excellent user experience for visitors to your website. Tuning Nginx and PHP-FPM together can help you get the most out of your web server by using FastCGI and proxy settings to increase speed and reliability. By configuring these two applications together, you can ensure that your website runs smoothly without any hiccups or slowdowns.

1. Prepare EPEL Repository for CentOS / RHEL 7 :

# yum install epel-release
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

2. Install PHP7.4 :

# yum --enablerepo=remi-php74 install php-mysql php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt

3. Prepare NGINX repository :

# vi /etc/yum.repos.d/nginx.repo

4. 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

5. Install Nginx :

# yum install nginx
# vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format blocked '$time_local: Blocked request from $remote_addr $request';
    access_log  /var/log/nginx/access.log  main;

    include /etc/nginx/sites-available/*.conf;
}

6. Configure basic php-fpm configuration to make it work with NGINX :

# vim /etc/nginx/sites-available/test.local8080.conf
server {
        listen 80;
        server_name test.local wwww.test.local;
        root  /var/www/html/test.local;
        index index.php index.html index.htm;

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

	location ~ \.php$ {
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	fastcgi_pass   127.0.0.1:9001;
	fastcgi_index index.php;
	fastcgi_send_timeout 300s;
	fastcgi_read_timeout 300s;
	include fastcgi_params;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	fastcgi_buffer_size 128k;
	fastcgi_buffers 256 4k;
	fastcgi_busy_buffers_size 256k;
	fastcgi_temp_file_write_size 256k;
	fastcgi_intercept_errors on;
	}

}

Learn How to Setup Nginx as Web Server on CentOS 7

Leave a Comment