Caddy + PHPfpm crashes randomly after RAM fills up

I made a little nixOS config for my test webservers to test NixOS as a Caddy and PHP-fpm setup

I am encountering a fatal issue in which the server’s PHPfpm will crash randomly, sometimes after the RAM fills up, despite allocating less than the max’s resources. The more sites on it, the more it crashes. This has kept me from deploying it into production, as it does not recover when it crashes and requires a full OS reboot.

Here is my lcmp.nix config file;


{ config, pkgs, ... }:

{
  # Caddy Webserver with PHP-FPM
  services.caddy = {
    enable = true;
    user = "www-data";
    group = "www-data";
    virtualHosts."example.com" = {
    extraConfig = ''
      root    * /var/www/example
      file_server
      php_fastcgi unix/var/run/phpfpm/caddy.sock
    '';
    };
  };
  # Ensure required webserver ports are open
  networking.firewall.allowedTCPPorts = [ 80 443];

  # MariaDB SQL
  services.mysql = {
  enable = true;
  package = pkgs.mariadb;
  };

  # PHP packages
  environment.systemPackages = with pkgs; [
    php81
  ];

  # PHP-FPM on required socketpath
  services.phpfpm.pools = {
  caddy = {
    phpPackage = pkgs.php81;
    user = "www-data";
    group = "www-data";
    phpOptions = ''
        upload_max_filesize = 64M
        post_max_size = 64M
        max_execution_time = 300
        max_input_time = 300
    '';
    settings = {
      "pm" = "dynamic";
      "pm.max_children" = 75;
      "pm.start_servers" = 10;
      "pm.min_spare_servers" = 5;
      "pm.max_spare_servers" = 20;
      "pm.max_requests" = 500;
    };
   };
  };

 # Create required user for PHP-FPM and Caddy
 users.users.www-data = {
  isSystemUser = true;
  home = "/var/lib/caddy";
  createHome = false;
  description = "System user for web services";
  group = "www-data";
  extraGroups = [ "root" ];
 };

 # Create the required group for PHP-FPM and Caddy
 users.groups.www-data = {};

}

turn on php fpm debug logging and see what happens, i found that very useful in the past

I did, but found nothing out of the ordinary. It appears to just go down.

I will check again and see what I find

I enabled logging for PHP-FPM and let it crash again, and it didn’t show a single line added to the log. Very strange.

that sounds like debug logging isn’t turned on then

when debug logging is turned on you get at least a ping entry in the logs every ~10 seconds or so

what configuration did you use?

cc @drupol in case he is able to provide any additional information off the top of his head :bowing_man:

Here is the full config with my latest changes:

{ config, pkgs, ... }:

{
  # Caddy Webserver with PHP-FPM
  services.caddy = {
    enable = true;
    user = "www-data";
    group = "www-data";
    virtualHosts."testwebsiteurl.com" = {
    extraConfig = ''
      root    * /var/www/web-test
      uri path_regexp ^/[a-zA-Z0-9]+/wp- /wp-
      file_server
      php_fastcgi 127.0.0.1:9000
    '';

  };
  # Ensure required webserver ports are open
  networking.firewall.allowedTCPPorts = [ 80 443];

  # MariaDB SQL
  services.mysql = {
  enable = true;
  package = pkgs.mariadb;
  };

  # PHP packages
  environment.systemPackages = with pkgs; [
    php81
    wp-cli
  ];

  # PHP-FPM on required socketpath
  services.phpfpm.pools = {
  caddy = {
    phpPackage = pkgs.php81;
    listen = "127.0.0.1:9000";
    phpOptions = ''
        upload_max_filesize = 64M
        post_max_size = 128M
        max_execution_time = 45
        max_input_time = 45
	memory_limit = 1024M
	max_input_vars = 2000
	log_level = debug
        error_log = /var/log/php-fpm-debug.log
        catch_workers_output = yes
    '';
    user = "www-data";
    group = "www-data";
    settings = {
      "pm" = "ondemand";
      "pm.max_children" = 2;
      "pm.start_servers" = 1;
      # "pm.min_spare_servers" = 1;
      # "pm.max_spare_servers" = 2;
      "pm.max_requests" = 500;
    };
   };
  };

 # Create required user for PHP-FPM and Caddy
 users.users.www-data = {
  isSystemUser = true;
  home = "/var/lib/caddy";
  createHome = false;
  description = "System user for web services";
  group = "www-data";
  extraGroups = [ "root" ];
 };

 # Create the required group for PHP-FPM and Caddy
 users.groups.www-data = {};

}

Not sure what I am doing wrong, or if it is a bug (I suspect the former, not the latter)