/srv is empty although apache is set up

I’ve freshly migrated from Arch and am running NixOS unstable with plasma. I set up an apache server as follows (i don’t need mysql, just php and don’t want the server to be reachable from outside):

  services.httpd.enable = true;
  services.httpd.enablePHP = true;
  services.httpd.adminAddr = "a@t.com";
  services.httpd.phpPackage = pkgs.php.buildEnv {
    extensions = ({ enabled, all }: enabled ++ (with all; [ xdebug ]));
    extraConfig = ''
      zend_extension=xdebug
      xdebug.mode = debug
      xdebug.start_with_request = yes
      display_errors = on
      display_startup_errors = true
      html_errors = true
      xdebug.discover_client_host = 1
    '';
  };

Opening 127.0.0.1 in a browser shows “It works!”.

How can i access my local Apache server in NixOS (preferrably via /srv as i’m used to)?

You set up an HTTP server, not FTP.

Also, can you please use a triple-backtick code block?

Ah thank you, i wasn’t aware of triple backtick. Looks much cleaner now :slightly_smiling_face:

In NixOS we made a decision to require virtual hosts to keep the code simple. You should add the following:

services.httpd.mpm = "prefork";
services.httpd.virtualHosts.localhost = {
  http2 = false;
  documentRoot = "/srv";
};

I just wrote that config from memory, on a mobile device, after not touching apache for a few months… so if it doesn’t work ping me back and I’ll take a look.

1 Like

I added your lines to configuration.nix and nixos-rebuild switch ran without an error. However, even after a reboot /srv is still empty.

Is there something else i need to do?

This is my config regarding httpd right now:

  services.httpd.enable = true;
  services.httpd.enablePHP = true;
  # services.mysql.enable = true;
  # services.mysql.package = pkgs.mariadb;
  services.httpd.mpm = "prefork";
  services.httpd.virtualHosts.localhost = {
    http2 = false;
    documentRoot = "/srv";
  };
  services.httpd.adminAddr = "a@t.com";
  services.httpd.phpPackage = pkgs.php.buildEnv {
    extensions = ({ enabled, all }: enabled ++ (with all; [ xdebug ]));
    extraConfig = ''
      # zend_extension=xdebug
      xdebug.mode = debug
      xdebug.start_with_request = yes
      display_errors = on
      display_startup_errors = true
      html_errors = true
      xdebug.discover_client_host = 1
    '';
  };

Also (i’m new to NixOS, sorry): do i need to put this into environment.systemPackages as well to have Emacs interface with php correctly or is it sufficient to define it with Apache only?

    (pkgs.php.buildEnv {
      extensions =
        ({ enabled, all }: enabled ++ (with all; [ xdebug ]));
      extraConfig = ''
        # zend_extension=xdebug
        xdebug.mode = debug
        xdebug.start_with_request = yes
        display_errors = on
        display_startup_errors = true
        html_errors = true
        xdebug.discover_client_host = 1
      '';
    })

Also also, is the correct way to install php81Packages.composer as an individual entry under environment.systemPackages or do i need to include it with Apache and the pkgs.php.buildEnv somehow?

Why would you expect there to be anything in /srv? Setting document root just makes Apache look in that directory but it is up to you to create and populate it.

services.httpd.phpPackage is only relevant to Apache, if you want to have PHP & composer available on PATH, you will need to install each of them separately, e.g. through environment.systemPackages.

You will want to do something like the following to have Composer run in the same PHP environment:

let
  php = pkgs.php.buildEnv {
    …
  };
in
{
  environment.systemPackages = [
    php
    php.packages.composer
  ];
}

Though, instead of installing development tools system-wide, it is generally preferred to scope them to each project with nix-shell or something like direnv. Editors like emacs usually have integration for that.

1 Like

Just go ahead and populate it with an index.php now and you’re ready to go. Let me know if you have any issues.

2 Likes

Alright, thank you both. In Arch /srv was automatically populated with two directories, that’s why i thought it wasn’t working.