静看光阴荏苒
不管不顾不问不说也不念

docker-compose安装nextcloud(包含备份/迁移步骤)

前两天用snap装了个nextcloud后来发现实在是太封闭了,几乎什么配置都不能自己改,然后就只是开关了一下ssl就boom了。难受的一批,亏我还传了50多G的“学习资料”上去。。

最后决定还是用docker吧,看了下dockerhub上的配置没符合自己需求的,就自己花了一点时间撸了一个docker-compose,可以说我只是尽量在把这篇文章里面的东西给容器化:

在Debian9上搭建一个完美的Nextcloud私有云

首先安装docker:

apt -y update
apt -y install curl
curl -sSL https://get.docker.com/ | sh
systemctl start docker
systemctl enable docker

安装docker-compose:

curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

接着新建一个docker-compose的项目目录以及conf.d目录(conf.d用于存放nginx配置文件):

mkdir -p /opt/nextcloud/conf.d

新建nginx配置文件:

nano /opt/nextcloud/conf.d/nextcloud.conf

直接粘贴下面的内容,什么都不用改动:

upstream php-handler {
    server app:9000;
}

server {
    listen       80;
    listen       443 ssl http2;
    server_name  ${FQDN};
    # WEB页面允许最大的文件上传大小
    client_max_body_size 20G;
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }

    # SSL以及TLS1.3支持
    ssl_certificate    /etc/letsencrypt/live/${FQDN}/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/${FQDN}/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    # 添加头信息修复Nextcloud后台的警告信息
    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-Frame-Options "SAMEORIGIN" always;
    add_header X-Permitted-Cross-Domain-Policies none;
    add_header Referrer-Policy no-referrer;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    fastcgi_hide_header X-Powered-By;

    root /var/www/html;

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

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

    # 启用Gzip压缩
    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$request_uri;
    }

    location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
        deny all;
    }
    location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }

    # PHP配置
    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 php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^\/(?:updater|ocs-provider)(?:$|\/) {
        try_files $uri/ =404;
        index index.php;
    }

    location ~ \.(?:css|js|woff2?|svg|gif)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463";
        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;
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
        try_files $uri /index.php$request_uri;
        # Optional: Don't log access to other assets
        access_log off;
    }
}

接着新建docker-compose的配置文件:

nano /opt/nextcloud/docker-compose.yml

粘贴下面的内容:

version: '3'

volumes:
    letsencrypt:
    nextcloud:

services: 
    proxy:
        image: staticfloat/nginx-certbot
        ports:
            - 80:80
            - 443:443
        environment: 
            CERTBOT_EMAIL: example@qq.com
            ENVSUBST_VARS: FQDN
            FQDN: nextcloud.233.fi
        volumes:
            - ./conf.d:/etc/nginx/user.conf.d:ro
            - letsencrypt:/etc/letsencrypt
            - nextcloud:/var/www/html
        restart: unless-stopped

    db:
        image: mariadb
        environment:
            - MYSQL_ROOT_PASSWORD=
            - MYSQL_PASSWORD=
            - MYSQL_DATABASE=nextcloud
            - MYSQL_USER=nextcloud
        volumes: 
            - ./mysql:/var/lib/mysql
        restart: unless-stopped
    
    app:
        image: nextcloud:fpm
        volumes:
            - nextcloud:/var/www/html
            - ./app/data:/var/www/html/data
            - ./app/config:/var/www/html/config
        restart: unless-stopped

    cron:
        image: rcdailey/nextcloud-cronjob
        environment:
            - NEXTCLOUD_CRON_MINUTE_INTERVAL=15
            - NEXTCLOUD_CONTAINER_NAME=app
            - NEXTCLOUD_PROJECT_NAME=nextcloud
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock:ro
            - /etc/localtime:/etc/localtime:ro
        restart: always

在上面这个配置文件内你需要改动的地方有以下几点:

1.CERTBOT_EMAIL设置你的邮箱地址

2.FQDN: nextcloud.233.fi换成你自己的域名(提前解析到你的VPS或是服务器上)

3.MYSQL_ROOT_PASSWORD=设置你的数据库root密码

4.MYSQL_PASSWORD=设置你的数据库用户密码(nextcloud这个用户的密码)

确认无误后up起来即可:

cd /opt/nextcloud
docker-compose up -d

打开你的域名在安装界面这块设置你的管理员账号密码,数据库选择MySQL,注意数据库主机这里要把localhost改为db:

安装完成之后,修复后台概览中的安全及设置警告:

docker-compose exec -u www-data app php occ db:add-missing-indices
docker-compose exec -u www-data app php occ db:convert-filecache-bigint

从本地添加文件到nextcloud内,添加文件的路径(imlala替换成你自己的用户名):

/opt/nextcloud/app/data/imlala/files

之后执行下面的命令扫描即可:

docker-compose exec -u www-data app php occ files:scan --all

如果需要安装ffmpeg:

docker exec -it nextcloud_app_1 bash
apt -y update
apt -y install ffmpeg

其他的东西都同理,切到容器内安装就行,不过建议别到容器内瞎折腾,容易BOOM。

下面演示一下备份和迁移的步骤。为了备份/迁移更方便,这个docker-compose内我将mysql以及nextcloud的data都配置成了匿名卷的形式,这样后续备份的时候直接打包这个docker-compose的目录即可。

旧机器内停止运行:

cd /opt/nextcloud
docker-compose stop

然后打包:

