本文所用軟體包,如無特别版本要求,建議使用該版本V5.08.
1.FastDFS的安裝
1.0 依賴安裝與環境配置
yum install make cmake gcc gcc-c++ zip unzip
hostname 'your hostname'
1.1 下載下傳并安裝libfastcommon
github address: https://github.com/happyfish100/libfastcommon.git
gitee address: https://gitee.com/fastdfs100/libfastcommon.git
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon; git checkout -b V1.0.50
./make.sh clean && ./make.sh && ./make.sh install
1.2 安裝FastDFS
cd /usr/local/src
tar -xzvf FastDFS_v5.08.tar.gz
cd FastDFS
./make.sh clean && ./make.sh && ./make.sh install
- 預設安裝完檢查啟動腳本是否生成.
/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_trackerd
- 檢查配置檔案
[[email protected] fdfs]# ll
total 20
-rw-r--r-- 1 root root 1461 Jun 23 13:47 client.conf.sample
-rw-r--r-- 1 root root 7927 Jun 23 13:47 storage.conf.sample
-rw-r--r-- 1 root root 7200 Jun 23 13:47 tracker.conf.sample
- 指令行工具(/usr/bin目錄下)
[[email protected] bin]# ll fdfs*
-rwxr-xr-x 1 root root 315392 Jun 23 13:47 fdfs_appender_test
-rwxr-xr-x 1 root root 315168 Jun 23 13:47 fdfs_appender_test1
-rwxr-xr-x 1 root root 302016 Jun 23 13:47 fdfs_append_file
-rwxr-xr-x 1 root root 301672 Jun 23 13:47 fdfs_crc32
-rwxr-xr-x 1 root root 302080 Jun 23 13:47 fdfs_delete_file
-rwxr-xr-x 1 root root 302816 Jun 23 13:47 fdfs_download_file
-rwxr-xr-x 1 root root 302400 Jun 23 13:47 fdfs_file_info
-rwxr-xr-x 1 root root 316216 Jun 23 13:47 fdfs_monitor
-rwxr-xr-x 1 root root 1101856 Jun 23 13:47 fdfs_storaged
-rwxr-xr-x 1 root root 325288 Jun 23 13:47 fdfs_test
-rwxr-xr-x 1 root root 320416 Jun 23 13:47 fdfs_test1
-rwxr-xr-x 1 root root 447832 Jun 23 13:47 fdfs_trackerd
-rwxr-xr-x 1 root root 303008 Jun 23 13:47 fdfs_upload_appender
-rwxr-xr-x 1 root root 304024 Jun 23 13:47 fdfs_upload_file
2. FastDFS Tracker伺服器配置
2.1 複制Tracker樣例配置檔案,并重命名
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
2.2 修改Tracker配置檔案
vim /etc/fdfs/tracker.conf
# 修改的内容如下:
disabled=false # 啟用配置檔案
port=22122 # tracker伺服器端口(預設22122)
base_path=/fastdfs/tracker # 存儲日志和資料的根目錄
其它參數保留預設配置, 具體配置解釋可參考官方文檔說明:http://bbs.chinaunix.net/thread-1941456-1-1.html
2.3 建立日志和資料的根目錄
mkdir -p /fastdfs/tracker
2.4 防火牆配置Tracker伺服器端口
vi /etc/sysconfig/iptables
# 附加:若/etc/sysconfig 目錄下沒有iptables檔案可随便寫一條iptables指令配置個防火牆規則:如:
iptables -P OUTPUT ACCEPT
# 然後用指令:service iptables save 進行儲存,預設就儲存到 /etc/sysconfig/iptables 檔案裡。這時既有了這個檔案。防火牆也可以啟動了。接下來要寫政策,也可以直接寫在/etc/sysconfig/iptables 裡了。
# 添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
# 重新開機防火牆
service iptables restart
2.5 啟動Tracker伺服器
/etc/init.d/fdfs_trackerd start
# 初次啟動,會在/fastdfs/tracker目錄下生成logs、data兩個目錄。
[[email protected] tracker]# ll
total 0
drwxr-xr-x 2 root root 60 Jun 23 14:05 data
drwxr-xr-x 2 root root 26 Jun 23 14:05 logs
# 檢查FastDFS Tracker Server是否啟動成功:
ps -ef | grep fdfs_trackerd
[[email protected] tracker]# ps -ef | grep fdfs_trackerd
root 17467 1 0 14:05 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root 17656 38280 0 14:05 pts/0 00:00:00 grep --color=auto fdfs_trackerd
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SNzIGMhhjNzMGZ1UDZxcTZxETZmVDM0gDOwATOkhDM38CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
3. 配置Storage伺服器
3.1 複制Storage樣例配置檔案,并重命名
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
3.2 修改Storage配置檔案
vi /etc/fdfs/storage.conf
# 修改的内容如下:
disabled=false # 啟用配置檔案
port=23000 # storage服務端口
base_path=/fastdfs/storage # 資料和日志檔案存儲根目錄
store_path0=/fastdfs/storage # 第一個存儲目錄
tracker_server=XMZ-OSS:22122 # tracker伺服器IP和端口
http.server_port=8888 # http通路檔案的端口
# 其它參數保留預設配置, 具體配置解釋可參考官方文檔說明:http://bbs.chinaunix.net/thread-1941456-1-1.html
3.3 建立基礎檔案路徑
mkdir -p /fastdfs/storage
3.4 防火牆配置Storage服務端口
vi /etc/sysconfig/iptables
# 添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT
# 重新開機防火牆
service iptables restart
3.5 啟動Storage服務
/etc/init.d/fdfs_storaged start
# 初次啟動,會在/fastdfs/storage目錄下生成logs、data兩個目錄。
[[email protected] storage]# ll
total 12
drwxr-xr-x 259 root root 8192 Jun 23 14:28 data
drwxr-xr-x 2 root root 26 Jun 23 14:28 logs
# 檢查FastDFS Tracker Server是否啟動成功:
[[email protected] storage]# ps -ef | grep fdfs_storaged
root 27300 1 1 14:28 ? 00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
root 27707 38280 0 14:29 pts/0 00:00:00 grep --color=auto fdfs_storaged
4 測試
4.1 修改Tracker伺服器用戶端配置檔案
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
# 修改以下配置,其它保持預設
base_path=/fastdfs/tracker
tracker_server='your hostname':22122
4.2 執行檔案上傳指令
# /etc/fdfs/client.conf.sample 是需要上傳檔案路徑
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/client.conf.sample
# 傳回檔案ID号:group1/M00/00/00/Dk7iazDS1q2AJpmgAAAFtTzeg5c.sample
#(能傳回以上檔案ID,說明檔案已經上傳成功)
5. 配合Nginx實作檔案通路
5.1 fastdfs-nginx-module 作用說明
FastDFS通過Tracker伺服器,将檔案放在Storage伺服器存儲,但是同組存儲伺服器之間需要進入檔案複制,有同步延遲的問題。
假設Tracker伺服器将檔案上傳到了A,上傳成功後檔案 ID 已經傳回給用戶端。此時FastDFS存儲叢集機制會将這個檔案同步到同組存儲B,在檔案還 沒有複制完成的情況下,用戶端如果用這個檔案ID在B上取檔案,就會出現檔案無法通路的錯誤。
而fastdfs-nginx-module 可以重定向檔案連接配接到源伺服器取檔案,避免用戶端由于複制延遲導緻的檔案無法通路錯誤。(解壓後的fastdfs-nginx-module在nginx安裝時使用)
5.2 fastdfs-nginx-module安裝
tar -xzvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src
vi config
配置檔案修改如下:
ngx_addon_name=ngx_http_fastdfs_module
HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
5.3 nginx編譯安裝
安裝所需依賴:
yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
安裝nginx:
cd /usr/local/src/
tar -zxvf nginx-1.10.0.tar.gz
cd nginx-1.10.0
./configure --add-module=/usr/local/src/fastdfs-nginx-module/src
make && make install
比對 nginx下Makefile
default: build
clean:
rm -rf Makefile objs
build:
$(MAKE) -f objs/Makefile
install:
$(MAKE) -f objs/Makefile install
modules:
$(MAKE) -f objs/Makefile modules
upgrade:
/usr/local/nginx/sbin/nginx -t
kill -USR2 'cat /usr/local/nginx/logs/nginx.pid'
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT 'cat /usr/local/nginx/logs/nginx.pid.oldbin'
5.4 fastdfs-nginx-module配置修改
預設安裝完nginx的路徑為/usr/local/nginx
複制 fastdfs-nginx-module 源碼中的配置檔案到/etc/fdfs 目錄,并修改
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
修改以下配置
connect_timeout=30
base_path=/tmp
tracker_server='your hostname':22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path0=/fastdfs/storage
5.5 複制FastDFS的部配置設定置檔案到/etc/fdfs 目錄
cd /usr/local/src/FastDFS/conf
cp http.conf mime.types /etc/fdfs/
5.6 檔案存儲目錄下建立軟連接配接,将其連結到實際存放資料的目錄
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
5.7 nginx配置修改
user root;
worker_processes 8;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8888;
server_name localhost;
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 800;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
5.8 nginx啟動
# 重新開機防火牆
# service iptables restart
# 啟動 Nginx
/usr/local/nginx/sbin/nginx
# (重新開機 Nginx 的指令為:/usr/local/nginx/sbin/nginx -s reload)
5.9 通路測試
http://'address':8888/group1/M00/00/00/Dk7iazDS1q2AJpmgAAAFtTzeg5c.sample