nextcloud: split main task and add vhost configuration

This commit is contained in:
Victor LABORIE 2018-03-26 15:16:55 +02:00
parent 4b6e2c8519
commit fed1ca89a7
9 changed files with 297 additions and 137 deletions

View file

@ -1,4 +1,5 @@
---
nextcloud_webserver: 'nginx'
nextcloud_root: '/home'
nextcloud_version: "13.0.1"
nextcloud_instances: {}

4
nextcloud/meta/main.yml Normal file
View file

@ -0,0 +1,4 @@
---
dependencies:
- { role: nginx, when: "{{ nextcloud_webserver == 'nginx' }}" }
- { role: php, php_fpm_enable: True }

View file

@ -0,0 +1,25 @@
---
- name: Retrieve Nextcloud archive
get_url:
url: "https://download.nextcloud.com/server/releases/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2"
dest: "/home/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- name: Retrieve Nextcloud sha256 checksum
get_url:
url: "https://download.nextcloud.com/server/releases/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2.sha256"
dest: "/home/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2.sha256"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- name: Verify Nextcloud sha256 checksum
command: "sha256sum -c nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2.sha256"
changed_when: False
args:
chdir: "/home/"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud

View file

@ -0,0 +1,67 @@
---
- name: Link config dir to global config dir
file:
src: "{{ nextcloud_root }}/{{ item }}/config/config.php"
dest: "{{ nextcloud_root }}/{{ item }}/nextcloud/config/config.php"
state: link
owner: "{{ item }}"
group: "{{ item }}"
force: True
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Install Nextcloud
shell: "php ./occ maintenance:install --database mysql --database-name {{ item.key }} --database-user {{ item.key }} --database-pass {{ item.value.db_pass }} --admin-user admin --admin-pass toor --data-dir {{ nextcloud_root }}/{{ item.key }}/data"
args:
chdir: "{{ nextcloud_root }}/{{ item.key }}/nextcloud/"
creates: "{{ nextcloud_root }}/{{ item.key }}/config/config.php"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- name: Configure Nextcloud cron
cron:
name: 'Nextcloud'
minute: "*/15"
job: "php -f ~/nextcloud/cron.php"
user: "{{ item }}"
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Erase previously trusted domains config
shell: "php ./occ config:system:set trusted_domains"
args:
chdir: "{{ nextcloud_root }}/{{ item }}/nextcloud/"
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Configure trusted domains
shell: "php ./occ config:system:set trusted_domains {{ item[1] }} --value {{ nextcloud_instances[item[0]].domains[item[1]] }}"
args:
chdir: "{{ nextcloud_root }}/{{ item[0] }}/nextcloud/"
with_nested:
- "{{ nextcloud_instances | list }}"
- "{{ range(0, nextcloud_instances | list | length ) | list }}"
tags:
- nextcloud
#- name: Configure memcache local to APCu
# shell: "php ./occ config:system:set memcache.local --value '\\OC\\Memcache\\APCu'"
# args:
# chdir: "{{ nextcloud_root }}/{{ item }}/nextcloud/"
# with_items: "{{ nextcloud_instances | list }}"
# tags:
# - nextcloud
- name: Fix right on config.php
file:
dest: "{{ nextcloud_root }}/{{ item }}/config/config.php"
owner: "{{ item }}"
group: "{{ item }}"
mode: "0660"
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud

View file

