Configure WordPress

Apache

Follow the Configure Apache documentation, using the wordpress configuration at the Add sites step.

In the server’s Pillar file, add, for example:

apache:
  public_access: True
  sites:
    coalition:
      configuration: wordpress
      servername: www.open-spending.eu
      serveraliases: ['open-spending.eu']
      context:
        user: coalition
        socket: /var/run/php/php-fpm-coalition.sock

MySQL and PHP

Follow the Configure MySQL and Configure PHP documentation.

PHP-FPM

Configure PHP-FPM to correspond to the Apache configuration. For example:

phpfpm:
  sites:
    coalition:
      configuration: default
      context:
        user: coalition
        listen_user: www-data
        socket: /var/run/php/php-fpm-coalition.sock

This will:

  • Enable the proxy_fcgi Apache module

  • Install the PHP-FPM service

  • Create a /var/log/php-fpm/coalition/ log directory

  • Create a /etc/php/-/fpm/pool.d/coalition.conf configuration file

  • Install PHP packages

The configuration file enables log_errors and sets error_log to /var/log/php-fpm/coalition/coalition.log.

Configure logrotate for the log files:

logrotate:
  conf:
    php-site-logs:
      source: php-site-logs
      context:
        php_version: '8.1'

Note

You can create a custom configuration, if needed.

WordPress

Note

Salt contains WordPress states, but they are limited. Also, WordPress is often deployed by copying files, rather than via fresh installs.

  1. Configure WP-CLI and the site. In the server’s Pillar file, add, for example:

    wordpress:
      cli_version: 2.7.1
      sites:
        coalition:
          database: coalition_wp
          cron:
            contact:
              - sysadmin@open-contracting.org
    
  2. Deploy the server.

  3. Connect to the server as the WordPress user (e.g. coalition).

  4. Change to the public_html directory:

    cd ~/public_html
    
  5. Download WordPress:

    wp core download --locale=en_US
    
  6. Create the wp-config.php file, and configure the database connection to correspond to the MySQL configuration. For example:

    wp config create --dbname=DBNAME --dbuser=USERNAME --dbpass=PASSWORD
    
  7. Install WordPress, creating a user for yourself. For example:

    wp core install --url=www.open-spending.eu --title="www.open-spending.eu" --admin_user=jmckinney --admin_password=PASSWORD --admin_email=jmckinney@open-contracting.org --skip-email
    
  8. Uninstall default plugins:

    wp plugin uninstall hello
    
  9. Install Two Factor (2FA is required for administrators by the require-two-factor must-use plugin):

    wp plugin install two-factor --activate
    
  10. Install Sentry for WordPress. Activation is optional, as it’s loaded by the 00-sentry must-use plugin.

    wp plugin install wp-sentry-integration
    
  11. Add or override any constants, setting values to strings of PHP source. (The constants under wordpress:constants in the pillar/cms.sls file are configured on every site.) For example:

    wordpress:
      sites:
        USERNAME:
          constants:
            WP_AUTO_UPDATE_CORE: 'false'
    
  12. Create a project in Sentry, and set its DSN in the site’s private Pillar file:

    wordpress:
      sites:
        USERNAME:
          constants:
            WP_SENTRY_PHP_DSN: "'https://1234567890abcdef1234567890abcdef@o123456.ingest.sentry.io/1234567890123456'"
    

    Note

    The SDK sends each event over HTTPS as the request ends, occupying a PHP-FPM worker until the send completes. WP_SENTRY_CLIENTBUILDER_CALLBACK caps each send at a second.

    Tip

    To monitor browser JavaScript errors, create a new project, and set the WP_SENTRY_BROWSER_DSN constant.

  13. Add any must-use plugins, with any context they need. (The must-use plugins under wordpress:mu_plugins in the pillar/cms.sls file are installed on every site.) For example:

    wordpress:
      sites:
        USERNAME:
          mu_plugins:
            - fathom-analytics
          context:
            FATHOM_ANALYTICS_ID: ABCDEFGH
    
  14. Schedule checks. Omit integrity if the site uses a security plugin’s file scanner, and updates if an administrator updates plugins manually:

    wordpress:
      sites:
        USERNAME:
          checks:
            scheduled:
              - integrity
              - updates
              - silent
            premium_plugins:
              - advanced-custom-fields-pro
    

    Interpreting the emails it sends

    • The integrity check lists core and plugin files that differ from wordpress.org’s copy: modified, missing or added. A plugin that writes to its own directory can cause a false positive. Confirm changes before restoring files from a backup.

    • wordpress.org has no copy of a premium plugin, so the integrity check instead lists the files that changed while the plugin’s version didn’t. It records the files of each new version in premium-plugin-checksums.json, in the site’s home directory.

    • The updates check lists the updates that won’t install automatically: a major version, if WP_AUTO_UPDATE_CORE is 'minor'; a plugin or theme whose auto-updates are off; and a version that requires a newer PHP version than the server runs.

    • The silent check lists the plugins whose update API gave no answer, which can be due to an HTTP timeout, an expired license, or the plugin being closed on wordpress.org.

  15. Deploy the server.

  16. If you have a custom theme, download and activate it. For example:

    git -C wp-content/themes/ clone https://github.com/open-contracting-partnership/www.open-spending.eu.git
    wp theme activate www.open-spending.eu
    

Reference: Hardening WordPress

Migration

When migrating domains or renaming themes, you might need to search and replace items in the database, using the wp search-replace command.

  1. Run the command with the --dry-run flag, for example:

    wp search-replace --report-changed-only --all-tables --precise --log=/tmp/wp-search-replace.log --dry-run 'open-spedning-coalition' 'www.open-spending.eu'
    
  2. Read the log file to check that no undesired replacements will be made:

    less /tmp/wp-search-replace.log
    
  3. Run the command without the --dry-run flag.

Strings to replace might include:

  • Developer email addresses

  • Domain names

  • Theme names

  • File paths