新聞中心
nginx && FastDFS實(shí)現(xiàn)分布式文件服務(wù)器
作者:微笑面對(duì)生活 2019-01-28 11:03:03
服務(wù)器
分布式 FastDFS是一個(gè)開源的輕量級(jí)分布式文件系統(tǒng),它對(duì)文件進(jìn)行管理,功能包括:文件存儲(chǔ)、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲(chǔ)和負(fù)載均衡的問題。

FastDFS是一個(gè)開源的輕量級(jí)分布式文件系統(tǒng),它對(duì)文件進(jìn)行管理,功能包括:文件存儲(chǔ)、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲(chǔ)和負(fù)載均衡的問題。特別適合以文件為載體的在線服務(wù),如相冊(cè)網(wǎng)站、視頻網(wǎng)站等等。
FastDFS為互聯(lián)網(wǎng)量身定制,充分考慮了冗余備份、負(fù)載均衡、線性擴(kuò)容等機(jī)制,并注重高可用、高性能等指標(biāo),使用FastDFS很容易搭建一套高性能的文件服務(wù)器集群提供文件上傳、下載等服務(wù)。
1. 下載安裝 libfastcommon
libfastcommon是從 FastDFS 和 FastDHT 中提取出來的公共 C 函數(shù)庫,基礎(chǔ)環(huán)境,安裝即可 。
1.1 下載:
- wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
1.2 解壓:
- tar -zxvf V1.0.7.tar.gz
1.3 安裝:
- cd libfastcommon-1.0.7
- ./make.sh
- ./make.sh install
1.4 復(fù)制文件:
解決FastDFS中l(wèi)ib配置文件路徑問題。
- cp/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
- cp/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
2. 下載安裝FastDFS
2.1 下載:
- wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
2.2 解壓:
- tar -zxvf V5.05.tar.gz
2.3 安裝:
- cd fastdfs-5.05
- ./make.sh
- ./make.sh install
安裝完成后,默認(rèn)配置文件目錄為: /etc/fdfs/,默認(rèn)命令放在 /usr/bin/中,以 fdfs_開頭。
3. 配置FastDFS
首先將配置文件復(fù)制到 /etc/fdfs目錄下。
- cd/home/fantj/download/fastdfs-5.05/conf
- cp*/etc/fdfs/
- 如下:
- [root@s168 conf]#cd/etc/fdfs/
- [root@s168 fdfs]#ls
- anti-steal.jpg client.conf.sample mime.types storage.conf.sample tracker.conf
- client.conf http.conf storage.conf storage_ids.conf tracker.conf.sample
3.1. 配置tracker.conf
FastDFS跟蹤器
修改如下配置:
- #Tracker 數(shù)據(jù)和日志目錄地址(根目錄必須存在,子目錄會(huì)自動(dòng)創(chuàng)建)
- base_path=/fastdfs/tracker
- port=22122
3.1.1 創(chuàng)建該目錄:
mkdir-p/fastdfs/tracker3.1.2 啟動(dòng)/關(guān)閉:
fdfs_trackerd/etc/fdfs/tracker.conf start/stop默認(rèn)使用 /etc/fdfs下的配置文件,如有指定配置可在后面追加參數(shù)。如有防火墻,開放防火墻規(guī)則。
3.1.3 設(shè)置開機(jī)啟動(dòng):
- # systemctl enable fdfs_trackerd
- 或者:
- # vim /etc/rc.d/rc.local
- 加入配置:
- /etc/init.d/fdfs_trackerd start
3.1.4 tracker server 目錄及文件結(jié)構(gòu) :
Tracker服務(wù)啟動(dòng)成功后,會(huì)在base_path下創(chuàng)建data、logs兩個(gè)目錄。目錄結(jié)構(gòu)如下:
- ${base_path}
- |__data
- | |__storage_groups.dat:存儲(chǔ)分組信息
- | |__storage_servers.dat:存儲(chǔ)服務(wù)器列表
- |__logs
- | |__trackerd.log:tracker server日志文件
3.2. 配置storage
下面只是基本配置,如有更細(xì)微的需要,則請(qǐng)查看所有配置。
- group_name=fantj #組名
- base_path=/fastdfs/storage #日志目錄
- store_path0=/fastdfs/storage #存儲(chǔ)目錄
- tracker_server=192.168.27.168:22122 #tracker節(jié)點(diǎn)
- http.server_port=8888 #端口
3.2.1 啟動(dòng)
- 可以用這種方式啟動(dòng)
- fdfs_storaged/etc/fdfs/storage.conf start
- 也可以用這種方式,后面都用這種
- # service fdfs_storaged start
3.2.2 檢查進(jìn)程
netstat-unltp|grep fdfs3.2.3 Storage 開機(jī)啟動(dòng)
- # systemctl enable fdfs_storaged
- 或者:
- # vim /etc/rc.d/rc.local
- 加入配置:
- /etc/init.d/fdfs_storaged start
3.2.4 Storage 目錄
Storage 啟動(dòng)成功后,在base_path 下創(chuàng)建了data、logs目錄,記錄著 Storage Server 的信息。
在 store_path0 目錄下,創(chuàng)建了N*N個(gè)子目錄:
- [root@s168 data]# pwd
- /fastdfs/storage/data
- [root@s168 data]# ls
- 00 07 0E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69 70 77 7E 85 8C 93 9A A1 A8 AF B6 BD C4 CB D2 D9 E0 E7 EE F5 FC
- 01 08 0F 16 1D 24 2B 32 39 40 47 4E 55 5C 63 6A 71 78 7F 86 8D 94 9B A2 A9 B0 B7 BE C5 CC D3 DA E1 E8 EF F6 FD
- 02 09 10 17 1E 25 2C 33 3A 41 48 4F 56 5D 64 6B 72 79 80 87 8E 95 9C A3 AA B1 B8 BF C6 CD D4 DB E2 E9 F0 F7 fdfs_storaged.pid
- 03 0A 11 18 1F 26 2D 34 3B 42 49 50 57 5E 65 6C 73 7A 81 88 8F 96 9D A4 AB B2 B9 C0 C7 CE D5 DC E3 EA F1 F8 FE
- 04 0B 12 19 20 27 2E 35 3C 43 4A 51 58 5F 66 6D 74 7B 82 89 90 97 9E A5 AC B3 BA C1 C8 CF D6 DD E4 EB F2 F9 FF
- 05 0C 13 1A 21 28 2F 36 3D 44 4B 52 59 60 67 6E 75 7C 83 8A 91 98 9F A6 AD B4 BB C2 C9 D0 D7 DE E5 EC F3 FA storage_stat.dat
- 06 0D 14 1B 22 29 30 37 3E 45 4C 53 5A 61 68 6F 76 7D 84 8B 92 99 A0 A7 AE B5 BC C3 CA D1 D8 DF E6 ED F4 FB sync
3.3. 配置client
- vim client.conf
- tracker_server=192.168.27.168:22122 #tracker節(jié)點(diǎn)
- base_path=/fastdfs/client #日志路徑
3.4. 本地圖片上傳測(cè)試
- [root@s168 fdfs]#fdfs_upload_file/etc/fdfs/client.conf/home/test.png
- fantj/M00/00/00/wKgbqFu7T7iAJh7lAABDYbhAMC4812.png
- 或者:
- [root@s168 fdfs]#fdfs_test/etc/fdfs/client.conf/home/test.png
- ThisisFastDFSclient test program v5.05
- Copyright(C)2008,HappyFish/YuQing
- FastDFSmay be copied only under the terms of the GNUGeneral
- PublicLicenseV3,which may be foundintheFastDFSsource kit.
- Pleasevisit theFastDFSHomePagehttp://www.csource.org/
- formore detail.
- [2018-5-0820:41:15]DEBUG-base_path=/fastdfs/client,connect_timeout=30,network_timeout=60,tracker_server_count=1,anti_steal_token=0,anti_steal_secret_key length=0,use_connection_pool=0,g_connection_pool_max_idle_time=3600s,use_storage_id=0,storage server id count:0
- invalid operation:/home/test.png
4. 安裝nginx
之前有寫過nginx的安裝文章,在這里不重復(fù)。沒有安裝過的請(qǐng)點(diǎn)擊:
點(diǎn)擊查看我的文章:nginx安裝
5. FastDFS 配置 Nginx 模塊
fastdfs-nginx-module 可以重定向文件鏈接到源服務(wù)器取文件,避免客戶端由于復(fù)制延遲導(dǎo)致的文件無法訪問錯(cuò)誤。
5.1 下載安裝
- wget https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip
- unzip5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip
- mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1/fastdfs-nginx-module
5.2 配置Nginx
- ngix-s stop
- cd nginx-1.12.1
- #添加模塊
- ./configure--add-module=../fastdfs-nginx-module/src
- (如果你是yum安裝的nginx,我暫時(shí)還沒找到解決辦法,目前的方法是nginx-V查看nginx版本,然后下載一個(gè)同版本的nginx,
- 然后將/etc/nginx下數(shù)據(jù)整體備份,make&&make install完成后再做恢復(fù))
- #重新編譯和安裝
- make
- make install
- #拷貝配置文件
- [root@s168 src]#pwd
- /home/fantj/download/fastdfs-nginx-module/src
- [root@s168 src]#cp mod_fastdfs.conf/etc/fdfs/
配置 /etc/fdfs/mod_fastdfs.conf文件
- #配置 /etc/fdfs/mod_fastdfs.conf文件
- base_path=/fastdfs/tmp #日志目錄
- store_path0=/fastdfs/storage
- tracker_server=192.168.27.168:22122
- # the group name of the local storage server
- group_name=fantj #和storage的groupname一一對(duì)應(yīng)
- url_have_group_name=true #開啟url中附帶group_name
配置nginx
- # 配置nginx
- [root@s168 fdfs]#cd/usr/local/nginx/conf/
- [root@s168 conf]#vim nginx.conf
- #添加如下配置
- server{
- listen88;
- server_name192.168.27.168;
- location/fantj/M00{
- ngx_fastdfs_module;
- }
- }
- # 進(jìn)入/usr/local/nginx/sbin目錄執(zhí)行配置檢測(cè)
- [root@s168 sbin]#./nginx -t
- ngx_http_fastdfs_set pid=6431
- nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntaxisok
- nginx:configuration file/usr/local/nginx/conf/nginx.conf testissuccessful
- # 啟動(dòng)nginx
- [root@s168 sbin]#./nginx
- ngx_http_fastdfs_set pid=6436
服務(wù)重啟并測(cè)試
- [root@s168 fdfs]#fdfs_trackerd tracker.conf restart
- waitingforpid[1202]exit...
- starting...
- [root@s168 fdfs]#fdfs_storaged storage.conf restart
- waitingforpid[1211]exit...
- starting...
- [root@s168 fdfs]#/usr/local/nginx/sbin/nginx-s reload
- ngx_http_fastdfs_set pid=6463
6. 配置成功測(cè)試
分享題目:Nginx&&FastDFS實(shí)現(xiàn)分布式文件服務(wù)器
分享地址:http://www.5511xx.com/article/cdsjgjo.html


咨詢
建站咨詢
