linux

LNMP中PHP-FPM配置

小樊
41
2025-05-11 11:10:01
栏目: 编程语言

LNMP是指Linux、Nginx、MySQL和PHP的组合,用于搭建高效的Web服务器。在LNMP架构中,PHP-FPM(FastCGI Process Manager)是一个用于处理PHP脚本的FastCGI实现。以下是配置PHP-FPM的基本步骤:

1. 安装PHP-FPM

首先,确保你的系统上已经安装了PHP和PHP-FPM。你可以使用包管理器来安装它们。例如,在基于Debian的系统上:

sudo apt update
sudo apt install php-fpm

在基于Red Hat的系统上:

sudo yum install php-fpm

2. 配置PHP-FPM

PHP-FPM的配置文件通常位于/etc/php/{version}/fpm/目录下,其中{version}是你的PHP版本号。主要的配置文件是php-fpm.confwww.conf

2.1 php-fpm.conf

这个文件包含了PHP-FPM的全局配置。你可以根据需要进行修改,但通常不需要做太多更改。

; Start a new pool named 'www'.
[www]

; 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 IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.4-fpm.sock

; Set listen(2) backlog.
listen.backlog = -1

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow inter-process communication.
; Common values are '0660' or '0666'.
;listen.owner = www-data
;listen.group = www-data

; When POSIX Access Control Lists are supported you can set them using
; these options, value is a comma separated list of user/group names.
;listen.allow_user = nobody
;listen.allow_group = nogroup

; For IPv6 sockets, this option is needed to allow other hosts to connect.
;listen.ipv6 = 0

; Specifies the maximum number of requests that a worker process will handle
; before it is terminated. If set to 0 no limit will be set. This option is
; useful to work around memory leaks in the PHP-FPM code.
pm.max_requests = 500

; Set the environment variable that will be used when running commands
; (like `php artisan` or `artisan schedule:run`) from the web server.
; Default is nothing.
;env[PHPRC] = /etc/php/{version}/fpm/php.ini

; Set the process manager to dynamic.
pm = dynamic

; The dynamic process manager supports 'ondemand' or 'dynamic' mode.
; In 'ondemand' mode, a new child process is forked only when there is a
; job ready to be processed by the child processes. In this mode,
; the maximum number of child processes that can be created by PHP-FPM
; is equal to the value of the 'pm.max_children' directive.
; In 'dynamic' mode, the 'pm.max_children' directive is ignored and the
; global 'pm.start_servers', 'pm.min_spare_servers' and 'pm.max_spare_servers'
; directives are used to dynamically adjust the number of child processes.
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

; The dynamic process manager uses the following formula to determine how many
; child processes to start:
; (available memory in kilobytes - overhead required by FPM)
; / (1 MB per child)
; If the result is a negative number, then the minimum number of child
; processes will be used ('pm.min_spare_servers').
; If the result is greater than the maximum number of child processes
; ('pm.max_children'), then the maximum number of child processes will be
; used ('pm.max_children').
; If the result is between the values of 'pm.min_spare_servers' and
; 'pm.max_children', then that number of child processes will be
; started.
; Default Values:
; pm.start_servers = pm.min_spare_servers = pm.max_spare_servers = 1/4 * pm.max_children

; Specify the location of the PID file.
pid = /run/php/php7.4-fpm.pid

; Specify the location of the error log file.
error_log = /var/log/php7.4-fpm.log

; Set the user and group under which the PHP-FPM pool will run.
; According to the FPM documentation the recommended user is 'nobody'.
; If you are sure that this is the right user you can change it as follows.
user = www-data
group = www-data

; Set the maximum number of processes FPM will fork off. If this is set to a
; higher value, then the user may see slow response times if the queue is
; very busy. If this is set too low, then FPM may not be able to handle a
; sudden increase in traffic.
pm.max_children = 50

; Set the maximum number of requests a child process will handle before
; being recycled. This can be used to ensure that old processes with memory
; leaks are killed before they use up all available memory.
pm.max_requests = 500

2.2 www.conf

这个文件包含了PHP-FPM的池配置。你需要确保Nginx使用这个池来处理PHP请求。

; Default settings for PHP-FPM static pool named 'www'
[www]

; 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 IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.4-fpm.sock

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow inter-process communication.
listen.owner = www-data
listen.group = www-data

; For IPv6 sockets, this option is needed to allow other hosts to connect.
listen.ipv6 = 0

; Set the process manager to dynamic.
pm = dynamic

; The dynamic process manager supports 'ondemand' or 'dynamic' mode.
; In 'ondemand' mode, a new child process is forked only when there is a
; job ready to be processed by the child processes. In this mode,
; the maximum number of child processes that can be created by PHP-FPM
; is equal to the value of the 'pm.max_children' directive.
; In 'dynamic' mode, the 'pm.max_children' directive is ignored and the
; global 'pm.start_servers', 'pm.min_spare_servers' and 'pm.max_spare_servers'
; directives are used to dynamically adjust the number of child processes.
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

; The dynamic process manager uses the following formula to determine how many
; child processes to start:
; (available memory in kilobytes - overhead required by FPM)
; / (1 MB per child)
; If the result is a negative number, then the minimum number of child
; processes will be used ('pm.min_spare_servers').
; If the result is greater than the maximum number of child processes
; ('pm.max_children'), then the maximum number of child processes will be
; used ('pm.max_children').
; If the result is between the values of 'pm.min_spare_servers' and
; 'pm.max_children', then that number of child processes will be
; started.
pm.max_children = 50

; Set the maximum number of requests a child process will handle before
; being recycled. This can be used to ensure that old processes with memory
; leaks are killed before they use up all available memory.
pm.max_requests = 500

; Set the environment variable that will be used when running commands
; (like `php artisan` or `artisan schedule:run`) from the web server.
env[PHPRC] = /etc/php/{version}/fpm/php.ini

; Set the user and group under which the PHP-FPM pool will run.
user = www-data
group = www-data

; Set the maximum number of processes FPM will fork off. If this is set to a
; higher value, then the user may see slow response times if the queue is
; very busy. If this is set too low, then FPM may not be able to handle a
; sudden increase in traffic.
pm.max_children = 50

3. 配置Nginx

确保Nginx配置文件中正确设置了PHP-FPM的socket或端口。以下是一个示例配置:

server {
    listen 80;
    server_name example.com;

    root /var/www/html;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

4. 重启服务

最后,重启PHP-FPM和Nginx服务以应用更改:

sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx

通过以上步骤,你应该能够成功配置PHP-FPM并与Nginx集成,从而在LNMP架构中处理PHP请求。

0
看了该问题的人还看了