]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/websites/piedsjaloux/production.nix
Migrate php sessions to redis
[perso/Immae/Config/Nix.git] / modules / private / websites / piedsjaloux / production.nix
1 { lib, pkgs, config, ... }:
2 let
3 secrets = config.myEnv.websites.piedsjaloux.production;
4 cfg = config.myServices.websites.piedsjaloux.production;
5 texlive = pkgs.texlive.combine { inherit (pkgs.texlive) attachfile preprint scheme-small; };
6 ftpRoot = "/var/lib/piedsjaloux_production";
7 phpRoot = "${ftpRoot}/php";
8 webRoot = "${phpRoot}/web";
9 varDir = "${ftpRoot}/var";
10 packagePath = "/var/lib/ftp/release.immae.eu/buildbot/Piedsjaloux";
11 branch = "master";
12 in {
13 options.myServices.websites.piedsjaloux.production.enable = lib.mkEnableOption "enable PiedsJaloux's website in production";
14
15 config = lib.mkIf cfg.enable {
16 services.webstats.sites = [ { name = "piedsjaloux.fr"; } ];
17 services.phpfpm.pools.piedsjaloux_production = {
18 user = config.services.httpd.Prod.user;
19 group = config.services.httpd.Prod.group;
20 settings = {
21 "listen.owner" = config.services.httpd.Prod.user;
22 "listen.group" = config.services.httpd.Prod.group;
23 "php_admin_value[open_basedir]" = builtins.concatStringsSep ":" [
24 ftpRoot
25 config.secrets.fullPaths."websites/piedsjaloux/production"
26 "/tmp"
27 ];
28 "php_admin_value[session.save_handler]" = "redis";
29 "php_admin_value[session.save_path]" = "'unix:///run/redis-php-sessions/redis.sock?persistent=1&prefix=PiedsJaloux:Production:'";
30 "php_admin_value[upload_max_filesize]" = "20M";
31 "php_admin_value[post_max_size]" = "20M";
32 #"php_admin_flag[log_errors]" = "on";
33 "pm" = "dynamic";
34 "pm.max_children" = "20";
35 "pm.start_servers" = "2";
36 "pm.min_spare_servers" = "1";
37 "pm.max_spare_servers" = "3";
38 };
39 phpPackage = pkgs.php74.withExtensions({ enabled, all }: enabled ++ [all.redis]);
40 };
41 systemd.services."phpfpm-piedsjaloux_production" = {
42 after = lib.mkAfter ["mysql.service"];
43 wants = ["mysql.service"];
44 path = lib.mkAfter [ pkgs.gnutar pkgs.gzip pkgs.php74 ];
45 preStart = let
46 script = pkgs.writeScriptBin "piedsjaloux-production-pre" ''
47 #! ${pkgs.stdenv.shell}
48
49 [ -f ${packagePath}/${branch}.tar.gz ] || exit 1
50
51 cd ${ftpRoot}
52 if ! [ -f .tarball_sum ] || ! sha256sum -c .tarball_sum; then
53 tar -xf ${packagePath}/${branch}.tar.gz --one-top-level=php_new
54 if [ -d php ]; then
55 mv php php_old
56 fi
57 mv php_new php
58 fi
59 cd php
60 rm -rf var/{logs,cache,data,miniatures,tmp}
61 ln -sf ${varDir}/{logs,cache,data,miniatures,tmp} var/
62 ln -sf ${config.secrets.fullPaths."websites/piedsjaloux/production"} app/config/parameters.yml
63 ./bin/console --env=${secrets.environment} cache:clear --no-warmup
64 sha256sum ${packagePath}/${branch}.tar.gz > ${ftpRoot}/.tarball_sum
65 '';
66 in
67 "/run/wrappers/bin/sudo -u ${config.services.httpd.Prod.user} ${script}/bin/piedsjaloux-production-pre";
68 postStart = let
69 script = pkgs.writeScriptBin "piedsjaloux-production-post" ''
70 #! ${pkgs.stdenv.shell}
71
72 cd ${ftpRoot}
73 if [ -d php_old ]; then
74 rm -rf php_old
75 fi
76 '';
77 in
78 "/run/wrappers/bin/sudo -u ${config.services.httpd.Prod.user} ${script}/bin/piedsjaloux-production-post";
79 serviceConfig.TimeoutStartSec="infinity";
80 };
81 services.filesWatcher.phpfpm-piedsjaloux_production = {
82 restart = true;
83 paths = [ "${packagePath}/${branch}.tar.gz" ];
84 };
85
86 system.activationScripts.piedsjaloux_production = {
87 deps = ["users"];
88 text = ''
89 install -m 0700 -o ${config.services.httpd.Prod.user} -g ${config.services.httpd.Prod.group} -d ${ftpRoot}
90 '';
91 };
92
93 secrets.keys."websites/piedsjaloux/production" = {
94 user = config.services.httpd.Prod.user;
95 group = config.services.httpd.Prod.group;
96 permissions = "0400";
97 text = ''
98 # This file is auto-generated during the composer install
99 parameters:
100 database_host: ${secrets.mysql.host}
101 database_port: ${secrets.mysql.port}
102 database_name: ${secrets.mysql.database}
103 database_user: ${secrets.mysql.user}
104 database_password: ${secrets.mysql.password}
105 database_server_version: ${config.myServices.databases.mariadb.package.mysqlVersion}
106 mailer_transport: smtp
107 mailer_host: 127.0.0.1
108 mailer_user: null
109 mailer_password: null
110 secret: ${secrets.secret}
111 pdflatex: "${texlive}/bin/pdflatex"
112 leapt_im:
113 binary_path: ${pkgs.imagemagick}/bin
114 '';
115 };
116
117 services.websites.env.production.vhostConfs.piedsjaloux_production = {
118 certName = "piedsjaloux";
119 certMainHost = "piedsjaloux.fr";
120 hosts = [ "piedsjaloux.fr" "www.piedsjaloux.fr" ];
121 root = webRoot;
122 extraConfig = [
123 ''
124 RewriteEngine on
125 RewriteCond "%{HTTP_HOST}" "!^www.piedsjaloux\.fr$" [NC]
126 RewriteRule ^(.+)$ https://www.piedsjaloux.fr$1 [R=302,L]
127
128 <FilesMatch "\.php$">
129 SetHandler "proxy:unix:${config.services.phpfpm.pools.piedsjaloux_production.socket}|fcgi://localhost"
130 </FilesMatch>
131
132 Use Stats piedsjaloux.fr
133
134 <Directory ${webRoot}>
135 Options Indexes FollowSymLinks MultiViews Includes
136 AllowOverride All
137 Require all granted
138 </Directory>
139 ''
140 ];
141 };
142 };
143 }