# 部署 Seafile 服務器(使用 MySQL)
此文檔用來說明通過預編譯好的安裝包,來安裝并運行基于 MySQL 的 Seafile服務器.
### 下載
到[下載頁面](http://www.seafile.com/download)下載最新的服務器安裝包.
### 部署和目錄設計
假設你公司的名稱為 **haiwen**,你也已經下載 seafile-server_1.4.0_* 到你的**home** 目錄下。 我們建議這樣的目錄結構:
~~~
mkdir haiwen
mv seafile-server_* haiwen
cd haiwen
#將 seafile-server_* 移動到 haiwen 目錄下后
tar -xzf seafile-server_*
mkdir installed
mv seafile-server_* installed
~~~
現在,你的目錄看起來應該像這樣:
~~~
#tree haiwen -L 2
haiwen
├── installed
│ └── seafile-server_1.8.2_x86-64.tar.gz
└── seafile-server-1.8.2
├── reset-admin.sh
├── runtime
├── seafile
├── seafile.sh
├── seahub
├── seahub.sh
├── setup-seafile.sh
└── upgrade
~~~
**這樣設計目錄的好處在于**
- 和 seafile 相關的配置文件都可以放在 **haiwen** 目錄下,便于集中管理.
- 后續升級時,你只需要解壓最新的安裝包到 **haiwen** 目錄下.
*這樣你可以重用 **haiwen** 目錄下已經存在的配置文件,而不用重新配置*.
### 準備 MySQL 數據庫
Seafile 服務器有三個組件需要擁有自己的數據庫:
- ccnet server
- seafile server
- seahub
Seafile 服務器組件更多信息請看[[Seafile服務器組件概覽]].
有兩種方法可以初始化數據庫:
1. 通過`setup-seafile-mysql.sh`腳本創建數據庫.
1. 自己或其他人(比如數據庫管理員)創建
我們建議采用第一種方法. 腳本會要求你提供 MySQL數據庫的的根密碼,之后創建:
- ccnet/seafile/seahub 數據庫
- 一個可以連接到這些數據庫的用戶
然而,有時你不得不使用第二種方法.如果你沒有根密碼,你需要通過擁有這種權限的人(比如數據庫管理員)來幫助你創建三個數據庫,和一個能連接這三個數據庫的用戶.例如,為 ccnet/seafile/seahub 分別創建如下三個數據庫: `ccnet-db` /`seafile-db` / `seahub-db`, 和一個可以連接這三個數據庫的 MySQL用戶`seafile`:
~~~
create database `ccnet-db` character set = 'utf8';
create database `seafile-db` character set = 'utf8';
create database `seahub-db` character set = 'utf8';
create user 'seafile'@'localhost' identified by 'seafile';
GRANT ALL PRIVILEGES ON `ccnet-db`.* to `seafile`;
GRANT ALL PRIVILEGES ON `seafile-db`.* to `seafile`;
GRANT ALL PRIVILEGES ON `seahub-db`.* to `seafile`;
~~~
### 安裝 Seafile 服務器
### 安裝前的準備工作
安裝 Seafile 服務器之前,請確認已安裝以下軟件
- python 2.7
- python-setuptools
- python-imaging
- python-mysqldb
~~~
#在Debian/Ubuntu系統下
apt-get update
apt-get install python2.7 python-setuptools python-imaging python-mysqldb
~~~
### 安裝
~~~
cd seafile-server-*
./setup-seafile-mysql.sh #運行安裝腳本并回答預設問題
~~~
如果你的系統中沒有安裝上面的某個軟件,那么 Seafile初始化腳本會提醒你安裝相應的軟件包.

該腳本會依次詢問你一些問題,從而一步步引導你配置 Seafile 的各項參數
<table class="calibre18"><tbody class="calibre19"><tr class="calibre20"><th class="calibre21">參數</th><th class="calibre21">作用</th><th class="calibre21">說明</th></tr></tbody><tbody class="calibre19"><tr class="calibre20"><td class="calibre22"><p class="calibre6">seafile server name</p></td><td class="calibre22"><p class="calibre6">seafile 服務器的名字,將來在客戶端會顯示為這個名字</p></td><td class="calibre22"><p class="calibre6">3 ~ 15 個字符,可以用英文字母,數字,下劃線</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">seafile server ip or domain</p></td><td class="calibre22"><p class="calibre6">seafile 服務器的 IP 地址或者域名</p></td><td class="calibre22"><p class="calibre6">客戶端將通過這個 IP 或者地址來訪問你的 Seafile 服務</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">ccnet server port</p></td><td class="calibre22"><p class="calibre6">ccnet 使用的 TCP 端口</p></td><td class="calibre22"><p class="calibre6">一般使用默認的10001 端口,如果已經被占用,可以設置為其他的端口</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">seafile data dir</p></td><td class="calibre22"><p class="calibre6">seafile 數據存放的目錄,用上面的例子,默認將是 /data/haiwen/seafile-data</p></td><td class="calibre22"><p class="calibre6">seafile 數據將隨著使用而逐漸增加,請把它放在一個有足夠大空閑空間的分區上</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">seafile server port</p></td><td class="calibre22"><p class="calibre6">seafile 服務器 使用的 TCP 端口</p></td><td class="calibre22"><p class="calibre6">一般使用默認的 12001 端口,如果已經被占用,可以設置為其他的端口</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">seafile fileserver port</p></td><td class="calibre22"><p class="calibre6">seafile fileserver 使用的 TCP 端口</p></td><td class="calibre22"><p class="calibre6">一般使用默認的 8082 端口,如果已經被占用,可以設置為其他的端口</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">seahub admin email</p></td><td class="calibre22"><p class="calibre6">sehaub管理員的登錄帳戶名</p></td><td class="calibre22"><p class="calibre6">使用一個 email 地址</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">seahub admin password</p></td><td class="calibre22"><p class="calibre6">seahub 管理員的密碼</p></td><td class="calibre22"/></tr><tr class="calibre20"/></tbody></table>
在這里, 你會被要求選擇一種創建 Seafile 數據庫的方式:
~~~
-------------------------------------------------------
Please choose a way to initialize seafile databases:
-------------------------------------------------------
[1] Create new ccnet/seafile/seahub databases
[2] Use existing ccnet/seafile/seahub databases
~~~
具體選擇哪項, 取決于你是否擁有根密碼.
- 如果選擇`1`, 你需要提供根密碼. 腳本程序會創建數據庫和用戶。
- 如果選擇`2`, ccnet/seafile/seahub數據庫應該已經被你(或者其他人)提前創建。
如果選擇 `[1] Create new ccnet/seafile/seahub databases`,你會被問三個問題:

<table class="calibre18"><tbody class="calibre19"><tr class="calibre20"><th class="calibre21">Question</th><th class="calibre21">Description</th><th class="calibre21">Note</th></tr></tbody><tbody class="calibre19"><tr class="calibre20"><td class="calibre22"><p class="calibre6">mysql server host</p></td><td class="calibre22"><p class="calibre6">the host address of the mysql server</p></td><td class="calibre22"><p class="calibre6">the default is localhost</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">mysql server port</p></td><td class="calibre22"><p class="calibre6">the port of the mysql server</p></td><td class="calibre22"><p class="calibre6">the default is 3306. Almost every mysql server uses this port.</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">root password</p></td><td class="calibre22"><p class="calibre6">the password of mysql root account</p></td><td class="calibre22"><p class="calibre6">the root password is required to create new databases and a new user</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">mysql user for seafile</p></td><td class="calibre22"><p class="calibre6">the username for seafile programs to use to access MySQL server</p></td><td class="calibre22"><p class="calibre6">if the user does not exist, it would be created</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">password for seafile mysql user</p></td><td class="calibre22"><p class="calibre6">the password for the user above</p></td><td class="calibre22"/></tr><tr class="even"><td class="calibre22"><p class="calibre6">ccnet dabase name</p></td><td class="calibre22"><p class="calibre6">the name of the database used by ccnet, default is "ccnet-db"</p></td><td class="calibre22"><p class="calibre6">the database would be created if not existing</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">seafile dabase name</p></td><td class="calibre22"><p class="calibre6">the name of the database used by seafile, default is "seafile-db"</p></td><td class="calibre22"><p class="calibre6">the database would be created if not existing</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">seahub dabase name</p></td><td class="calibre22"><p class="calibre6">the name of the database used by seahub, default is "seahub-db"</p></td><td class="calibre22"><p class="calibre6">the database would be created if not existing</p></td></tr></tbody></table>
如果你選擇`[2] Use existing ccnet/seafile/seahub databases`,你會被問到如下三個問題:

<table class="calibre18"><tbody class="calibre19"><tr class="calibre20"><th class="calibre21">Question</th><th class="calibre21">Description</th><th class="calibre21">Note</th></tr></tbody><tbody class="calibre19"><tr class="calibre20"><td class="calibre22"><p class="calibre6">mysql server host</p></td><td class="calibre22"><p class="calibre6">the host address of the mysql server</p></td><td class="calibre22"><p class="calibre6">the default is localhost</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">mysql server port</p></td><td class="calibre22"><p class="calibre6">the port of the mysql server</p></td><td class="calibre22"><p class="calibre6">the default is 3306. Almost every mysql server uses this port</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">mysql user for seafile</p></td><td class="calibre22"><p class="calibre6">the user for seafile programs to use to access MySQL server</p></td><td class="calibre22"><p class="calibre6">the user must already exists</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">password for seafile mysql user</p></td><td class="calibre22"><p class="calibre6">the password for the user above</p></td><td class="calibre22"/></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">ccnet dabase name</p></td><td class="calibre22"><p class="calibre6">the name of the database used by ccnet</p></td><td class="calibre22"><p class="calibre6">this database must already exist</p></td></tr><tr class="even"><td class="calibre22"><p class="calibre6">seafile dabase name</p></td><td class="calibre22"><p class="calibre6">the name of the database used by seafile, default is "seafile-db"</p></td><td class="calibre22"><p class="calibre6">this database must already exist</p></td></tr><tr class="calibre20"><td class="calibre22"><p class="calibre6">seahub dabase name</p></td><td class="calibre22"><p class="calibre6">the name of the database used by seahub, default is "seahub-db"</p></td><td class="calibre22"><p class="calibre6">this database must already exist</p></td></tr></tbody></table>
如果安裝正確完成,你會看到下面這樣的輸出

現在你的目錄結構看起來應該是這樣:
~~~
#tree haiwen -L 2
haiwen
├── ccnet # configuration files
│ ├── ccnet.conf
│ ├── mykey.peer
│ ├── PeerMgr
│ └── seafile.ini
├── installed
│ └── seafile-server_1.8.2_x86-64.tar.gz
├── seafile-data
│ └── seafile.conf
├── seafile-server-1.8.2 # active version
│ ├── reset-admin.sh
│ ├── runtime
│ ├── seafile
│ ├── seafile.sh
│ ├── seahub
│ ├── seahub.sh
│ ├── setup-seafile.sh
│ └── upgrade
├── seafile-server-latest # symbolic link to seafile-server-1.8.2
├── seahub-data
│ └── avatars
├── seahub_settings.py # optional config file
└── seahub_settings.pyc
~~~
`seafile-server-latest`文件夾為指向當前 Seafile 服務器文件夾的符號鏈接.將來你升級到新版本后, 升級腳本會自動更新使其始終指向最新的 Seafile服務器文件夾..
### 啟動 Seafile 服務器
### 啟動之前
因為 Seafile 在客戶端和服務器之間使用持續連接,如果你的客戶端**數量巨大**, 你應該在啟動 Seafile 之前修改你的 Linux 文件最大打開數,如下:
~~~
ulimit -n 30000
~~~
### 啟動 Seafile 服務器和 Seahub 網站
在 seafile-server-1.8.2 目錄下,運行如下命令
- 啟動 Seafile:
~~~
./seafile.sh start # 啟動 Seafile 服務
~~~
- 啟動 Seahub
~~~
./seahub.sh start <port> # 啟動 Seahub 網站 (默認運行在8000端口上)
~~~
**小貼士:** 你第一次啟動 seahub 時,`seahub.sh` 腳本會提示你創建一個 seafile 管理員帳號。
服務啟動后, 打開瀏覽器并輸入以下地址
~~~
http://192.168.1.111:8000/
~~~
你會被重定向到登陸頁面. 輸入你在安裝 Seafile 時提供的用戶名和密碼后,你會進入 Myhome 頁面,新建資料庫.
**恭喜!** 現在你已經成功的安裝了 Seafile 服務器.
#### 在另一端口上運行 Seahub
如果你不想在默認的 8000 端口上運行 Seahub, 而是想自定義端口(比如8001)中運行,請按以下步驟操作:
- 關閉 Seafile 服務器
~~~
./seahub.sh stop # 停止 Seafile 進程
./seafile.sh stop # 停止 Seahub
~~~
- 更改`haiwen/ccnet/ccnet.conf`文件中`SERVICE_URL` 的值(假設你的 ip或者域名時`192.168.1.100`), 如下:
~~~
SERVICE_URL = http://192.168.1.100:8001
~~~
- 重啟 Seafile 服務器
~~~
./seafile.sh start # 啟動 Seafile 服務
./seahub.sh start 8001 # 啟動 Seahub 網站 (運行在8001端口上)
~~~
`ccnet.conf`更多細節請看(server_configuration.md) .
### 關閉/重啟 Seafile 和 Seahub
#### 關閉
~~~
./seahub.sh stop # 停止 Seahub
./seafile.sh stop # 停止 Seafile 進程
~~~
#### 重啟
~~~
./seafile.sh restart # 停止當前的 Seafile 進程,然后重啟 Seafile
./seahub.sh restart # 停止當前的 Seahub 進程,并在 8000 端口重新啟動 Seahub
~~~
#### 如果停止/重啟的腳本運行失敗
大多數情況下 seafile.sh seahub.sh 腳本可以正常工作。如果遇到問題:
- 使用**pgrep**命令檢查 seafile/seahub 進程是否還在運行中
~~~
pgrep -f seafile-controller # 查看 Seafile 進程
pgrep -f "manage.py run_gunicorn" # 查看 Seahub 進程
~~~
- 使用**pkill**命令殺掉相關進程
~~~
pkill -f seafile-controller # 結束 Seafile 進程
pkill -f "manage.py run_gunicorn" # 結束 Seafile 進程
~~~
### OK!
查看seafile更多信息請移至..
- [Nginx 下配置 Seahub](#) / [Apache 下配置 Seahub](#)
- [Nginx 下啟用 Https](#) / [Apache 下啟用 Https](#)
- [Seafile LDAP配置](#)
- [管理員手冊](#)
- 介紹
- 概覽
- Seafile 組件
- 研發路線圖
- 常見問題解答
- 修改日志
- 我要參與
- Linux 下部署 Seafile 服務器
- 部署 Seafile 服務器(使用 SQLite)
- 部署 Seafile 服務器(使用 MySQL)
- Nginx 下配置 Seahub
- Nginx 下啟用 Https
- Apache 下配置 Seahub
- Apache 下啟用 Https
- Seafile LDAP 配置
- 開機啟動 Seafile
- 防火墻設置
- Logrotate 管理系統日志
- 使用 Memcached
- 使用 NAT
- 非根域名下部署 Seahub
- 從 SQLite 遷移至 MySQL
- 安裝常見問題
- 升級
- Windows 下部署 Seafile 服務器
- 下載安裝 Windows 版 Seafile 服務器
- 安裝 Seafile 為 Windows 服務
- 所用端口說明
- 升級
- 從 Windows 遷移到 Linux
- 垃圾回收
- 部署 Seafile 專業版服務器
- 下載安裝 Seafile 專業版服務器
- 從社區版遷移至專業版
- 升級
- Amazon S3 下安裝
- OpenStackSwift 下安裝
- Ceph 下安裝
- 配置選項
- 文件搜索說明
- 集群部署
- 集群中啟用搜索和后臺服務
- NFS 下集群安裝
- 常見問題解答
- 軟件許可協議
- 服務器個性化配置
- ccnet.conf
- seafile.conf
- seahub_settings.py
- 發送郵件提醒
- 個性化郵件提醒
- 用戶管理
- 存儲容量與文件上傳/下載大小限制
- 自定義 Web
- 管理員手冊
- 賬戶管理
- 日志
- 備份與恢復
- Seafile FSCK
- Seafile GC
- WebDAV 和 FUSE 擴展
- WebDAV 擴展
- FUSE 擴展
- 安全選項
- 安全特性
- 日志和審計
- 開發文檔
- 編譯 Seafile
- Linux
- Windows
- Max OS X
- Server
- 開發環境
- 編程規范
- Web API
- Python API
- 數據模型
- 服務器組件
- 同步算法