准備linux服務器或虛擬機,這里是虛擬機,操作系統CentOS 6.4
Tracker 和 Storage 安裝在一台機器上
FastDFS 5.08版本
1,准備軟件
軟件下載:http://sourceforge.net/projects/fastdfs/files/
文中用到的 https://pan.baidu.com/s/1IeZqzt6Gc5Yc34RMwew3nQ 提取碼:7dvf
2,安裝gcc
FastDFS是C語言開發的應用。安裝必須使用make、cmake和gcc編譯器。
命令:yum install make cmake gcc gcc-c++

(1)進行解壓libfastcommon-master.zip,命令:unzip libfastcommon-master.zip -d /usr/local/fastdfs
(2)進入目錄 cd /usr/local/fastdfs/libfastcommon-master
(3)進行編譯安裝
命令: ./make.sh
命令: ./make.sh install
注意安裝的路徑:也就是說,我們的libfastcommon默認安裝到了/usr/lib64/這個位置。
(4)創建軟連接
FastDFS主程序設置的目錄為/usr/local/lib/,所以我們需要創建/usr/lib64/下的一些核心執行程序的軟連接文件。
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
4,安裝FastDFS
4.1 進入到cd /lhy/software/ 下,
解壓 FastDFS_v5.08.tar.gz,命令:tar -zxf FastDFS_v5.08.tar.gz -C /usr/local/fastdfs
4.2 編譯安裝
命令:cd /usr/local/fastdfs/FastDFS/
編譯:./make.sh
安裝:./make.sh install
4.3 采用默認安裝方式腳本文件說明
(1)服務腳本在
/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_trackerd
(2)配置文件在
/etc/fdfs/client.conf.sample - 跟蹤器服務配置文件模板
/etc/fdfs/storage.conf.sample - 存儲服務器配置文件模板
/etc/fdfs/tracker.conf.sample -- FastDFS提供的命令行客戶端配置文件模板。可以通過命令行測試FastDFS有效性。
(3)命令行工具在/usr/bin/目錄下,是一些可執行文件
5,
1,進入到 /etc/fdfs/ 目錄,執行命令: 把tracker.conf.sample復制一份
命令:cd /etc/fdfs/
命令:cp tracker.conf.sample tracker.conf
2,修改tracker.conf
命令 :vim /etc/fdfs/tracker.conf #########base_path應該放在/var/data下, base_path=/var/data/fastdfs-tracker(自定義目錄)
***************************************
如果/usr是安裝時會占用較大硬盤容量目錄,那么/var就是在系統運行后才會漸漸占用硬盤容量的目錄。因為/var目錄主要針對常態性變動文件,包括緩存(cache)、登錄文件(logfile)以及某些軟件運行所產生的文件,包括程序文 件 (lock file,run file),或者例如Mysql數據庫的文件等
*********************************************************************************************************************
3,配置中的路徑需要先創建好才能啟動服務
命令:mkdir -p /fastdfs/tracker 遞歸創建目錄
4,啟動追蹤器
命令:/etc/init.d/fdfs_trackerd start
啟動成功后,配置文件中base_path指向的目錄中出現FastDFS服務相關數據目錄(data目錄、logs目錄)
啟動tracker命令:/etc/init.d/fdfs_trackerd start
查看進程命令:ps -el | grep fdfs
停止tracker命令:/etc/init.d/fdfs_trackerd stop
重啟服務:/etc/init.d/fdfs_trackerd restart
5 可以設置開機啟動跟蹤器
(一般生產環境需要開機啟動一些服務,如keepalived、linux、tomcat等等)命令:vim /etc/rc.d/rc.local加入配置:/etc/init.d/fdfs_trackerd start
6,配置FastDFS存儲(Storage)
1, 進入到 /etc/fdfs/ ,進行copy storage文件一份
命令:cd /etc/fdfs/
命令:cp storage.conf.sample storage.conf
2,修改storage.conf文件
命令:vim /etc/fdfs/storage.conf
修改內容:
base_path=/fastdfs/storage/base ( base_path - 基礎路徑。用於保存storage server基礎數據內容和日志內容的目錄)
#######base_path也應該放在/var/data下 /var/data/fastdfs-storage/base
store_path0=/fastdfs/storage/store (store_path0 - 存儲路徑。是用於保存FastDFS中存儲文件的目錄,就是要創建256*256個子目錄的位置。base_path和store_path0可以使用同一個目錄。)
tracker_server=192.168.1.172:22122 (tracker_server - 跟蹤服務器位置。就是跟蹤服務器的ip和端口。)
3,遞歸創建存儲目錄(上個步驟中的目錄)
命令 :mkdir -p /fastdfs/storage/base
命令:mkdir -p /fastdfs/storage/store
4,啟動服務 (要求tracker服務必須已啟動)
命令:/etc/init.d/fdfs_storaged start
啟動成功后,配置文件中base_path指向的目錄中出現FastDFS服務相關數據目錄(data目錄、logs目錄)
配置文件中的store_path0指向的目錄中同樣出現FastDFS存儲相關數據錄(data目錄)
5,查看FastDFS storage 是否啟動成功
查看服務狀態:/etc/init.d/fdfs_storaged status
停止服務:/etc/init.d/fdfs_storaged stop
重啟服務:/etc/init.d/fdfs_storaged restart
6,同理,也可以設置開機啟動存儲器
(一般生產環境需要開機啟動一些服務,如keepalived、linux、tomcat等等)命令:vim /etc/rc.d/rc.local加入配置:/etc/init.d/fdfs_storaged start
++++++++++++++++++到此為止我們的FastDFS環境已經搭建完成!(真是有點復雜)++++++++++++++++++++++++++++++
7,測試環境
使用FastDFS自帶的客戶端進行文件上傳(注意:是在tracker(跟蹤器)中上傳)
1,進入到/etc/fdfs/ 目錄, copy一份client.conf文件
命令:cd /etc/fdfs/
命令:cp client.conf.sample client.conf
2,編輯client.conf文件
命令:vim /etc/fdfs/client.conf
修改內容:
base_path=/fastdfs/client( base_path - 就是客戶端命令行執行過程時臨時數據存儲位置。)
tracker_server=192.168.85.3:22122
3,創建自定義文件夾
命令:mkdir -p /fastdfs/client
4,上傳文件測試
命令:/usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf /要上傳的文件
例:/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /lhy/software/test.png
返回:group1/M00/00/00/wKhVA138_q2APqgyAAq7elxtEkk385.png
上傳結束后,返回group1/M00/00/00/xxxxxxxxxx.xxx,檢查storage服務結點中的$store_path0/data/00/00/目錄中是否有上傳的文件(一般情況上傳的文件按順序保存在$store_path0/data/00/00/目錄中,不能完全保證)。
/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKiWDV0xfqWAFe1OAAAib-i5DLU637.log
命令的腳本位置
8 FastDFS與Nginx整合
安裝Nginx是為了WEB應用中可以使用HTTP協議直接訪問Storage服務中存儲的文件(如你的圖片)。在storage(存儲節點)結點所在服務器安裝Nginx組件。
需要安裝兩部分內容,Nginx應用,在安裝nginx應用的時候,同時要在nginx中增加一個FastDFS的組件。
1,安裝
2, 解壓解壓命令:
tar -zxvf /lhy/software/fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fastdfs/
3, 進入目錄:cd fastdfs-nginx-module/src/
4,修改配置
命令:vim /usr/local/fastdfs/fastdfs-nginx-module/src/config
修改CORE_INC這一行,修改后為:CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/" (去掉了/local 層級)
修改后:
5,FastDFS與nginx進行集成
如果你是已有的nginx,不要重新安裝nginx,那么進入到nginx的安裝包目錄:
執行命令:make clean (清除上次的make命令所產生的object文件(后綴為“.o”的文件)及可執行文件)
加入模塊命令:./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs/fastdfs-nginx-module/src/ (執行了這個configure,再執行一遍make clean會清除本次config,可以多次執行make clean、./configure,沒事)
重新編譯命令: make && make install
+++++++++++如果是新的nginx+++++++++++++++++++++++++++++++++
刪除命令:rm -rf nginx
進入到nginx目錄命令:cd nginx-1.6.2/
加入模塊命令:./configure --add-module=/usr/local/fastdfs/fastdfs-nginx-module/src/
重新編譯命令: make && make install
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6,
7, 進行修改 /etc/fdfs/ 目錄下,剛剛copy過來的mod_fastdfs.conf 文件。
命令:vim /etc/fdfs/mod_fastdfs.conf
修改內容:比如連接超時時間、跟蹤器路徑配置、url的group配置
store_path0=/fastdfs/storage/store
url_have_group_name = true
tracker_server=192.169.85.3:22122
connect_timeout=30
8,
復制FastDFS安裝包中的兩個配置文件(http.conf和mime.types)到/etc/fdfs目錄中
進入到FastDFS安裝目錄 :cd /usr/local/fastdfs/FastDFS/conf/
復制命令:cp http.conf mime.types /etc/fdfs/
9,
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #設置客戶端真實ip地址 #proxy_set_header X-real-ip $remote_addr; #負載均衡反向代理 #proxy_pass http://myapp; root html; index index.html index.htm; } #FastDFS-2019-12-21 location ~ /group([0-9])/M00 { ngx_fastdfs_module; } #配置反向代理tomcat服務器:攔截.jsp結尾的請求轉向到tomcat #location ~ \.jsp$ { #設置客戶端真實ip地址 # proxy_set_header X-real-ip $remote_addr; # proxy_pass http://192.168.85.3:8080; #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
用fastDFS帶的客戶端重新上傳一個圖片:
命令:/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /lhy/software/test.png
返回文件ID:group1/M00/00/00/wKhVA139D5yAb_oiAAq7elxtEkk387.png
11,用瀏覽器訪問該圖片
我直接http://192.168.85.3/group1/M00/00/00/wKhVA139D5yAb_oiAAq7elxtEkk387.png訪問圖片,但是nginx返回404,
命令: /usr/local/nginx/sbin/nginx -s reload 重啟Nginx,看到有fastdfs相關的進程,但是還是訪問不到圖片,nginx返回404。
/usr/local/nginx/sbin/nginx -s stop 關了nginx,/usr/local/nginx/sbin/nginx 再啟動,還不行,還是返回404,現在半夜2:32了,安裝了半天,難道要前功盡棄了??
最終,reboot,重啟了服務器,圖片終於出來了!~~~
group1/M00/00/00/wKhVA139D5yAb_oiAAq7elxtEkk387.png
12,如果是下載文件,如何動態指定文件名
在nginx的fastdfs的location配置里,加上這句話:add_header Content-Disposition "attachment;filename=$arg_attname";
訪問 :http://192.168.85.3/group1/M00/00/00/wKhVA139D5yAb_oiAAq7elxtEkk387.png?attname=123.jpg 這個后綴名,就可以從數據庫讀出來了。
常用命令:
查看服務狀態:ps -ef | grep fdfs
啟動Tracker:/etc/init.d/fdfs_trackerd start
停止Tracker:/etc/init.d/fdfs_trackerd stop
重啟Tracker:/etc/init.d/fdfs_trackerd restart
啟動Storage:/etc/init.d/fdfs_storaged start
查看Storage服務狀態:/etc/init.d/fdfs_storaged status
停止Storage:/etc/init.d/fdfs_storaged stop
重啟Storage:/etc/init.d/fdfs_storaged restart
FastDFS自帶的客戶端上傳文件命令:/usr/bin/fdfs_upload_file /etc/fdfs/client.conf