ubuntu 20 部署Seafile 8.0.3個人網盤過程記錄


寫在前面

這只是一個基於官方手冊的流水帳式記錄,目的不是提供一個攻略,並且也不保證其他人能成功部署。

個人一點不負責的總結&感想:Seafile這個網盤解決方案,可以分為兩個部分,seafile和seahub,網盤的各種功能實現在前者,后者應該僅僅用作提供一個web界面。seafile的后端是用C語言寫的,所以效率很好(看網上風評應該是碾壓nextcloud);而seafile暴露出來的網絡交互接口應該是用Python3寫的,seahub的web服務器應該也是用的Python3,不然為啥這么多地方需要Python。

大致步驟如下:

  • 准備環境
  • 下載Seafile服務端
  • 安裝python3的各種包
  • 安裝MySQL(mariadb)
  • 安裝Seafile
  • 啟動Seafile
  • 配置Nginx反向代理
  • 設置上傳下載文件的URL
  • 啟用HTTPS(可選)

看上去很復雜,其實每一步都挺容易的。

准備環境

我用的是Ubuntu 20,不過看官方手冊應該別的版本或別的發行版也可以。准備好服務器資源后,看能否ssh它,后續全部在服務器的操作都是用遠程shell操作。

下載Seafile服務端

先ssh服務器,然后身份切為root

sudo -i

本地瀏覽器打開Seafile下載頁面,找到想要的版本的下載鏈接,這里以8.0.3為例子。復制下載鏈接,回到ssh,新建Seafile目錄,然后下載到這里

mkdir /opt/seafile
cd /opt/seafile
wget https://seafile-downloads.oss-cn-shanghai.aliyuncs.com/seafile-server_8.0.3_x86-64.tar.gz

然后解壓,並保留原始壓縮包(暫時不是很清楚保留這個壓縮包有什么用,但是手冊上是這么寫的,那就照做吧)

tar -xzf seafile-server_8.0.3_x86-64.tar.gz
mkdir installed
mv seafile-server_8.0.3_x86-64.tar.gz installed/

現在目錄結構應該差不多長這樣

root@VM-8-3-ubuntu:/opt/seafile# tree -L 2
.
├── installed
│   └── seafile-server_8.0.3_x86-64.tar.gz
└── seafile-server-8.0.3
    ├── check_init_admin.py
    ├── reset-admin.sh
    ├── runtime
    ├── seaf-fsck.sh
    ├── seaf-fuse.sh
    ├── seaf-gc.sh
    ├── seafile
    ├── seafile.sh
    ├── seahub
    ├── seahub.sh
    ├── setup-seafile-mysql.py
    ├── setup-seafile-mysql.sh
    ├── setup-seafile.sh
    ├── sql
    └── upgrade

7 directories, 11 files

安裝python3的各種包

不是python專業用戶,所以不是很懂每個包是拿來干嘛的,問題不大。

這里把手冊里需要的包分開兩部分安裝,因為第一部分可以直接install成功,第二部分的包需要系統先安裝memcached(一個高速緩存解決方案,估計是用來加速seafile的處理效率的 )和sqlclient。那么先安裝第一部分

pip3 install Pillow captcha jinja2 sqlalchemy psd-tools django-simple-captcha future

OK,應該沒有任何報錯就搞掂了第一部分。接下來是第二部分,首先給系統安裝memcached和mysqlclient,然后再安裝第二部分Python包

apt update
apt install memcached
apt install libmemcached-dev
apt install libmysqlclient-dev

pip3 install pylibmc django-pylibmc mysqlclient

這樣以來,應該所有的Python3包就安裝完畢,進入下一步。

安裝MySQL(mariadb)

數據庫應該是用來保存用戶信息之類的吧,因為Seafile也是有用戶帳號系統的。首先一句話安裝MySQL

apt install mariadb-server

MariaDB會帶下來一個secure installation腳本,執行它

mysql_secure_installation

跟着提示走就行了,我這里全部回答都保持了默認的回答,輕輕松松配置完畢。注意設置的Root password需要牢記。

安裝Seafile

