]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/websites/chloe/production.nix
Refactor websites
[perso/Immae/Config/Nix.git] / modules / private / websites / chloe / production.nix
index 83f6c9b6a07720ffc9bb783c67d53e1603fc5065..067e8e7a32223ae456103f50a69c4f0a165c38a6 100644 (file)
 { lib, pkgs, config,  ... }:
 let
-  chloe = pkgs.callPackage ./builder.nix {
-    inherit (pkgs.webapps) chloe;
-    config = config.myEnv.websites.chloe.production;
-    apacheUser = config.services.httpd.Prod.user;
-    apacheGroup = config.services.httpd.Prod.group;
+  apacheUser = config.services.httpd.Prod.user;
+  apacheGroup = config.services.httpd.Prod.group;
+  ccfg = config.myEnv.websites.chloe.production;
+  app = pkgs.callPackage ./app {
+    inherit (ccfg) environment;
+    inherit (pkgs.webapps) spip;
+    varDir = "/var/lib/chloe_production";
   };
-
   cfg = config.myServices.websites.chloe.production;
+  webappdir = config.services.websites.webappDirsPaths.chloe_production;
 in {
   options.myServices.websites.chloe.production.enable = lib.mkEnableOption "enable Chloe's website in production";
 
   config = lib.mkIf cfg.enable {
-    services.duplyBackup.profiles.chloe_prod.rootDir = chloe.app.varDir;
-    secrets.keys = chloe.keys;
+    services.duplyBackup.profiles.chloe_production.rootDir = app.varDir;
+    secrets.keys = [
+      {
+        dest = "websites/chloe/production";
+        user = apacheUser;
+        group = apacheGroup;
+        permissions = "0400";
+        text = ''
+          SetEnv SPIP_CONFIG_DIR     "${./config}"
+          SetEnv SPIP_VAR_DIR        "${app.varDir}"
+          SetEnv SPIP_SITE           "chloe-${app.environment}"
+          SetEnv SPIP_LDAP_BASE      "dc=immae,dc=eu"
+          SetEnv SPIP_LDAP_HOST      "ldaps://ldap.immae.eu"
+          SetEnv SPIP_LDAP_SEARCH_DN "${ccfg.ldap.dn}"
+          SetEnv SPIP_LDAP_SEARCH_PW "${ccfg.ldap.password}"
+          SetEnv SPIP_LDAP_SEARCH    "${ccfg.ldap.filter}"
+          SetEnv SPIP_MYSQL_HOST     "${ccfg.mysql.host}"
+          SetEnv SPIP_MYSQL_PORT     "${ccfg.mysql.port}"
+          SetEnv SPIP_MYSQL_DB       "${ccfg.mysql.database}"
+          SetEnv SPIP_MYSQL_USER     "${ccfg.mysql.user}"
+          SetEnv SPIP_MYSQL_PASSWORD "${ccfg.mysql.password}"
+        '';
+      }
+    ];
     services.webstats.sites = [ { name = "osteopathe-cc.fr"; } ];
 
-    systemd.services.phpfpm-chloe_prod.after = lib.mkAfter chloe.phpFpm.serviceDeps;
-    systemd.services.phpfpm-chloe_prod.wants = chloe.phpFpm.serviceDeps;
-    services.phpfpm.pools.chloe_prod = {
+    systemd.services.phpfpm-chloe_production.after = lib.mkAfter [ "mysql.service" ];
+    systemd.services.phpfpm-chloe_production.wants =  [ "mysql.service" ];
+    services.phpfpm.pools.chloe_production = {
       user = config.services.httpd.Prod.user;
       group = config.services.httpd.Prod.group;
-      settings = chloe.phpFpm.pool;
+      settings = {
+        "listen.owner" = apacheUser;
+        "listen.group" = apacheGroup;
+        "php_admin_value[upload_max_filesize]" = "20M";
+        "php_admin_value[post_max_size]" = "20M";
+        # "php_admin_flag[log_errors]" = "on";
+        "php_admin_value[open_basedir]" = "${app.spipConfig}:${./config}:${app}:${app.varDir}:/tmp";
+        "php_admin_value[session.save_path]" = "${app.varDir}/phpSessions";
+        "pm" = "dynamic";
+        "pm.max_children" = "20";
+        "pm.start_servers" = "2";
+        "pm.min_spare_servers" = "1";
+        "pm.max_spare_servers" = "3";
+      };
       phpOptions = config.services.phpfpm.phpOptions + ''
         extension=${pkgs.php}/lib/php/extensions/mysqli.so
       '';
     };
-    system.activationScripts.chloe_prod = chloe.activationScript;
-    myServices.websites.webappDirs."${chloe.apache.webappName}" = chloe.app.webRoot;
-    services.websites.env.production.modules = chloe.apache.modules;
+    system.activationScripts.chloe_production = {
+      deps = [ "wrappers" ];
+      text = ''
+        install -m 0755 -o ${apacheUser} -g ${apacheGroup} -d ${app.varDir} ${app.varDir}/IMG ${app.varDir}/tmp ${app.varDir}/local
+        install -m 0750 -o ${apacheUser} -g ${apacheGroup} -d ${app.varDir}/phpSessions
+      '';
+    };
+    services.websites.webappDirs.chloe_production = app.webRoot;
+    services.websites.env.production.modules = [ "proxy_fcgi" ];
     services.websites.env.production.vhostConfs.chloe = {
       certName     = "chloe";
       certMainHost = "osteopathe-cc.fr";
       hosts        = ["osteopathe-cc.fr" "www.osteopathe-cc.fr" ];
-      root         = chloe.apache.root;
+      root         = webappdir;
       extraConfig  = [
         ''
+          Use Stats osteopathe-cc.fr
+
           RewriteEngine On
           RewriteCond "%{HTTP_HOST}" "!^www\.osteopathe-cc\.fr$" [NC]
           RewriteRule ^(.+)$ https://www.osteopathe-cc.fr$1 [R=302,L]
+
+          Include ${config.secrets.fullPaths."websites/chloe/production"}
+
+          RewriteEngine On
+          RewriteRule ^/news.rss  /spip.php?page=backend&id_rubrique=1
+
+          <FilesMatch "\.php$">
+            SetHandler "proxy:unix:${config.services.phpfpm.pools.chloe_production.socket}|fcgi://localhost"
+          </FilesMatch>
+
+          <Directory ${webappdir}>
+            DirectoryIndex index.php index.htm index.html
+            Options -Indexes +FollowSymLinks +MultiViews +Includes
+            Include ${webappdir}/htaccess.txt
+
+            AllowOverride AuthConfig FileInfo Limit
+            Require all granted
+          </Directory>
+
+          <DirectoryMatch "${webappdir}/squelettes">
+            Require all denied
+          </DirectoryMatch>
+
+          <FilesMatch "(.htaccess|rewrite-rules|.gitignore)$">
+            Require all denied
+          </FilesMatch>
           ''
-        (chloe.apache.vhostConf config.services.phpfpm.pools.chloe_prod.socket)
       ];
     };
     services.websites.env.production.watchPaths = [
-      "/var/secrets/webapps/${chloe.app.environment}-chloe"
+      config.secrets.fullPaths."websites/chloe/production"
     ];
   };
 }