Phpfpm: Difference between revisions

From NixOS Wiki
imported>Makefu
add outdated tag
imported>Florianjacob
update to 19.09 phpfpm syntax
Line 1: Line 1:
php-fpm is a fastcgi interface for php.
php-fpm is a fastcgi interface for php.


{{outdated|phpfpm has a more granuar configuration via [https://nixos.org/nixos/options.html#phpfpm.pools services.phpfpm.pools]}}
== Configuration for nginx==
== Configuration for nginx==
This configuration will set up phpfpm for serving php files from <code>/var/www/example.com</code>.
This configuration will set up phpfpm for serving php files from <code>/var/www/example.com</code>.
Line 7: Line 6:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{
{ pkgs, lib, config, ... }:
  services.phpfpm.poolConfigs."example.com" = ''
let
        listen = /var/run/example.com-phpfpm.sock
  app = "phpdemo";
        user = nginx
  domain = "${app}.example.com";
        group = nginx
  dataDir = "/srv/http/${domain}";
        pm = dynamic
in {
        pm.max_children = 32
  services.phpfpm.pools.${app} = {
        pm.max_requests = 500
    user = app;
        pm.start_servers = 2
    settings = {
        pm.min_spare_servers = 2
      "listen.owner" = config.services.nginx.user;
        pm.max_spare_servers = 5
      "pm" = "dynamic";
        listen.owner = nginx
      "pm.max_children" = 32;
        listen.group = nginx
      "pm.max_requests" = 500;
        php_admin_value[error_log] = 'stderr'
      "pm.start_servers" = 2;
        php_admin_flag[log_errors] = on
      "pm.min_spare_servers" = 2;
        env[PATH] = ${lib.makeBinPath [ pkgs.php ]}
      "pm.max_spare_servers" = 5;
        catch_workers_output = yes
      "php_admin_value[error_log]" = "stderr";
   '';
      "php_admin_flag[log_errors]" = true;
      "catch_workers_output" = true;
    };
    phpEnv."PATH" = lib.makeBinPath [ pkgs.php ];
   };
   services.nginx = {
   services.nginx = {
     enable = true;
     enable = true;
     virtualHosts."example.com".locations."/" = {
     virtualHosts.${domain}.locations."/" = {
       root = "/var/www/example.com";
       root = dataDir;
       extraConfig = ''
       extraConfig = ''
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/example.com-phpfpm.sock;
        fastcgi_pass unix:${config.services.phpfpm.pools.${app}.socket};
        include ${pkgs.nginx}/conf/fastcgi_params;
        include ${pkgs.nginx}/conf/fastcgi_params;
        include ${pkgs.nginx}/conf/fastcgi.conf;
        include ${pkgs.nginx}/conf/fastcgi.conf;
      '';
      '';
     };
     };
  };
  };
  };
  users.users.${app} = {
    isSystemUser = true;
    createHome = true;
    home = dataDir;
    group = app;
  };
  users.groups.${app} = {};
}
}
</syntaxHighlight>
</syntaxHighlight>
Line 43: Line 52:
== PHP Extensions ==
== PHP Extensions ==


To use certain PHP extensions you will need to configure them in the <code>php.ini</code>-configuration of phpfpm:
To use certain PHP extensions you will need to configure them in the <code>php.ini</code>-configuration of phpfpm via <code>services.phpfpm.phpOptions</code> or <code>services.phpfpm.pools.${pool}.phpOptions</code>:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{
{

Revision as of 21:57, 28 October 2019

php-fpm is a fastcgi interface for php.

Configuration for nginx

This configuration will set up phpfpm for serving php files from /var/www/example.com. put into your configuration.nix

{ pkgs, lib, config, ... }:
let
  app = "phpdemo";
  domain = "${app}.example.com";
  dataDir = "/srv/http/${domain}";
in {
  services.phpfpm.pools.${app} = {
    user = app;
    settings = {
      "listen.owner" = config.services.nginx.user;
      "pm" = "dynamic";
      "pm.max_children" = 32;
      "pm.max_requests" = 500;
      "pm.start_servers" = 2;
      "pm.min_spare_servers" = 2;
      "pm.max_spare_servers" = 5;
      "php_admin_value[error_log]" = "stderr";
      "php_admin_flag[log_errors]" = true;
      "catch_workers_output" = true;
    };
    phpEnv."PATH" = lib.makeBinPath [ pkgs.php ];
  };
  services.nginx = {
    enable = true;
    virtualHosts.${domain}.locations."/" = {
      root = dataDir;
      extraConfig = ''
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:${config.services.phpfpm.pools.${app}.socket};
        include ${pkgs.nginx}/conf/fastcgi_params;
        include ${pkgs.nginx}/conf/fastcgi.conf;
      '';
     };
  };
  users.users.${app} = {
    isSystemUser = true;
    createHome = true;
    home = dataDir;
    group  = app;
  };
  users.groups.${app} = {};
}

PHP Extensions

To use certain PHP extensions you will need to configure them in the php.ini-configuration of phpfpm via services.phpfpm.phpOptions or services.phpfpm.pools.${pool}.phpOptions:

{
  services.phpfpm.phpOptions = ''
    extension=${pkgs.phpPackages.redis}/lib/php/extensions/redis.so
    extension=${pkgs.phpPackages.apcu}/lib/php/extensions/apcu.so
  '';
}