PHP: Difference between revisions

imported>Drupol
m fix nix typo
imported>Drupol
No edit summary
Line 64: Line 64:
         xdebug.mode=debug
         xdebug.mode=debug
     '';
     '';
};
</syntaxhighlight>
== OCI image ==
Here's an example on how to build an OCI image running a PHP application:
Using nginx:
<syntaxhighlight lang="nix>
packages = let
  src = ./.;
  php = pkgs.php81;
in {
  app = php.buildComposerProject {
    inherit src;
    pname = "app-demo";
    version = "1.0.0";
    vendorHash = "sha256-SrE51k3nC5idaDHNxiNM7NIbIERIf8abrCzFEdxOQWA=";
  };
  oci-image = let
    nginxPort = "8000";
    nginxWebRoot = "${self'.packages.app}/share/php/app-demo/public";
    nginxConf = pkgs.writeText "nginx.conf" ''
      user nobody nobody;
      daemon off;
      error_log /dev/stdout info;
      pid /dev/null;
      events {}
      http {
        access_log /dev/stdout;
        server {
          listen ${nginxPort};
          index index.php index.html;
          charset utf-8;
          add_header X-Frame-Options "SAMEORIGIN";
          add_header X-Content-Type-Options "nosniff";
          location / {
            root ${nginxWebRoot};
            try_files $uri $uri/ /index.php?$query_string;
          }
          location ~ \.php$ {
            root ${nginxWebRoot};
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include ${pkgs.nginx}/conf/fastcgi_params;
            include ${pkgs.nginx}/conf/fastcgi.conf;
          }
        }
      }
    '';
  in
  pkgs.dockerTools.buildLayeredImage {
    name = self'.packages.app.pname;
    tag = "latest";
    contents = [
      php
      pkgs.nginx
      pkgs.fakeNss
      (pkgs.writeScriptBin "start-server" ''
        #!${pkgs.runtimeShell}
        php-fpm -y /etc/php-fpm.d/www.conf.default & nginx -c ${nginxConf};
      '')
    ];
    extraCommands = ''
      mkdir -p var/log/nginx
      mkdir -p var/cache/nginx
      mkdir -p tmp
      chmod 1777 tmp
    '';
    config = {
      Cmd = [ "start-server" ];
      ExposedPorts = {
        "${nginxPort}/tcp" = {};
      };
    };
  };
};
</syntaxhighlight>
Using Caddy:
<syntaxhighlight lang="nix>
packages = let
          src = ./.;
          php = pkgs.php1;
in {
  app = php.buildComposerProject {
    inherit src;
    pname = "app-demo";
    version = "1.0.0";
    vendorHash = "sha256-SrE51k3nC5idaDHNxiNM7NIbIERIf8abrCzFEdxOQWA=";
  };
  oci-image = let
    webport = "8000";
    webroot = "${self'.packages.app}/share/php/app-demo/public";
    caddyFile = pkgs.writeText "Caddyfile" ''
    :${webport}
    root * ${webroot}
    log
    encode gzip
    php_fastcgi 127.0.0.1:9000
    file_server
    '';
  in
  pkgs.dockerTools.buildLayeredImage {
    name = self'.packages.app.pname;
    tag = "latest";
    contents = [
      php
      pkgs.caddy
      pkgs.fakeNss
      (pkgs.writeScriptBin "start-server" ''
        #!${pkgs.runtimeShell}
        php-fpm -D -y /etc/php-fpm.d/www.conf.default
        caddy run --adapter caddyfile --config ${caddyFile}
      '')
    ];
    extraCommands = ''
      mkdir -p tmp
      chmod 1777 tmp
    '';
    config = {
      Cmd = [ "start-server" ];
      ExposedPorts = {
        "${webport}/tcp" = {};
      };
    };
  };
};
};
</syntaxhighlight>
</syntaxhighlight>