[WIP] Improve FPM management

- store configuration in local facts
- better socket generator
- php_admin_value/php_value
improve
Emilien Mantel 2019-12-26 11:38:06 +01:00
parent 1eea41cd39
commit ec1175af21
8 changed files with 80 additions and 60 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
*.swp *.swp
*.retry *.retry
*.log *.log
/filter_plugins/*.pyc

View File

@ -90,10 +90,10 @@ php_xdebug_var_display_max_depth: '3'
# PHP-FPM # PHP-FPM
php_fpm_poold: php_fpm_poold:
- pool_name: 'www' - name: 'www'
listen: '{{ php_default_fpm_sock }}' listen: '{{ php_default_fpm_sock }}'
pm: 'dynamic' pm: 'dynamic'
pm_max_children: 250 pm_max_children: 100
pm_start_servers: 10 pm_start_servers: 10
pm_min_spare_servers: 10 pm_min_spare_servers: 10
pm_max_spare_servers: 20 pm_max_spare_servers: 20

Binary file not shown.

View File

@ -24,13 +24,13 @@
- name: TEMPLATE | Deploy pool configuration - name: TEMPLATE | Deploy pool configuration
template: template:
src: etc/__php__/fpm/pool.d/pool.conf.j2 src: etc/__php__/fpm/pool.d/pool.conf.j2
dest: '{{ php_fpm_pool_dir }}/{{ item.pool_name }}.conf' dest: '{{ php_fpm_pool_dir }}/{{ item.name }}.conf'
loop: "{{ php_fpm_poold }}" loop: "{{ ansible_local.hanxhx_php.fpm_pool }}"
notify: restart php-fpm notify: restart php-fpm
- name: FILE | Delete default pool if necessary - name: FILE | Delete default pool if necessary
file: file:
path: "{{ php_fpm_pool_dir }}/www.conf" path: "{{ php_fpm_pool_dir }}/www.conf"
state: absent state: absent
when: '"www" not in (php_fpm_poold | map(attribute="pool_name") | list)' when: '"www" not in (ansible_local.hanxhx_php.fpm_pool | map(attribute="name") | list)'
notify: restart php-fpm notify: restart php-fpm

View File

@ -22,13 +22,13 @@
{% for p in php_fpm_poold %} {% for p in php_fpm_poold %}
{ {
name: "{{ p.name | default(p.pool_name) }}", name: "{{ p.name | default(p.pool_name) }}",
listen: "{{ item.listen | default(php_version | php_socket(p.name | default(p.pool_name))) }}", listen: "{{ p.listen | default(php_version | php_socket(p.name | default(p.pool_name))) }}",
user: "{{ item.user | default(php_default_user_group) }}", user: "{{ p.user | default(php_default_user_group) }}",
group: "{{ item.group | default(php_default_user_group) }}", group: "{% if p.user is defined %}{{ p.group | default(p.user) }}{% else %}{{ p.group | default(php_default_user_group) }}{% endif %}",
listen_owner: "{{ item.listen_owner | default(php_default_user_group) }}", php_value: {% if p.php_value is defined %}{{ p.php_value | to_nice_json }}{% else %}{}{% endif %},
listen_group: "{{ item.listen_owner | default(php_default_user_group) }}", php_admin_value: {% if p.php_admin_value is defined %}{{ p.php_admin_value | to_nice_json }}{% else %}{}{% endif %},
{% for k, v in p.items() %} {% for k, v in p.items() | list %}
{% if k not in ['name', 'pool_name', 'listen', 'user', 'group', 'listen_owner'] %} {% if k not in ['name', 'pool_name', 'listen', 'user', 'group', 'php_value', 'php_admin_value'] %}
{{ k }}: "{{ v }}"{% if not loop.last %},{% endif %} {{ k }}: "{{ v }}"{% if not loop.last %},{% endif %}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
@ -46,28 +46,32 @@
state: directory state: directory
recurse: yes recurse: yes
- name: COPY | Manage current repositories - name: COPY | Manage facts
copy: copy:
content: "{ \"php_fpm_full_pool\": {{ php_fpm_full_pool | to_json }} }" content: "{ \"fpm_pool\": {{ php_fpm_full_pool | to_nice_json }} }"
dest: /etc/ansible/facts.d/php_fpm_pool.fact dest: /etc/ansible/facts.d/hanxhx_php.fact
register: f
- debug: var=php_fpm_full_pool - name: SETUP | Gathers new facts
- fail: msg='ok' setup:
when: f.changed
tags:
- skip_ansible_lint
- name: APT | Install PHP packages - name: APT | Install PHP packages
apt: apt:
pkg: "{{ item }}" pkg: "{{ pkgs }}"
state: present state: present
update_cache: yes update_cache: yes
cache_valid_time: 3600 cache_valid_time: 3600
loop: "{{ php_packages + php_extra_packages | flatten }}" vars:
pkgs: "{{ php_packages + php_extra_packages | flatten }}"
notify: restart php-fpm notify: restart php-fpm
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
- name: PKGNG | Install PHP packages - name: PKGNG | Install PHP packages
pkgng: pkgng:
name: "{{ item }}" name: "{{ php_packages + php_extra_packages | flatten | join(',') }}"
loop: "{{ php_packages + php_extra_packages | flatten }}"
notify: restart php-fpm notify: restart php-fpm
when: ansible_os_family == 'FreeBSD' when: ansible_os_family == 'FreeBSD'

View File

@ -1,7 +1,7 @@
; Start a new pool named '{{ item.pool_name }}'. ; Start a new pool named '{{ item.name }}'.
; the variable $pool can we used in any directive and will be replaced by the ; the variable $pool can we used in any directive and will be replaced by the
; pool name ('{{ item.pool_name }}' here) ; pool name ('{{ item.name }}' here)
[{{ item.pool_name }}] [{{ item.name }}]
; Per pool prefix ; Per pool prefix
; It only applies on the following directives: ; It only applies on the following directives:
@ -20,8 +20,8 @@
; Unix user/group of processes ; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group ; Note: The user is mandatory. If the group is not set, the default user's group
; will be used. ; will be used.
user = {{ item.user | default(php_default_user_group) }} user = {{ item.user }}
group = {{ item.group | default(php_default_user_group) }} group = {{ item.group }}
; The address on which to accept FastCGI requests. ; The address on which to accept FastCGI requests.
; Valid syntaxes are: ; Valid syntaxes are:
@ -95,7 +95,7 @@ listen.group = {{ item.listen_owner | default(php_default_user_group) }}
; pm.process_idle_timeout - The number of seconds after which ; pm.process_idle_timeout - The number of seconds after which
; an idle process will be killed. ; an idle process will be killed.
; Note: This value is mandatory. ; Note: This value is mandatory.
pm = {{ item.pm }} pm = {{ item.pm | default('dynamic') }}
; The number of child processes to be created when pm is set to 'static' and the ; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'. ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
@ -106,27 +106,27 @@ pm = {{ item.pm }}
; forget to tweak pm.* to fit your needs. ; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand' ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory. ; Note: This value is mandatory.
pm.max_children = {{ item.pm_max_children }} pm.max_children = {{ item.pm_max_children | default('250') }}
; The number of child processes created on startup. ; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic' ; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2 ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = {{ item.pm_start_servers }} pm.start_servers = {{ item.pm_start_servers | default('10') }}
; The desired minimum number of idle server processes. ; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic' ; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = {{ item.pm_min_spare_servers }} pm.min_spare_servers = {{ item.pm_min_spare_servers | default('10') }}
; The desired maximum number of idle server processes. ; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic' ; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = {{ item.pm_max_spare_servers }} pm.max_spare_servers = {{ item.pm_max_spare_servers | default('20') }}
; The number of seconds after which an idle process will be killed. ; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand' ; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s ; Default Value: 10s
;pm.process_idle_timeout = 10s; pm.process_idle_timeout = {{ item.pm_process_idle_timeout | default('10s') }};
; The number of requests each child process should execute before respawning. ; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For ; This can be useful to work around memory leaks in 3rd party libraries. For
@ -369,7 +369,7 @@ chdir = /
; Note: on highloaded environement, this can cause some delay in the page ; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms). ; process time (several ms).
; Default Value: no ; Default Value: no
;catch_workers_output = yes catch_workers_output = {{ item.catch_workers_output | default('no') }}
; Clear environment in FPM workers ; Clear environment in FPM workers
; Prevents arbitrary environment variables from reaching FPM worker processes ; Prevents arbitrary environment variables from reaching FPM worker processes
@ -421,12 +421,10 @@ chdir = /
;php_admin_value[error_log] = /var/log/fpm-php.www.log ;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on ;php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M ;php_admin_value[memory_limit] = 32M
{% if item.php_flag is defined %} {% for k, v in item.php_value.items() | list %}
{% for k, v in item.php_flag.items() %} php_value[{{ k }}] = {{ v }}
{% endfor %} {% endfor %}
{% endif %} {% for k, v in item.php_admin_value.items() | list %}
{% if item.php_admin_flag is defined %} php_admin_value[{{ k }}] = {{ v }}
{% for k, v in item.php_admin_flag.items() %}
{% endfor %} {% endfor %}
{% endif %}
; vim:filetype=dosini ; vim:filetype=dosini

View File

@ -15,22 +15,27 @@ http {
root /var/www; root /var/www;
{% if php_fpm_poold.0.status_path is defined %} {% if ansible_local.hanxhx_php.fpm_pool.0.status_path is defined %}
location = {{ php_fpm_poold.0.status_path }} { location = {{ ansible_local.hanxhx_php.fpm_pool.0.status_path }} {
include fastcgi.conf; include fastcgi.conf;
fastcgi_pass unix:{{ php_default_fpm_sock }}; fastcgi_pass unix:{{ ansible_local.hanxhx_php.fpm_pool.0.listen }};
} }
{% endif %} {% endif %}
{% if php_fpm_poold.0.ping_path is defined %} {% if ansible_local.hanxhx_php.fpm_pool.0.ping_path is defined %}
location = {{ php_fpm_poold.0.ping_path }} { location = {{ ansible_local.hanxhx_php.fpm_pool.0.ping_path }} {
include fastcgi.conf; include fastcgi.conf;
fastcgi_pass unix:{{ php_default_fpm_sock }}; fastcgi_pass unix:{{ ansible_local.hanxhx_php.fpm_pool.0.listen }};
} }
{% endif %} {% endif %}
location = /ini.php {
include fastcgi.conf;
fastcgi_pass unix:{{ ansible_local.hanxhx_php.fpm_pool.1.listen }};
}
location ~ \.php$ { location ~ \.php$ {
include fastcgi.conf; include fastcgi.conf;
fastcgi_pass unix:{{ php_default_fpm_sock }}; fastcgi_pass unix:{{ ansible_local.hanxhx_php.fpm_pool.0.listen }};
} }
} }
} }

View File

@ -12,7 +12,7 @@
error_reporting: 'E_ALL' error_reporting: 'E_ALL'
php_fpm_poold: php_fpm_poold:
- pool_name: 'test_ansible' - pool_name: 'test_ansible'
listen: '{{ php_default_fpm_sock }}' listen: '/run/php/php-ansible1.sock'
pm: 'dynamic' pm: 'dynamic'
pm_max_children: 250 pm_max_children: 250
pm_start_servers: 10 pm_start_servers: 10
@ -21,26 +21,25 @@
status_path: '/status' status_path: '/status'
ping_path: '/ping' ping_path: '/ping'
ping_response: 'ok' ping_response: 'ok'
php_flag: - name: 'test_ansible2'
user: 'foo'
php_value:
display_errors: 'Off' display_errors: 'Off'
php_admin_flag: php_admin_value:
memory_limit: '128M' memory_limit: '98M'
- pool_name: 'test_ansible2'
pm: 'dynamic'
pm_max_children: 250
pm_start_servers: 10
pm_min_spare_servers: 10
pm_max_spare_servers: 20
php_flag:
display_errors: 'On'
php_admin_flag:
memory_limit: '64M'
pre_tasks: pre_tasks:
- name: INCLUDE_TASKS | Pre tasks related to OS - name: INCLUDE_TASKS | Pre tasks related to OS
include_tasks: "includes/pre_{{ ansible_os_family }}.yml" include_tasks: "includes/pre_{{ ansible_os_family }}.yml"
- name: USER | Create PHP user
user:
name: 'foo'
system: yes
create_home: no
shell: '/usr/sbin/nologin'
tasks: tasks:
- name: TEMPLATE | Nginx site config - name: TEMPLATE | Nginx site config
@ -122,6 +121,11 @@
dest: /var/www/phpinfo.php dest: /var/www/phpinfo.php
content: '<?php phpinfo();' content: '<?php phpinfo();'
- name: COPY | Add ini test file
copy:
dest: /var/www/ini.php
content: '<?php echo ini_get("memory_limit") . "\n";'
- name: SHELL | Check vhost - name: SHELL | Check vhost
shell: "curl -v -H 'Host: {{ vhost }}' http://127.0.0.1/phpinfo.php 2> /dev/null | grep h1 | grep -o 'PHP Version {{ php_version }}' | sed -r 's/<//g'" shell: "curl -v -H 'Host: {{ vhost }}' http://127.0.0.1/phpinfo.php 2> /dev/null | grep h1 | grep -o 'PHP Version {{ php_version }}' | sed -r 's/<//g'"
args: args:
@ -130,6 +134,14 @@
register: c register: c
failed_when: c.stdout == '' failed_when: c.stdout == ''
- name: SHELL | Check custom php value
shell: "curl -H 'Host: {{ vhost }}' http://127.0.0.1/ini.php 2> /dev/null"
args:
warn: false
changed_when: false
register: c
failed_when: 'php_fpm_poold.1.php_admin_value.memory_limit not in c.stdout'
- name: URI | Check ping - name: URI | Check ping
uri: uri:
url: "http://localhost{{ php_fpm_poold.0.ping_path }}" url: "http://localhost{{ php_fpm_poold.0.ping_path }}"