官方已經有一個安裝腳本了,只需運行它即可。腳本會提示你輸入一些預設參數(比如你的服務器ip等),按照喜歡的輸入就好。這里保持了seafile fileserver端口為默認的8082。

cd seafile-server-8.0.3/
./setup-seafile-mysql.sh

一頓操作之后,可以看到腳本在正式執行安裝前會把你的設置展示一遍,給你一個反悔的機會(下面我把域名給碼了)。

---------------------------------
This is your configuration
---------------------------------

    server name:            seafile
    server ip/domain:       mydomain.com

    seafile data dir:       /opt/seafile/seafile-data
    fileserver port:        8082

    database:               create new
    ccnet database:         ccnet-db
    seafile database:       seafile-db
    seahub database:        seahub-db
    database user:          seafile



---------------------------------
Press ENTER to continue, or Ctrl-C to abort
---------------------------------

回車之后,馬上就可以看見安裝成功提示:

-----------------------------------------------------------------
Your seafile server configuration has been finished successfully.
-----------------------------------------------------------------

run seafile server:     ./seafile.sh { start | stop | restart }
run seahub  server:     ./seahub.sh  { start <port> | stop | restart <port> }

-----------------------------------------------------------------
If you are behind a firewall, remember to allow input/output of these tcp ports:
-----------------------------------------------------------------

port of seafile fileserver:   8082
port of seahub:               8000

When problems occur, Refer to

        https://download.seafile.com/published/seafile-manual/home.md

for information.

現在的目錄組織應該長這樣

root@VM-8-3-ubuntu:/opt/seafile# tree -L 2
.
├── ccnet
├── conf
│   ├── ccnet.conf
│   ├── gunicorn.conf.py
│   ├── seafdav.conf
│   ├── seafile.conf
│   └── seahub_settings.py
├── installed
│   └── seafile-server_8.0.3_x86-64.tar.gz
├── seafile-data
│   └── library-template
├── seafile-server-8.0.3
│   ├── check_init_admin.py
│   ├── reset-admin.sh
│   ├── runtime
│   ├── seaf-fsck.sh
│   ├── seaf-fuse.sh
│   ├── seaf-gc.sh
│   ├── seafile
│   ├── seafile.sh
│   ├── seahub
│   ├── seahub.sh
│   ├── setup-seafile-mysql.py
│   ├── setup-seafile-mysql.sh
│   ├── setup-seafile.sh
│   ├── sql
│   └── upgrade
├── seafile-server-latest -> seafile-server-8.0.3
└── seahub-data
    └── avatars

14 directories, 16 files

啟動Seafile

seafile-server-latest是一個軟鏈接,指向了最新版seafile目錄,這個軟鏈接應該是為了后續升級時方便處理用的,先不糾結,反正這里只有一個剛剛安裝的8.0.3版本。

cd到這個目錄,先啟動seafile。

root@VM-8-3-ubuntu:/opt/seafile/seafile-server-latest# ./seafile.sh start

Starting seafile server, please wait ...
** Message: 19:02:49.055: seafile-controller.c(535): No seafevents.

Seafile server started

Done.

啟動成功!然后再啟動seahub,前面說過這是一個有用戶帳號系統的網盤,既然如此當然要有一個管理員,第一次啟動seahub需要設置管理員帳號密碼(下面我碼掉了我的個人Email地址)。

root@VM-8-3-ubuntu:/opt/seafile/seafile-server-latest# ./seahub.sh start

LC_ALL is not set in ENV, set to en_US.UTF-8
Starting seahub at port 8000 ...

----------------------------------------
It's the first time you start the seafile server. Now let's create the admin account
----------------------------------------

What is the email for the admin account?
[ admin email ] myemail@example.com

What is the password for the admin account?
[ admin password ] 

Enter the password again:
[ admin password again ] 



----------------------------------------
Successfully created seafile admin
----------------------------------------




Seahub is started

Done.

注意,seahub是監聽在localhost:8000上的,所以不能本地瀏覽器直接輸入mydomain.com:8000訪問,所以要繼續下一步。

配置Nginx反向代理

