geerlingguy.php Ansible Role
repository·master·Indexed 19 days ago
https://github.com/geerlingguy/ansible-role-phpAn 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.
What's inside geerlingguy-ansible-role-php
- This Ansible role automates the installation of PHP on RedHat/CentOS and Debian/Ubuntu servers. It is designed to work with PHP versions that are currently supported by the PHP community.
Install PHP from source
masterIf 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: Usemake --jobs=X(where X is the number of cores) to speed up compilation.php_source_configure_command: The./configurecommand with necessary flags.
Webserver Specific Requirements for Source Builds:
- Apache with
mpm_prefork: Requiresapxs2installed on the system and the--with-apxs2flag inphp_source_configure_command. - Apache with
mpm_eventormpm_worker: Requires compiling PHP with FPM (use--enable-fpminphp_source_configure_command). - Nginx: Requires compiling PHP with FPM (use
--enable-fpminphp_source_configure_command).
PHP version requirements
masterThis 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.Configure OpCache settings
masterOpCache is available for PHP 5.5+. You can tune performance using these variables. Ensurephp_opcache_memory_consumption(in MB) andphp_opcache_max_accelerated_filesare large enough to hold your application code to avoid performance degradation.Manage php.ini settings
masterThe role allows managing
php.inisettings through variables. This is only active ifphp_use_managed_iniis set totrue(which is the default).If you want to manage your own
php.inifile manually, setphp_use_managed_ini: false. In this case, all otherphp_*configuration variables will be ignored.Configure APCu settings
masterTo 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-apcuinphp_packages. - Debian/Ubuntu: Include
php-apcuinphp_packages.
Ensure
php_apc_shm_sizeis large enough to hold all cache entries to prevent dramatic performance slowdowns.- RHEL/CentOS: Include
Configure webserver integration
masterThe 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_webserverdefaults totrue. - Set
php_webserver_daemonto the name of the daemon (e.g.,httpdfor Apache on RedHat/CentOS,apache2for Apache on Debian/Ubuntu, ornginxfor Nginx).
- If PHP is used for server-side tasks or small applications without a webserver, set
Configure PHP-FPM pools
masterTo use PHP-FPM instead of
mod_php, setphp_enable_php_fpm: true.You can define multiple FPM pools using the
php_fpm_poolslist. Each item in the list defines a pool's configuration. By default, awwwpool 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: /statusConfigure PHP package installation
masterUse
php_packagesto define the list of PHP packages to install. By default, it uses OS-specific defaults, but you should include common packages likephp,php-cli,php-devel, andphp-pdo. You can add extensions likephp-gdorphp-ldaphere.To add packages without replacing the defaults, use
php_packages_extra.Debian/Ubuntu Note: If using Apache with
mod_php, you must manually addlibapache2-mod-fastcgiorlibapache2-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-ldapConfigure PHP packages and settings in vars/main.yml
masterYou 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-xmlUse the PHP Ansible role in a playbook
masterTo install PHP using this role, include
geerlingguy.phpin your playbook'srolessection. It is recommended to use avars_filesdeclaration to manage your PHP configuration and package list separately from the playbook logic.Example Playbook Structure
- Create a
vars/main.ymlfile to define your PHP settings and required packages. - Reference that file in your playbook.
- Call the role.
- hosts: webservers vars_files: - vars/main.yml roles: - { role: geerlingguy.php }- Create a