XP-rience

Something that I'd like to share with you!

Showing posts with label nginx. Show all posts

Thursday, May 07, 2020

Nginx PHP-FPM setup under Kali Linux

No comments :
Another Nginx PHP-FPM setup post similar to previous post [ here ]

To start with, install Nginx...

root@kali:~# apt-get -y install nginx

Start Nginx

root@kali:~# service nginx start

Install PHP-FPM, note the version

root@kali:~# apt-get -y install php-fpm

Edit default configuration

root@kali:~# vi /etc/nginx/sites-available/default

Add index.php into server block

index index.html index.htm index.nginx-debian.html index.php;

Add location block

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}

Restart Nginx

root@kali:~# service nginx restart

Edit php.ini, note the PHP-FPM version, add cgi.fix_pathinfo=0

root@kali:~# vi /etc/php/7.3/fpm/php.ini

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

Restart PHP-FPM

root@kali:~# service php7.3-fpm restart

Create a PHP test page "test.php" as content below and save it to /var/www/html/

<?php
phpinfo();
?>

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






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