1. 程式人生 > 其它 >樹莓派利用Docker實現NextCloud

樹莓派利用Docker實現NextCloud

如何初始化和設定樹莓派,請參考我另外一篇博文,好像還沒寫,哈哈。

言歸正傳,我們來說一下,如何在樹莓派利用Docker實現NextCloud,個人感覺使用docker實現,比直接安裝方便太多,前提是你要稍微熟悉下Docker和編排。

1、Docker規劃

首先確認需要使用適用樹莓派的nextcloud的fpm封裝,不使用apache版本,降低資源消耗,方便配置https。對nextcloud的卷的規劃,需要適應後期版本更新的需求。根據需要完成下面的容器規劃。

所有的卷都規劃到home目錄下的nextcloud目錄下,方便管理。除了nextcloud所需要的data目錄,這個是單獨掛載了一個硬碟作為儲存。

2、編寫編排檔案

根據容器的規劃,完成編排檔案的編寫。

docker-compose.yml

version: '2'

volumes:
  nextcloud:
  db:

services:
  db:
    image: jsurf/rpi-mariadb
    restart: always
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root@
123 - MYSQL_PASSWORD=nextcloud@123 - MYSQL_DATABASE=nextcloud - MYSQL_USER=nextcloud app: image: arm32v7/nextcloud:fpm privileged: true links: - db environment: - MYSQL_HOST=db - REDIS_HOST=redis - MYSQL_PASSWORD=nextcloud@123 - MYSQL_DATABASE=nextcloud - MYSQL_USER=nextcloud volumes: - ./nextcloud/html:/var/www/html - ./nextcloud/custom_apps:/var/www/html/custom_apps - ./nextcloud/config:/var/www/html/config - /data:/var/www/html/data restart: always web: image: nginx restart: always ports: -
80:80 - 443:443 environment: TZ : 'Asia/Shanghai' links: - app volumes_from: - app volumes: - ./nextcloud/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d - ./nginx/sslcerts:/etc/nginx/sslcerts redis: image: redis volumes_from: - app restart: always

3、安裝Docker

curl -fsSL get.docker.com -o get-docker.shsudo sh get-docker.sh --mirror Aliyun

可以把當前使用者加入到 docker 組,以便這個使用者能方便的使用 docker 命令

sudo usermod -aG docker 當前使用者

docker-compose 是基於 python 的一個 docker 編排工具

apt-get install docker-compose

4、安裝Portainer

Portainer是一個視覺化的容器映象的圖形管理工具,方便後期對容器的啟停和管理,該軟體提供了很便利的日誌查詢、容器內操作及容器網路管理。

#下載 Docker 圖形化介面 portainer
sudo docker pull portainer/portainer
#建立 portainer 容器
sudo docker volume create portainer_data
#執行 portainer
sudo docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

5、準備volumes目錄

根據前期的規劃,為nextcloud掛載外部儲存,這個請參考其他的文件了。對於配置檔案和其他的東西,都在home目錄下建立好主目錄即可,比如我這裡,就在/home/pi/下建立了nextcloud目錄,並將編排檔案放到了該目錄下。

6、準備nginx相應配置檔案和ssl證書

需要根據規劃,對nginx的配置檔案,及證書進行準備。ssl證書可以到阿里雲去申請免費證書。

nginx/conf.d/default.conf

upstream php-handler {
    server app:9000;    # app為docker容器的名稱,9000為nextcloud fpm的埠
} server { listen
80; listen [::]:80; server_name xxx; # 修改為自己的域名 # enforce https return 301 https://$server_name:443$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name xxx; # 修改為自己的域名 # Use Mozilla's guidelines for SSL/TLS settings # https://mozilla.github.io/server-side-tls/ssl-config-generator/ # NOTE: some settings below might be redundant ssl_certificate /etc/nginx/sslcerts/xxx.pem; # 替換為自己證書檔名 ssl_certificate_key /etc/nginx/sslcerts/xxx.key; # 替換為自己證書檔名 # Add headers to serve security related headers # 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; add_header Referrer-Policy no-referrer; add_header Strict-Transport-Security 15552000; add_header X-Frame-Options SAMEORIGIN; # Remove X-Powered-By, which is an information leak fastcgi_hide_header X-Powered-By; # Path to the root of your installation root /var/www/html; # 此處需要對應nextcloud的檔案目錄 # The following 2 rules are only needed for the user_webfinger app. # Uncomment it if you're planning to use this app. #rewrite ^/.well-known/host-meta /public.php?service=host-meta last; #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last; # The following rule is only needed for the Social app. # Uncomment it if you're planning to use this app. #rewrite ^/.well-known/webfinger /public.php?service=webfinger last; location = /.well-known/carddav { return 301 $scheme://$host:$server_port/remote.php/dav; } location = /.well-known/caldav { return 301 $scheme://$host:$server_port/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; # Uncomment if your server is build with the ngx_pagespeed module # This module is currently not supported. #pagespeed off; location / { rewrite ^ /index.php$request_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\/.+|oc[ms]-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; # Enable pretty urls fastcgi_param front_controller_active true; fastcgi_pass php-handler; fastcgi_intercept_errors on; fastcgi_request_buffering off; } location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) { try_files $uri/ =404; index index.php; } # Adding the cache control header for js, css and map files # Make sure it is BELOW the PHP block location ~ \.(?:css|js|woff2?|svg|gif|map)$ { try_files $uri /index.php$request_uri; 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; add_header Referrer-Policy no-referrer; # Optional: Don't log access to assets access_log off; } location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ { try_files $uri /index.php$request_uri; # Optional: Don't log access to other assets access_log off; } }

7、執行 Docker Compose

一切準備好後,執行編排檔案,會將docker下載好,並建立好卷和網路。必須在docker-compose.yml 檔案所在目錄。

docker-compose up -d

8、配置NextCloud

一切完成後,進入Portainer,檢視容器執行情況,一切正常,就可以通過你配置的地址開啟頁面了。直接進入到nextcloud的配置介面,建立一個使用者名稱密碼,就可以登入了。

9、更新NextCloud

如果nextcloud版本有更新,可以刪除現有NextClolud Docker映象進行更新

docker stop arm32v7/nextcloud:fpm
docker rm   arm32v7/nextcloud:fpm
rm     -rvf /home/pi/nextcloud/nextcloud/html