@ -25,141 +25,8 @@
tags:
- nextcloud
- name: Create Nextcloud groups
group:
name: "{{ item }}"
state: present
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Create Nextcloud users
user:
name: "{{ item }}"
group: "{{ item }}"
home: "{{ nextcloud_root }}/{{ item }}"
shell: '/bin/bash'
createhome: True
state: present
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Create needed directories
file:
dest: "{{ nextcloud_root }}/{{ item[0] }}/{{ item[1] }}"
state: directory
mode: "0770"
owner: "{{ item[0] }}"
group: "{{ item[0] }}"
with_nested:
- "{{ nextcloud_instances | list }}"
- [ 'logs', 'config', 'data', 'tmp' ]
tags:
- nextcloud
- name: Retrieve Nextcloud archive
get_url:
url: "https://download.nextcloud.com/server/releases/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2"
dest: "/home/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- name: Retrieve Nextcloud sha256 checksum
get_url:
url: "https://download.nextcloud.com/server/releases/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2.sha256"
dest: "/home/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2.sha256"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- name: Verify Nextcloud sha256 checksum
command: "sha256sum -c nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2.sha256"
changed_when: False
args:
chdir: "/home/"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- name: Extract Nextcloud archive
unarchive:
src: '/home/nextcloud-{{ item.value.version | default(nextcloud_version) }}.tar.bz2'
dest: "{{ nextcloud_root }}/{{ item.key }}"
remote_src: True
mode: "0750"
owner: "{{ item.key }}"
group: "{{ item.key }}"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- include: user.yml
- include: archive.yml
- include: vhost.yml
- include: mysql.yml
- name: Link config dir to global config dir
file:
src: "{{ nextcloud_root }}/{{ item }}/config/config.php"
dest: "{{ nextcloud_root }}/{{ item }}/nextcloud/config/config.php"
state: link
owner: "{{ item }}"
group: "{{ item }}"
force: True
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Install Nextcloud
shell: "php ./occ maintenance:install --database mysql --database-name {{ item.key }} --database-user {{ item.key }} --database-pass {{ item.value.db_pass }} --admin-user admin --admin-pass toor --data-dir {{ nextcloud_root }}/{{ item.key }}/data"
args:
chdir: "{{ nextcloud_root }}/{{ item.key }}/nextcloud/"
creates: "{{ nextcloud_root }}/{{ item.key }}/config/config.php"
with_dict: "{{ nextcloud_instances }}"
tags:
- nextcloud
- name: Configure Nextcloud cron
cron:
name: 'Nextcloud'
minute: "*/15"
job: "php -f ~/nextcloud/cron.php"
user: "{{ item }}"
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Erase previously trusted domains config
shell: "php ./occ config:system:set trusted_domains"
args:
chdir: "{{ nextcloud_root }}/{{ item }}/nextcloud/"
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Configure trusted domains
shell: "php ./occ config:system:set trusted_domains {{ item[1] }} --value {{ nextcloud_instances[item[0]].domains[item[1]] }}"
args:
chdir: "{{ nextcloud_root }}/{{ item[0] }}/nextcloud/"
with_nested:
- "{{ nextcloud_instances | list }}"
- "{{ range(0, nextcloud_instances | list | length ) | list }}"
tags:
- nextcloud
#- name: Configure memcache local to APCu
# shell: "php ./occ config:system:set memcache.local --value '\\OC\\Memcache\\APCu'"
# args:
# chdir: "{{ nextcloud_root }}/{{ item }}/nextcloud/"
# with_items: "{{ nextcloud_instances | list }}"
# tags:
# - nextcloud
- name: Fix right on config.php
file:
dest: "{{ nextcloud_root }}/{{ item }}/config/config.php"
owner: "{{ item }}"
group: "{{ item }}"
mode: "0660"
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- include: config.yml

33
nextcloud/tasks/user.yml Normal file
View file

@ -0,0 +1,33 @@
---
- name: Create Nextcloud groups
group:
name: "{{ item }}"
state: present
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Create Nextcloud users
user:
name: "{{ item }}"
group: "{{ item }}"
home: "{{ nextcloud_root }}/{{ item }}"
shell: '/bin/bash'
createhome: True
state: present
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
- name: Create needed directories
file:
dest: "{{ nextcloud_root }}/{{ item[0] }}/{{ item[1] }}"
state: directory
mode: "0770"
owner: "{{ item[0] }}"
group: "{{ item[0] }}"
with_nested:
- "{{ nextcloud_instances | list }}"
- [ 'logs', 'config', 'data', 'tmp' ]
tags:
- nextcloud

