一、前言
之前一直採用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_certificate
和ssl_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.