Puppet Class: profile::mediawiki::php

Defined in:
modules/profile/manifests/mediawiki/php.pp

Overview

Class profile::mediawiki::php

This class declares packages that make up Wikimedia's PHP7-based MediaWiki deployment stack. See T172165

overview of modules needed and their use:

apcu - used by MediaWiki for local server caching bz2 - dumps curl - HTTP requests gd - ZeroBanner geoip - fundraising intl, mbstring, xml - MediaWiki dependencies memcached, mysql, redis - obvious from the name bcmath, gmp - various extensions and vendor libraries

Parameters:

  • enable_fpm (Boolean) (defaults to: hiera('profile::mediawiki::php::enable_fpm'))
  • fpm_config (Optional[Hash]) (defaults to: hiera('profile::mediawiki::php::fpm_config', undef))
  • php_version (Enum['7.0', '7.2', '7.3']) (defaults to: hiera('profile::mediawiki::php::php_version', '7.0'))
  • port (Optional[Wmflib::UserIpPort]) (defaults to: hiera('profile::php_fpm::fcgi_port', undef))
  • fcgi_pool (String) (defaults to: hiera('profile::mediawiki::fcgi_pool', 'www'))
  • request_timeout (Integer) (defaults to: hiera('profile::mediawiki::php::request_timeout', 240))
  • apc_shm_size (String) (defaults to: hiera('profile::mediawiki::apc_shm_size'))
  • cluster (String) (defaults to: lookup('cluster'))
  • statsd (String) (defaults to: lookup('statsd'))
  • fpm_workers_multiplier (Float) (defaults to: lookup('profile::mediawiki::php::fpm_workers_multiplier', {'default_value' => 1.5}))
  • enable_request_profiling (Boolean) (defaults to: lookup('profile::mediawiki::php::enable_request_profiling', {'default_value' => false}))
  • enable_php_core_dumps (Optional[Boolean]) (defaults to: lookup('profile::mediawiki::php:::enable_php_core_dumps', {'default_value' => false}))
  • slowlog_limit (Integer) (defaults to: lookup('profile::mediawiki::php::slowlog_limit', {'default_value' => 15}))
  • phpdbg (Boolean) (defaults to: lookup('profile::mediawiki::php::phpdbg', {'default_value' => false}))


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'modules/profile/manifests/mediawiki/php.pp', line 18