37
nextcloud/tasks/vhost.yml Normal file
View file

@ -0,0 +1,37 @@
---
- block:
- name: Copy Nginx vhost
template:
src: nginx.conf.j2
dest: "/etc/nginx/sites-available/{{ name }}.conf"
mode: "0640"
notify: reload nginx
with_dict: "{{ nextcloud_instances }}"
vars:
- name: "{{ item.key }}"
- domains: "{{ item.value.domains }}"
tags:
- nextcloud
- name: Enable Nginx vhost
file:
src: "/etc/nginx/sites-available/{{ item }}.conf"
dest: "/etc/nginx/sites-enabled/{{ item }}.conf"
state: link
notify: reload nginx
with_items: "{{ nextcloud_instances | list }}"
tags:
- nextcloud
when: "{{ nextcloud_webserver == 'nginx' }}"
- name: Copy PHP-FPM pool
template:
src: php-fpm.conf.j2
dest: "/etc/php/7.0/fpm/pool.d/{{ name }}.conf"
mode: "0640"
notify: restart php-fpm
with_items: "{{ nextcloud_instances | list }}"
vars:
- name: "{{ item }}"
tags:
- nextcloud

View file

@ -0,0 +1,108 @@
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name{% for domain in domains %} {{ domain }}{% endfor %};
include /etc/nginx/ssl/{{ name }}.conf;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
root /home/{{ name }}/nextcloud;
include /etc/nginx/snippets/letsencrypt.conf;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
location = /.well-known/carddav {
return 301 $scheme://$host/remote.php/dav;
}
location = /.well-known/caldav {
return 301 $scheme://$host/remote.php/dav;
}
# set max upload size
client_max_body_size 512M;
fastcgi_buffers 64 4K;
# Enable gzip but do not remove ETag headers
gzip on;
gzip_vary on;
gzip_comp_level 4;
gzip_min_length 256;
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
location / {
rewrite ^ /index.php$uri;
}
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS on;
#Avoid sending the security headers twice
fastcgi_param modHeadersAvailable true;
fastcgi_param front_controller_active true;
fastcgi_pass unix:///var/run/php/php7.0-fpm-{{ name }}.sock;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ ^/(?:updater|ocs-provider)(?:$|/) {
try_files $uri/ =404;
index index.php;
}
# Adding the cache control header for js and css files
# Make sure it is BELOW the PHP block
location ~ \.(?:css|js|woff|svg|gif)$ {
try_files $uri /index.php$uri$is_args$args;
add_header Cache-Control "public, max-age=15778463";
# Add headers to serve security related headers (It is intended to
# have those duplicated to the ones above)
# Before enabling Strict-Transport-Security headers please read into
# this topic first.
# add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
#
# WARNING: Only add the preload option once you read about
# the consequences in https://hstspreload.org/. This option
# will add the domain to a hardcoded list that is shipped
# in all major browsers and getting removed from this list
# could take several months.
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
# Optional: Don't log access to assets
access_log off;
}
location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
try_files $uri /index.php$uri$is_args$args;
# Optional: Don't log access to other assets
access_log off;
}
}

View file

@ -0,0 +1,18 @@
[{{ name }}]
user = {{ name }}
group = {{ name }}
listen = /run/php/php7.0-fpm-{{ name }}.sock
listen.owner = {{ name }}
listen.group = {{ name }}
pm = ondemand
pm.max_children = 100
pm.process_idle_timeout = 10s
pm.status_path = /fpm_status
request_terminate_timeout = 60s
chdir = /home/{{ name }}/nextcloud
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /home/{{ name }}/tmp
env[TMPDIR] = /home/{{ name }}/tmp
env[TEMP] = /home/{{ name }}/tmp