如何安装Docker版Nextcloud-基础部署以及错误排查

2024年12月8日
829 次阅读
Exp1oit

前言

介绍如何在 Docker 环境中安装 Nextcloud,进行基础配置,并提供一些常见问题的错误排查策略。在接下来的章节中,我们将首先介绍安装的步骤,然后探讨可能遇到的常见问题及其解决方案,帮助您快速搭建和维护自己的 Nextcloud 实例。无论您是技术小白还是经验丰富的开发者,这里都有您需要的信息,以便您顺利完成 Nextcloud 的部署与运行。


TIPS

docker版本的nextcloud分为两个版本

  • 自带了Apache的docker版本
  • 只有FPM接口的docker版本

前者你只需要启动docker即可访问服务。除开你自己想要配置SSL等,而后者,只有FPM接口,需要你自己利用反向代理服务器代理接口,以及静态文件。相对来说,如果你是小白,无脑推荐第一种

FPM版本Nextcloud搭建

docker-compose.yml

version: '2'

services:
  db:
    image: mariadb
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD= 自己的密码
      - MYSQL_PASSWORD= 自己的密码
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
    networks:
      - nextcloud_network

  redis:
    image: redis:alpine
    command: redis-server --requirepass 自己的密码
    restart: always
    volumes:
      - redis:/data
    networks:
      - nextcloud_network

  app:
    image: nextcloud:fpm #如果想简单搭建,直接写latest 版本即可
    ports:
      - 9000:9000  # 将Docker的80端口,映射成主机的8888端口,可以自行修改
    links:
      - db
      - redis
    volumes:
      -  /var/www/nextcloud/:/var/www/html/ #此处有坑,这个静态文件目录必须和nginx反向代理用的是同一个目录,可以自己下载一个新的包
      - /data/mnt/186cloud/nextcloud:/186cloud/
    restart: always
    networks:
      - nextcloud_network
    environment:
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=自己的密码
      - MYSQL_HOST=db
      - REDIS_HOST=redis
      - REDIS_HOST_PASSWORD=自己的密码
    depends_on:
      - db
      - redis

networks:
  nextcloud_network:

volumes:
  db:
  redis:
  nextcloud:

Docker启动

docker-compose up -d

FPM版本的nginx反向代理配置

upstream php-handler {
    server 127.0.0.1:9000; #这个是docker-nextcloud的端口
        #server unix:/var/run/php/php8.1-fpm.sock;
}

# Set the `immutable` cache control options only for assets with a cache busting `v` argument
map $arg_v $asset_immutable {
    "" "";
    default "immutable";
}


