---
- name: "[APACHE] -  enable APC for php CLI"
  ansible.builtin.lineinfile:
    dest: "{{ php_dir }}/cli/php.ini"
    line: "apc.enable_cli = 1"
    insertbefore: "^; End:$"
    state: present
    # validate: "/usr/sbin/{{ php_bin }} -t #%s"

- name: "[APACHE] -  enable PHP OPcache for php.ini"
  ansible.builtin.lineinfile:
    dest: "{{ php_dir }}/apache2/php.ini"
    state: present
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
    backrefs: true
  with_items:
    - {regexp: 'opcache.enable=0', line: 'opcache.enable=1'}
    - {regexp: 'opcache.enable_cli', line: 'opcache.enable_cli=1'}
    - {regexp: 'opcache.interned_strings_buffer', line: 'opcache.interned_strings_buffer=8'}
    - {regexp: 'opcache.max_accelerated_files', line: 'opcache.max_accelerated_files=10000'}
    - {regexp: 'opcache.memory_consumption', line: 'opcache.memory_consumption=128'}
    - {regexp: 'opcache.save_comments', line: 'opcache.save_comments=1'}
    - {regexp: 'opcache.revalidate_freq', line: 'opcache.revalidate_freq=1'}
    - {regexp: 'memory_limit', line: 'memory_limit={{ php_memory_limit }}'}
    # validate: "/usr/sbin/{{ php_bin }} -t #%s"
  notify: reload http

- name: "[APACHE] -  Required Apache2 modules are enabled"
  apache2_module:
    name: "{{ item }}"
    state: present
  with_items:
    - rewrite
    - headers
    - env
    - dir
    - mime
  notify: restart http

- name: "[APACHE] -  Ssl Apache2 module is enabled"
  apache2_module:
    state: present
    name: "{{ item }}"
  with_items:
    - ssl
  when: (nextcloud_install_tls | bool)
  notify: restart http

- name: "[APACHE] -  generate Nextcloud configuration for apache"
  ansible.builtin.template:
    dest: /etc/apache2/sites-available/nc_{{ nextcloud_instance_name }}.conf
    src: "{{ nextcloud_websrv_template }}"
    mode: 0640
  notify: reload http

- name: "[APACHE] -  Enable Nextcloud site in apache conf"
  ansible.builtin.file:
    path: /etc/apache2/sites-enabled/nc_{{ nextcloud_instance_name }}.conf
    src: /etc/apache2/sites-available/nc_{{ nextcloud_instance_name }}.conf
    state: link
  notify: reload http

- name: "[APACHE] -  Disable apache default site"
  ansible.builtin.file:
    path: /etc/apache2/sites-enabled/000-default.conf
    state: absent
  when: nextcloud_disable_websrv_default_site | bool
  notify: reload http
