geerlingguy.php Ansible Role

repository·master·Indexed 19 days ago

https://github.com/geerlingguy/ansible-role-php

An Ansible role for installing and configuring PHP on RedHat/CentOS and Debian/Ubuntu. It supports package-based installation and compilation from source, management of php.ini settings, PHP-FPM pool configuration, and integration with webservers like Apache and Nginx. Includes support for OpCache and APCu tuning.

Tokens
1.6K
Snippets
4
Records
11
Agent score
18%

What's inside geerlingguy-ansible-role-php

  1. Install PHP from source

    master

    If a specific version is required that is not available in system repositories, you can compile PHP from source by setting php_install_from_source: true.

    Key Configuration Variables:

    • php_source_version: The git branch, tag, or commit hash to use.
    • php_source_install_path: Where PHP will be installed (e.g., /opt/php).
    • php_source_make_command: Use make --jobs=X (where X is the number of cores) to speed up compilation.
    • php_source_configure_command: The ./configure command with necessary flags.

    Webserver Specific Requirements for Source Builds:

    • Apache with mpm_prefork: Requires apxs2 installed on the system and the --with-apxs2 flag in php_source_configure_command.
    • Apache with mpm_event or mpm_worker: Requires compiling PHP with FPM (use --enable-fpm in php_source_configure_command).
    • Nginx: Requires compiling PHP with FPM (use --enable-fpm in php_source_configure_command).
  2. PHP version requirements

    master
    This role only supports PHP versions that are currently maintained by the PHP community. If you are using an older LTS release of Ubuntu or RHEL that provides an outdated PHP version, you must first configure a repository or PPA that provides a maintained PHP version before using this role.
  3. Configure OpCache settings

    master
    OpCache is available for PHP 5.5+. You can tune performance using these variables. Ensure php_opcache_memory_consumption (in MB) and php_opcache_max_accelerated_files are large enough to hold your application code to avoid performance degradation.
  4. Manage php.ini settings

    master

    The role allows managing php.ini settings through variables. This is only active if php_use_managed_ini is set to true (which is the default).

    If you want to manage your own php.ini file manually, set php_use_managed_ini: false. In this case, all other php_* configuration variables will be ignored.

  5. Configure APCu settings

    master

    To use APCu, set php_enable_apc: true.

    Important: If you customize php_packages, you must manually include the APCu package:

    • RHEL/CentOS: Include php-pecl-apcu in php_packages.
    • Debian/Ubuntu: Include php-apcu in php_packages.

    Ensure php_apc_shm_size is large enough to hold all cache entries to prevent dramatic performance slowdowns.

  6. Configure webserver integration

    master

    The role can interact with webservers to set up PHP.

    • If PHP is used for server-side tasks or small applications without a webserver, set php_enable_webserver: false.
    • If using a webserver, php_enable_webserver defaults to true.
    • Set php_webserver_daemon to the name of the daemon (e.g., httpd for Apache on RedHat/CentOS, apache2 for Apache on Debian/Ubuntu, or nginx for Nginx).
  7. Configure PHP-FPM pools

    master

    To use PHP-FPM instead of mod_php, set php_enable_php_fpm: true.

    You can define multiple FPM pools using the php_fpm_pools list. Each item in the list defines a pool's configuration. By default, a www pool is created.

    Common pool settings include:

    • pool_name: The name of the pool.
    • pool_template: Path to the Jinja2 template for the pool config.
    • pool_listen: The address/port to listen on (e.g., 127.0.0.1:9000).
    • pool_pm: Process manager type (e.g., dynamic).
    • pool_pm_max_children: Maximum number of child processes.
    php_enable_php_fpm: true
    php_fpm_pools:
      - pool_name: www
        pool_template: www.conf.j2
        pool_listen: "127.0.0.1:9000"
        pool_listen_allowed_clients: "127.0.0.1"
        pool_pm: dynamic
        pool_pm_max_children: 5
        pool_pm_start_servers: 2
        pool_pm_min_spare_servers: 1
        pool_pm_max_spare_servers: 3
        pool_pm_max_requests: 500
        pool_pm_status_path: /status
  8. Configure PHP package installation

    master

    Use php_packages to define the list of PHP packages to install. By default, it uses OS-specific defaults, but you should include common packages like php, php-cli, php-devel, and php-pdo. You can add extensions like php-gd or php-ldap here.

    To add packages without replacing the defaults, use php_packages_extra.

    Debian/Ubuntu Note: If using Apache with mod_php, you must manually add libapache2-mod-fastcgi or libapache2-mod-php7.0 (depending on version) to your package list.

    php_packages:
      - php
      - php-cli
      - php-devel
      - php-pdo
      - php-gd
    php_packages_extra:
      - php-ldap
  9. Configure PHP packages and settings in vars/main.yml

    master

    You can customize the PHP installation by defining specific variables in a YAML file (e.g., vars/main.yml). This allows you to control the memory limit, execution time, upload limits, and the specific list of PHP packages to be installed via the package manager.

    Key variables include:

    • php_memory_limit: Sets the PHP memory limit (e.g., "128M").
    • php_max_execution_time: Sets the maximum execution time in seconds.
    • php_upload_max_filesize: Sets the maximum allowed upload size.
    • php_packages: A list of PHP-related packages to install (e.g., php, php-cli, php-gd, php-mbstring).
    php_memory_limit: "128M"
    php_max_execution_time: "90"
    php_upload_max_filesize: "256M"
    php_packages:
      - php
      - php-cli
      - php-common
      - php-devel
      - php-gd
      - php-mbstring
      - php-pdo
      - php-pecl-apcu
      - php-xml
  10. Use the PHP Ansible role in a playbook

    master

    To install PHP using this role, include geerlingguy.php in your playbook's roles section. It is recommended to use a vars_files declaration to manage your PHP configuration and package list separately from the playbook logic.

    Example Playbook Structure

    1. Create a vars/main.yml file to define your PHP settings and required packages.
    2. Reference that file in your playbook.
    3. Call the role.
    - hosts: webservers
      vars_files:
        - vars/main.yml
      roles:
        - { role: geerlingguy.php }