entanet_devops.ansible_role_php
Ansible Role: PHP
This role installs PHP on RedHat/CentOS and Debian/Ubuntu servers.
Requirements
If you are using an older version of Ubuntu or RHEL with an outdated PHP version, you'll need to get PHP from a maintained repository. This role only supports currently supported PHP versions, which you can check here.
Role Variables
Here are the available variables, along with their default settings (found in defaults/main.yml
):
php_packages: [] A list of PHP packages to install (specific to each operating system). Common packages to include are
php
,php-cli
,php-devel
, andphp-pdo
. You can also add packages likephp-gd
for image handling orphp-ldap
for LDAP authentication.Note: For Debian/Ubuntu, you must also install
libapache2-mod-fastcgi
for PHP-FPM orlibapache2-mod-php7.0
(or a similar package depending on your PHP version) if you're usingmod_php
with Apache.php_packages_extra: [] Extra PHP packages to install without replacing the default list.
php_enable_webserver: true Set to
true
if you're using PHP with a web server like Apache or Nginx. Set tofalse
if you're only using PHP for server-side scripts or small applications.php_webserver_daemon: "httpd" The default web server daemon is
httpd
for RedHat/CentOS andapache2
for Debian/Ubuntu. Change this if you're using a different web server likenginx
.php_enablerepo: "" (For RedHat/CentOS) If you have any extra repositories enabled, you can list them here (e.g.
remi-php70,epel
). This is useful for installing the latest PHP version from repositories like Remi.php_default_version_debian: "7.0" (For Debian/Ubuntu) This sets the default PHP version in the OS repository, typically the latest Ubuntu LTS. For Ubuntu 18.04, use
"7.2"
since PHP 7.0 is not included in the default packages.
If you want to easily switch PHP versions or use a version not available in system packages: Use the geerlingguy.php-versions
role to switch PHP versions more easily.
php_packages_state: "present" If using extra repositories, you can set this to
"latest"
to automatically upgrade to the latest version without needing to uninstall existing packages.php_install_recommends: true (For Debian/Ubuntu) Determines whether to install recommended packages along with
php_packages
. You may want to set it tono
if you do not want certain packages.php_executable: "php" The command to run PHP from the command line. Change this only if the default command does not point to the right PHP executable.
PHP-FPM
PHP-FPM is a FastCGI Process Manager for PHP that helps improve the scalability of PHP applications. It is commonly used with web servers like Nginx.
To enable PHP-FPM using this role, change this variable to true
:
- php_enable_php_fpm: false
If using Apache, you can configure it to work with PHP-FPM using the geerlingguy.apache-php-fpm role.
- php_fpm_listen: "127.0.0.1:9000"
- php_fpm_listen_allowed_clients: "127.0.0.1"
- php_fpm_pm_max_children: 50
- php_fpm_pm_start_servers: 5
- php_fpm_pm_min_spare_servers: 5
- php_fpm_pm_max_spare_servers: 5
These are specific settings for the www.conf
PHP-FPM pool. You can manage them by replacing the configuration file or using a method like lineinfile
.
php.ini Settings
- php_use_managed_ini: true
By default, the role applies standard settings to the included
php.ini
file. To manage your ownphp.ini
, set this tofalse
(and other variables will be ignored).
The following settings can be configured if php_use_managed_ini
is true
:
- php_fpm_pool_user, php_fpm_pool_group (default varies by OS)
- php_memory_limit: "256M"
- php_max_execution_time: "60"
- php_file_uploads: "On"
- php_upload_max_filesize: "64M"
- php_post_max_size: "32M"
- php_date_timezone: "America/Chicago"
- php_display_errors: "Off"
- php_expose_php: "On"
OpCache-related Variables
OpCache is built into PHP from version 5.5 onward. The following settings are effective only if PHP 5.5 or higher is installed:
- php_opcache_enable: "1"
- php_opcache_memory_consumption: "96"
- php_opcache_max_wasted_percentage: "5"
Make sure you allocate enough memory and file slots for OpCache to optimize PHP performance!
APCu-related Variables
APCu is a caching solution for PHP. To enable it:
- php_enable_apc: true
- php_apc_shm_size: "96M"
Ensure that APCu is installed by including it in the php_packages
list on your system.
Installing from Source
If you need a specific PHP version or want to test the latest version, you can compile PHP from source:
- php_install_from_source: false
- php_source_version: "master"
Set php_install_from_source
to true
to install from source. Specify the source version, clone directory, and installation paths as needed.
Dependencies
There are no additional dependencies.
Example Playbook
- hosts: webservers
vars_files:
- vars/main.yml
roles:
- { role: geerlingguy.php }
In vars/main.yml
:
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
...
License
MIT / BSD
Author Information
This role was created in 2014 by Jeff Geerling, author of Ansible for DevOps.
PHP for RedHat/CentOS/Fedora/Debian/Ubuntu.
ansible-galaxy install entanet_devops.ansible_role_php