From e4b5bb2a32dbaedc1cf0010c4dfa4dcd1d7d037a Mon Sep 17 00:00:00 2001 From: Emilien Mantel Date: Tue, 9 Aug 2016 16:02:09 +0200 Subject: [PATCH] Support many php versions (php7) + drop wheezy support --- Vagrantfile | 3 +-- defaults/main.yml | 9 +++++--- doc/php.md | 4 ++-- doc/vhost.md | 1 + meta/main.yml | 1 - tasks/main.yml | 1 - tasks/upstream.yml | 2 +- .../etc/nginx/sites-available/_nagios3.j2 | 4 ++-- templates/etc/nginx/sites-available/_php.j2 | 22 ++++++++++++++++++- .../etc/nginx/sites-available/_php_index.j2 | 2 +- templates/etc/nginx/upstream/php.conf.j2 | 19 ++++++++++++++-- tests/includes/pre_Debian.yml | 9 ++++++++ tests/includes/pre_FreeBSD.yml | 3 ++- tests/test.yml | 18 ++++++++++++++- vars/main.yml | 3 +++ 15 files changed, 83 insertions(+), 18 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index 19ee73b..e0cd80a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -6,9 +6,8 @@ Vagrant.configure("2") do |config| vms_debian = [ - [ "debian-wheezy", "debian/wheezy64" ], [ "debian-jessie", "debian/jessie64" ], - [ "debian-stretch", "sharlak/debian_stretch_64" ], + [ "debian-stretch", "sharlak/debian_stretch_64" ] ] vms_freebsd = [ diff --git a/defaults/main.yml b/defaults/main.yml index 8dabf08..0d10d2b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -31,9 +31,12 @@ nginx_helper_dir: '{{ nginx_etc_dir}}/helper' # # PHP -nginx_php: false -nginx_php_sockets: - - unix_socket: "/var/run/php5-fpm.sock" +nginx_php5: false +nginx_php7: false +nginx_php5_sockets: + - unix_socket: "/run/php5-fpm.sock" +nginx_php7_sockets: + - unix_socket: "/run/php/php7.0-fpm.sock" nginx_upstreams: [] # diff --git a/doc/php.md b/doc/php.md index 81bd97c..965a7aa 100644 --- a/doc/php.md +++ b/doc/php.md @@ -1,7 +1,7 @@ PHP === -- `nginx_php`: boolean if you need to preconfigure PHP (default: false) +- `nginx_php5` and `nginx_php7`: boolean if you need to preconfigure PHP (default: false) - `nginx_php_sockets`: list of sockets (see bellow) You should see [Nginx upstream module doc](http://nginx.org/en/docs/http/ngx_http_upstream_module.html). @@ -15,4 +15,4 @@ Each socket have: - `max_fails` - `fail_timeout` -With default configuration, it works fine with PHP-FPM. But if you install PHP7 with Dotdeb, path changed between version, you must set well this list. +With default configuration, it works fine with PHP-FPM. diff --git a/doc/vhost.md b/doc/vhost.md index 1d93cde..bf52eeb 100644 --- a/doc/vhost.md +++ b/doc/vhost.md @@ -27,6 +27,7 @@ Common - `proto`: (O) list of protocol used. Default is a list with "http". If you need http and https, you must set a list with "http" and "https". You can only set "https" without http support. - `ssl_name`: (D) name of the key used when using TLS/SSL. Mandatory when `proto` contains "https" - `ssl_template` (O) "strong" (default) or "legacy". You can disable SSL helpers and add your own directives by setting "false". +- `php_version` (O) Sepecify PHP version (5 or 7) (O): Optional (M): Mandatory diff --git a/meta/main.yml b/meta/main.yml index 3c3edc3..4b9cb73 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -8,7 +8,6 @@ galaxy_info: platforms: - name: Debian versions: - - wheezy - jessie - name: FreeBSD versions: diff --git a/tasks/main.yml b/tasks/main.yml index 7768d31..39b7cf5 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,6 +1,5 @@ --- - - name: INCLUDE_VARS | Related to OS include_vars: "{{ ansible_distribution }}.yml" diff --git a/tasks/upstream.yml b/tasks/upstream.yml index 8a9ec8f..27c274b 100644 --- a/tasks/upstream.yml +++ b/tasks/upstream.yml @@ -4,7 +4,7 @@ template: > src=etc/nginx/upstream/php.conf.j2 dest="{{ nginx_etc_dir }}/conf.d/php.conf" - when: nginx_php + when: nginx_php5 or nginx_php7 notify: reload nginx - name: TEMPLATE | Deploy other upstreams diff --git a/templates/etc/nginx/sites-available/_nagios3.j2 b/templates/etc/nginx/sites-available/_nagios3.j2 index aebb0ee..33a5f80 100644 --- a/templates/etc/nginx/sites-available/_nagios3.j2 +++ b/templates/etc/nginx/sites-available/_nagios3.j2 @@ -1,4 +1,4 @@ -{% extends "_base.j2" %} +{% extends "_php.j2" %} {% block root %} root {{ nginx_nagios_root }}; @@ -56,7 +56,7 @@ fastcgi_param REMOTE_USER $remote_user; } location ~ \.php$ { - fastcgi_pass php; + fastcgi_pass {{ php_upstream }}; fastcgi_index index.php; {% if nginx_version.stdout | version_compare('1.6.1', 'lt') %} include fastcgi_params; diff --git a/templates/etc/nginx/sites-available/_php.j2 b/templates/etc/nginx/sites-available/_php.j2 index 5cc7332..95e410f 100644 --- a/templates/etc/nginx/sites-available/_php.j2 +++ b/templates/etc/nginx/sites-available/_php.j2 @@ -1,4 +1,24 @@ {% extends "_base.j2" %} + +{% macro phpv(version) %} +{% if version == 5 %} +{{ nginx_upstream_php5 -}} +{% elif version == 7 %} +{{ nginx_upstream_php7 -}} +{% else %} +{# Hack... define another upstream #} +{{ version -}} +{% endif %} +{%- endmacro -%} + +{% if item.php_version is defined %} +{% set php_upstream = phpv(item.php_version) %} +{% elif nginx_php5 %} +{% set php_upstream = phpv(5) %} +{% elif nginx_php7 %} +{% set php_upstream = phpv(7) %} +{% endif %} + {% block template_index %} index {{ item.index | default('index.html index.htm index.php') }}; {% endblock %} @@ -9,7 +29,7 @@ {% block template_upstream_location %} location ~ \.php$ { - fastcgi_pass php; + fastcgi_pass {{ php_upstream }}; fastcgi_index index.php; {% if item.upstream_params is defined and item.upstream_params is iterable %} {% for param in item.upstream_params %} diff --git a/templates/etc/nginx/sites-available/_php_index.j2 b/templates/etc/nginx/sites-available/_php_index.j2 index e56cb34..3bbb158 100644 --- a/templates/etc/nginx/sites-available/_php_index.j2 +++ b/templates/etc/nginx/sites-available/_php_index.j2 @@ -2,7 +2,7 @@ {% block template_upstream_location %} location = /index.php { - fastcgi_pass php; + fastcgi_pass {{ php_upstream }}; fastcgi_index index.php; {% if item.upstream_params is defined and item.upstream_params is iterable %} {% for param in item.upstream_params %} diff --git a/templates/etc/nginx/upstream/php.conf.j2 b/templates/etc/nginx/upstream/php.conf.j2 index dc4cd52..834fb06 100644 --- a/templates/etc/nginx/upstream/php.conf.j2 +++ b/templates/etc/nginx/upstream/php.conf.j2 @@ -2,8 +2,9 @@ # {{ ansible_managed }} # -upstream php { -{% for item in nginx_php_sockets %} +{% if nginx_php5 %} +upstream {{ nginx_upstream_php5 }} { +{% for item in nginx_php5_sockets %} {% if item.unix_socket is defined %} server unix:{{ item.unix_socket }} weight={{ item.weight | default('1') }}; {% else %} @@ -12,4 +13,18 @@ upstream php { {% endfor %} } +{% endif %} +{% if nginx_php7 %} +upstream {{ nginx_upstream_php7 }} { +{% for item in nginx_php7_sockets %} +{% if item.unix_socket is defined %} + server unix:{{ item.unix_socket }} weight={{ item.weight | default('1') }}; +{% else %} + server {{ item.host }}:{{ item.port }} weight={{ item.weight | default('1') }} max_fails={{ item.max_fails | default('5') }} fail_timeout={{ item.fail_timeout | default('10s') }}; +{% endif %} +{% endfor %} +} + +{% endif %} + # vim:filetype=nginx diff --git a/tests/includes/pre_Debian.yml b/tests/includes/pre_Debian.yml index 866a31a..06a6bc7 100644 --- a/tests/includes/pre_Debian.yml +++ b/tests/includes/pre_Debian.yml @@ -3,10 +3,18 @@ - name: APT_REPOSITORY | Install backports apt_repository: repo='deb http://httpredir.debian.org/debian {{ ansible_distribution_release }}-backports main' state=present +- block: + - name: APT | Install DotDeb key + apt_key: url='http://www.dotdeb.org/dotdeb.gpg' state=present + - name: APT_REPOSITORY | Install dotdeb (PHP 7) + apt_repository: repo='deb http://packages.dotdeb.org {{ ansible_distribution_release }} all' state=present + when: ansible_distribution_release == 'jessie' + - name: APT | Install needed packages apt: pkg={{ item }} update_cache=yes cache_valid_time=3600 state=present with_items: - php5-fpm + - php7.0-fpm - curl - fcgiwrap @@ -19,4 +27,5 @@ register: sf with_items: - php5-fpm + - php7.0-fpm - fcgiwrap diff --git a/tests/includes/pre_FreeBSD.yml b/tests/includes/pre_FreeBSD.yml index e86bdb0..8ae6c1d 100644 --- a/tests/includes/pre_FreeBSD.yml +++ b/tests/includes/pre_FreeBSD.yml @@ -4,7 +4,8 @@ set_fact: nginx_pkgng_package: 'nginx-devel' nginx_user: 'www' - nginx_php_sockets: + nginx_php7: false + nginx_php5_sockets: - host: '127.0.0.1' port: 9000 diff --git a/tests/test.yml b/tests/test.yml index ec36356..3efac2a 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -16,7 +16,8 @@ # Role vars nginx_worker_processes: 1 # Ansible+FreeBSD can't detect CPU number nginx_backports: true - nginx_php: true + nginx_php5: true + nginx_php7: true nginx_upstreams: - name: 'test' servers: @@ -145,6 +146,7 @@ '/': - 'alias /var/tmp;' - name: 'test-php.local' + php_version: 7 upstream_params: - 'fastcgi_param FOO bar;' redirect_from: @@ -240,6 +242,20 @@ failed_when: p.stdout.find('PHP Version') == -1 with_items: ['test-php.local', 'test-php-index.local'] + - name: -- VERIFY PHP5 VHOSTS (implicit default) -- + command: "curl -H 'Host: {{ item }}' http://127.0.0.1/" + register: p + changed_when: false + failed_when: p.stdout.find('PHP Version 5') == -1 + with_items: ['test-php-index.local'] + + - name: -- VERIFY PHP7 VHOSTS -- + command: "curl -H 'Host: {{ item }}' http://127.0.0.1/" + register: p + changed_when: false + failed_when: p.stdout.find('PHP Version 7') == -1 + with_items: ['test-php.local'] + # -------------------------------- # Basic Auth # -------------------------------- diff --git a/vars/main.yml b/vars/main.yml index 6c89185..3fff0ba 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -31,3 +31,6 @@ nginx_templates_no_dir: - '_proxy' - '_nagios3' - '_backuppc' + +nginx_upstream_php5: 'php5' +nginx_upstream_php7: 'php7'