server{

      listen *:443  ssl http2;
    server_name  nextcloud.exploit-db.xyz;
  ssl_stapling on;
  resolver 8.8.8.8 8.8.4.4 223.5.5.5 valid=300s;
  resolver_timeout 5s;
  ssl_certificate ssl证书路径;
  ssl_certificate_key sslkey路径;    
  ssl_protocols  TLSv1.1 TLSv1.2 TLSv1.3;
  root /var/www/nextcloud;
  index index.php;
  #index index.php index.html /index.php$request_uri;
        # Prevent nginx HTTP Server Detection
    server_tokens off;

    # HSTS settings
    # 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 Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always;

    # set max upload size and increase upload timeout:
    client_max_body_size 1024G;
    client_body_timeout 300s;
    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/wasm 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;

    # Pagespeed is not supported by Nextcloud, so if your server is built
    # with the `ngx_pagespeed` module, uncomment this line to disable it.
    #pagespeed off;

    # The settings allows you to optimize the HTTP2 bandwitdth.
    # See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/
    # for tunning hints
    client_body_buffer_size 512k;

    # HTTP response headers borrowed from Nextcloud `.htaccess`
    add_header Referrer-Policy                      "no-referrer"   always;
    add_header X-Content-Type-Options               "nosniff"       always;
    add_header X-Download-Options                   "noopen"        always;
    add_header X-Frame-Options                      "SAMEORIGIN"    always;
    add_header X-Permitted-Cross-Domain-Policies    "none"          always;
    add_header X-Robots-Tag                         "none"          always;
    add_header X-XSS-Protection                     "1; mode=block" always;

    # Remove X-Powered-By, which is an information leak
    fastcgi_hide_header X-Powered-By;

    # Specify how to handle directories -- specifying `/index.php$request_uri`
    # here as the fallback means that Nginx always exhibits the desired behaviour
    # when a client requests a path that corresponds to a directory that exists
    # on the server. In particular, if that directory contains an index.php file,
    # that file is correctly served; if it doesn't, then the request is passed to
    # the front-end controller. This consistent behaviour means that we don't need
    # to specify custom rules for certain paths (e.g. images and other assets,
    # `/updater`, `/ocm-provider`, `/ocs-provider`), and thus
    # `try_files $uri $uri/ /index.php$request_uri`
    # always provides the desired behaviour.
    index index.php index.html /index.php$request_uri;

    # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
    location = / {
        if ( $http_user_agent ~ ^DavClnt ) {
            return 302 /remote.php/webdav/$is_args$args;
        }
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }


    # Make a regex exception for `/.well-known` so that clients can still
    # access it despite the existence of the regex rule
    # `location ~ /(\.|autotest|...)` which would otherwise handle requests
    # for `/.well-known`.
    location ^~ /.well-known {
        # The rules in this block are an adaptation of the rules
        # in `.htaccess` that concern `/.well-known`.

        location = /.well-known/carddav { return 301 /remote.php/dav/; }
        location = /.well-known/caldav  { return 301 /remote.php/dav/; }

        location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
        location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

        # Let Nextcloud's API for `/.well-known` URIs handle all other
        # requests by passing them to the front-end controller.
        return 301 /index.php$request_uri;
    }

    # Rules borrowed from `.htaccess` to hide certain paths from clients
    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console)                { return 404; }

    # Ensure this block, which passes PHP files to the PHP process, is above the blocks
    # which handle static assets (as seen below). If this block is not declared first,
    # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
    # to the URI, resulting in a HTTP 500 error response.
    location ~ \.php(?:$|/) {
        # Required for legacy support
        rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;

        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        set $path_info $fastcgi_path_info;

        try_files $fastcgi_script_name =404;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; #此处为FPM的反向代理路径,如果你是直接安装,请修改这个地址,因为不加上这个的话,docker-FPM会找不到文件
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param HTTPS on;
        fastcgi_read_timeout 600;
        fastcgi_send_timeout 600;
        fastcgi_connect_timeout 600;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        send_timeout 600;
        fastcgi_param modHeadersAvailable true;         # Avoid sending the security headers twice
        fastcgi_param front_controller_active true;     # Enable pretty urls
        fastcgi_pass php-handler;

        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;

        fastcgi_max_temp_file_size 0;
    }

    location ~ \.(?:css|mjs|js|svg|gif|png|jpg|ico|wasm|tflite|map)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463, $asset_immutable";
        access_log off;     # Optional: Don't log access to assets

        location ~ \.wasm$ {
            default_type application/wasm;
        }
        location ~ \.mjs$ {
            default_type application/javascript;
        }
    }

    location ~ \.woff2?$ {
        try_files $uri /index.php$request_uri;
        expires 7d;         # Cache-Control policy borrowed from `.htaccess`
        access_log off;     # Optional: Don't log access to assets
    }

    # Rule borrowed from `.htaccess`
    location /remote {
        return 301 /remote.php$request_uri;
    }

    location / {
        try_files $uri $uri/ /index.php$request_uri;
    }
}

检查配置文件是否存在问题。

nginx -t

随后,重启nginx服务。

systemctl restart nginx.service

然后就可以打开你对应的域名,进行初始化配置了。

其他提醒

因为你是架设在docker中的,所以如果你想做BT下载的话,如果NCdownloader可能会出现无法链接或者被其他链接的情况,导致下载速度很慢,如果你想做BT下载,那么我还是建议假设到宿主机上.

AList和Nextcloud结合的骚操作

如果我们在小鸡上面假设的nextcloud,会遇到一个新的问题,那就是小鸡上面有个很少的硬盘,完全不好进行BT下载,非常影响观影体验,所以,我们可以将自己无用的云盘作为临时存储。优点就是境外的主机上下传速度比较快,缺点就是如果传输了某些视频,会被河蟹,这就看自己的取舍了。我是使用了天翼云盘,10TB的云盘,挂载到本地即可。亲测,最大上传速度为188MB/s,相当于一个普通机械盘。

Alist的安装部署

Alist-官网-点击跳转

安装完成后的设置项

我的做法是将已经建立好的AList上已经挂载好的云盘,映射为WebDav(AList自带),随后,将Webdav挂载到Nextcloud的小鸡目录上,随后,在nextcloud中挂在这个目录,你就可以看到一个新的目录了,他继承的就是你云盘的权限。此处注意,挂载上来了的文件,以及文件夹,需要修改为www-data权限,要不然Nextcloud无法写入。且建议直接挂载文件的根目录,否则可能会出现,你在nextcloud建立的文件夹,过一段时间被自动删除,暂时还不知道原因。

评论区

0 / 500
* 为必填项