# Copyright 2019-2024 Chris Croome
#
# This file is part of the Webarchitects PHP Ansible role.
#
# The Webarchitects PHP Ansible role is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# The Webarchitects PHP Ansible role is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with the Webarchitects PHP Ansible role. If not, see <https://www.gnu.org/licenses/>.
---
- name: Run as root
  # become: true
  connection: local
  # gather_facts: true
  hosts:
    - localhost
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:

    - name: Debug ansible_effective_user_id
      ansible.builtin.debug:
        var: ansible_effective_user_id

    - name: Debug ansible_user_id
      ansible.builtin.debug:
        var: ansible_user_id

    - name: Debug ansible_user_dir
      ansible.builtin.debug:
        var: ansible_user_dir

    - name: Include php role as root
      ansible.builtin.include_role:
        name: php
      vars:  # noqa var-naming[no-role-prefix]
        php: true
        php_sury: false
        php_fpm_pool_check_fail: true
        php_config:
          - name: PHP 8.2 configuration
            version: "8.2"
            state: present
            files:
              - name: PHP 8.2 CLI configuration
                path: /etc/php/8.2/cli/php.ini
                state: present
                conf:
                  apc:
                    "apc.coredump_unmap": "0"
                    "apc.enable_cli": "1"
                    "apc.enabled": "1"
                    "apc.entries_hint": "4096"
                    "apc.gc_ttl": "3600"
                    "apc.serializer": "php"
                    "apc.shm_segments": "1"
                    "apc.shm_size": "32M"
                    "apc.slam_defense": "1"
                    "apc.ttl": "0"
                    "apc.use_request_time": "0"
                  "CLI Server":
                    "cli_server.color": "on"
                  MySQLi:
                    "mysqli.allow_local_infile": "Off"
                    "mysqli.allow_persistent": "On"
                    "mysqli.default_port": '3306'
                    "mysqli.max_links": "-1"
                    "mysqli.max_persistent": "-1"
                    "mysqli.reconnect": "Off"
                    "mysqli.default_socket": "/run/mysqld/mysqld.sock"
                  Pdo_mysql:
                    "pdo_mysql.default_socket": "/run/mysqld/mysqld.sock"
              - name: PHP 8.2 FPM configuration
                path: /etc/php/8.2/fpm/php.ini
                state: edited
                conf:
                  apc:
                    "apc.enabled": "1"
                    "apc.shm_size": "32M"
                  Date:
                    "date.timezone": "Europe/London"
                  MySQLi:
                    "mysqli.allow_local_infile": "Off"
                  opcache:
                    "opcache.enable": "1"
                    "opcache.interned_strings_buffer": "32"
                    "opcache.max_accelerated_files": "100000"
                    "opcache.memory_consumption": "1024"
                    "opcache.revalidate_freq": "2"
                    "opcache.use_cwd": "1"
                    "opcache.validate_permission": "1"
                    "opcache.validate_root": "1"
                    "opcache.validate_timestamps": "1"
                  PHP:
                    allow_url_include: "Off"
                    default_socket_timeout: "600"
                    disable_functions: "chgrp,chown,dl,exec,passthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,popen,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,show_source,socket_create,symlink,system,system_exec"
                    max_file_uploads: "60"
                    max_execution_time: "600"
                    max_input_nesting_level: "512"
                    max_input_time: "600"
                    max_input_vars: "100000"
                    memory_limit: "512M"
                    output_buffering: "4096"
                    post_max_size: "512M"
                    short_open_tag: "Off"
                    upload_max_filesize: "512M"
                  Session:
                    "session.save_path": "${TMPDIR}"
              - name: PHP 8.2 FPM www pool configuration
                path: /etc/php/8.2/fpm/pool.d/www.conf
                state: edited
                conf:
                  www:
                    "user": www-data
                    "group": www-data
                    "listen": /run/php/php8.2-fpm.sock
                    "listen.owner": www-data
                    "listen.group": www-data
                    "pm": dynamic
                    "pm.max_children": "4"
                    "pm.start_servers": "1"
                    "pm.min_spare_servers": "1"
                    "pm.max_spare_servers": "1"
                    "pm.process_idle_timeout": 10s
                    "pm.max_requests": "1000"
        php_modules:
          - name: PHP 8.2 modules
            version: "8.2"
            state: present
            sapis:
              - name: PHP 8.2 CLI modules
                sapi: cli
                mods_enabled:
                  - apcu
                  - bcmath
                  - bz2
                  - calendar
                  - ctype
                  - curl
                  - dom
                  - exif
                  - ffi
                  - fileinfo
                  - ftp
                  - gd
                  - gettext
                  - gmp
                  - iconv
                  - imagick
                  - imap
                  - intl
                  - ldap
                  - mbstring
                  - mysqli
                  - mysqlnd
                  - opcache
                  - pdo
                  - pdo_mysql
                  - pdo_sqlite
                  - phar
                  - posix
                  - readline
                  - shmop
                  - simplexml
                  - soap
                  - sockets
                  - sqlite3
                  - sysvmsg
                  - sysvsem
                  - sysvshm
                  - tokenizer
                  - uploadprogress
                  - xml
                  - xmlreader
                  - xmlrpc
                  - xmlwriter
                  - xsl
                  - zip
        php_versions:
          - name: PHP 8.2 packages
            version: "8.2"
            state: present
            pkg_absent:
              - libapache2-mod-php8.2
              - php8.2-phpdbg
            pkg_present:
              - php8.2-apcu
              - php8.2-bcmath
              - php8.2-bz2
              - php8.2-cli
              - php8.2-common
              - php8.2-curl
              - php8.2-gd
              - php8.2-gmp
              - php8.2-fpm
              - php8.2-imagick
              - php8.2-imap
              - php8.2-intl
              - php8.2-ldap
              - php8.2-mbstring
              - php8.2-mysql
              - php8.2-opcache
              - php8.2-readline
              # - php8.2-redis
              - php8.2-sqlite3
              - php8.2-soap
              - php8.2-uploadprogress
              - php8.2-xml
              - php8.2-xmlrpc
              - php8.2-xsl
              - php8.2-zip
              - php-pear
...