使用docker-compose搭建Nextcloud个人云盘并开启https教程

一、前言

之前一直采用LANMP直接搭建服务,但后来使用docker后,搭建和迁移数据都很方便,所以将服务都容器化。

下面介绍一下使用docker-compose搭建Nextcloud,并且使用Nginx的反向代理开启https的方法。

直接搭建的文章可以参考我的这一篇博文:Centos 7.6搭建Nextcloud 17.0.0个人云盘详细教程

二、环境准备

基础环境的搭建这里不做细讲,默认环境已经安装或者拥有下面的服务和文件:

  • Nginx服务器
  • docker
  • docker-compose
  • 域名证书(Nginx适用)

docker的安装可以参考我的这一篇博文:Ubuntu使用Docker搭建编译环境完整教程

三、docker-compose配置

(一)docker-compose.yml

注意点:

  • 数据库挂载到当前./db文件夹
  • Nextcloud应用挂载到当前./app文件夹
  • 主机ports使用的是19080端口,可以自行修改,后面Nginx需要反向代理到这个端口
  • ./init_secrets文件夹每个文件里是初始化用户名、密码等参数,需要自行设置并创建
version: '3.2'

services:
  db:
    image: postgres:12
    container_name: nextcloud_db
    restart: always
    volumes:
      - ./db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB_FILE=/run/secrets/postgres_db
      - POSTGRES_USER_FILE=/run/secrets/postgres_user
      - POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
    secrets:
      - postgres_db
      - postgres_password
      - postgres_user

  app:
    image: nextcloud
    container_name: nextcloud
    restart: always
    ports:
      - 19080:80
    volumes:
      - ./app:/var/www/html
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_DB_FILE=/run/secrets/postgres_db
      - POSTGRES_USER_FILE=/run/secrets/postgres_user
      - POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
      - NEXTCLOUD_ADMIN_PASSWORD_FILE=/run/secrets/nextcloud_admin_password
      - NEXTCLOUD_ADMIN_USER_FILE=/run/secrets/nextcloud_admin_user
    depends_on:
      - db
    secrets:
      - nextcloud_admin_password
      - nextcloud_admin_user
      - postgres_db
      - postgres_password
      - postgres_user

secrets:
  nextcloud_admin_password:
    file: ./init_secrets/nextcloud_admin_password.txt # put admin password to this file
  nextcloud_admin_user:
    file: ./init_secrets/nextcloud_admin_user.txt # put admin username to this file
  postgres_db:
    file: ./init_secrets/postgres_db.txt # put postgresql db name to this file
  postgres_password:
    file: ./init_secrets/postgres_password.txt # put postgresql password to this file
  postgres_user:
    file: ./init_secrets/postgres_user.txt # put postgresql username to this file

(二)启动运行

docker-compose.yml所在文件夹运行下面的命令启动:

docker-compose up -d

可以使用下面的命令查看运行日志:

docker-compose logs -f

等日志显示Nexcloud安装完毕后,我们在浏览器里输入http://127.0.0.1:19080应该可以看到登陆界面,用户名密码就是./init_secrets你设置的用户名和密码。

但是我们一般使用域名访问,而且本文还开启了https,所以还需要配置Nginx反向代理。

四、Nginx配置

/etc/nginx/conf.d里创建Nextcloud.conf

注意点:

  • server_name换成你的域名
  • ssl_certificatessl_certificate_key换成你的域名证书
  • client_max_body_size设置最大上传文件大小
  • proxy_pass换成你的链接和端口号
server {
    listen 80;
    listen [::]:80;

    server_name yasin.store www.yasin.store;

    # redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    return 301 https://yasin.store$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name yasin.store;
  
  ssl on;
  ssl_certificate /yasin/ssl/nginx/yasin_store/yasin.store.crt;
  ssl_certificate_key /yasin/ssl/nginx/yasin_store/yasin.store.key;
  client_max_body_size 10G;
  
  add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

  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;
  }

  location / {
      proxy_redirect off;
      proxy_pass http://127.0.0.1:19080;
      proxy_set_header Host $http_host;
  }
  location = /.htaccess {
      return 404;
  }
}

重启Nginx使配置生效:

systemctl restart nginx

五、问题解决

按照上面的步骤一切正确的话,我们在浏览器中输入你的域名时,应该会自动跳转到https://你的域名。但是应该会出现下面的第一个错误:

(一)通过不被信任的域名访问

这是因为Nextcloud默认只能通过localhost访问,需要信任你的域名。

编辑下面的文件:

vim ./app/config/config.php

修改下面的内容,第二行记得换成你的域名:

'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'yasin.store',
  ),

(二)登陆后不自动跳转

修改完成后刷新页面,应该可以看见登陆界面了,用户名密码就是./init_secrets你设置的用户名和密码。

但是你会发现页面不会自动跳转,但是手动刷新后可以访问。

要想自动跳转,需要修改下面的文件:

vim ./app/config/config.php

增加下面的内容:

'overwriteprotocol' => 'https',

这时候你的云盘已经可以正常使用了,但是如果你是个强迫症的话,在设置里查看概览时应该还会遇到并想解决下面的两个错误。

如果你不在意的话,那么本文到这里就已经结束了。

(三)dav设置不正确

错误提示如下(为了减少操作,上面的Nginx配置文件里已添加该内容,所以你应该看不到这个错误了):

  • 您的网页服务器未正确设置以解析“/.well-known/caldav”。更多信息请参见文档。
  • 您的网页服务器未正确设置以解析“/.well-known/carddav”。更多信息请参见文档。

解决方法,nginx反向代理时添加下面的配置:

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;
} 

(四)数据库错误

错误1:

  • 数据库丢失了一些索引。由于给大的数据表添加索引会耗费一些时间,因此程序没有自动对其进行修复。您可以在 Nextcloud 运行时通过命令行手动执行 “occ db:add-missing-indices” 命令修复丢失的索引。索引修复后会大大提高相应表的查询速度。
    • 在数据表 “oc_calendarobjects_props” 中无法找到索引 “calendarobject_calid_index”。
    • 在数据表 “oc_schedulingobjects” 中无法找到索引 “schedulobj_principuri_index”。

解决方法:

进入容器:

docker exec -u www-data -it nextcloud bash

执行下面的命令:

/var/www/html/occ db:add-missing-indices

错误2:

  • 数据库中的一些列由于进行长整型转换而缺失。由于在较大的数据表重改变列类型会耗费一些时间,因此程序没有自动对其更改。您可以通过命令行手动执行 “occ db:convert-filecache-bigint” 命令以应用挂起的更改。该操作需要当整个实例变为离线状态后执行。查阅相关文档以获得更多详情。
    • mounts.storage_id
    • mounts.root_id
    • mounts.mount_id

解决方法:

进入容器:

docker exec -u www-data -it nextcloud bash

执行下面的命令:

/var/www/html/occ db:convert-filecache-bigint

选择y并且回车。

这时候再刷新网页,可以发现概览中的所有错误和警告都已经解决。

六、升级Nextcloud

当提示有新的版本可以升级时,可以使用下面的命令进行升级:

docker-compose down
docker-compose pull
docker-compose up -d

强烈建议升级前先备份你的数据。

七、结束

对比直接搭建,会发现使用docker会简单很多,而且比较方便数据的迁移。

需要换机器部署时,只需将当前文件夹内的所有文件打包,并且在新机器上配置好Nginx反向代理即可。

参考官方镜像的文档,甚至可以将Nginx也使用容器部署,这里就不再详细介绍了。

Enjoy it now.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章