博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用NGINX配置PHP-FPM
阅读量:2533 次
发布时间:2019-05-11

本文共 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的方法(例如SUPHPmod_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脚本。

PHP-FPM NGINX配置先决条件 (PHP-FPM NGINX Configuration Prerequisites)

  • You can open a SSH session to your Ubuntu 18.04 system using root or a enabled user.

    您可以使用root或启用用户打开到Ubuntu 18.04系统的SSH会话。
  • You have already installed and PHP in your Ubuntu 18.04 system.

    您已经在Ubuntu 18.04系统中安装了和PHP。

NGINX PHP-FPM配置步骤 (NGINX PHP-FPM Configuration Steps)

  • Install PHP-FPM

    安装PHP-FPM
  • Configure PHP-FPM Pool

    配置PHP-FPM池
  • Configure NGINX for PHP-FPM

    为PHP-FPM配置NGINX
  • Test NGINX PHP-FPM Configuration

    测试NGINX PHP-FPM配置

1.安装PHP-FPM (1. Install PHP-FPM)

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

2.配置PHP-FPM池 (2. Configure PHP-FPM Pool)

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配置选项及其值如下所述。

  • [wordpress_site]: The name of the pool and must be unique across all pool names.

    [wordpress_site] :池的名称,并且在所有池名称中必须唯一。
  • user and group: The user and group under which the pool will run.

    user and group :将在其下运行池的用户和组。
  • listen: The name of the socket file for this pool.

    listen :此池的套接字文件的名称。
  • listen.owner and listen.group: Must match to the user and group on which NGINX is running. In our case it is www-data.

    listen.owner和listen.group :必须与运行NGINX的用户和组匹配。 在我们的例子中是www-data。
  • php_admin_value: Allows to set custom php configuration values.

    php_admin_value :允许设置自定义php配置值。
  • php_admin_flag: Allows to set PHP boolean flags.

    php_admin_flag :允许设置PHP布尔标志。
  • 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 :进程管理器设置,其值为Dynamic表示根据以下指令动态设置子进程的数量。
  • pm.max_children: The maximum number of children that can be alive at the same time.

    pm.max_children :可以同时存活的最大孩子数。
  • pm.start_servers: The number of children created on startup.

    pm.start_servers :启动时创建的子级数
  • 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.min_spare_servers :处于“空闲”状态(等待处理)的最小子代数 。 如果空闲进程的数目小于此数目,则将创建一些子进程。
  • 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.max_spare_servers :处于空闲状态(等待处理)的最大子代数 。 如果空闲进程的数量大于该数量,那么一些孩子将被杀死。
  • pm.process_idle_timeout: The desired maximum number of idle server processes. Used only when pm value is set to dynamic.

    pm.process_idle_timeout :所需的最大空闲服务器进程数。 仅当pm值设置为动态时使用。
  • 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:

流程管理器的其他配置选项是:

  • Static: A fixed number of PHP processes will be maintained.

    静态的 :将维护固定数量PHP进程。
  • ondemand: No children are created at startup. Children will be forked when new requests are received in the server.

    ondemand :启动时不会创建任何子级。 当服务器中收到新请求时,将对子节点进行分叉。
  • 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服务,从而使您能够启动/停止该池而不会影响其他池。

    3.为PHP-FPM配置NGINX (3. Configure NGINX for PHP-FPM)

    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;    }}
    NGINX Server Block PHP-FPM

    NGINX Server Block

    NGINX服务器块

    Make sure the above configuration setting is syntactically correct and restart NGINX.

    确保以上配置设置在语法上正确,然后重新启动NGINX。

    # nginx-t# systemctl restart nginx

    4.测试PHP-FPM NGINX配置 (4. Test PHP-FPM NGINX Configuration)

    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页面。

    NGINX PHP-FPM Test Php Info Page

    NGINX PHP-FPM Testing

    NGINX PHP-FPM测试

    摘要 (Summary)

    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/

你可能感兴趣的文章
大一上学期C语言学习总结
查看>>
CI框架 QQ接口(第三方登录接口PHP版)
查看>>
软件工程 speedsnail 第二次冲刺9
查看>>
文件的上传(可以上传照片,word文档,等单个文件)
查看>>
NI Vision for LabVIEW 基础(一):NI Vision 简介
查看>>
BZOJ 4804: 欧拉心算 欧拉函数
查看>>
在WIN7、WIN10操作系统用WebDAV映射网络驱动器需要的操作
查看>>
(转)Elasticsearch聚合初探——metric篇
查看>>
安卓中常用快捷键
查看>>
系统设计与构架
查看>>
浅谈值类型和引用类型
查看>>
[转] Centos 系统swap虚拟内存添加与删除配置
查看>>
零碎的知识点及常用特效
查看>>
单例设计模式中懒汉式线程安全的处理
查看>>
子集生成
查看>>
SRT字幕格式
查看>>
解决在Win10上无法安装Aurora的问题
查看>>
Javascript小知识点
查看>>
桌面程序开发语言选择易语言还是快手aauto
查看>>
使用delphi 10.2 开发linux 上的webservice
查看>>