本文共 11097 字,大约阅读时间需要 36 分钟。
(FastCGI Process Manager) is an alternative to FastCGI implementation of PHP with some additional features useful for sites with high traffic. It is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php
for running a PHP script.
(FastCGI流程管理器)是PHP的FastCGI实现的替代方法,具有一些附加功能,可用于高流量站点。 它是使用NGINX处理PHP页面的首选方法,并且比传统的基于CGI的方法(例如SUPHP或mod_php
运行PHP脚本要快。
The main advantage of using PHP-FPM is that it uses a considerable amount of less memory and CPU as compared with any other methods of running PHP. The primary reason is that it demonizes PHP, thereby transforming it to a background process while providing a CLI script for managing PHP request.
使用PHP-FPM的主要优点是,与运行PHP的任何其他方法相比,它使用大量的内存和CPU。 主要原因是它妖魔化了PHP,从而将其转换为后台进程,同时提供了用于管理PHP请求的CLI脚本。
Nginx doesn’t know how to run a PHP script of its own. It needs a PHP module like PHP-FPM to efficiently manage PHP scripts. PHP-FPM, on the other hand, runs outside the NGINX environment by creating its own process. Therefore when a user requests a PHP page the nginx server will pass the request to PHP-FPM service using FastCGI.
Nginx不知道如何运行自己PHP脚本。 它需要像PHP-FPM这样PHP模块来有效地管理PHP脚本。 另一方面,PHP-FPM通过创建自己的进程在NGINX环境之外运行。 因此,当用户请求一个PHP页面时,nginx服务器将使用FastCGI将请求传递给PHP-FPM服务。
The installation of php-fpm in Ubuntu 18.04 depends on PHP and its version. Check the documentation of installed PHP before proceeding with installing FPM in your server. Assuming you have already installed the latest PHP 7.3, then you can install FPM using the following apt-get command.
在Ubuntu 18.04中php-fpm的安装取决于PHP及其版本。 在继续在服务器中安装FPM之前,请检查已安装PHP的文档。 假设您已经安装了最新PHP 7.3,则可以使用以下apt-get命令安装FPM。
# apt-get install php7.3-fpm
The FPM service will start automatically, once the installation is over. You can verify that using the following systemd command:
安装结束后,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
The php-fpm service creates a default pool, the configuration (www.conf) for which can be found in /etc/php/7.3/fpm/pool.d
folder. You can customize the default pool as per your requirements. But it is a standard practice to create separate pools to have better control over resource allocation to each FPM processes.
php-fpm服务创建一个默认池,该默认池(www.conf)可以在/etc/php/7.3/fpm/pool.d
文件夹中找到。 您可以根据需要自定义默认池。 但是,创建单独的池以更好地控制对每个FPM流程的资源分配是一种标准做法。
Furthermore, segregating FPM pool will enable them to run independently by creating its own master process. That means each php application can be configured with its own cache settings using PHP-FPM. A change in one pool’s configuration does not require you to start or stop the rest of the FPM pools.
此外,隔离FPM池将通过创建自己的主进程使它们独立运行。 这意味着可以使用PHP-FPM为每个php应用程序配置其自己的缓存设置。 更改一个池的配置不需要您启动或停止其余FPM池。
Let us create an FPM pool for running a PHP application effectively through a separate user. To start with, create a new user who will have exclusive rights over this pool:
让我们创建一个FPM池,以通过单独的用户有效地运行PHP应用程序。 首先,创建一个新用户,该用户将对该池具有独占权限:
# groupadd wordpress_user# useradd -g wordpress_user wordpress_user
Now navigate to the FPM configuration directory and create a configuration file using your favorite text editor like vi:
现在,导航到FPM配置目录,并使用喜欢的文本编辑器(如vi)创建配置文件:
# cd /etc/php/7.3/fpm/pool.d# vi wordpress_pool.conf[wordpress_site]user = wordpress_usergroup = wordpress_userlisten = /var/run/php7.2-fpm-wordpress-site.socklisten.owner = www-datalisten.group = www-dataphp_admin_value[disable_functions] = exec,passthru,shell_exec,systemphp_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
The above FPM configuration options and their values are described below.
上面的FPM配置选项及其值如下所述。
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
.
除了上述设置外,还可以使用诸如env['PHP_FOO'] = $bar
类的一些系统环境变量传递给php-fpm服务。
For example, adding the following options in the above configuration file will set the hostname and temporary folder location to the PHP environment.
例如,在上述配置文件中添加以下选项,会将主机名和临时文件夹位置设置为PHP环境。
......env[HOSTNAME] = $HOSTNAMEenv[TMP] = /tmp......
Also, the process managers settings in the above pool configuration file are set to dynamic. Choose a setting that best suits your requirement.
另外,上述池配置文件中的流程管理器设置被设置为动态。 选择最适合您要求的设置。
The other configuration options for process manager are:
流程管理器的其他配置选项是:
Once you are done with creating the above configuration file, restart fpm service to apply new settings:
创建完上述配置文件后,请重新启动fpm服务以应用新设置:
# systemctl start php7.3-fpm
The FPM pool will be created immediately to serve php pages. Remember, you can create a separate systemd service by specifying the above FPM configuration file thereby enabling you to start/stop this pool without affecting other pools.
FPM池将立即创建以服务php页面。 请记住,可以通过指定上面的FPM配置文件来创建单独的systemd服务,从而使您能够启动/停止该池而不会影响其他池。
Now create an NGINX server block that will make use of the above FPM pool. To do that, edit your NGINX configuration file and pass the path of pool’s socket file using the option fastcgi_pass
inside location block for php.
现在创建一个将使用上述FPM池的NGINX服务器块。 为此,请编辑您的NGINX配置文件,并使用php的location块内部的fastcgi_pass
选项传递池套接字文件的路径。
server { listen 80; server_name example.journaldev.com; root /var/www/html/wordpress; access_log /var/log/nginx/example.journaldev.com-access.log; error_log /var/log/nginx/example.journaldev.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; }}
Make sure the above configuration setting is syntactically correct and restart NGINX.
确保以上配置设置在语法上正确,然后重新启动NGINX。
# nginx-t# systemctl restart nginx
To test if the above NGINX configuration file is indeed using the newly created FPM pool, create a php info file inside the web root. I have used /var/www/html/wordpress
as a web root in the above NGINX configuration file. Adjust this value according to your environment.
要测试以上NGINX配置文件是否确实在使用新创建的FPM池,请在Web根目录内创建一个php信息文件。 我已经在上面的NGINX配置文件中将/var/www/html/wordpress
用作网络根目录。 根据您的环境调整此值。
# cd /var/www/html/wordpress# echo " " > info.php
Once you are done with creating the PHP info page, point your favorite web browser to it.
创建完PHP信息页面后,将您喜欢的Web浏览器指向该页面。
You will notice that the value of $_SERVER['USER']
and $_SERVER['HOME']
variable are pointing to wordpress_user
and /home/wordpress_user
respectively that we set in the FPM configuration file previously and thus confirms that the NGINX is serving the php pages using our desired FPM pool.
您会注意到$_SERVER['USER']
和$_SERVER['HOME']
变量的值分别指向我们之前在FPM配置文件中设置的wordpress_user
和/home/wordpress_user
,从而确认NGINX正在提供服务使用我们所需的FPM池的php页面。
In this article, we learned how to install php-fpm and configure separate pools for different users and applications. We also learned how to configure an NGINX server block to connect to a PHP-FPM service. PHP-FPM provides reliability, security, scalability, and speed along with a lot of performance tuning options. You can now split the default PHP-FPM pool into multiple resource pools to serve different applications. This will not only enhance your server security but also enable you to allocate server resources optimally!
在本文中,我们学习了如何安装php-fpm以及为不同的用户和应用程序配置单独的池。 我们还学习了如何配置NGINX服务器块以连接到PHP-FPM服务。 PHP-FPM提供可靠性,安全性,可伸缩性和速度,以及许多性能调整选项。 现在,您可以将默认PHP-FPM池拆分为多个资源池,以服务于不同的应用程序。 这不仅可以增强服务器的安全性,还可以使您最佳地分配服务器资源!
翻译自:
转载地址:http://avqzd.baihongyu.com/