上一篇文章使用NextCloud快速搭建你自己的工作中心(上)有一些工作沒有介紹完,這篇文章進行了補充。並且修改了docker-compose文件,重新打包了docker鏡像等優化操作,使得部署起來更方便些。
首先要明確的是,到底有沒有使用網盤的需求,如果你經常共享文件,或者需要備份自己的資料,或者你有多個終端,或者你需要移動辦公。。。等等
如果你確實有這樣的需求,或者你正在使用市面上的網盤,自己再喜歡動動手,或者需要保存一些小秘密,那麼繼續往下看。
選型網盤又分為備份盤和同步盤。顧名思義,備份盤主要是用來存儲文件,比如百度網盤(除工作空間功能)、阿里雲盤、微雲、天翼雲盤、115網盤等等,都算是備份盤,或者大家主要使用的功能都是備份盤的功能。而同步盤則主要有堅果雲、Onedrive等(國外的了解的不多),其中百度網盤、天翼雲盤、微雲,也都有同步盤的功能。
我之前一直用的是Onedrive,期間嘗試過上述的各種盤,但是效果均不盡人意,算是各有各的缺點吧。我的場景主要是用來同步全盤的數據,這樣做有個好處,就是如果我重做系統,或者換設備了,不用來回倒數據,而且同步盤一般也都支持文件版本的功能,寫word文檔或者畫圖的時候,如果忘記保存副本就是個很難辦的事情,總不能所有的內容都用git管理,也不方便,這個時候同步盤的優勢就出來了。這樣的場景也導致了,我這裡會有大量的小文件和部分大文件,小文件的數量可能有幾十萬(包含著一些開源項目的源碼)。所以我這個場景對同步盤的選擇就很苛刻。
下面列一下我用過這些盤之後的感受。
堅果云:同步功能很強大,速度也快,但是容量太小,花錢買也不多。
百度網盤、微雲、天翼雲盤,客戶端的校驗速度和穩定性均不如Onedrive,動不動就有衝突。
Onedrive本身功能沒啥毛病,但是網絡情況不穩定,就算這個不穩定我也忍了,但是教育版又經常和學校的管理有關,總是抽風,網頁端都打不開,這已經第二次了,每次都要持續幾天,這次失效截止目前也快一周了,還沒有修復好,想著索性自己搭建一個服務。
個人云盤搭建也有多種選擇Nextcloud、seafile等等,還有filerun、可道雲等,在這裡的選型主要選商業化不是特別嚴重的,也就是開源版本功能沒被閹割太多的,而且性能還算過得去的。
實際大家過程中搭建了Nextcloud和seafile,最終是選擇了Nextcloud,雖然nextcloud的性能有些問題,但是可以對性能優化一些,而Seafile的功能感覺被閹割不少,而且文件管理還得通過客戶端,與文件管理器集成的不到位。後來發現Nextcloud的虛擬文件支持一用就崩潰,不過考慮Nextcloud的商店裡插件很多,功能可擴展,就還是選這個了。
好了,廢話說了一堆,下面開始說正式的搭建過程。
環境說明與準備工作環境本地一臺臺式機做Nextcloud服務端
公網伺服器用來進行公網訪問
路由器用來進行DNS劫持
軟體準備安裝Docker和Docker-compose的教程很多,比如https://www.runoob.com/docker/ubuntu-docker-install.html。這裡不展開說docker安裝過程,之所以選擇docker安裝,是可以保證不在環境上出現過多的差異,而且後續如果要遷移數據也比較方便。
Nextcloud本地搭建(Nextcloud+Redis+Mysql)Nextcloud的單獨搭建其實很簡單,直接docker run nextcloud就行了(誇張一下,實際還得設置數據路徑和埠映射),但是並不推薦這麼執行,實際運行的性能比較差,我們這裡直接使用redis和mysql搭建。
不過得益於Docker-compose,可以直接使用我的這個docker-compose.yml,其中nextcloud使用了自定義的鏡像,集成了離線下載的aria2和系統定時工具cron。
(在下面同樣附上了Dockerfile文件,可以根據需求進行自定義,如果自己懶得build鏡像,直接使用這個鏡像即可。)
Dockerfile:
FROM nextcloud:fpm
RUN apt update && apt install -y aria2 libmagickcore-6.q16-6-extra cron&& apt clean
RUN rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/www/aria2
WORKDIR /var/www/aria2
RUN touch aria2.session
RUN { \
echo 'dir=/Downloads'; \
echo 'enable-rpc=true'; \
echo 'rpc-allow-origin-all=true'; \
echo 'rpc-listen-all=true'; \
echo 'continue=true'; \
echo 'input-file=/var/www/aria2/aria2.session'; \
echo 'save-session=/var/www/aria2/aria2.session'; \
echo 'max-concurrent-downloads=20'; \
echo 'save-session-interval=120'; \
echo 'connect-timeout=120'; \
echo 'max-connection-per-server=10'; \
echo 'min-split-size=10M'; \
echo 'split=10'; \
echo 'check-certificate=false'; \
} > aria2.conf; \
chown -R www-data:root /var/www/aria2; \
chmod -R g=u /var/www/aria2
VOLUME ["/var/www/aria2"]
RUN sed =i '194 i service cron start' /entrypoint.sh
RUN sed -i '194 i su -s /bin/bash -c "aria2c --conf-path=/var/www/aria2/aria2.conf -D" www-data' /entrypoint.sh
WORKDIR /var/www/html
ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm"]docker-compose.yml:
version: '3.9'
services:
db:
image: mysql:8.0
container_name: nextcloud-mysql
command: --skip-log-bin --innodb_buffer_pool_size=2048M --innodb_flush_method=O_DIRECT --innodb_write_io_threads=16
# 內存配置根據實際情況來
environment:
- MYSQL_DATABASE=nextcloud
- MYSQL_ROOT_PASSWORD=db_dev # 設置mysql的root用戶的密碼
- MYSQL_LOG_CONSOLE=true
volumes:
- type: volume # 必須。設置資料庫數據路徑的映射,數據持久化保存
source: db-data
target: /var/lib/mysql
networks:
- nextcloud-net
memcached:
image: redis:6-alpine
container_name: nextcloud-memcached
command: redis-server --requirepass cached_dev
volumes: # 設置redis數據路徑的映射,根據自己的配置看要不要映射出來吧,隨意。
- type: volume
source: cache-data
target: /data
networks:
- nextcloud-net
nextcloud:
image: registry.cn-beijing.aliyuncs.com/env_halfcoke/nextcloud
container_name: nextcloud-fpm
volumes:
- type: volume
source: nextcloud-data
target: /var/www/html # nextcloud 數據目錄,必須,數據持久化保存
- type: volume
source: nextcloud-aria2
target: /var/www/aria2 # nextcloud 數據目錄,建議
- type: volume
source: nextcloud-conf
target: /usr/local/etc/php-fpm.d # nextcloud使用的php-fpm 配置目錄,建議
environment:
- PHP_UPLOAD_LIMIT=16G
- PHP_MEMORY_LIMIT=4G # 內存配置根據實際情況來
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=root
- MYSQL_PASSWORD=db_dev
- MYSQL_HOST=db
- REDIS_HOST=memcached
- REDIS_HOST_PORT=6379
- REDIS_HOST_PASSWORD=cached_dev
depends_on:
- db
- memcached
networks:
- nextcloud-net
server:
image: nginx
container_name: nextcloud-server
ports:
- 443:443
volumes:
- type: volume
source: nextcloud-data
target: /var/www/html # nextcloud 數據目錄
- type: volume
source: nginx-conf
target: /etc/nginx/conf.d # nginx 配置
- type: volume
source: nginx-ssl
target: /etc/nginx/ssl_certs # nginx 證書
depends_on:
- nextcloud
networks:
- nextcloud-net
volumes:
db-data:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/data/db-data'
cache-data:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/data/cache-data'
nextcloud-data:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/data/nextcloud-data'
nextcloud-aria2:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/data/nextcloud-aria2'
nextcloud-conf:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/data/nextcloud-conf'
nginx-conf:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/data/server/conf'
nginx-ssl:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/data/server/ssl_certs'
networks:
nextcloud-net:執行過程如下:
# 執行
vim docker-compose.yml
# 將上面文件的docker-compose.yml內容複製進去,保存退出
# 使用docker compose啟動
docker-compose up -d
# 這次啟動是為了把相關數據目錄的路徑都建立出來
# 查看是否所有容器都正常啟動
docker ps接下來,需要對nginx進行配置
nginx配置文件路徑在:/data/server/conf,這個是在docker-compose文件中配置的,現在這個路徑應該是空的(是不是空的都無所謂),我們新建一個nextcloud.conf文件。
文件名只要是以conf結尾即可,這裡命名為nextcloud.conf
官方實際上是提供了nginx配置文件的(https://docs.nextcloud.com/server/latest/admin_manual/installation/nginx.html#nextcloud-in-the-webroot-of-nginx),但是裡面需要修改一些配置,注意中文注釋。
upstream php-handler {
server nextcloud:9000; # 這裡因為咱們是docker部署,需要更改為服務名
#server unix:/var/run/php/php7.4-fpm.sock;
}
server {
listen 80;
listen [::]:80;
server_name cloud.example.com; # 這裡根據自己實際情況修改主機名
# Enforce HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name cloud.example.com; # 這裡根據自己實際情況修改主機名
# Use Mozilla's guidelines for SSL/TLS settings
# https://mozilla.github.io/server-side-tls/ssl-config-generator/
ssl_certificate /etc/ssl/nginx/cloud.example.com.crt; # 這裡根據自己實際情況修改證書文件
ssl_certificate_key /etc/ssl/nginx/cloud.example.com.key;
# 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
client_max_body_size 16G; # 這裡根據自己實際情況修改大小,這個關係到能夠上傳的最大文件大小
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;
# 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;
# 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;
add_header Strict-Transport-Security 15552000; # 補充這條配置
# 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/nextcloud;
# 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 $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param HTTPS on;
fastcgi_param modHeadersAvailable true; # Avoid sending the security headers twice
fastcgi_param front_controller_active true; # Enable pretty urls
fastcgi_pass php-handler;
fastcgi_read_timeout 18000; # 補充這條配置
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ \.(?:css|js|svg|gif|png|jpg|ico)$ {
try_files $uri /index.php$request_uri;
expires 6M; # Cache-Control policy borrowed from `.htaccess`
access_log off; # Optional: Don't log access to assets
}
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;
}
}至此,訪問本機的https://localhost應該可以打開nextcloud了
可以進行一些配置,大體上如下圖所示
同樣可以通過手機端、電腦端對其進行訪問(接入同一個區域網)
Nextcloud公網訪問(可選)僅能區域網訪問很多時候不能滿足我們的需求,而且特別不方便,為此,可以使用公網伺服器進行轉發,這樣我們就可以通過公網伺服器訪問了。
在這裡我是直接轉發了本機的443埠,到公網伺服器的8002埠。
轉發埠用到的工具可以看我們的這篇文章《FRP埠轉發工具》,對Frp工具進行了介紹。
因為我在公網伺服器上部署了很多服務,因此在公網伺服器上也是通過nginx進行代理。
nginx配置文件如下:
server {
listen 80;
listen [::]:80;
server_name cloud.example.com; # 這裡根據自己實際情況修改主機名
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name cloud.example.com; # 這裡根據自己實際情況修改主機名
ssl_certificate /etc/ssl/nginx/cloud.example.com.crt; # 這裡根據自己實際情況修改證書文件
ssl_certificate_key /etc/ssl/nginx/cloud.example.com.crt; # 這裡根據自己實際情況修改證書文件
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
client_max_body_size 16G;
location ~* /.* { # 轉發所有請求
proxy_pass https://172.20.0.1:8002$request_uri; # 這裡這個ip是公網伺服器的宿主IP(因為我也部署了docker)
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}現在直接訪問公網的ip就可以直接訪問到自己的nextcloud了。
強烈建議註冊一個域名,通過域名對自己的服務進行訪問,這樣https顯示就正常了。
使用同一域名解析至公網或內網IP(可選)註:這裡主要是為了進行域名劫持,如果所用路由器有相關的功能,直接設置即可,可以跳過這一節。
接下來搞一個騷操作
回顧一下使用場景
我的客戶端電腦是筆記本,服務端電腦是臺式機,我是想同步我筆記上的所有的數據。臺式機是放那不動的,而筆記本我經常要拿走。
因為公網伺服器的帶寬很低,所以直接使用公網IP進行初始同步不太現實,速度太慢了。而我實際上是可以把筆記本和臺式機接在一個區域網裡的。
然而,我看了看自己的路由器,並沒有DNS的功能,最多我只能設置一個DNS伺服器。
但是,我的臺式機放在那就可以搭一個DNS服務,這樣我自己劫持一下DNS,在內網的時候直接把我的域名劫持到區域網IP就可以了,說搞就搞。
DNS服務搭建在ubuntu 18 上搭建DNS服務時,需要先關閉systemd-reslove服務,釋放53埠,在此之前,先執行docker pull sameersbn/bind把鏡像拉下來
sudo systemctl stop systemd-reslove
sudo systemctl disable systemd-reslove編輯/etc/systemd/resolved.conf文件
[Resolve]
DNS=127.0.0.1
#FallbackDNS=
#Domains=
LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
DNSStubListener=no重啟電腦
DNS服務搭建,也使用docker-compose部署了,寫個配置文件比較方便。配置文件如下。
version: '2.0'
services:
dns_server:
image: sameersbn/bind
container_name: dns_server
environment:
- WEBMIN_ENABLED=true
ports:
- "53:53/udp"
- "53:53/tcp"
- 10000:10000
volumes:
- /opt/dns_server:/data訪問https://localhost:10000
帳號root 密碼password
設置中文後打開bind dns server頁面
進行轉發和傳輸設置
進行訪問控制列表設置
在這裡創建新的主區域
這裡根據自己的實際情況去填,Email地址隨便寫
點擊新建以後,點擊地址
按照如下填寫即可
等待一段時間,把路由器的DNS伺服器設置為臺式機,然後連結到路由器的區域網裡,就可以使用host example.cn來查看地址,如果可以定位到臺式機,就沒問題了。
其他重要的補充工作使用DemoWeb頁面主頁面基本就是這個樣子,很簡潔,在右側可以進行一些設置。
在文件頁面是這個樣子,有文件版本記錄。
在本地刪除的文件,雲端也會有記錄,可以進行文件恢復
Windows客戶端Windows客戶端可以設置忽略同步的文件或文件夾,這個也是比較方便的地方,比如node_modules終於不需要同步了(之前onedrive不支持進行這樣的設置)。總體的使用也是比較方便易懂的。
同步文件夾也可以進行設置,我直接用之前Onedrive創建的文件夾了。
在文件管理器中看基本上是這個樣子。
nextcloud有虛擬文件支持,就是類似onedrive的文件隨選。但是我這邊一開啟就容易卡死,關閉後就一直正常。
性能優化按照上述的搭建過程後,已經進行了初步的性能優化,在啟動MySQL時,已經增加了相關參數來提高MySQL在當前使用環境下的性能。
PHP-fpm下面給出對Nextcloud使用的PHP-fpm的優化參數。官網提供了一個調優工具PHP-FPM Process Calculator的連結。
但是對於不熟悉PHP-FPM的人找這個配置文件時候還花了一番功夫。
在前文中,已經把相關配置文件暴露在了/data/nextcloud-conf路徑下
直接編輯路徑下的www.conf文件即可,找到pm.max_children、pm.start_servers、pm.min_spare_servers、pm.max_spare_servers這幾個參數,按照上面工具輸出的參數進行修改即可。
插件安裝插件安裝時,如果在線安裝則伺服器需要能訪問外網,或者可以通過離線安裝的方式進行。
在線安裝:點擊應用,然後選擇你需要安裝的插件,下載並啟用即可
離線安裝:訪問nextcloud官方插件商店https://apps.nextcloud.com/,選擇想要的插件,並下載指定版本。
將插件壓縮包,放入/data/nextcloud-data/custom_apps中解壓即可。
返回你自己的nextcloud,在應用頁面會發現放入的插件,啟用即可。
剛安裝好的話應該是不支持用戶註冊的,可以安裝一個註冊插件,方便朋友臨時用一下。
問題記錄安全設置警告使用我提供的docker鏡像和nginx配置文件,應該可以通過所有檢查。使用官方默認的docker鏡像,需要在docker內部執行如下命令,來消除幾個錯誤。
apt install -y libmagickcore-6.q16-6-extra #支持svg
apt install -y cron # 執行定時任務