PHP Ansible Role

This Ansible role installs and configures PHP with support for multiple versions, custom modules, and PHP-FPM pool configurations.

Features

  • Install multiple PHP versions simultaneously (e.g., PHP 8.3 and 8.4)
  • Install single PHP version (just provide one version in the list)
  • Configure different module lists for each PHP version
  • 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)

Requirements

  • Ubuntu/Debian-based systems
  • Ansible 2.9+
  • sudo/root privileges

Role Variables

PHP Installation Configuration

php_versions:
  - version: "8.3"
    modules:
      - mysql
      - gd
      - xml
      - mbstring
      - zip
  - version: "8.4"
    modules:
      - mysql
      - gd
      - xml
      - mbstring
      - zip
      - redis

PHP-FPM Pool Configuration

php_pools:
  - name: "www"
    user: "www-data"
    group: "www-data"
    php_versions:
      - version: "8.3"
      - version: "8.4"
    # Pool management settings
    pm: "dynamic"
    pm_start_servers: 5
    pm_min_spare_servers: 3
    pm_max_spare_servers: 8
    pm_max_children: 100
    pm_max_requests: 200
    # Timeouts and limits
    request_slowlog_timeout: "30s"
    request_terminate_timeout: "60s"
    rlimit_files: 65536
    catch_workers_output: "yes"
    # PHP settings
    admin_flag_display_errors: "Off"
    admin_flag_log_errors: "On"
    admin_value_error_reporting: "E_ALL & ~E_NOTICE"
    admin_value_memory_limit: "64M"
    admin_value_upload_max_filesize: "16M"
    admin_value_post_max_size: "16M"

# Remove default www pool
php_remove_default_pool: false

# Logrotate configuration for PHP-FPM logs
php_logrotate_enabled: true

Example Playbooks

Multi-Version Setup with Custom Pools

---
- hosts: webservers
  become: yes
  vars:
    php_versions:
      - version: "8.3"
        modules:
          - mysql
          - gd
          - xml
          - mbstring
          - zip
      - version: "8.4"
        modules:
          - mysql
          - gd
          - xml
          - mbstring
          - zip
          - redis
    
    php_pools:
      - name: "app1"
        user: "app1"
        group: "app1"
        php_versions:
          - version: "8.3"
        pm: "static"
        pm_max_children: 50
        admin_value_memory_limit: "128M"
      
      - name: "app2"
        user: "app2"
        group: "app2"
        php_versions:
          - version: "8.4"
        pm: "dynamic"
        pm_max_children: 100
        admin_value_memory_limit: "256M"
    
    php_remove_default_pool: true
  roles:
    - php

Single Version with Simple Pool

---
- hosts: webservers
  become: yes
  vars:
    php_versions:
      - version: "8.4"
        modules:
          - mysql
          - gd
    
    php_pools:
      - name: "website"
        user: "www-data"
        group: "www-data"
        php_versions:
          - version: "8.4"
  roles:
    - php

Pool Configuration Details

Each pool configuration creates:

  • Socket file: /run/php/php{version}-fpm-{pool_name}.sock
  • Log directories: /var/log/php/{pool_name}/{version}/
  • Error log: /var/log/php/{pool_name}/{version}/php-errors.log
  • Slow log: /var/log/php/{pool_name}/{version}/php-slow.log

The pool configuration supports all standard PHP-FPM pool directives with sensible defaults.

Installed Packages

For each PHP version, the following base packages are automatically installed:

  • php{version}-common
  • php{version}-cli

Additional modules are installed based on the modules list for each version.

Services

The role will manage PHP-FPM services for each installed version:

  • php8.3-fpm
  • php8.4-fpm
  • etc.

Notes

  • The role uses the Sury repository to provide the latest PHP versions
  • Each PHP version runs its own FPM service
  • Module names are simplified (e.g., mysql instead of php8.3-mysql)
  • 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

MIT

Description
No description provided
Readme MIT 42 KiB
Languages
Jinja 100%