4 cfg = config.services.phpApplication;
5 cfgByEnv = lists.groupBy (x: x.websiteEnv) (builtins.attrValues cfg.apps);
8 options = with types; {
9 services.phpApplication.apps = mkOption {
12 php applications to define
14 type = attrsOf (submodule {
19 Path to application’s vardir.
26 Mode to apply to the vardir
29 phpSession = mkOption {
32 description = "Handle phpsession files separately in vardir";
34 phpListen = mkOption {
37 description = "Name of the socket to listen to. Defaults to app name if null";
42 description = "Pool configuration to append";
44 phpOptions = mkOption {
47 description = "php configuration to append";
49 phpOpenbasedir = mkOption {
53 paths to add to php open_basedir configuration in addition to app and vardir
56 phpWatchFiles = mkOption {
60 Path to other files to watch to trigger preStart scripts
63 websiteEnv = mkOption {
66 website instance name to use
69 httpdUser = mkOption {
71 default = config.services.httpd.user;
73 httpd user to run the prestart scripts as.
76 httpdGroup = mkOption {
78 default = config.services.httpd.group;
80 httpd group to run the prestart scripts as.
83 httpdWatchFiles = mkOption {
87 Path to other files to watch to trigger httpd reload
93 Path to application root
96 webappName = mkOption {
100 Alias name for the app, to be used in services.websites.webappDirs
106 Path to the web root path of the application. May differ from the application itself (usually a subdirectory)
109 preStartActions = mkOption {
113 List of actions to run as apache user at preStart when
114 whatchFiles or app dir changed.
117 serviceDeps = mkOption {
121 List of systemd services this application depends on
127 # Read-only variables
128 services.phpApplication.phpListenPaths = mkOption {
130 default = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
131 name "/run/phpfpm/${if icfg.phpListen == null then name else icfg.phpListen}.sock"
135 Full paths to listen for php
138 services.phpApplication.webappDirs = mkOption {
140 default = attrsets.filterAttrs (n: v: builtins.hasAttr n cfg.apps) config.services.websites.webappDirsPaths;
143 Stable name webapp dirs for httpd
149 services.websites.env = attrsets.mapAttrs' (name: cfgs: attrsets.nameValuePair
151 modules = [ "proxy_fcgi" ];
152 watchPaths = builtins.concatLists (map (c: c.httpdWatchFiles) cfgs);
156 services.phpfpm.pools = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
158 listen = cfg.phpListenPaths."${name}";
160 user = ${icfg.httpdUser}
161 group = ${icfg.httpdGroup}
162 listen.owner = ${icfg.httpdUser}
163 listen.group = ${icfg.httpdGroup}
164 ${optionalString (icfg.phpSession) ''
165 php_admin_value[session.save_path] = "${icfg.varDir}/phpSessions"''}
166 php_admin_value[open_basedir] = "${builtins.concatStringsSep ":" ([icfg.app icfg.varDir] ++ icfg.phpOpenbasedir)}"
168 phpOptions = config.services.phpfpm.phpOptions + icfg.phpOptions;
172 services.websites.webappDirs = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
173 (if icfg.webappName == null then name else icfg.webappName) icfg.webRoot
174 ) (attrsets.filterAttrs (n: v: !isNull v.webRoot) cfg.apps);
176 systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
178 after = lib.mkAfter icfg.serviceDeps;
179 wants = icfg.serviceDeps;
180 preStart = lib.mkAfter (optionalString (!isNull icfg.varDir) ''
181 watchFilesChanged() {
182 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 1"}
183 [ ! -f "${icfg.varDir}"/watchedFiles ] \
184 || ! sha512sum -c --status ${icfg.varDir}/watchedFiles
187 [ ! -f "${icfg.varDir}/currentWebappDir" -o \
188 "${icfg.app}" != "$(cat ${icfg.varDir}/currentWebappDir 2>/dev/null)" ]
191 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 0"}
192 sha512sum ${builtins.concatStringsSep " " icfg.phpWatchFiles} > ${icfg.varDir}/watchedFiles
195 if watchFilesChanged || appDirChanged; then
196 pushd ${icfg.app} > /dev/null
197 ${builtins.concatStringsSep "\n " (map (c: "/run/wrappers/bin/sudo -u ${icfg.httpdUser} ${c}") icfg.preStartActions) }
199 echo -n "${icfg.app}" > ${icfg.varDir}/currentWebappDir
206 system.activationScripts = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
209 text = optionalString (!isNull icfg.varDir) ''
210 install -m ${icfg.mode} -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}
211 '' + optionalString (icfg.phpSession) ''
212 install -m 0700 -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}/phpSessions