Ansible Role linuxfabrik.lfops.grav¶
This role installs Grav (a simple file-based flat-file CMS platform) using PHP composer, and keeps Grav, its plugins and themes up to date with the Grav Package Manager (GPM).
Available since LFOps 2.0.0.
How the Role Behaves¶
- The code of Grav, its configuration (
user/config), pages, plugins and themes belong torootand are read-only for the web server, so a hole in Grav or in one of its plugins cannot plant code in the site. The web server can only write the directories Grav writes to at runtime, which are the ones the problems plugin shipped with Grav checks for:assets,backup,cache,images,logs,tmp,user/accountsanduser/data. Only these get the SELinux typehttpd_sys_rw_content_t, everything else getshttpd_sys_content_t. The role sets the owners on every run and reports a change only if it had to change one. - Content and configuration below
user/reach the server by other means than Grav, for example a CI/CD pipeline. Deploy them asroot; the role hands every file outside the runtime directories torooton its next run anyway. - Grav therefore cannot update itself.
grav-update-<instance>.timerupdates the plugins and themes,grav-selfupgrade-<instance>.timerupgrades Grav, both with/usr/local/sbin/grav-gpm. It runsbin/gpmasrootwith a cache, log and tmp directory of its own below/var/cache/grav-gpm, since Grav loads PHP code from its cache, which the web server can write. Afterwards it hands the runtime directories back to the web server, restores the SELinux labels and clears the cache of the web server. Use it for every otherbin/gpmcommand as well, for examplegrav-gpm /var/www/html/www.example.com uninstall form. Abin/gpmrun asrootwithout it executes code the web server can write; one asapachefails. - The role does not install the Admin Panel. It could not save any setting, since the configuration belongs to
root, and installing plugins or upgrading Grav from the web interface fails for the same reason. At the end of the play the role lists the Admin Panel plugins that an earlier version installed (admin,admin2,api,login), but leaves their removal to you. - The vHost allows the HTTP methods
GETandOPTIONSonly. A plugin that takes form submissions, such asform, needsPOSTinallowed_http_methods. setup_gravexcludes the file lists belowcache/compiled/*/filelist-*.phpfrom OPcache (viaphp__opcache_blacklist__dependent_var). Grav touches them on nearly every request, so OPcache would compile them again and again and throw the whole cache away every few days.- Grav generates
user/config/security-private.php(the secret for nonces and sessions) anduser/config/versions.yamlon the first request, which it cannot do in a read-onlyuser/config. The role creates both if they are missing; the web server can readsecurity-private.php, but not change it. - Everything that exists once per Grav instance carries
grav__urlas the name of the instance, with/replaced by-:grav-selfupgrade-<instance>.timerandgrav-update-<instance>.timer, so several instances can share a host. The role removesgrav-selfupgrade.timerandgrav-update.timerof older role versions.
Known Limitations¶
There might be more to implement:
- https://learn.getgrav.org/17/security/configuration
Dependent Roles¶
Any LFOps playbook that installs this role runs these for you. Optional ones can be disabled via the playbook's skip variables.
- A web server (for example Apache httpd) with a virtual host configured for Grav must be available (role: linuxfabrik.lfops.apache_httpd).
- PHP 8.3+ must be installed (roles: linuxfabrik.lfops.repo_remi and linuxfabrik.lfops.php).
- The SELinux file contexts are set by the role linuxfabrik.lfops.selinux.
Multiple Instances on One Host¶
Several Grav instances can share a host as pseudo hosts in the inventory: one inventory host per instance, all with the same ansible_host, each with its own grav__url. The real host can keep one of the instances. The instances share the web server and PHP-FPM, so:
- Add the pseudo hosts to
lfops_setup_gravonly, and the real host to all other playbooks, such assetup_basicor the monitoring. - Put everything that is not specific to one instance, such as
php__*,repo_remi__*orapache_httpd__*settings other than the vHosts, into a group that contains the real host and all pseudo hosts of the machine, not into theirhost_vars. Hosts that disagree about a shared configuration file overwrite each other on every run. - Each instance gets its own vHost (
grav__url), installation directory (grav__install_dir), SELinux file contexts andgrav-selfupgrade-<instance>.timer/grav-update-<instance>.timer. The default schedules of the timers are seeded with theinventory_hostname, so the instances update at different times./usr/local/sbin/grav-gpmis shared and takes the installation directory as its first argument. - Do not run the hosts of a machine in parallel, for example with
--forks 1or one--limitafter the other. Otherwise Ansible configures the same machine several times at once, which leads to package manager lock timeouts and overlapping service restarts. - All instances run as
apache, so a hole in one instance can modify the runtime directories (cache, logs, user data) of every other instance on the host, but not their code.
Example, with the real host serving www.example.com and a pseudo host for test.example.com:
# host_vars/web01.example.com.yml
grav__url: 'www.example.com'
# host_vars/web01.example.com-test.yml
ansible_host: 'web01.example.com'
grav__url: 'test.example.com'
# group_vars/grav_web01.yml, a group of web01.example.com and web01.example.com-test
repo_remi__enabled_php_version: '8.4'
Post-Installation Steps¶
-
Behind a reverse proxy that terminates TLS, set
session.securein the site'suser/config/system.yaml. Grav otherwise sets theSecureflag on its session cookies only on requests it sees arriving over HTTPS, and the proxy talks plain HTTP to it. The role does not touch this file, since it belongs to the site content. Leave it unset for a site that browsers reach over plain HTTP.session: secure: true
Tags¶
grav
composer create-project getgrav/grav {{ grav__install_dir }}, if Grav is not installed yet.- Hand everything below
{{ grav__install_dir }}toroot:rootwith modes0644and0755, but the runtime directories, which go toapache:apache. - Deploy
user/config/security-private.phpanduser/config/versions.yaml, if they are missing. restorecon -Fvr {{ grav__install_dir }}.- Report installed Admin Panel plugins.
- Deploy
/usr/local/sbin/grav-gpm. - Remove
grav-selfupgrade.{service,timer}andgrav-update.{service,timer}of older role versions. - Deploy
/etc/systemd/system/grav-selfupgrade-<instance>.{service,timer}. - Deploy
/etc/systemd/system/grav-update-<instance>.{service,timer}. systemctl enable/disable grav-selfupgrade-<instance>.timer --now.systemctl enable/disable grav-update-<instance>.timer --now.- Triggers: none.
grav:state
systemctl enable/disable grav-selfupgrade-<instance>.timer --now.systemctl enable/disable grav-update-<instance>.timer --now.- Triggers: none.
Mandatory Role Variables¶
grav__url
- The Grav URL, without
http://orhttps://. - Type: String.
Example:
# mandatory
grav__url: 'grav.example.com'
Optional Role Variables¶
grav__install_dir
- Grav installation directory.
- Type: String.
- Default:
'/var/www/html/{{ grav__url }}'
grav__on_calendar_selfupgrade
- Time to upgrade Grav (systemd timer notation).
- Type: String.
- Default:
'22:{{ 59 | random(seed=inventory_hostname) }}'
grav__on_calendar_update
- Time to update the Grav plugins and themes (systemd timer notation).
- Type: String.
- Default:
'23:{{ 59 | random(seed=inventory_hostname) }}'
grav__timer_selfupgrade_enabled
- Enables/disables the systemd timer that upgrades Grav.
- Type: Bool.
- Default:
true
grav__timer_update_enabled
- Enables/disables the systemd timer that updates the Grav plugins and themes.
- Type: Bool.
- Default:
true
Example:
# optional
grav__install_dir: '/var/www/html/grav'
grav__on_calendar_selfupgrade: '22:30'
grav__on_calendar_update: '23:30'
grav__timer_selfupgrade_enabled: true
grav__timer_update_enabled: true