Phpfpm: Difference between revisions

From NixOS Wiki
imported>Florianjacob
m correct surrounding text
imported>Sikmir
No edit summary
Line 56: Line 56:
{
{
   services.phpfpm.phpOptions = ''
   services.phpfpm.phpOptions = ''
     extension=${pkgs.phpPackages.redis}/lib/php/extensions/redis.so
     extension=${pkgs.phpExtensions.redis}/lib/php/extensions/redis.so
     extension=${pkgs.phpPackages.apcu}/lib/php/extensions/apcu.so
     extension=${pkgs.phpExtensions.apcu}/lib/php/extensions/apcu.so
   '';
   '';
}
}
</syntaxHighlight>
</syntaxHighlight>

Revision as of 05:55, 4 June 2023

php-fpm is a fastcgi interface for php.

Configuration for nginx

This configuration will set up phpfpm for serving php files from ${dataDir}. Import this from 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.phpExtensions.redis}/lib/php/extensions/redis.so
    extension=${pkgs.phpExtensions.apcu}/lib/php/extensions/apcu.so
  '';
}