From 140610f8ee501d36c39201614185c965a747cc2a Mon Sep 17 00:00:00 2001 From: Ludovic Cartier Date: Wed, 24 Sep 2025 15:37:04 +0200 Subject: [PATCH] handle php logrotate --- README.md | 6 ++++++ defaults/main.yml | 3 +++ tasks/fpm_pools.yml | 16 ++++++++++++++++ templates/php-fpm.logrotate.j2 | 22 ++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 templates/php-fpm.logrotate.j2 diff --git a/README.md b/README.md index 06a7f50..0f14e6b 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ This Ansible role installs and configures PHP with support for multiple versions - Create and manage PHP-FPM pools with custom configurations - Support for multiple pools across different PHP versions - Automatic log directory creation with proper ownership +- Automatic logrotate configuration for PHP-FPM logs - Support for PHP-FPM - Automatic repository setup (Sury repository for latest PHP versions) @@ -74,6 +75,9 @@ php_pools: # Remove default www pool php_remove_default_pool: false + +# Logrotate configuration for PHP-FPM logs +php_logrotate_enabled: true ``` ## Example Playbooks @@ -184,6 +188,8 @@ The role will manage PHP-FPM services for each installed version: - Log directories are automatically created with proper ownership - Pool sockets are created with specific naming: `php{version}-fpm-{pool_name}.sock` - You can create multiple pools for the same PHP version with different configurations +- Logrotate configuration is automatically created if logrotate is installed on the system +- PHP-FPM logs are rotated daily with 31 days retention and bzip2 compression ## License diff --git a/defaults/main.yml b/defaults/main.yml index 56047ff..15dc006 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -34,3 +34,6 @@ php_pools: admin_value_post_max_size: "16M" php_remove_default_pool: false + +# Logrotate configuration for PHP logs +php_logrotate_enabled: true diff --git a/tasks/fpm_pools.yml b/tasks/fpm_pools.yml index 6028190..9c74260 100644 --- a/tasks/fpm_pools.yml +++ b/tasks/fpm_pools.yml @@ -33,3 +33,19 @@ - php_remove_default_pool | bool notify: - restart php-fpm services + +- name: php | check if logrotate is installed + package_facts: + manager: "auto" + +- name: php | create logrotate configuration for php-fpm logs + template: + src: php-fpm.logrotate.j2 + dest: "/etc/logrotate.d/php-fpm" + owner: root + group: root + mode: '0644' + when: + - php_logrotate_enabled | default(true) | bool + - "'logrotate' in ansible_facts.packages" + - php_pools is defined and php_pools | length > 0 diff --git a/templates/php-fpm.logrotate.j2 b/templates/php-fpm.logrotate.j2 new file mode 100644 index 0000000..ea53379 --- /dev/null +++ b/templates/php-fpm.logrotate.j2 @@ -0,0 +1,22 @@ +daily +rotate 31 +compress +compresscmd /bin/bzip2 +uncompresscmd /bin/bunzip2 +compressoptions -9 +compressext .bz2 +missingok +dateext +dateformat -%Y%m%d-%s +dateyesterday +notifempty +sharedscripts +su root adm + +/var/log/php/*/*/php-slow.log +/var/log/php/*/*/php-errors.log +{ + postrotate + if [ -d /run/systemd/system ]; then systemctl kill -s HUP rsyslog.service; else invoke-rc.d rsyslog rotate > /dev/null; fi + endscript +} \ No newline at end of file