diff --git a/defaults/main.yml b/defaults/main.yml index 38e3534..e58dc7f 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -8,6 +8,18 @@ php_enable_webserver: true # Start and enable the PHP fpm service. php_enable_php_fpm: false +# OpCache settings (useful for PHP >=5.5). +php_opcache_enabled_in_ini: false +php_opcache_enable: "1" +php_opcache_enable_cli: "0" +php_opcache_memory_consumption: "96" +php_opcache_interned_strings_buffer: "16" +php_opcache_max_accelerated_files: "4096" +php_opcache_max_wasted_percentage: "5" +php_opcache_validate_timestamps: "1" +php_opcache_revalidate_freq: "2" +php_opcache_max_file_size: "0" + # APC settings (useful for PHP <5.5). php_enable_apc: true php_apc_enabled_in_ini: false diff --git a/tasks/configure.yml b/tasks/configure.yml index d3835d8..4f2baee 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -29,6 +29,17 @@ when: php_enable_apc notify: restart webserver +- name: Place OpCache configuration file in place. + template: + src: opcache.ini.j2 + dest: "{{ php_extension_conf_path }}/{{ php_opcache_conf_filename }}" + owner: root + group: root + force: yes + mode: 0644 + when: php_opcache_enable + notify: restart webserver + - name: Ensure php-fpm is started and enabled at boot (if configured). service: name: "{{ php_fpm_daemon }}" diff --git a/tasks/main.yml b/tasks/main.yml index 4ae5688..9f3deb9 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -30,5 +30,15 @@ - include: setup-Debian.yml when: ansible_os_family == 'Debian' +- name: Check the installed version of PHP. + shell: php -r "echo PHP_VERSION;" + register: php_version + changed_when: false + +- name: Disable opcache if PHP version is < 5.5. + set_fact: + php_opcache_enable: 0 + when: "php_version.stdout.split('.')[0] == '5' and php_version.stdout.split('.')[1] < '5'" + # Configure PHP. - include: configure.yml diff --git a/templates/opcache.ini.j2 b/templates/opcache.ini.j2 new file mode 100644 index 0000000..f95a1a5 --- /dev/null +++ b/templates/opcache.ini.j2 @@ -0,0 +1,13 @@ +{% if php_opcache_enabled_in_ini %} +zend_extension=opcache.so +{% endif %} + +opcache.enable={{ php_opcache_enable }} +opcache.enable_cli={{ php_opcache_enable_cli }} +opcache.memory_consumption={{ php_opcache_memory_consumption }} +opcache.interned_strings_buffer={{ php_opcache_interned_strings_buffer }} +opcache.max_accelerated_files={{ php_opcache_max_accelerated_files }} +opcache.max_wasted_percentage={{ php_opcache_max_wasted_percentage }} +opcache.validate_timestamps={{ php_opcache_validate_timestamps }} +opcache.revalidate_freq={{ php_opcache_revalidate_freq }} +opcache.max_file_size={{ php_opcache_max_file_size }} diff --git a/vars/Debian.yml b/vars/Debian.yml index 889e9d4..52d3298 100644 --- a/vars/Debian.yml +++ b/vars/Debian.yml @@ -19,4 +19,5 @@ __php_conf_path: "{{ '/etc/php5' if php_webserver_daemon and php_webserver_daemo __php_extension_conf_path: "{{ __php_conf_path }}/conf.d" php_apc_conf_filename: 20-apc.ini +php_opcache_conf_filename: 05-opcache.ini php_fpm_daemon: php5-fpm diff --git a/vars/RedHat.yml b/vars/RedHat.yml index af9e5a2..7e3e303 100644 --- a/vars/RedHat.yml +++ b/vars/RedHat.yml @@ -21,4 +21,5 @@ __php_conf_path: /etc php_extension_conf_path: /etc/php.d php_apc_conf_filename: apc.ini +php_opcache_conf_filename: opcache.ini php_fpm_daemon: php-fpm