如何使用NGINX配置PHP-FPM
PHP-FPM(FastCGI进程管理器)是PHP的FastCGI实现的一种替代方案,具有一些适用于高流量网站的额外功能。它是使用NGINX处理PHP页面的首选方法,并且比基于传统CGI的方法(如SUPHP或mod_php)更快速地运行PHP脚本。使用PHP-FPM的主要优势是它使用相对较少的内存和CPU资源与其他任何运行PHP的方法相比。主要原因是它使PHP变成了后台进程,同时提供了一个CLI脚本来管理PHP请求。
PHP-FPM NGINX配置前提要求
- You can open a SSH session to your Ubuntu 18.04 system using root or a sudo enabled user.
- You have already installed NGINX and PHP in your Ubuntu 18.04 system.
NGINX PHP-FPM 配置步骤:
- Install PHP-FPM
- Configure PHP-FPM Pool
- Configure NGINX for PHP-FPM
- Test NGINX PHP-FPM Configuration
安装PHP-FPM。
Nginx不知道如何运行自己的PHP脚本。它需要一个像PHP-FPM这样的PHP模块来高效地管理PHP脚本。另一方面,PHP-FPM通过创建自己的进程在NGINX环境之外运行。因此,当用户请求一个PHP页面时,nginx服务器将使用FastCGI将请求传递给PHP-FPM服务。在Ubuntu 18.04中安装php-fpm取决于PHP及其版本。在继续在服务器上安装FPM之前,请检查已安装的PHP的文档。假设您已经安装了最新的PHP 7.3,然后可以使用以下apt-get命令安装FPM。
# apt-get install php7.3-fpm
一旦安装完成,FPM服务将自动启动。您可以使用以下systemd命令进行验证。
# systemctl status php7.3-fpm
● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-02-17 06:29:31 UTC; 30s ago
Docs: man:php-fpm7.3(8)
Main PID: 32210 (php-fpm7.3)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 1152)
CGroup: /system.slice/php7.3-fpm.service
├─32210 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
├─32235 php-fpm: pool www
└─32236 php-fpm: pool www
2. 配置PHP-FPM池
php-fpm服务会创建一个默认的池子,其配置文件(www.conf)可以在/etc/php/7.3/fpm/pool.d文件夹中找到。您可以根据需要自定义默认池子。但是,通常的做法是创建单独的池子,以便更好地控制资源分配给每个FPM进程。此外,分离FPM池子将使它们能够通过创建自己的master进程独立运行。这意味着每个PHP应用程序可以使用PHP-FPM来配置自己的缓存设置。对一个池子的配置的更改不需要您启动或停止其他FPM池子。让我们为有效地运行PHP应用程序创建一个FPM池子,通过一个独立的用户来运行。首先,创建一个新用户,该用户将对这个池子拥有独占权限。
# groupadd wordpress_user
# useradd -g wordpress_user wordpress_user
现在切换到FPM配置目录并使用你最喜欢的文本编辑器(如vi)创建一个配置文件。
# cd /etc/php/7.3/fpm/pool.d
# vi wordpress_pool.conf
[wordpress_site]
user = wordpress_user
group = wordpress_user
listen = /var/run/php7.2-fpm-wordpress-site.sock
listen.owner = www-data
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
; Choose how the process manager will control the number of child processes.
pm = dynamic
pm.max_children = 75
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.process_idle_timeout = 10s
上述的 FPM 配置选项及其取值如下所述。
- [wordpress_site]: The name of the pool and must be unique across all pool names.
- user and group: The user and group under which the pool will run.
- listen: The name of the socket file for this pool.
- listen.owner and listen.group: Must match to the user and group on which NGINX is running. In our case it is www-data.
- php_admin_value: Allows to set custom php configuration values.
- php_admin_flag: Allows to set PHP boolean flags.
- pm: The process manager settings and the value is Dynamic means the number of child processes are set dynamically based on the following directives.
- pm.max_children: The maximum number of children that can be alive at the same time.
- pm.start_servers: The number of children created on startup.
- pm.min_spare_servers: The minimum number of children in ‘idle’ state (waiting to process). If the number of idle processes is less than this number then some children will be created.
- pm.max_spare_servers: The maximum number of children in idle state (waiting to process). If the number of idle processes is greater than this number then some children will be killed.
- pm.process_idle_timeout: The desired maximum number of idle server processes. Used only when pm value is set to dynamic.
Apart from above settings, it is also possible to pass few system environmental variable to php-fpm service using something like env[‘PHP_FOO’] = $bar. For example, adding the following options in the above configuration file will set the hostname and temporary folder location to the PHP environment.
...
...
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
...
...
此外,在上述的池配置文件中,进程管理器的设置被设置为动态。选择一个最符合您要求的设置。进程管理器的其他配置选项有:静态:将维护固定数量的PHP进程。
- ondemand: No children are created at startup. Children will be forked when new requests are received in the server.
完成上述配置文件后,重新启动fpm服务以应用新的设置。
# systemctl start php7.3-fpm
FPM池将立即创建,用于服务php页面。请记住,您可以通过指定上述FPM配置文件来创建一个单独的systemd服务,从而使您能够在不影响其他池的情况下启动/停止该池。
3. 为PHP-FPM配置NGINX
现在创建一个NGINX服务器块,将利用上述的FPM池。为了做到这一点,编辑您的NGINX配置文件,并在php的location块中使用fastcgi_pass选项传递池的套接字文件的路径。
server {
listen 80;
server_name example.scdev.com;
root /var/www/html/wordpress;
access_log /var/log/nginx/example.scdev.com-access.log;
error_log /var/log/nginx/example.scdev.com-error.log error;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php7.2-fpm-wordpress-site.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}

确保上述配置设置在语法上正确,并重新启动NGINX。
# nginx-t
# systemctl restart nginx
4. 测试 PHP-FPM 和 NGINX 的配置。
为了测试上述NGINX配置文件是否确实使用了新创建的FPM池,请在Web根目录中创建一个php信息文件。我在上述NGINX配置文件中使用了/var/www/html/wordpress作为Web根目录。根据你的环境调整该值。
# cd /var/www/html/wordpress
# echo "<?php echo phpinfo();?>" > info.php
当你创建完PHP信息页面后,将你喜欢的网页浏览器指向它。你会注意到$_SERVER[‘USER’]和$_SERVER[‘HOME’]变量的值分别指向之前我们在FPM配置文件中设置的wordpress_user和/home/wordpress_user,从而证实了NGINX正使用我们所需的FPM池来提供PHP页面。

摘要
在本文中,我们学习了如何安装php-fpm并为不同用户和应用程序配置独立的池。我们还学会了如何配置NGINX服务器块以连接到PHP-FPM服务。PHP-FPM提供了可靠性、安全性、可扩展性和速度,同时还具有许多性能调优选项。现在,您可以将默认的PHP-FPM池拆分为多个资源池以为不同的应用程序提供服务。这不仅可以提升服务器安全性,还可以使您能够更优化地分配服务器资源!