Learn Ansible
上QQ阅读APP看书,第一时间看更新

Installing PHP

Like the previous three parts of the stack, we are going to start by installing the packages. As before, we are defining a variable in roles/php/default/main.yml that lists all of the packages we need:

php_packages:
- "php72u"
- "php72u-bcmath"
- "php72u-cli"
- "php72u-common"
- "php72u-dba"
- "php72u-fpm"
- "php72u-fpm-httpd"
- "php72u-gd"
- "php72u-intl"
- "php72u-json"
- "php72u-mbstring"
- "php72u-mysqlnd"
- "php72u-odbc"
- "php72u-pdo"
- "php72u-process"
- "php72u-snmp"
- "php72u-soap"
- "php72u-xml"
- "php72u-xmlrpc"

This is installed by using the YUM module in php/roles/tasks/main.yml:

- name: install the php packages
yum:
name: "{{ item }}"
state: "installed"
with_items: "{{ php_packages }}"
notify:
- "restart php-fpm"
- "restart httpd"

As you can see from this task, we are notifying two different handlers, the one for Apache and one for PHP-FPM. You may be thinking to yourself: why do we need to notify Apache?

FastCGI Process Manager (FPM) is a PHP FastCGI implementation that helps busy PHP websites run more efficiently. It also adds the ability to start PHP workers with different user and group IDs, which can listen on different ports using different php.ini files, allowing you to create pools of PHP workers to handle your load.

As we are installing the php72u-fpm package, we need to configure Apache to use the configuration put in place by the php72u-fpm-httpd package; if we don't, then Apache will not load the configuration, which instructs it on how to interact with PHP-FPM.

The handler for PHP-FPM can be found in roles/php/handlers/main.yml, and it contains the following:

- name: "restart php-fpm"
service:
name: "php-fpm"
state: "restarted"
enabled: "yes"

That is kind of it for the PHP installation and configuration; we should now have a working PHP installation, and we can test this using a phpinfo file.