傳統安裝
准備
安裝fastDFS需要分別安裝fastdfs-nginx-module、fastdfs、nginx、libfastcommon。
本文所用版本:
官方的github下載地址:https://github.com/happyfish100/fastdfs/releases
官網:https://sourceforge.net/projects/fastdfs/files/
1、安裝gcc(編譯時需要)
yum install -y gcc gcc-c++
2、安裝libevent(運行時需要)
yum -y install libevent
3、安裝創建目錄上傳所有文件
mkdir -p /fileservice/fast
cd /fileservice/fast
安裝libfastcommon
tar -zxvf libfastcommon-1.0.35.tar.gz #解壓
[root@localhost fast]# cd libfastcommon-1.0.35/
#編譯並運行
[root@localhost libfastcommon-1.0.35]# ./make.sh
[root@localhost libfastcommon-1.0.35]# ./make.sh install
安裝fastdfs
1、安裝相關依賴庫
yum install perl
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
2、安裝fastdfs
進入fast目錄:cd /fileservice/fast
解壓文件:tar -zxvf fastdfs-5.11.tar.gz
進入解壓后的目錄:cd fastdfs-5.11
執行編譯:./make.sh
安裝:./make.sh install
成功之后
3、查看tracker和storage的可執行腳本
ll /etc/init.d/ | grep fdfs #后面配置需要用到
4、准備配置文件,默認在/etc/fdfs/下面
cd /etc/fdfs/
先把配置文件名中的sample去了。[可以復制一份]
cp client.conf.sample client.conf
cp storage.conf.sample storage.conf
cp storage_ids.conf.sample storage_ids.conf
cp tracker.conf.sample tracker.conf
然后修改tracker的存放數據和日志的目錄
mkdir -p /home/coydone/fastdfs/tracker
配置和啟動tracker
#切換目錄到: /etc/fdfs/ 目錄下
cd /etc/fdfs/
#修改tracker.conf
vim tracker.conf
22行 base_path=/home/yuqing/fastdfs改為: base_path=/home/coydone/fastdfs/tracker
#啟動tracker
service fdfs_trackerd start
注意:在/home/coydone/fastdfs/tracker 目錄下生成兩個目錄, 一個是數據,一個是日志;
配置和啟動storage
由於上面已經安裝過FastDFS,這里只需要配置storage就好了。
#切換目錄到: /etc/fdfs/ 目錄下
cd /etc/fdfs/
#修改storage.conf
vim storage.conf
--------------------------------------
11行 group_name=group1 #配置組名
41行 base_path=/home/coydone/fastdfs/storage
#store存放文件的位置(store_path)
109行 store_path0=/home/yuqing/fastdfs改為: store_path0=/home/coydone/fastdfs/storage
#如果有多個掛載磁盤則定義多個store_path,如下
#store_path1=.....
#store_path2=......
#配置tracker服務器:IP
118行 tracker_server=192.168.81.130:22122
-------------------------------------------
#創建/home/coydone/fastdfs/storage 目錄
mkdir -p /home/coydone/fastdfs/storage
#啟動storage
service fdfs_storaged start
啟動完成后進入 /home/coydone/fastdfs/storage/data 目錄下,顯示目錄如下:
使用FastDFS自帶工具測試
#切換目錄到 /etc/fdfs/ 目錄下
cd /etc/fdfs/
#修改client.conf
vim client.conf
#修改基本路徑和tracker_server如下:
第10行 base_path=/home/coydone/fastdfs/storage
第14行 tracker_server=192.168.81.130:22122
注意:若tracker有多個,可以配置多個,如下:
#tracker_server=......
#tracker_server=......
拷貝一張圖片到Centos服務器上的/etc/fdfs
目錄下
進行測試:運行如下(運行測試程序,讀取/etc/fdfs/client.conf 文件,上傳/etc/fdfs/lolqinnv.jpg 文件)。
[root@localhost fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/lolqinnv.jpg
group1/M00/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg #輸出圖片路徑表示搭建成功
[root@localhost fdfs]# ls /home/coydone/fastdfs/storage/data/00/00/
wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg #發現在此目錄下存在該圖片
以上圖中的文件地址:http://192.168.81.130/group1/M00/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg
對應storage服務器上的/home/coydone/fastdfs/storage/data/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg
文件,由於現在還沒有和nginx整合無法使用http下載。
FastDFS 和nginx整合
在tracker上安裝 nginx
在每個tracker上安裝nginx的主要目的是做負載均衡及實現高可用。如果只有一台tracker可以不配置nginx。
一個tracker對應多個storage,通過nginx對storage負載均衡;
在storage上安裝nginx
上傳fastdfs-nginx-module-1.20.tar.gz 到Centos服務器上,解壓fastdfs-nginx-module-1.20.tar.gz 並移動到 /usr/local目錄下。
cd /fileservice/fast/ #切換目錄
tar -zxvf fastdfs-nginx-module-1.20.tar.gz #解壓
#切換目錄到: fastdfs-nginx-module-1.20/src 目錄下
[root@localhost fast]# cd fastdfs-nginx-module-1.20/src/
#修改config文件,將文件中的所有 /usr/local/ 路徑改為 /usr/
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon"
#將fastdfs-nginx-module/src下的mod_fastdfs.conf拷貝至/etc/fdfs/下
[root@localhost src]# cp mod_fastdfs.conf /etc/fdfs/
#修改 /etc/fdfs/mod_fastdfs.conf 的內容
vim /etc/fdfs/mod_fastdfs.conf
------------------------------------------
40行 tracker_server=192.168.81.130:22122 #多個tracker配置多行
53行 url_have_group_name=true #url中包含group名稱
62行 store_path0=/home/coydone/fastdfs/storage #指定文件存儲路徑(上面配置的store路徑)
------------------------------------------
#進入之前解壓的fastdfs目錄的配置目錄下(/fastdfs-5.11/conf),把http.conf、mime.conf配置文件移動至/etc/fdfs。
[root@localhost src]# cd /fileservice/fast/fastdfs-5.11/conf
[root@localhost conf]# cp http.conf mime.types /etc/fdfs/
Nginx的安裝
cd /fileservice/fast/
tar -zxvf nginx-1.15.2.tar.gz
[root@localhost fast]# cd nginx-1.15.2/
#加入模塊配置命令
[root@localhost nginx-1.15.2]# ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/fileservice/fast/fastdfs-nginx-module-1.20/src
#編譯並安裝
[root@localhost nginx-1.15.2]# make && make install
#修改nginx配置
cd /opt/nginx/conf
vim nginx.conf
server_name img.coydone.com;
location ~/group([0-9]) {
# root html;
# index index.html index.htm;
ngx_fastdfs_module;
}
#啟動nginx
cd /usr/bin/
[root@localhost bin]# ./nginx
ngx_http_fastdfs_set pid=117614 #看到有關fastdfs的輸出
#需要關閉Linux防火牆,就可以通過瀏覽器訪問了
#http://192.168.81.130/group1/M00/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg
使用Docker安裝
# 拉取鏡像並啟動
docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.149.128 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs registry.cn-beijing.aliyuncs.com/tianzuo/fastdfs
其中-v {HOME}/fastdfs:/var/local/fdfs是指:將{HOME}/fastdfs這個目錄掛載到容器里的/var/local/fdfs這個目錄里。
所以上傳的文件將被持久化到${HOME}/fastdfs/storage/data里,IP 后面是自己的服務器公網ip或者虛擬機ip,
-e WEB_PORT=80 指定nginx端口
#測試上傳
#進入容器
docker exec -it fastdfs /bin/bash
#創建文件
echo "Hello FastDFS!">index.html
#測試文件上傳
fdfs_test /etc/fdfs/client.conf upload index.html
#瀏覽器輸入 http://192.168.149.128/group1/M00/00/00/wKiVgF3dfV6ANGAyAAAADwL5vO455_big.html訪問
配置安全組