這一步開始,就需要用到本地瀏覽器了。先把我自己之前踩過的坑寫在最前面:修改nginx配置后(比如改端口、http換https等),有時本地瀏覽器訪問網站卻看不出修改后的效果,這是因為本地瀏覽器有緩存,發生這個問題時可以嘗試清除瀏覽器本地緩存。

回到主題,首先安裝Nginx。

apt install nginx

然后cd到 /etc/nginx/sites-available/ 這個目錄下,這個目錄保存的是網站的配置文件。在這個目錄下新建一個文本文件seafile.conf,內容如下

server {
    listen 80;
    server_name mydomain.com;
    proxy_set_header X-Forwarded-For $remote_addr;

    location / {
         proxy_pass         http://127.0.0.1:8000;
         proxy_set_header   Host $host;
         proxy_set_header   X-Real-IP $remote_addr;
         proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header   X-Forwarded-Host $server_name;
         proxy_read_timeout  1200s;

         # used for view/edit office file via Office Online Server
         client_max_body_size 0;

         access_log      /var/log/nginx/seahub.access.log;
         error_log       /var/log/nginx/seahub.error.log;
    }

# If you are using [FastCGI](http://en.wikipedia.org/wiki/FastCGI),
# which is not recommended, you should use the following config for location `/`.
#
#    location / {
#         fastcgi_pass    127.0.0.1:8000;
#         fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
#         fastcgi_param   PATH_INFO           $fastcgi_script_name;
#
#         fastcgi_param     SERVER_PROTOCOL     $server_protocol;
#         fastcgi_param   QUERY_STRING        $query_string;
#         fastcgi_param   REQUEST_METHOD      $request_method;
#         fastcgi_param   CONTENT_TYPE        $content_type;
#         fastcgi_param   CONTENT_LENGTH      $content_length;
#         fastcgi_param     SERVER_ADDR         $server_addr;
#         fastcgi_param     SERVER_PORT         $server_port;
#         fastcgi_param     SERVER_NAME         $server_name;
#         fastcgi_param   REMOTE_ADDR         $remote_addr;
#         fastcgi_read_timeout 36000;
#
#         client_max_body_size 0;
#
#         access_log      /var/log/nginx/seahub.access.log;
#         error_log       /var/log/nginx/seahub.error.log;
#    }

    location /seafhttp {
        rewrite ^/seafhttp(.*)$ $1 break;
        proxy_pass http://127.0.0.1:8082;
        client_max_body_size 0;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_connect_timeout  36000s;
        proxy_read_timeout  36000s;
        proxy_send_timeout  36000s;
        send_timeout  36000s;
    }

    location /media {
        root /opt/seafile/seafile-server-latest/seahub;
    }

}

然后cd到 ../sites-enabled/ 目錄,刪除原有的default,再創建軟鏈接,鏈接到剛剛那個文件上

root@VM-8-3-ubuntu:/etc/nginx/sites-enabled# ls
default
root@VM-8-3-ubuntu:/etc/nginx/sites-enabled# rm default 
root@VM-8-3-ubuntu:/etc/nginx/sites-enabled# ln -s ../sites-available/seafile.conf seafile.conf
root@VM-8-3-ubuntu:/etc/nginx/sites-enabled# ls
seafile.conf

這里要注意一下,上面的nginx配置了監聽80端口,這個端口一般防火牆都會默認通過的。如果監聽了別的端口,

然后,重啟nginx以使得更新生效

systemctl restart nginx

此時,本地瀏覽器輸入 mydomain.com,就能看到登陸界面了。

登陸界面

輸入剛剛配置的seahub管理員帳號密碼,即可登陸,看到主頁。

主頁

設置上傳下載文件的URL

點右上角頭像,點“系統管理”,左邊一欄去到“設置”。把SERVICE_URL改為http://mydomain.com/,FILE_SERVER_ROOT改為http://mydomain.com/seafhttp。如果你的nginx不監聽80端口就把端口號也加上。

修改完之后,點右上角頭像,“退出系統管理”,即可回到主頁。

至此,一個網盤已經搭建好。面向用戶的客戶端網盤用法應該參閱官方客戶端手冊(其實不用參閱也行,界面還是很清晰的,稍微點一點琢磨一下就知道用法了)。

啟用HTTPS(可選)

