本文源碼:GitHub·點這裏 || GitEE·點這裏linux
FastDFS是一個開源的輕量級分佈式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件上傳、文件下載等,解決了大容量存儲和負載均衡的問題。nginx
一、默認存在Gcc編譯環境,Centos7虛擬機 二、安裝LibFastCommon環境 三、FastDFS中間件安裝 四、Nginx代理服務器安裝
核心流程git
下載->解壓->編譯->安裝github
## 下載 [root@localhost mysoft]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.38.tar.gz ## 解壓 [root@localhost mysoft]# tar -zxvf V1.0.38.tar.gz [root@localhost mysoft]# cd libfastcommon-1.0.38/ ## 編譯 [root@localhost libfastcommon-1.0.38]# ./make.sh ## 安裝 [root@localhost libfastcommon-1.0.38]# ./make.sh install
流程:下載->解壓->編譯->安裝->建立相關路徑->配置跟蹤器->
配置數據存儲->配置客戶端->Nginx環境配置vim
## 下載 [root@localhost mysoft]# wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz ## 解壓 [root@localhost mysoft]# tar -zxvf V5.11.tar.gz ## 編譯 [root@localhost mysoft]# cd fastdfs-5.11/ [root@localhost fastdfs-5.11]# ./make.sh ## 安裝 [root@localhost fastdfs-5.11]# ./make.sh install
用處後續說明。服務器
[root@localhost data]# mkdir -p /data/fastdfs/log [root@localhost data]# mkdir -p /data/fastdfs/data [root@localhost data]# mkdir -p /data/fastdfs/tracker [root@localhost data]# mkdir -p /data/fastdfs/client
Tracker -- >> 跟蹤器app
1)查看配置文件負載均衡
注意這裏目錄的轉換,這裏給的是樣例,具體的配置還要本身動手。dom
[root@localhost fastdfs-5.11]# cd /etc/fdfs/ [root@localhost fdfs]# ll total 24 client.conf.sample storage.conf.sample storage_ids.conf.sample tracker.conf.sample
2)配置tracker.conf文件tcp
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf [root@localhost fdfs]# vim tracker.conf ## 關注以下幾個配置 ## 存儲數據和日誌文件的基本路徑 base_path=/data/fastdfs/tracker ## Http服務端口 http.server_port=80 ## 默認提供服務端口 port=22122
3)啓動跟蹤器
## 啓動 [root@localhost fdfs]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start ## 查看狀態 [root@localhost fdfs]# netstat -apn|grep fdfs
1)查看配置文件
[root@localhost fastdfs-5.11]# cd /etc/fdfs/ [root@localhost fdfs]# ll storage.conf.sample
2)配置storage.conf文件
[root@localhost fdfs]# cp storage.conf.sample storage.conf [root@localhost fdfs]# vim storage.conf ## 關注以下幾個配置 ## storage存儲data和log的跟路徑 base_path=/data/fastdfs/data ## 默認組名 group_name=group1 ## 默認端口,相同組的storage端口號必須一致 port=23000 ## 配置一個存儲路徑 store_path_count=1 store_path0=/data/fastdfs/data ## 配置跟蹤器IP和端口 tracker_server=192.168.72.130:22122
3)啓動存儲服務
## 啓動 [root@localhost fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start ## 查看進程 [root@localhost fdfs]# netstat -apn|grep fdfs tcp 0:22122 LISTEN 4845/fdfs_trackerd tcp 0:45422 SYN_SENT 5410/fdfs_storaged ## 查看啓動日誌 [root@localhost fdfs]# tail -f /data/fastdfs/data/logs/storaged.log ## 日誌展現:單臺FastDFS安裝成功 set tracker leader: 192.168.72.130:22122 ## 查看Storage和Tracker是否在通訊 [root@localhost fdfs]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf Storage 1: id = 192.168.72.130 ip_addr = 192.168.72.130 (localhost.localdomain) ACTIVE
1)查看配置文件
[root@localhost /]# cd /etc/fdfs [root@localhost fdfs]# ll total 40 client.conf.sample
2)配置client.conf文件
[root@localhost fdfs]# cp client.conf.sample client.conf [root@localhost fdfs]# vim client.conf ## 關注以下幾個配置 ## client數據和日誌目錄 base_path=/data/fastdfs/client ## 配置跟蹤器IP和端口 tracker_server=192.168.72.130:22122
3)客戶端測試
調用客戶端文件上傳命令
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf
返回文件上傳的相對路徑和編號
group1/M00/00/00/wKhIgl0mmE-ATEXPAAQ2pIoAy98392.jpg
[root@localhost fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /data/img/img1.jpg group1/M00/00/00/wKhIgl0mmE-ATEXPAAQ2pIoAy98392.jpg
這樣FastDFS單臺環境就安裝好了,步驟有點繁雜,不過這就是生活。
文件成功上傳storage服務器,可是還沒法查看下載。須要安裝Nginx服務器用來支持Http方式訪問文件。
## 下載nginx [root@localhost mysoft]# wget http://nginx.org/download/nginx-1.15.2.tar.gz ## 解壓nginx [root@localhost mysoft]# tar -zxvf nginx-1.15.2.tar.gz
## 下載fastdfs-nginx [root@localhost mysoft]#wget https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip ## 解壓fastdfs-nginx [root@localhost mysoft]# mv 5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip fast-nginx.zip [root@localhost mysoft]# unzip fast-nginx.zip [root@localhost mysoft]# mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1/ fastdfs-nginx-module
## pcre-devel 環境 [root@localhost nginx-1.15.2]# yum install -y pcre pcre-devel ## zlib-devel 環境 [root@localhost nginx-1.15.2]# yum install -y zlib zlib-devel ## openssl-devel 環境 [root@localhost nginx-1.15.2]# yum install -y openssl openssl-devel
[root@localhost nginx-1.15.2]# ./configure --add-module=/usr/local/mysoft/fastdfs-nginx-module/src [root@localhost nginx-1.15.2]# make && make install
版本問題致使,Fast-Nginx必須使用這個修復版本。
https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip
make[1]: *** [objs/addon/src/ngx_http_fastdfs_module.o] Error 1 make[1]: Leaving directory `/usr/local/mysoft/nginx-1.15.2' make: *** [build] Error 2
以下狀況則表示安裝成功了。
[root@localhost nginx-1.15.2]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.15.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) configure arguments: --add-module=/usr/local/mysoft/fastdfs-nginx-module/src
## 移動配置文件 [root@localhost src]# pwd /usr/local/mysoft/fastdfs-nginx-module/src [root@localhost src]# ll total 76 Apr 14 2017 mod_fastdfs.conf [root@localhost src]# cp mod_fastdfs.conf /etc/fdfs/ [root@localhost fdfs]# pwd /etc/fdfs [root@localhost fdfs]# vim mod_fastdfs.conf ## 調整以下配置 ## 連接超時 connect_timeout=20 ## 配置跟蹤器IP和端口 tracker_server=192.168.72.130:22122 ## 路徑包含group url_have_group_name = true # 必須和storage配置相同 store_path0=/data/fastdfs/data
[root@localhost fdfs]# cd /usr/local/mysoft/fastdfs-5.11/conf/ [root@localhost conf]# cp anti-steal.jpg http.conf mime.types /etc/fdfs/
在Nginx的80服務端口下添加以下配置。注意這裏的路徑是Nginx安裝自動生成的路徑。
[root@localhost nginx]# cd /usr/local/nginx/conf/ [root@localhost conf]# vim nginx.conf server { listen 80; location ~/group([0-9])/M00 { root /data/fastdfs/data; ngx_fastdfs_module; } }
查看配置結果
[root@localhost conf]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.15.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) configure arguments: --add-module=/usr/local/mysoft/fastdfs-nginx-module/src
這樣就配置成功了。
啓動Nginx服務。
## 啓動 /usr/local/nginx/sbin/nginx ## 中止 /usr/local/nginx/sbin/nginx -s stop ## 重啓 /usr/local/nginx/sbin/nginx -s reload
喵喵的,竟然成功了,下篇文章見。
http://192.168.72.130 /group1/M00/00/00/wKhIgl0mmE-ATEXPAAQ2pIoAy98392.jpg
GitHub·地址 https://github.com/cicadasmile/linux-system-base GitEE·地址 https://gitee.com/cicadasmile/linux-system-base