class profile::mediawiki::php(
    Boolean $enable_fpm = hiera('profile::mediawiki::php::enable_fpm'),
    Optional[Hash] $fpm_config = hiera('profile::mediawiki::php::fpm_config', undef),
    Enum['7.0', '7.2', '7.3'] $php_version = hiera('profile::mediawiki::php::php_version', '7.0'),
    Optional[Wmflib::UserIpPort] $port = hiera('profile::php_fpm::fcgi_port', undef),
    String $fcgi_pool = hiera('profile::mediawiki::fcgi_pool', 'www'),
    Integer $request_timeout = hiera('profile::mediawiki::php::request_timeout', 240),
    String $apc_shm_size = hiera('profile::mediawiki::apc_shm_size'),
    # temporary, for php restarts
    String $cluster = lookup('cluster'),
    # Needed for wmerrors
    String $statsd = lookup('statsd'),
    # Allows to tune up or down the number of workers.
    Float $fpm_workers_multiplier = lookup('profile::mediawiki::php::fpm_workers_multiplier', {'default_value' => 1.5}),
    Boolean $enable_request_profiling = lookup('profile::mediawiki::php::enable_request_profiling', {'default_value' => false}),
    Optional[Boolean] $enable_php_core_dumps = lookup('profile::mediawiki::php:::enable_php_core_dumps', {'default_value' => false}),
    Integer $slowlog_limit = lookup('profile::mediawiki::php::slowlog_limit', {'default_value' => 15}),
    Boolean $phpdbg = lookup('profile::mediawiki::php::phpdbg', {'default_value' => false}),
    ) {

    if os_version('debian == stretch') {
        # We get our packages for our repositories again
        file { '/etc/apt/preferences.d/php_wikidiff2.pref':
            ensure => absent,
            notify => Exec['apt_update_php'],
        }
        # Use component/php72. The core php7.2 package is imported from Ondrej Sury's repository,
        # but it's rebuilt (along with a range of extensions) against Stretch only (while
        # the repository also imports/forks a number of low level libraries to accomodate
        # the PHP packages for jessie
        if $php_version == '7.2' {
            apt::repository { 'wikimedia-php72':
                uri        => 'http://apt.wikimedia.org/wikimedia',
                dist       => 'stretch-wikimedia',
                components => 'component/php72',
                notify     => Exec['apt_update_php'],
                before     => Package["php${php_version}-common", "php${php_version}-opcache"]
            }
        }

        # First installs can trip without this
        exec {'apt_update_php':
            command     => '/usr/bin/apt-get update',
            refreshonly => true,
        }
    }

    $config_cli = {
        'include_path'           => '".:/usr/share/php"',
        'error_log'              => 'syslog',
        'pcre.backtrack_limit'   => 5000000,
        'date.timezone'          => 'UTC',
        'display_errors'         => 'stderr',
        'memory_limit'           => '500M',
        'error_reporting'        => 'E_ALL & ~E_STRICT',
        'mysql'                  => { 'connect_timeout' => 3 },
        'default_socket_timeout' => 60,
    }

    # Custom config for php-fpm
    # basic optimizations for opcache. See T206341
    $rlimit_core = $enable_php_core_dumps ? {
        true    => 'unlimited',
        default => '0',
    }
    $base_config_fpm = {
        'opcache.enable'                  => 1,
        'opcache.interned_strings_buffer' => 50,
        'opcache.memory_consumption'      => 300,
        'opcache.max_accelerated_files'   => 24000,
        'opcache.max_wasted_percentage'   => 10,
        'opcache.validate_timestamps'     => 1,
        'opcache.revalidate_freq'         => 10,
        'auto_prepend_file'               => '/srv/mediawiki/wmf-config/PhpAutoPrepend.php',
        'display_errors'                  => 0,
        'session.upload_progress.enabled' => 0,
        'enable_dl'                       => 0,
        'apc.shm_size'                    => $apc_shm_size,
        'rlimit_core'                     => $rlimit_core,
    }
    if $enable_fpm {
        $_sapis = ['cli', 'fpm']
        $_config = {
            'cli' => $config_cli,
            'fpm' => merge($config_cli, $base_config_fpm, $fpm_config)
        }
        # Add systemd override for php-fpm, that should prevent a reload
        # if the fpm config files are broken.
        # This should prevent us from shooting our own foot as happened before.
        systemd::unit { "php${php_version}-fpm.service":
            ensure   => present,
            content  => template('profile/mediawiki/php-fpm-systemd-override.conf.erb'),
            override => true,
            restart  => false,
        }
    } else {
        $_sapis = ['cli']
        $_config = {
            'cli' => $config_cli,
        }
    }
    # Install the runtime
    class { '::php':
        ensure         => present,
        version        => $php_version,
        sapis          => $_sapis,
        config_by_sapi => $_config,
    }

    # Extensions that need no custom settings

    # First, extensions provided as core extensions; these are version-specific
    # and are provided as php$version-$extension
    #
    $core_extensions =  [
        'bcmath',
        'bz2',
        'curl',
        'gd',
        'gmp',
        'intl',
        'mbstring',
    ]

    $core_extensions.each |$extension| {
        php::extension { $extension:
            package_name => "php${php_version}-${extension}"
        }
    }
    # Extensions that are installed with package-name php-$extension and, based
    # on the php version selected above, will install the proper extension
    # version based on apt priorities.
    # php-luasandbox and  php-wikidiff2 are special cases as the package is *not*
    # compatible with all supported PHP versions.
    # Technically, it would be needed to inject ensure => latest in the packages,
    # but we prefer to handle the transitions with other tools than puppet.
    php::extension { [
        'apcu',
        'geoip',
        'msgpack',
        'redis',
        'luasandbox',
        'wikidiff2',
    ]:
        ensure => present
    }

    # Extensions that require configuration.
    php::extension {
        'xml':
            package_name => "php${php_version}-xml",
            priority     => 15;
        'memcached':
            priority => 25,
            config   => {
                'extension'                   => 'memcached.so',
                'memcached.serializer'        => 'php',
                'memcached.store_retry_count' => '0'
            };
        'igbinary':
            config   => {
                'extension'                => 'igbinary.so',
                'igbinary.compact_strings' => 'Off',
            };
        'mysqli':
            package_name => "php${php_version}-mysql",
            config       => {
                'extension'                 => 'mysqli.so',
                'mysqli.allow_local_infile' => 'Off',
            }
            ;

        'dba':
            package_name => "php${php_version}-dba",
    }

    # Additional config files are needed by some extensions, add them
    # MySQL
    php::extension {
        default:
            package_name => '',;
        'pdo_mysql':
            ;
        'mysqlnd':
            priority => 10,
    }
    # XML
    php::extension{ [
        'dom',
        'simplexml',
        'xmlreader',
        'xmlwriter',
        'xsl',
        'wddx',
    ]:
        package_name => '',
    }

    if $phpdbg {
      package { "php${php_version}-phpdbg":
        ensure => present
      }
    }

    ### FPM configuration
    # You can check all configuration options at
    # http://php.net/manual/en/install.fpm.configuration.php
    if $enable_fpm {
        class { '::php::fpm':
            ensure => present,
            config => {
                'emergency_restart_interval'  => '60s',
                'emergency_restart_threshold' => $facts['processors']['count'],
                'process.priority'            => -19,
            }
        }

        # This will add an fpm pool listening on port $port
        # We want a minimum of 8 workers, and (we default to 1.5 * number of processors.
        # That number will be raised. Also move to pm = static as pm = dynamic caused some
        # edge-case spikes in p99 latency
        $num_workers = max(floor($facts['processors']['count'] * $fpm_workers_multiplier), 8)
        php::fpm::pool { $fcgi_pool:
            port   => $port,
            config => {
                'pm'                        => 'static',
                'pm.max_children'           => $num_workers,
                'request_terminate_timeout' => $request_timeout,
                'request_slowlog_timeout'   => $slowlog_limit,
            }
        }

        # Send logs locally to /var/log/php7.x-fpm/error.log
        # Please note: this replaces the logrotate rule coming from the package,
        # because we use syslog-based logging. This will also prevent an fpm reload
        # for every logrotate run.
        $fpm_programname = "php${php_version}-fpm"
        systemd::syslog { $fpm_programname:
            base_dir     => '/var/log',
            owner        => 'www-data',
            group        => 'wikidev',
            readable_by  => 'group',
            log_filename => 'error.log'
        }
    }

    # Set up request profiling (T206152, see also T253547)
    # Install tideways-xhprof and mongodb
    $profiling_ensure = $enable_request_profiling ? {
        true    => 'present',
        default => 'absent'
    }
    php::extension { 'mongodb':
        ensure   => $profiling_ensure,
        priority => 30,
    }
    php::extension { 'tideways-xhprof':
        ensure   => $profiling_ensure,
        priority => 30,
        config   => {
            'extension'                       => 'tideways_xhprof.so',
            'tideways_xhprof.clock_use_rdtsc' => '0',
        }
    }

    ## Install excimer, our php profiler, if we're on a newer version of php
    # Please note this is not a single request profiler, it is rather a profiling sampler.
    # Thus, it is active on all appserver and not just on the ones that allow running profiling.
    if $php_version != '7.0' {
        php::extension { 'excimer':
            ensure => present,
        }
    }

    # Set the default interpreter to php7
    $cli_path = "/usr/bin/php${php_version}"
    $pkg = "php${php_version}-cli"
    alternatives::select { 'php':
        path    => $cli_path,
        require => Package[$pkg],
    }

    ## Install wmerrors, on fpm only.
    if $php_version != '7.0' and $enable_fpm {
        php::extension { 'wmerrors':
            ensure => present,
            sapis  => ['fpm'],
            config => {
                'extension'                  => 'wmerrors.so',
                'wmerrors.error_script_file' => '/etc/php/php7-fatal-error.php',
                'wmerrors.enabled'           => true,
            },
        }
        file { '/etc/php/php7-fatal-error.php':
            ensure => present,
            mode   => '0444',
            owner  => 'root',
            group  => 'root',
            source => 'puppet:///modules/profile/mediawiki/php/php7-fatal-error.php',
        }

        $statsd_parts = split($statsd, ':')
        $statsd_host = $statsd_parts[0]
        $statsd_port = $statsd_parts[1]

        file { '/etc/php/error-params.php':
            ensure  => present,
            mode    => '0444',
            owner   => 'root',
            group   => 'root',
            content => template('profile/mediawiki/error-params.php.erb'),
        }
    }
    else {
        # Temoprary cleanup for the previous error
        php::extension { 'wmerrors':
            ensure => absent,
        }
    }
}