nix-phps icon indicating copy to clipboard operation
nix-phps copied to clipboard

Guide on running these versions with apache (mod php)

Open aszenz opened this issue 1 year ago • 21 comments

Any idea on how we could use these old php version (5.5) on apache

aszenz avatar Feb 08 '24 15:02 aszenz

Hi !

AFAIK, we don't maintain 5.5, we start at 5.6.

drupol avatar Feb 08 '24 15:02 drupol

Hi !

AFAIK, we don't maintain 5.5, we start at 5.6.

Ok, 5.6 is also fine for us.

aszenz avatar Feb 08 '24 15:02 aszenz

First you need to add the overlay of this flake in your own configuration.

In the whole process, this is probably the most complex part. An overlay is just the default standard mechanism to extends pkgs in Nix by adding a layer on top of your configuration so that when you do pkgs.php56, it works.

Then, once it's done, a quick search on Google led me to https://nixos.wiki/wiki/PHP, where I can see a snippet related to Apache.

You would just need to replace pkgs.php with pkgs.php56 and it should be good to go.

drupol avatar Feb 08 '24 15:02 drupol

First you need to add the overlay of this flake in your own configuration.

In the whole process, this is probably the most complex part. An overlay is just the default standard mechanism to extends pkgs in Nix by adding a layer on top of your configuration so that when you do pkgs.php56, it works.

Then, once it's done, a quick search on Google led me to https://nixos.wiki/wiki/PHP, where I can see a snippet related to Apache.

You would just need to replace pkgs.php with pkgs.php56 and it should be good to go.

Could I point an ubuntu installed apache server to use this nix php binary (maybe by just hardcoding the path in nix store)

aszenz avatar Feb 08 '24 15:02 aszenz

Technically it should work, but using raw /nix/store/... paths is a bad practice since it can be garbage collected. I would simply recommend you to add it to your profile using nix profile so now your can be sure that it will never be GC'd.

❯ nix profile install github:fossar/nix-phps#php56
❯ php -v
PHP 5.6.40 (cli) (built: Jan  9 2019 10:25:59) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
   with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
❯ type php
php is /home/pol/.nix-profile/bin/php
❯          

drupol avatar Feb 08 '24 15:02 drupol

Technically it should work, but using raw /nix/store/... paths is a bad practice since it can be garbage collected. I would simply recommend you to add it to your profile using nix profile so now your can be sure that it will never be GC'd.

❯ nix profile install github:fossar/nix-phps#php56
❯ php -v
PHP 5.6.40 (cli) (built: Jan  9 2019 10:25:59) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
   with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
❯ type php
php is /home/pol/.nix-profile/bin/php
❯          

Perfect, so apache would just call nix php automatically since it's on the path

aszenz avatar Feb 08 '24 15:02 aszenz

If there are no other php on your system and if the user running apache is the same user who run nix profile, yes.

drupol avatar Feb 08 '24 15:02 drupol

If there are no other php on your system and if the user running apache is the same user who run nix profile, yes.

Seems like mod-php embeds php within apache, and doesn't call cli process at all, so this idea wouldn't work

aszenz avatar Feb 08 '24 15:02 aszenz

In theory, it should be possible but unless Apache has stable ABI for modules, you would probably also need to ensure that the PHP is linked against the same version of Apache as the one you are running (and possibly other parameters).

I think at that point it would be easier to replace the Ubuntu’s Apache with a one from Nixpkgs.

jtojnar avatar Feb 08 '24 15:02 jtojnar

In theory, it should be possible but unless Apache has stable ABI for modules, you would probably also need to ensure that the PHP is linked against the same version of Apache as the one you are running (and possibly other parameters).

I think at that point it would be easier to replace the Ubuntu’s Apache with a one from Nixpkgs.

Yes but does nixpkgs apache have older versions of mod-php?

aszenz avatar Feb 08 '24 16:02 aszenz

No. But that is what this repo is for.

jtojnar avatar Feb 08 '24 18:02 jtojnar

No. But that is what this repo is for.

Ok, so older modphp versions can be added to this flake?

aszenz avatar Feb 08 '24 18:02 aszenz

If you want to to run Apache from Nix, you are best off looking at the relevant NixOS module. For example, this is how you get PHP with mod_php:

https://github.com/NixOS/nixpkgs/blob/8a3e1cf40a6eaeb122c8321b97a0518cfa6ed779/nixos/modules/services/web-servers/apache-httpd/default.nix#L21

jtojnar avatar Feb 09 '24 11:02 jtojnar

If you want to to run Apache from Nix, you are best off looking at the relevant NixOS module. For example, this is how you get PHP with mod_php:

https://github.com/NixOS/nixpkgs/blob/8a3e1cf40a6eaeb122c8321b97a0518cfa6ed779/nixos/modules/services/web-servers/apache-httpd/default.nix#L21

Yes, but can i replace the php version in them to older ones like 5.6

aszenz avatar Feb 12 '24 09:02 aszenz

