From 5d1878f9db558182bcfdae3656cbd32e8ceca8fa Mon Sep 17 00:00:00 2001 From: Ludovic Cartier Date: Wed, 24 Sep 2025 11:13:12 +0200 Subject: [PATCH] handle multiple PHP versions and their modules --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++- defaults/main.yml | 17 ++++++++++-- handlers/main.yml | 5 ++-- tasks/install.yml | 18 ++++++------- tasks/requirements.yml | 10 +++---- 5 files changed, 90 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3c568ec..f55781c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,59 @@ -# php +# PHP Ansible Role +This Ansible role installs and configures PHP with support for multiple versions and custom modules for each version. + +## 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 +- 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 + +### Configuration + +```yaml +php_versions: + - version: "8.3" + modules: + - mysql + - gd + - xml + - mbstring + - zip + - version: "8.4" + modules: + - -mysql + - gd + - xml + - redis + - ip +``` + +## 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. + +## Notes + +- The role uses the Sury repository to provide the latest PHP versions +- Each PHP version runs its own FPM service on different sockets/ports +- Module names should include the PHP version prefix (e.g., `php8.3-mysql`) +- For single version installations, just provide one entry in the `php_versions` list + +## License + +MIT \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml index dd59c73..c459089 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,3 +1,16 @@ --- -php_sapi: fpm -php_modules: [] +# php_versions: +# - version: "8.3" +# modules: +# - php8.3-mysql +# - php8.3-gd +# - php8.3-xml +# - php8.3-mbstring +# - php8.3-zip +# - version: "8.4" +# modules: +# - php8.4-mysql +# - php8.4-gd +# - php8.4-xml +# - php8.4-mbstring +# - php8.4-zip diff --git a/handlers/main.yml b/handlers/main.yml index 6a37cd1..89218f3 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,5 +1,6 @@ --- -- name: restart php-fpm +- name: restart php-fpm services systemd_service: - name: php{{ php_version }}-fpm + name: php{{ item.version }}-fpm state: restarted + loop: "{{ php_versions }}" diff --git a/tasks/install.yml b/tasks/install.yml index b5054da..60720ef 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -1,20 +1,20 @@ --- -- name: php | installing php and modules +- name: php | installing php common and base modules apt: name: - - php{{ php_version }}-fpm - - php{{ php_version }}-common - - php{{ php_version }}-cli - - php{{ php_version }}-curl - - php{{ php_version }}-opcache + - php{{ item.version }}-common + - php{{ item.version }}-cli state: present + loop: "{{ php_versions }}" notify: - - restart php-fpm + - restart php-fpm services - name: php | install extra modules apt: - name: '{{ php_modules | list }}' + name: "{{ item.modules | map('regex_replace', '^(.*)$', 'php' + item.version + '-\\1') | list }}" state: present force: yes + loop: "{{ php_versions }}" + when: item.modules is defined and item.modules | length > 0 notify: - - restart php-fpm + - restart php-fpm services diff --git a/tasks/requirements.yml b/tasks/requirements.yml index 4768733..ef6d0ac 100644 --- a/tasks/requirements.yml +++ b/tasks/requirements.yml @@ -1,11 +1,11 @@ --- - name: php | apt update cache - apt: + ansible.builtin.apt: update_cache: yes cache_valid_time: 86400 - name: php | install requirements - apt: + ansible.builtin.apt: name: - apt-transport-https - lsb-release @@ -14,13 +14,13 @@ state: present - name: php | add sury key - apt_key: + ansible.builtin.apt_key: url: https://packages.sury.org/php/apt.gpg keyring: /etc/apt/trusted.gpg.d/php.gpg state: present -- name: php | add sury repository - apt_repository: +- name: php | add sury repository + ansible.builtin.apt_repository: repo: deb https://packages.sury.org/php/ {{ ansible_distribution_release }} main state: present filename: php-sury