- hosts: localhost vars: meta: application_name: WordPress playbook_version: 0.2 app_installation_path: "/var/www/virtual/{{ uberspace_asteroid_name }}/html" app_db_name: "{{ uberspace_asteroid_name }}_wordpress" app_locale: en_GB app_title: "{{ uberspace_asteroid_name }}'s new Website" app_domain: "{{ uberspace_default_web_domain }}" app_admin_username: "{{ uberspace_asteroid_name }}" app_admin_mailaddress: "{{ uberspace_mail_sysmail }}" app_admin_password: RANDOM _home: "/home/{{ uberspace_asteroid_name }}" _webroot: "/var/www/virtual/{{ uberspace_asteroid_name }}" _wp: [/usr/bin/wp, "--path={{ app_installation_path }}"] tasks: # Required by `uberspace app install`; skipped during the actual installation. - name: _Return resolved playbook vars debug: var: vars tags: [playbook-vars] - name: Validate installation options assert: that: - app_installation_path.startswith(_webroot + '/') - "'..' not in app_installation_path.split('/')" - app_db_name is match('^[a-zA-Z0-9_]+$') - app_db_name | length <= 64 - app_db_name.startswith(uberspace_asteroid_name + '_') - app_locale is match('^[a-z]{2,3}(_[A-Z]{2})?(_[a-z]+)?$') - app_title | trim | length > 0 - app_admin_username is match('^[A-Za-z0-9_.@-]+$') - app_admin_mailaddress is match('^[^@\s]+@[^@\s]+$') - uberspace_mariadb_password | default('') | length > 0 fail_msg: "Use an installation path below {{ _webroot }}, an account-prefixed database name, a locale such as de_DE, a title, a simple admin username, and an email address. The installer must provide your MariaDB password." - name: Inspect the installation path stat: path: "{{ app_installation_path }}" register: root - name: Require an ordinary directory assert: that: not root.stat.exists or (root.stat.isdir and not root.stat.islnk) fail_msg: "{{ app_installation_path }} must be a directory, not a file or symlink." - name: Inspect existing files including hidden files find: paths: "{{ app_installation_path }}" hidden: true file_type: any excludes: [nocontent.html] when: root.stat.exists register: contents - name: Refuse to overwrite existing files assert: that: contents.matched | default(0) == 0 fail_msg: "{{ app_installation_path }} must be empty. This playbook does not upgrade or repair existing installations." - name: List existing databases command: argv: [mariadb, --batch, --skip-column-names, --execute=SHOW DATABASES] register: databases changed_when: false check_mode: false - name: Refuse to reuse an existing database assert: that: app_db_name not in databases.stdout_lines fail_msg: "Database {{ app_db_name }} already exists. Choose a new db_name; do not delete a database that contains data you need." - name: Inspect existing web routes command: argv: [uberspace, web, backend, list, --json] register: routes changed_when: false check_mode: false - name: Refuse to replace another application's route assert: that: item.path != '/' or item.destination == 'APACHE' fail_msg: "{{ app_domain }}/ is routed to another backend. Resolve it before installing WordPress." loop: "{{ (routes.stdout | default('[]') | from_json) | selectattr('domain', 'equalto', app_domain) | list }}" loop_control: label: "{{ item.path }}" - name: Create directories file: path: "{{ item.path }}" state: directory mode: "{{ item.mode }}" loop: - {path: "{{ app_installation_path }}", mode: '0755'} - {path: "{{ _home }}/.config/php", mode: '0700'} - {path: "{{ _home }}/tmp/opcache", mode: '0700'} - name: Select PHP 8.4 command: argv: [uberspace, tool, php, version, set, '8.4'] when: not ansible_check_mode - name: Configure PHP limits and OPcache copy: dest: "{{ _home }}/.config/php/{{ item.name }}.ini" mode: '0600' content: "{{ item.content }}" loop: - name: wordpress content: | max_execution_time = 180 memory_limit = 256M post_max_size = 64M upload_max_filesize = 64M max_input_time = 60 max_input_vars = 3000 - name: opcache content: | opcache.enable=1 opcache.enable_cli=0 opcache.validate_timestamps=1 opcache.revalidate_freq=300 opcache.revalidate_path=0 opcache.max_accelerated_files=16229 opcache.max_wasted_percentage=5 opcache.memory_consumption=256 opcache.interned_strings_buffer=64 opcache.fast_shutdown=1 opcache.file_cache={{ _home }}/tmp/opcache opcache.save_comments=1 loop_control: label: "{{ item.name }}.ini" - name: Reload PHP-FPM command: argv: [uberspace, web, php, reload] when: not ansible_check_mode - name: Register the WordPress domain uberspace.api.web_domain: data: name: "{{ app_domain }}" state: present - name: Install WordPress when: not ansible_check_mode block: - name: Remove the default placeholder file: path: "{{ app_installation_path }}/nocontent.html" state: absent - name: Find the latest WordPress release for the locale uri: url: "https://api.wordpress.org/core/version-check/1.7/?locale={{ app_locale }}" return_content: true register: release - name: Require a release in the chosen language assert: that: release.json.offers[0].locale == app_locale fail_msg: "WordPress.org offers no {{ app_locale }} release. Choose a locale listed at https://translate.wordpress.org/." # WP-CLI unpacks .tar.gz packages with PharData, which truncates paths # longer than 100 characters; the .zip package keeps them intact. - name: Download WordPress as a zip package command: argv: "{{ _wp + ['core', 'download', release.json.offers[0].download] }}" register: download until: download is succeeded retries: 3 delay: 5 - name: Verify the WordPress core checksums command: argv: "{{ _wp + ['core', 'verify-checksums', '--locale=' + app_locale] }}" changed_when: false # Passwords go through stdin, so they never appear in the process list. - name: _Create wp-config.php command: argv: "{{ _wp + ['config', 'create', '--dbname=' + app_db_name, '--dbuser=' + uberspace_asteroid_name, '--prompt=dbpass'] }}" stdin: "{{ uberspace_mariadb_password }}" no_log: true - name: Create the MariaDB database command: argv: "{{ _wp + ['db', 'create'] }}" - name: _Install WordPress core command: argv: "{{ _wp + ['core', 'install', '--url=https://' + app_domain, '--title=' + app_title, '--admin_user=' + app_admin_username, '--admin_email=' + app_admin_mailaddress] + (['--prompt=admin_password'] if app_admin_password != 'RANDOM' else []) }}" stdin: "{{ app_admin_password if app_admin_password != 'RANDOM' else omit }}" no_log: true - name: Run scheduled events through cron instead of page views command: argv: "{{ _wp + ['config', 'set', 'DISABLE_WP_CRON', 'true', '--raw'] }}" - name: Inspect existing web headers command: argv: [uberspace, web, header, list, --json] register: headers changed_when: false check_mode: false - name: Enable HSTS command: argv: [uberspace, web, header, add, "{{ app_domain }}/", Strict-Transport-Security, max-age=15552000] when: - not ansible_check_mode - headers.stdout | default('[]') | from_json | selectattr('domain', 'equalto', app_domain) | selectattr('name', 'equalto', 'Strict-Transport-Security') | list | length == 0 - name: Schedule WordPress events every five minutes cron: name: WordPress cron events minute: '*/5' job: "systemd-cat -t wp-cron /usr/bin/wp cron event run --due-now --path={{ app_installation_path }}" - name: Enable automatic updates for all plugins and themes daily cron: name: "WordPress {{ item }} auto-updates" special_time: daily job: "/usr/bin/wp {{ item }} auto-updates enable --all --quiet --path={{ app_installation_path }} >/dev/null 2>&1 || true" loop: [plugin, theme] - name: Verify the public WordPress installation uri: url: "https://{{ app_domain }}/wp-login.php" return_content: true register: site until: site.status == 200 and 'wp-submit' in site.content retries: 12 delay: 5 when: not ansible_check_mode - name: _Report when: not ansible_check_mode debug: msg: | WordPress: https://{{ app_domain }} Admin dashboard: https://{{ app_domain }}/wp-admin Username: {{ app_admin_username }} Password recovery email: {{ app_admin_mailaddress }} {% if app_admin_password == 'RANDOM' %} Use "Lost your password?" on the login page to set your password. {% else %} Log in with the password you provided. {% endif %} PHP limits and OPcache are configured. Scheduled events run every five minutes. WordPress updates its core automatically; plugin and theme auto-updates are enabled daily. Keep regular backups. Guide: https://u8lab.uberspace.de/guide_wordpress/