Sure, just do as mentioned above https://github.com/fossar/nix-phps/issues/325#issuecomment-1934387999

jtojnar avatar Feb 12 '24 13:02 jtojnar

Sure, just do as mentioned above #325 (comment)

I'm yet to try this overlays approach, it would be handier if it was prepackaged in this flake itself, basically I'm imagining this flake to be a simple way to maintain legacy versions of our software, we are also exploring lxd containers in this space.

aszenz avatar Mar 08 '24 10:03 aszenz

You do not need the overlays approach but it the simplest way to use this flake. Or at least with fewest caveats – you could just use the packages.${system}.php output of the flake directly, but then you would need to make sure the Nixpkgs revision is the same as the one used to built PHP in this flake.

We are not going to include Apache in this flake, that just does not compose.

jtojnar avatar Mar 08 '24 13:03 jtojnar

i don't think what has been discussed in this issue is really the biggest challenge with what @aszenz is attempting to do - generating a configuration file for apache and running the software is going to be the biggest challenge when attempting to run a nix provided apache+php on ubuntu, in my opinion


so how can @aszenz proceed? personally i would suggest using system-manager as it would make running older php versions with apache on ubuntu relatively easy compared to other options

i was able to get the latest version of apache running php version 5.6 on ubuntu in just a few minutes from scratch with the following configuration:

{ nixosModulesPath, config, pkgs, lib, ... }:
{
  imports = [
    "${nixosModulesPath}/services/web-servers/apache-httpd"
    ./compat.nix
  ];

  nixpkgs.hostPlatform = "x86_64-linux";

  services.httpd = {
    enable = true;
    enablePHP = true;
    user = "www-data";
    group = "www-data";
    phpPackage = pkgs.php56;

    virtualHosts."localhost" = {
      documentRoot = "/srv/www";
    };
  };
}

@aszenz if you're interested in exploring this option further i would be happy to provide additional details for you

aanderse avatar Mar 08 '24 16:03 aanderse

i don't think what has been discussed in this issue is really the biggest challenge with what @aszenz is attempting to do - generating a configuration file for apache and running the software is going to be the biggest challenge when attempting to run a nix provided apache+php on ubuntu, in my opinion

so how can @aszenz proceed? personally i would suggest using system-manager as it would make running older php versions with apache on ubuntu relatively easy compared to other options

i was able to get the latest version of apache running php version 5.6 on ubuntu in just a few minutes from scratch with the following configuration:

{ nixosModulesPath, config, pkgs, lib, ... }:
{
  imports = [
    "${nixosModulesPath}/services/web-servers/apache-httpd"
    ./compat.nix
  ];

  nixpkgs.hostPlatform = "x86_64-linux";

  services.httpd = {
    enable = true;
    enablePHP = true;
    user = "www-data";
    group = "www-data";
    phpPackage = pkgs.php56;

    virtualHosts."localhost" = {
      documentRoot = "/srv/www";
    };
  };
}

@aszenz if you're interested in exploring this option further i would be happy to provide additional details for you

Thanks, i tried this on my own nixos system (the server will be ubuntu though) and got this error:

getting status of '/nix/store/jp2kw74bigwv1npgi02n22hqm7dj7r6f-source/compat.nix': No such file or directory

i don't think what has been discussed in this issue is really the biggest challenge with what @aszenz is attempting to do - generating a configuration file for apache and running the software is going to be the biggest challenge when attempting to run a nix provided apache+php on ubuntu, in my opinion

so how can @aszenz proceed? personally i would suggest using system-manager as it would make running older php versions with apache on ubuntu relatively easy compared to other options

i was able to get the latest version of apache running php version 5.6 on ubuntu in just a few minutes from scratch with the following configuration:

{ nixosModulesPath, config, pkgs, lib, ... }:
{
  imports = [
    "${nixosModulesPath}/services/web-servers/apache-httpd"
    ./compat.nix
  ];

  nixpkgs.hostPlatform = "x86_64-linux";

  services.httpd = {
    enable = true;
    enablePHP = true;
    user = "www-data";
    group = "www-data";
    phpPackage = pkgs.php56;

    virtualHosts."localhost" = {
      documentRoot = "/srv/www";
    };
  };
}

@aszenz if you're interested in exploring this option further i would be happy to provide additional details for you

Thanks i created https://github.com/numtide/system-manager/issues/72, initially tested it on NixOS before i can test it on Ubuntu server

aszenz avatar Mar 08 '24 17:03 aszenz

as mentioned - if you're interested in exploring this option further i would be happy to provide additional details for you

i did not include all details so please close that ticket against system-manager - there is no problem there, the problem is that you don't have all the source

i pushed the source here so you can take a look and have a fully running example

aanderse avatar Mar 08 '24 17:03 aanderse

@aszenz i missed the part where you said you were testing on NixOS instead of Ubuntu, sorry.

Did my example help at all? Did you get what you needed?

aanderse avatar Mar 29 '24 13:03 aanderse