This page looks best with JavaScript enabled

Nginx php-fpm on Gentoo

 ·  🎃 kr0m

Running PHP code through PHP-FPM provides several advantages over traditional execution, including greater concurrency and security. In this article, I will explain how to configure it under Nginx/Gentoo.

First, we install the web server, in this case Nginx:

emerge -av www-servers/nginx

We create a test document to verify that the server is working correctly:

mkdir /var/www/localhost/htdocs
echo ‘Hello, world!’ > /var/www/localhost/htdocs/index.html

We start the server:

/etc/init.d/nginx start
rc-update add nginx default

We make a test request:

curl http://localhost

We compile PHP with the necessary use flags:

echo “dev-lang/php acl berkdb bzip2 cli ctype fileinfo filter flatfile gdbm iconv ipv6 json nls opcache phar posix readline session simplexml ssl tokenizer unicode xml zlib -apache2 -argon2 -bcmath -calendar -cdb -cgi -cjk -coverage -curl -debug -embed -enchant -exif -ffi -firebird fpm -ftp -gd -gmp -imap -inifile -intl -iodbc -kerberos -ldap -ldap-sasl -libedit -libressl -lmdb -mhash -mssql -mysql -mysqli -oci8-instant-client -odbc -pcntl -pdo -phpdbg -postgres -qdbm -selinux -session-mm -sharedmem -snmp -soap -sockets -sodium -spell -sqlite -systemd -sysvipc -test -threads -tidy -tokyocabinet -truetype -webp -xmlreader -xmlrpc -xmlwriter -xpm -xslt -zip” > /etc/portage/package.use/php
echo “app-eselect/eselect-php fpm” » /etc/portage/package.use/php

We compile PHP:

emerge -av dev-lang/php

We adjust the FPM socket path and the user it will run with:

vi /etc/php/fpm-php7.4/fpm.d/www.conf

listen = /run/php-fpm.socket  
listen.owner = nginx

The final configuration for the FPM pool is as follows:

egrep -v ‘^($|[[:space:]]*#|;)’ /etc/php/fpm-php7.4/fpm.d/www.conf

[www]
user = nobody
group = nobody
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
listen = /run/php-fpm.socket
listen.owner = nginx

We adjust the PHP timezone:

vi /etc/php/fpm-php7.4/php.ini

date.timezone = Europe/Madrid

We start PHP-FPM:

/etc/init.d/php-fpm start
rc-update add php-fpm default

We configure Nginx to use FPM:

vi /etc/nginx/nginx.conf

    server {
    ...
         location ~ \.php$ {
             # Test for non-existent scripts or throw a 404 error
             # Without this line, nginx will blindly send any request ending in .php to php-fpm
             try_files $uri =404;
             include /etc/nginx/fastcgi.conf;
             fastcgi_pass unix:/run/php-fpm.socket;
        }
    }

We restart the service:

/etc/init.d/nginx restart

We generate a test php file:

echo “” > /var/www/localhost/htdocs/info.php

We check that it works:

curl http://localhost/info.php

We delete the file to avoid problems:

rm /var/www/localhost/htdocs/info.php

The entire Nginx configuration would be as follows:

egrep -v ‘^($|[[:space:]]*#|;)’ /etc/nginx/nginx.conf

user nginx nginx;
worker_processes 1;
error_log /var/log/nginx/error_log info;
events {
    worker_connections 1024;
    use epoll;
}
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main
        '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $bytes_sent '
        '"$http_referer" "$http_user_agent" '
        '"$gzip_ratio"';
    client_header_timeout 10m;
    client_body_timeout 10m;
    send_timeout 10m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 2k;
    request_pool_size 4k;
    gzip off;
    output_buffers 1 32k;
    postpone_output 1460;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 75 20;
    ignore_invalid_headers on;
    index index.html;
    server {
        listen 80;
        server_name localhost;
        access_log /var/log/nginx/localhost.access_log main;
        error_log /var/log/nginx/localhost.error_log info;
        root /var/www/localhost/htdocs;
        location ~ \.php$ {
                    try_files $uri =404;
                    include /etc/nginx/fastcgi.conf;
                    fastcgi_pass unix:/run/php-fpm.socket;
        }
    }
}

We restart the service:

/etc/init.d/nginx restart

If you liked the article, you can treat me to a RedBull here