事實上到剛剛那一步已經可以用網盤了,但是安全性不夠,有被中間人監聽的風險。因此想折騰啟用HTTPS的話可以進行這一步。

申請SSL證書的過程就不寫了,當然也可以用自頒發證書,不過這樣子的話,就算自己電腦添加了信任,在別的電腦上打開網站就會被瀏覽器提示證書有問題。

這里假定服務器上已經有證書文件了(如果證書文件在本地,可以scp上去),我的證書保存在 /etc/ssl/1_mydomain.com_bundle.crt,私鑰存在 /etc/ssl/2_mydomain.com.key

修改nginx的配置文件 /etc/nginx/sites-available/seafile.conf,改為如下,注意這里監聽的是8888端口,而不是443端口,因為很久以前我監聽了443,然后過了幾天一打開網站就被提醒說沒有備案。之后換了一個非標准端口,就沒問題了。記得配置防火牆允許8888端口流量進入。

server {
    listen 80;
    server_name mydomain.com;
    rewrite ^ https://$http_host:8888$request_uri? permanent;    # force redirect http to https
}

server {
    listen 8888 ssl;
    server_name mydomain.com;
    ssl_certificate /etc/ssl/1_mydomain.com_bundle.crt;
    ssl_certificate_key /etc/ssl/2_mydomain.com.key;
    ssl_session_timeout 5m;
    ssl_session_cache shared:SSL:5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-CAMELLIA256-SHA:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-SEED-SHA:DHE-RSA-CAMELLIA128-SHA:HIGH:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS';
    ssl_prefer_server_ciphers on;

    proxy_set_header X-Forwarded-For $remote_addr;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

    location / {
         proxy_pass         http://127.0.0.1:8000;
         proxy_set_header   Host $host:8888;
         proxy_set_header   X-Real-IP $remote_addr;
         proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header   X-Forwarded-Host $server_name;
	 proxy_set_header   X-Forwarded-Proto https;
         proxy_read_timeout  1200s;

         # used for view/edit office file via Office Online Server
         client_max_body_size 0;

         access_log      /var/log/nginx/seahub.access.log;
         error_log       /var/log/nginx/seahub.error.log;
    }

# If you are using [FastCGI](http://en.wikipedia.org/wiki/FastCGI),
# which is not recommended, you should use the following config for location `/`.
#
#    location / {
#         fastcgi_pass    127.0.0.1:8000;
#         fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
#         fastcgi_param   PATH_INFO           $fastcgi_script_name;
#
#         fastcgi_param     SERVER_PROTOCOL     $server_protocol;
#         fastcgi_param   QUERY_STRING        $query_string;
#         fastcgi_param   REQUEST_METHOD      $request_method;
#         fastcgi_param   CONTENT_TYPE        $content_type;
#         fastcgi_param   CONTENT_LENGTH      $content_length;
#         fastcgi_param     SERVER_ADDR         $server_addr;
#         fastcgi_param     SERVER_PORT         $server_port;
#         fastcgi_param     SERVER_NAME         $server_name;
#         fastcgi_param   REMOTE_ADDR         $remote_addr;
#         fastcgi_read_timeout 36000;
#
#         client_max_body_size 0;
#
#         access_log      /var/log/nginx/seahub.access.log;
#         error_log       /var/log/nginx/seahub.error.log;
#    }

    location /seafhttp {
        rewrite ^/seafhttp(.*)$ $1 break;
        proxy_pass http://127.0.0.1:8082;
        client_max_body_size 0;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_connect_timeout  36000s;
        proxy_read_timeout  36000s;
        proxy_send_timeout  36000s;
        send_timeout  36000s;
    }

    location /media {
        root /opt/seafile/seafile-server-latest/seahub;
    }

}

然后重啟nginx即可

systemctl restart nginx

由於配置了重定向,瀏覽器輸入 mydomain.com 或 https://mydomain.com:8888 都可以正常使用。

最后,記得按照之前說的步驟,去系統管理里 ,改一下SERVICE_URL和FILE_SERVER_ROOT。

至此,網盤搭建完畢。后續還有一些可選操作(比如配置開機自啟動),以后再補充吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM