Something that I'd like to share with you!

Monday, April 20, 2020

Nginx PHP-FPM setup under Centos 7

No comments :
Nginx (pronounced as Engine-X) is a web and reverse proxy server well known for its speed and capability to handle large number of requests

While PHP-FPM (PHP-FastCGI process manager) is a common gateway interface between the web server and dynamic content serving programs. It listens on a port or socket and passes the request between the PHP and web server

To start with, install Nginx...

[root@localhost ~]# yum install nginx -y

Start the service

[root@localhost ~]# service nginx start

Now test the Nginx by opening its default web page http://<your host>/



Find Nginx web directory by checking service status

[root@localhost ~]# service nginx status

Apr 20 20:42:56 localhost.localdomain nginx[5942]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

Apr 20 20:42:56 localhost.localdomain nginx[5942]: nginx: configuration file /etc/nginx/nginx.conf test is successful

Grep "root" from the "nginx.conf" file

[root@localhost ~]# grep root /etc/nginx/nginx.conf
root         /usr/share/nginx/html;

Create a PHP test page "test.php" as content below and save it to /usr/share/nginx/html/

<?php
phpinfo();
?>

Try to run the PHP file = http://<your host>/test.php

As we know it won't parse. It will just download or show the PHP file content. This is expected since we didn't link Nginx with PHP-FPM yet.

Install PHP-FPM

[root@localhost ~]# yum install php-fpm -y

Check the PHP-FPM installation

[root@localhost ~]# service php-fpm status

Locate the PHP=FPM "www.conf" file

[root@localhost ~]# locate www.conf
/etc/php-fpm.d/www.conf

Edit the "/etc/php-fpm.d/www.conf" change listen ip:port to socket

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /run/php-fpm/php-fpm.sock

Restart PHP-FPM

[root@localhost ~]# service php-fpm restart

Backup and edit /etc/nginx/nginx.conf
Under server scope {} block, add location scope {} block to enable PHP-FPM to parsePHP file

server {
listen       80 default_server;
listen       [::]:80 default_server;
server_name  _;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

#location / {
#}

location ~* \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
include         fastcgi_params;
fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

}

Make sure this line is pointing to correct Unix socket

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


Restart both Nginx and PHP-FPM

[root@localhost ~]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service

[root@localhost ~]# service php-fpm restart
Redirecting to /bin/systemctl restart php-fpm.service

Try to run the PHP file = http://<your host>/test.php again





x

No comments :