diff --git a/README.md b/README.md index bddd6f2..48dad1c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -Ansible PHP (+FPM) role for Debian / Ubuntu -=========================================== +Ansible PHP (+FPM) role for Debian / Ubuntu / FreeBSD +===================================================== [![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-HanXHX.php-blue.svg)](https://galaxy.ansible.com/HanXHX/php) [![Build Status](https://travis-ci.org/HanXHX/ansible-php.svg?branch=master)](https://travis-ci.org/HanXHX/ansible-php) @@ -13,6 +13,8 @@ Managed OS / Versions | Debian Strech (9) | Yes | Yes (Sury) | Yes (Sury) | | Ubuntu Xenial (16.04) | Yes | No | No | | Ubuntu Bionic (18.04) | No | No | Yes | +| FreeBSD 11 | Yes | Yes | Yes | +| FreeBSD 12 | Yes | Yes | Yes | Links: - [Dotdeb](https://www.dotdeb.org) @@ -23,6 +25,14 @@ Requirements If you need PHP-FPM, you must install a webserver with FastCGI support. You can use my [nginx role](https://github.com/HanXHX/ansible-nginx). + +FreeBSD limitations +------------------- + +- It doesn't split ini file for FPM/CLI. It's hardcoded as `/usr/local/etc/php.ini`. +- It can't manage multiple PHP versions at the time (like old Debian versions) +- You must explicitely set xdebug package name (use `pkg search xdebug` to find the good one) + Role Variables -------------- diff --git a/Vagrantfile b/Vagrantfile index b41f7f0..27db954 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -13,6 +13,11 @@ Vagrant.configure("2") do |config| { :name => "ubuntu-bionic-php72", :box => "ubuntu/bionic64", :vars => { }}, ] + vms_freebsd = [ + { :name => "freebsd-11", :box => "freebsd/FreeBSD-11.1-STABLE", :vars => {} }, + { :name => "freebsd-12", :box => "freebsd/FreeBSD-12.0-CURRENT", :vars => {} } + ] + conts = [ { :name => "docker-debian-stretch-php70", :docker => "hanxhx/vagrant-ansible:debian9", :vars => { }}, { :name => "docker-debian-stretch-php71", :docker => "hanxhx/vagrant-ansible:debian9", :vars => { "php_version": '7.1' }}, @@ -55,4 +60,24 @@ Vagrant.configure("2") do |config| end end end + + vms_freebsd.each do |opts| + config.vm.base_mac = "080027D14C66" + config.vm.define opts[:name] do |m| + m.vm.box = opts[:box] + m.vm.provider "virtualbox" do |v, override| + override.ssh.shell = "csh" + v.cpus = 2 + v.memory = 512 + end + m.vm.provision "shell", inline: "pkg install -y python bash" + m.vm.provision "ansible" do |ansible| + ansible.playbook = "tests/test.yml" + ansible.verbose = 'vv' + ansible.become = true + ansible.extra_vars = opts[:vars].merge({ "ansible_python_interpreter": '/usr/local/bin/python' }) + end + end + end + end diff --git a/defaults/main.yml b/defaults/main.yml index fe376ed..6cd4fb2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -4,6 +4,7 @@ php_version: '{{ php_default_version }}' php_install_fpm: true php_install_xdebug: false php_extra_packages: [] +php_xdebug_package: null # php.ini config php_ini: diff --git a/meta/main.yml b/meta/main.yml index 99079d2..fb8b2a1 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -13,6 +13,11 @@ galaxy_info: versions: - xenial - bionic + - name: FreeBSD + versions: + - 11.0 + - 11.1 + - 12.0 galaxy_tags: - development - web @@ -21,5 +26,6 @@ galaxy_info: - php7 - debian - ubuntu + - freebsd dependencies: [] diff --git a/tasks/fpm.yml b/tasks/fpm.yml index bd088e3..ad35f49 100644 --- a/tasks/fpm.yml +++ b/tasks/fpm.yml @@ -1,14 +1,23 @@ --- -- name: APT | Install PHP-FPM +- name: APT | Install PHP-FPM for Debian based systems apt: pkg: "{{ php_fpm_service }}" + state: "{{ 'present' if php_install_fpm else 'absent' }}" + when: ansible_os_family == 'Debian' + +- name: SERVICE | Enable service on FreeBSD + service: + name: "{{ php_fpm_service }}" + enabled: "{{ 'yes' if php_install_fpm else 'no' }}" + when: ansible_os_family == 'FreeBSD' - name: LINEINFILE | PHP configuration lineinfile: dest: '{{ php_cli_ini }}' regexp: '^;?{{ item.key }}' line: '{{ item.key }} = {{ item.value }}' + create: yes with_dict: "{{ php_ini | combine(php_ini_fpm) }}" notify: restart php-fpm diff --git a/tasks/main.yml b/tasks/main.yml index ea7b473..7fc665b 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -16,17 +16,26 @@ - "{{ php_packages }}" - "{{ php_extra_packages }}" notify: restart php-fpm + when: ansible_os_family == 'Debian' + +- name: PKGNG | Install PHP packages + pkgng: + name: "{{ item }}" + with_flattened: + - "{{ php_packages }}" + - "{{ php_extra_packages }}" + notify: restart php-fpm + when: ansible_os_family == 'FreeBSD' - name: INCLUDE | PHP-FPM include: fpm.yml - when: php_install_fpm -- name: LINEINFILE | PHP configuration +- name: LINEINFILE | PHP CLI configuration lineinfile: dest: '{{ php_cli_ini }}' regexp: '^;?{{ item.key }}' line: '{{ item.key }} = {{ item.value }}' - with_dict: "{{ php_ini_cli }}" + with_dict: "{{ php_ini | combine(php_ini_cli) }}" - name: INCLUDE | Xdebug include: xdebug.yml diff --git a/tasks/opcache.yml b/tasks/opcache.yml index bc8e6d1..23f603e 100644 --- a/tasks/opcache.yml +++ b/tasks/opcache.yml @@ -1,12 +1,28 @@ --- -- name: APT | Install APCu - apt: - pkg: "{{ php_package_prefix }}apcu" +- block: -- name: APT | Install Opcache - apt: - pkg: "{{ php_package_prefix }}opcache" + - name: APT | Install APCu + apt: + pkg: "{{ php_package_prefix }}apcu" + + - name: APT | Install Opcache + apt: + pkg: "{{ php_package_prefix }}opcache" + + when: ansible_os_family == 'Debian' + +- block: + + - name: PKGNG | Install APCu + pkgng: + name: "php{{ php_version | replace('.', '') }}-pecl-APCu" + + - name: PKGNG | Install Opcache + pkgng: + name: "{{ php_package_prefix }}opcache" + + when: ansible_os_family == 'FreeBSD' - name: TEMPLATE | Configure Opcache template: diff --git a/tasks/xdebug.yml b/tasks/xdebug.yml index cfc1fc2..bdc3491 100644 --- a/tasks/xdebug.yml +++ b/tasks/xdebug.yml @@ -2,12 +2,18 @@ - block: - - name: APT | Install php-xdebug + - name: APT | Install xdebug apt: - pkg: "{{ php_package_prefix }}xdebug" + pkg: "{{ php_xdebug_package }}" state: present update_cache: yes cache_valid_time: 3600 + when: ansible_os_family == 'Debian' + + - name: PKGNG | Install xdebug + pkgng: + name: "{{ php_xdebug_package }}" + when: ansible_os_family == 'FreeBSD' and php_xdebug_package is defined - name: TEMPLATE | Deploy module configurations template: @@ -19,8 +25,17 @@ when: php_install_xdebug -- name: APT | Uninstall php-xdebug - apt: - pkg: "{{ php_package_prefix }}xdebug" - state: absent +- block: + + - name: APT | Uninstall xdebug + apt: + pkg: "{{ php_xdebug_package }}" + state: absent + when: ansible_os_family == 'Debian' + + - name: PKGNG | Install xdebug + pkgng: + name: "{{ php_xdebug_package }}" + when: ansible_os_family == 'FreeBSD' and php_xdebug_package is defined + when: not php_install_xdebug diff --git a/templates/etc/__php__/fpm/pool.d/pool.conf.j2 b/templates/etc/__php__/fpm/pool.d/pool.conf.j2 index eec303f..24fa286 100644 --- a/templates/etc/__php__/fpm/pool.d/pool.conf.j2 +++ b/templates/etc/__php__/fpm/pool.d/pool.conf.j2 @@ -20,8 +20,8 @@ ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. -user = {{ item.user | default('www-data') }} -group = {{ item.group | default('www-data') }} +user = {{ item.user | default(php_default_user_group) }} +group = {{ item.group | default(php_default_user_group) }} ; The address on which to accept FastCGI requests. ; Valid syntaxes are: @@ -46,8 +46,8 @@ listen = {{ item.listen }} ; BSD-derived systems allow connections regardless of permissions. ; Default Values: user and group are set as the running user ; mode is set to 0660 -listen.owner = {{ item.listen_owner | default('www-data') }} -listen.group = {{ item.listen_owner | default('www-data') }} +listen.owner = {{ item.listen_owner | default(php_default_user_group) }} +listen.group = {{ item.listen_owner | default(php_default_user_group) }} ;listen.mode = 0660 ; When POSIX Access Control Lists are supported you can set them using ; these options, value is a comma separated list of user/group names. diff --git a/tests/includes/pre_Debian.yml b/tests/includes/pre_Debian.yml index d8e32d4..bbf1a55 100644 --- a/tests/includes/pre_Debian.yml +++ b/tests/includes/pre_Debian.yml @@ -2,7 +2,7 @@ - name: SET_FACT | Prepare test vars set_fact: - __nginx_site_dir: /etc/nginx/sites-enabled + __nginx_conf: /etc/nginx/nginx.conf - name: APT | Install packages apt: diff --git a/tests/includes/pre_FreeBSD.yml b/tests/includes/pre_FreeBSD.yml new file mode 100644 index 0000000..5630871 --- /dev/null +++ b/tests/includes/pre_FreeBSD.yml @@ -0,0 +1,13 @@ +--- + +- name: SET_FACT | Prepare test vars + set_fact: + __nginx_conf: /usr/local/etc/nginx/nginx.conf + php_xdebug_package: 'php72-pecl-xdebug26' + +- name: PKGNG | Install packages + pkgng: + name: "{{ item }}" + with_items: + - curl + - nginx diff --git a/tests/templates/nginx.conf.j2 b/tests/templates/nginx.conf.j2 new file mode 100644 index 0000000..80852ab --- /dev/null +++ b/tests/templates/nginx.conf.j2 @@ -0,0 +1,36 @@ +events { + worker_connections 1024; +} + +user {{ php_default_user_group }}; + +http { + include mime.types; + default_type application/octet-stream; + sendfile on; + keepalive_timeout 65; + + server { + server_name {{ vhost }}; + + root /var/www; + +{% if php_fpm_poold.0.status_path is defined %} + location = {{ php_fpm_poold.0.status_path }} { + include fastcgi.conf; + fastcgi_pass unix:{{ php_default_fpm_sock }}; + } +{% endif %} +{% if php_fpm_poold.0.ping_path is defined %} + location = {{ php_fpm_poold.0.ping_path }} { + include fastcgi.conf; + fastcgi_pass unix:{{ php_default_fpm_sock }}; + } +{% endif %} + + location ~ \.php$ { + include fastcgi.conf; + fastcgi_pass unix:{{ php_default_fpm_sock }}; + } + } +} diff --git a/tests/templates/site.j2 b/tests/templates/site.j2 deleted file mode 100644 index 6ea739e..0000000 --- a/tests/templates/site.j2 +++ /dev/null @@ -1,23 +0,0 @@ -server { - server_name {{ vhost }}; - - root /var/www; - -{% if php_fpm_poold.0.status_path is defined %} - location = {{ php_fpm_poold.0.status_path }} { - include fastcgi.conf; - fastcgi_pass unix:{{ php_default_fpm_sock }}; - } -{% endif %} -{% if php_fpm_poold.0.ping_path is defined %} - location = {{ php_fpm_poold.0.ping_path }} { - include fastcgi.conf; - fastcgi_pass unix:{{ php_default_fpm_sock }}; - } -{% endif %} - - location ~ \.php$ { - include fastcgi.conf; - fastcgi_pass unix:{{ php_default_fpm_sock }}; - } -} diff --git a/tests/test.yml b/tests/test.yml index 2764439..1295b25 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -31,14 +31,21 @@ - name: TEMPLATE | Nginx site config template: - src: "templates/site.j2" - dest: "{{ __nginx_site_dir }}/{{ vhost }}" + src: "templates/nginx.conf.j2" + dest: "{{ __nginx_conf }}" notify: reload nginx - - name: FILE | Delete default site - file: - path: "{{ __nginx_site_dir }}/default" - state: absent + - name: COMMAND | Fix nginx config + command: "cp {{ __nginx_conf | dirname }}/fastcgi_params {{ __nginx_conf | dirname }}/fastcgi.conf" + args: + creates: "{{ __nginx_conf | dirname }}/fastcgi.conf" + notify: reload nginx + + - name: LINEINFILE | Fix nginx config (second step) + lineinfile: + regexp: '^fastcgi_param\s+SCRIPT_FILENAME' + line: "fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;" + dest: "{{ __nginx_conf | dirname }}/fastcgi.conf" notify: reload nginx handlers: diff --git a/vars/FreeBSD-12.0-CURRENT.yml b/vars/FreeBSD-12.0-CURRENT.yml new file mode 100644 index 0000000..ebebaae --- /dev/null +++ b/vars/FreeBSD-12.0-CURRENT.yml @@ -0,0 +1 @@ +php_default_version: '7.2' diff --git a/vars/OS_Family_Debian.yml b/vars/OS_Family_Debian.yml index 8313c28..ccc27dc 100644 --- a/vars/OS_Family_Debian.yml +++ b/vars/OS_Family_Debian.yml @@ -7,14 +7,17 @@ php_packages: - '{{ php_package_prefix }}mysql' - '{{ php_package_prefix }}intl' +php_xdebug_package: 'php-xdebug' + php_package_prefix: 'php{{ php_version }}-' -php_etc_dir: '/etc/php/{{ php_version }}' -php_mods_dir: '{{ php_etc_dir }}/mods-available' -php_fpm_pool_dir: '{{ php_etc_dir }}/fpm/pool.d' +php_mods_dir: '/etc/php/{{ php_version }}/mods-available' +php_fpm_pool_dir: '/etc/php/{{ php_version }}/fpm/pool.d' php_fpm_service: 'php{{ php_version }}-fpm' php_default_fpm_sock: '/var/run/php/php{{ php_version }}-fpm.sock' -php_cli_ini: '{{ php_etc_dir }}/cli/php.ini' -php_fpm_ini: '{{ php_etc_dir }}/fpm/php.ini' +php_cli_ini: '/etc/php/{{ php_version }}/cli/php.ini' +php_fpm_ini: '/etc/php/{{ php_version }}/fpm/php.ini' + +php_default_user_group: 'www-data' diff --git a/vars/OS_Family_FreeBSD.yml b/vars/OS_Family_FreeBSD.yml new file mode 100644 index 0000000..8c675f2 --- /dev/null +++ b/vars/OS_Family_FreeBSD.yml @@ -0,0 +1,20 @@ +--- + +php_packages: + - '{{ php_package_prefix }}curl' + - '{{ php_package_prefix }}gd' + - '{{ php_package_prefix }}mysqli' + - '{{ php_package_prefix }}intl' + +php_package_prefix: 'php{{ php_version | replace(".", "") }}-' + +php_mods_dir: '/usr/local/etc/php' +php_fpm_pool_dir: '/usr/local/etc/php-fpm.d' + +php_fpm_service: 'php-fpm' +php_default_fpm_sock: '/var/run/php-fpm.sock' + +php_cli_ini: '/usr/local/etc/php.ini' +php_fpm_ini: '/usr/local/etc/php.ini' + +php_default_user_group: 'www'