FastDFS是分布式高可用、自動負載均衡的文件系統(tǒng),特別適合小文件的在線服務,比如圖片服務等。本文介紹一下,如何快速部署FastDFS服務器
FastDFS系列
說明
說明 | 位置 |
---|---|
數(shù)據(jù)存儲目錄 | /home/dfs |
安裝包 | /usr/local/src |
安裝依賴
- 創(chuàng)建數(shù)據(jù)存儲目錄
mkdir /home/dfs
- 安裝依賴的第三方庫
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
- 安裝
libfastcommon
cd /usr/local/src
git clone https://github.com/happyfish100/libfastcommon.git --depth 1
cd libfastcommon/
./make.sh && ./make.sh install #編譯安裝
安裝FastDFS
cd /usr/local/src
git clone https://github.com/happyfish100/fastdfs.git --depth 1
cd fastdfs/
./make.sh && ./make.sh install #編譯安裝
#配置文件準備
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客戶端文件,測試用
cp /usr/local/src/fastdfs/conf/http.conf /etc/fdfs/ #供nginx訪問使用
cp /usr/local/src/fastdfs/conf/mime.types /etc/fdfs/ #供nginx訪問使用
單機部署
tracker配置
vim /etc/fdfs/tracker.conf
#需要修改的內(nèi)容如下
port=22122 # tracker服務器端口(默認22122,一般不修改)
base_path=/home/dfs # 存儲日志和數(shù)據(jù)的根目錄
storage配置
vim /etc/fdfs/storage.conf
#需要修改的內(nèi)容如下
port=23000 # storage服務端口(默認23000,一般不修改)
base_path=/home/dfs # 數(shù)據(jù)和日志文件存儲根目錄
store_path0=/home/dfs # 第一個存儲目錄
tracker_server=192.168.58.101:22122 # tracker服務器IP和端口
http.server_port=8888 # http訪問文件的端口(默認8888,看情況修改,和nginx中保持一致)
服務啟動
- 關(guān)閉防火墻
#不關(guān)閉防火墻的話無法使用
systemctl stop firewalld.service #關(guān)閉
systemctl restart firewalld.service #重啟
- 啟動tracker
/etc/init.d/fdfs_trackerd start #啟動tracker服務
chkconfig fdfs_trackerd on #自啟動tracker服務
- 啟動storage
/etc/init.d/fdfs_storaged start #啟動storage服務
chkconfig fdfs_storaged on #自啟動storage服務
Client測試
vim /etc/fdfs/client.conf
#需要修改的內(nèi)容如下
base_path=/home/dfs
tracker_server=192.168.58.101:22122 #tracker服務器IP和端口
#保存后測試,返回ID表示成功 如:group1/M00/00/00/xx.tar.gz
fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/nginx-1.15.4.tar.gz
Nginx直接訪問
FastDFS支持Nginx直接訪問,不需要經(jīng)過Client中轉(zhuǎn),效率更高
安裝fastdfs-nginx-module
cd ../ #返回上一級目錄
git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
安裝nginx
wget http://nginx.org/download/nginx-1.15.4.tar.gz #下載nginx壓縮包
tar -zxvf nginx-1.15.4.tar.gz #解壓
cd nginx-1.15.4/
#添加fastdfs-nginx-module模塊
./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/
make && make install #編譯安裝
/etc/fdfs/mod_fastdfs.conf
tracker_server=192.168.58.101:22122 #tracker服務器IP和端口
url_have_group_name=true
store_path0=/home/dfs
/usr/local/nginx/conf/nginx.conf
server {
listen 8888; ## 該端口為storage.conf中的http.server_port相同
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
- 瀏覽器訪問
http://192.168.58.101:8888/group1/M00/00/00/wKgAQ1pysxmAaqhAAA76tz-dVgg.tar.gz
分布式部署
- 增加部署
按照單機部署模式在192.168.58.201
上部署FastDFS(數(shù)據(jù)目錄/home/dfs
,tracker.conf
配置)
- 修改
/etc/fdfs/storage.conf
port=23000 # storage服務端口(默認23000,一般不修改)
base_path=/home/dfs # 數(shù)據(jù)和日志文件存儲根目錄
store_path0=/home/dfs # 第一個存儲目錄
tracker_server=192.168.58.101:22122 # 服務器1
tracker_server=192.168.58.201:22122 # 服務器2
http.server_port=8888 # http訪問文件的端口(默認8888,看情況修改,和nginx中保持一致)
- 修改
/etc/fdfs/client.conf
base_path=/home/dfs
tracker_server=192.168.58.101:22122 # 服務器1
tracker_server=192.168.58.201:22122 # 服務器2
- 修改
/etc/fdfs/mod_fastdfs.conf
tracker_server=192.168.58.101:22122 # 服務器1
tracker_server=192.168.58.201:22122 # 服務器2
url_have_group_name=true
store_path0=/home/dfs
重要信息
- 日志目錄:
/home/dfs/logs
如果遇到問題可以查看日志/home/dfs/logs
常見問題
-
tracker_query_storage fail, error no: 2, error info: No such file or directory
:檢查配置文件/etc/fdfs/tracker.conf
,storage.conf
,client.conf
是否正確。還可以查看日志/home/dfs/logs/