PHP

From NixOS Wiki
Jump to: navigation, search

Since the first of April, an official wiki has been created and you can find up-to-date information at: https://wiki.nixos.org/wiki/PHP

This page will no longer be maintained as of today.

Install

  environment.systemPackages = with pkgs; [ php ];

See nix search php (nix search nixpkgs php with Flakes) for additional versions like php74, etc.


Configuration

Setting custom php.ini configurations

The `buildEnv` attribute on php can add extra configuration options. For instance, to set a memory_limit in the NixOS configuration.nix:

environment.systemPackages =
  let
    php = pkgs.php.buildEnv { extraConfig = "memory_limit = 2G"; };
  in [
    php
  ];

In case of using php-fpm, the following example enables error reporting. Use this only in development environments

services.phpfpm.phpOptions = ''
  display_errors = on;
'';

Setting custom plugins and php.ini configurations

In this example we install the xdebug extension, and add a php.ini directive to enable it.

environment.systemPackages = [
  (pkgs.php.buildEnv {
    extensions = ({ enabled, all }: enabled ++ (with all; [
      xdebug
    ]));
    extraConfig = ''
      xdebug.mode=debug
    '';
  })
];


Apache, plugins, settings

Here's how to configure Apache to use a particular PHP configuration/version/etc

# in /etc/nixos/configuration.nix (not inside systemPackages)
services.httpd.phpPackage = pkgs.php.buildEnv {
    extensions = ({ enabled, all }: enabled ++ (with all; [
        xdebug
    ]));
    extraConfig = ''
        xdebug.mode=debug
    '';
};

Troubleshooting

Memcached Extension Isn't Enabled

Using phpExtensions.memcached inside of environment.systemPackages will lead to the memcached php extension not being enabled in the php.ini file. Here's how to fix it:

let
  # Replace pkgs.php with the php version you want; ex pkgs.php83
  php = pkgs.php.buildEnv {
    extensions = { enabled, all }: enabled ++ (with all; [ memcached ]); 
  };
in {
  environment.systemPackages = with pkgs; [ php ];
}