From 38431cf21c59a84c0ddedccc0cd66540a550ec26 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Tue, 3 Aug 2021 15:24:34 +0200 Subject: [PATCH] nixos/wordpress: caddy support --- .../from_md/release-notes/rl-2111.section.xml | 4 +-- .../manual/release-notes/rl-2111.section.md | 2 +- nixos/modules/services/web-apps/wordpress.nix | 29 ++++++++++++++++++- nixos/tests/wordpress.nix | 18 +++++++++++- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml b/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml index 7a0406662b71..3c60226b224d 100644 --- a/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml +++ b/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml @@ -1056,8 +1056,8 @@ Superuser created successfully. The wordpress module provides a new interface which allows to use different webservers with the new option services.wordpress.webserver. - Currently httpd and - nginx are supported. The definitions of + Currently httpd, caddy + and nginx are supported. The definitions of wordpress sites should now be set in services.wordpress.sites. diff --git a/nixos/doc/manual/release-notes/rl-2111.section.md b/nixos/doc/manual/release-notes/rl-2111.section.md index 0ff62983b057..256c15fb4988 100644 --- a/nixos/doc/manual/release-notes/rl-2111.section.md +++ b/nixos/doc/manual/release-notes/rl-2111.section.md @@ -317,7 +317,7 @@ To be able to access the web UI this port needs to be opened in the firewall. - The `claws-mail` package now references the new GTK+ 3 release branch, major version 4. To use the GTK+ 2 releases, one can install the `claws-mail-gtk2` package. -- The wordpress module provides a new interface which allows to use different webservers with the new option [`services.wordpress.webserver`](options.html#opt-services.wordpress.webserver). Currently `httpd` and `nginx` are supported. The definitions of wordpress sites should now be set in [`services.wordpress.sites`](options.html#opt-services.wordpress.sites). +- The wordpress module provides a new interface which allows to use different webservers with the new option [`services.wordpress.webserver`](options.html#opt-services.wordpress.webserver). Currently `httpd`, `caddy` and `nginx` are supported. The definitions of wordpress sites should now be set in [`services.wordpress.sites`](options.html#opt-services.wordpress.sites). Sites definitions that use the old interface are automatically migrated in the new option. This backward compatibility will be removed in 22.05. diff --git a/nixos/modules/services/web-apps/wordpress.nix b/nixos/modules/services/web-apps/wordpress.nix index 6f1ef815bc46..eb91256045cc 100644 --- a/nixos/modules/services/web-apps/wordpress.nix +++ b/nixos/modules/services/web-apps/wordpress.nix @@ -278,7 +278,7 @@ in }; options.webserver = mkOption { - type = types.enum [ "httpd" "nginx" ]; + type = types.enum [ "httpd" "nginx" "caddy" ]; default = "httpd"; description = '' Whether to use apache2 or nginx for virtual host management. @@ -458,5 +458,32 @@ in }; }) + (mkIf (cfg.webserver == "caddy") { + services.caddy = { + enable = true; + virtualHosts = mapAttrs' (hostName: cfg: ( + nameValuePair "http://${hostName}" { + extraConfig = '' + root * /${pkg hostName cfg}/share/wordpress + file_server + + php_fastcgi unix/${config.services.phpfpm.pools."wordpress-${hostName}".socket} + + @uploads { + path_regexp path /uploads\/(.*)\.php + } + rewrite @uploads / + + @wp-admin { + path not ^\/wp-admin/* + } + rewrite @wp-admin {path}/index.php?{query} + ''; + } + )) eachSite; + }; + }) + + ]); } diff --git a/nixos/tests/wordpress.nix b/nixos/tests/wordpress.nix index 45c58b5b65c8..f7f39668c86e 100644 --- a/nixos/tests/wordpress.nix +++ b/nixos/tests/wordpress.nix @@ -45,6 +45,21 @@ import ./make-test-python.nix ({ pkgs, ... }: networking.firewall.allowedTCPPorts = [ 80 ]; networking.hosts."127.0.0.1" = [ "site1.local" "site2.local" ]; }; + + wp_caddy = { ... }: { + services.wordpress.webserver = "caddy"; + services.wordpress.sites = { + "site1.local" = { + database.tablePrefix = "site1_"; + }; + "site2.local" = { + database.tablePrefix = "site2_"; + }; + }; + + networking.firewall.allowedTCPPorts = [ 80 ]; + networking.hosts."127.0.0.1" = [ "site1.local" "site2.local" ]; + }; }; testScript = '' @@ -54,10 +69,11 @@ import ./make-test-python.nix ({ pkgs, ... }: wp_httpd.wait_for_unit("httpd") wp_nginx.wait_for_unit("nginx") + wp_caddy.wait_for_unit("caddy") site_names = ["site1.local", "site2.local"] - for machine in (wp_httpd, wp_nginx): + for machine in (wp_httpd, wp_nginx, wp_caddy): for site_name in site_names: machine.wait_for_unit(f"phpfpm-wordpress-{site_name}")