cd /opt
tar -zcvf nextcloud.tar.gz nextcloud

将压缩包传到新机器上:

scp nextcloud.tar.gz root@serverip:/opt

新机器内安装好docker和docker-compose后解压压缩包

cd /opt
tar -xzvf nextcloud.tar.gz

进入到docker-compose目录直接up即可完成迁移:

cd nextcloud
docker-compose up -d
赞(1)
未经允许不得转载:荒岛 » docker-compose安装nextcloud(包含备份/迁移步骤)
分享到: 更多 (0)

评论 22

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; 个人习惯只保留tls 1.3,喜新厌旧系列…
    listen 443 ssl http2; 我一般不用h2,这玩意难说

    S72周前 (02-06) Google Chrome 79.0.3945.130 Google Chrome 79.0.3945.130 Windows 10 x64 Edition Windows 10 x64 Edition回复
  2. #2

    其实我一直在等你放资源

    Fcat2周前 (02-06) Google Chrome 79.0.3945.130 Google Chrome 79.0.3945.130 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • :razz:

      LALA2周前 (02-08) Google Chrome 80.0.3987.87 Google Chrome 80.0.3987.87 Windows 10 x64 Edition Windows 10 x64 Edition回复
  3. #3

    哈哈,明了易懂的说明….我之前安装的是ownCloud,貌似该开发者团队集体转会之后又搞出了nextCloud,记不清楚了,2333(有空我再换程序吧,那台机器上跑了图床,BT下载,音乐盒还有这个网盘程序,折腾折腾要吐血 :cry: :cry: )。容器真是个好东西。

    Summer2周前 (02-06) Google Chrome 79.0.3945.130 Google Chrome 79.0.3945.130 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • nextcloud得需要很高的机器配置,最好单独运行,不然文件多了会很卡 :neutral:

      LALA2周前 (02-08) Google Chrome 80.0.3987.87 Google Chrome 80.0.3987.87 Windows 10 x64 Edition Windows 10 x64 Edition回复
  4. #4

    翻过墙来看看LALA, 这几天好严.

    Never.2周前 (02-07) Google Chrome 79.0.3945.130 Google Chrome 79.0.3945.130 Windows 10 x64 Edition Windows 10 x64 Edition回复
  5. #5

    lala你现在在那里?

    paa2周前 (02-07) Google Chrome 79.0.3945.130 Google Chrome 79.0.3945.130 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 在你心里

      LALA2周前 (02-08) Google Chrome 80.0.3987.87 Google Chrome 80.0.3987.87 Windows 10 x64 Edition Windows 10 x64 Edition回复
  6. #6

    nextcloud怎么添加talk组件?

    Foliage1周前 (02-11) Chrome 80.0.3987.88 Chrome 80.0.3987.88 iPhone iOS 13.3 iPhone iOS 13.3回复
    • 后台应用直接安装就可以了。

      LALA1周前 (02-12) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 没看到talk的插件,还有后台警告那里代码从哪里带入?

        Foliage7天前 Chrome 80.0.3987.88 Chrome 80.0.3987.88 iPhone iOS 13.3 iPhone iOS 13.3回复
        • 后台-应用-Social & communication里面有Talk插件。docker-compose exec -u www-data 这些?直接在SSH终端执行

          LALA7天前 Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
          • 还不是在它说跟第二个编辑目录有关,直接输入代码说找不到目录

            Foliage7天前 Google Chrome 79.0.3945.136 Google Chrome 79.0.3945.136 Android 8.1.0 Android 8.1.0
          • docker-compose exec -u www-data app php occ db:add-missing-indices
            docker-compose exec -u www-data app php occ db:convert-filecache-bigint
            这两行代码在哪输入?

            Foliage6天前 Google Chrome 79.0.3945.136 Google Chrome 79.0.3945.136 Android 8.1.0 Android 8.1.0
          • 你的docker-compose.yml在哪个目录下就cd到对应的目录下执行。或者直接用docker exec这种形式也可以。

            LALA6天前 Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition
          • 有新版本怎么升级,浏览器打开后台更新器升级不了

            Foliage6天前 Google Chrome 79.0.3945.136 Google Chrome 79.0.3945.136 Android 8.1.0 Android 8.1.0
          • 在你的docker-compose.yml所在目录内依次执行下面的命令:
            docker-compose pull
            docker-compose up -d

            LALA6天前 Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition
      • 新机器装好nextcloud,打包的压缩包上传到哪个目录,怎么解压出来?大佬谢谢你的解答,再次麻烦你。

        Foliage6天前 Chrome 80.0.3987.88 Chrome 80.0.3987.88 iPhone iOS 13.3 iPhone iOS 13.3回复
        • 文章后面写的很清楚了。

          LALA5天前 Google Chrome 80.0.3987.87 Google Chrome 80.0.3987.87 Windows 10 x64 Edition Windows 10 x64 Edition回复
          • cd nextcloud
            docker-compose up -d
            我解压完直接输了这两条命令后,网盘打不开了 :cry:

            Foliage5天前 Google Chrome 80.0.3987.106 Google Chrome 80.0.3987.106 Windows 10 x64 Edition Windows 10 x64 Edition
  7. #7

    安装完了,发现PHP最大上传大小2M…. :eek:

    sukka7天前 Google Chrome 80.0.3987.100 Google Chrome 80.0.3987.100 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 这个不影响文件上传的,你用web或者客户端传个大文件试试就知道了。

      LALA7天前 Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复

分享创造快乐

广告合作资源投稿