1. 程式人生 > >檔案系統FastDFS安裝與配置(單機)

檔案系統FastDFS安裝與配置(單機)

安裝包如下: fastdfs-nginx-module_v1.16.tar.gz FastDFS_v5.05.tar.gz libfastcommon-master.zip nginx-1.8.0.tar.gz

一、安裝libfastcommon

FastDFS 5.05版本不再依賴libevent,而依賴於libfastcommon,因此需要先安裝libfastcommon。 軟體包下載地址:https://github.com/happyfish100/libfastcommon

# unzip libfastcommon-master.zip
# cd libfastcommon-master
# ./make.sh
# ./make.sh install

libfastcommon.so 預設安裝到了/usr/lib64/libfastcommon.so,而FastDFS主程式設定的lib目錄是/usr/local/lib,因此需要設定軟連結(如果已存在,可以忽略)。

# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so

二、安裝FastDFS

軟體包下載地址: 1、https://sourceforge.net/projects/fastdfs/files/ 2、https://code.google.com/archive/p/fastdfs/downloads 3、https://github.com/happyfish100/fastdfs

# tar zxf FastDFS_v5.05.tar.gz
# cd FastDFS
# ./make.sh
# ./make.sh install

安裝完成後可以看到/etc/fdfs目錄下生成了3個檔案:

三、配置tracker節點

# mkdir /data/fastdfs  #建立tracker的資料檔案和日誌儲存目錄
# cd /etc/fdfs
# mv tracker.conf.sample tracker.conf 
# vim tracker.conf #修改的檔案內容如下(基礎配置,不考慮效能調優情況下):
base_path=/data/fastdfs  #設定 tracker 的資料檔案和日誌目錄(需預先建立)
http.server_port=6666  #設定http埠號,預設為8080
# ln -s /usr/bin/fdfs_trackerd /usr/local/bin
# ln -s /usr/bin/stop.sh /usr/local/bin
# ln -s /usr/bin/restart.sh /usr/local/bin
# service fdfs_trackerd start  #啟動tracker節點

啟動成功後,在/data/fastdfs目錄下生成了data和logs兩個目錄。 檢視日誌如下:

檢查對應埠是否監聽:

四、配置storage節點

# mkdir /data/fastdfs-storage  #建立
# cd /etc/fdfs/
# mv storage.conf.sample storage.conf
# vim storage.conf#修改的檔案內容如下(基礎配置,不考慮效能調優情況下):
group_name=group1   #組名,可根據實際情況修改
base_path=/data/fastdfs-storage #設定storage資料檔案和日誌目錄,需預先建立
store_path_count=1  #儲存路徑個數,需要和 store_path 個數匹配、
store_path0=/data/fastdfs-storage #儲存路徑
tracker_server=192.168.116.145:22122 # #tracker 伺服器的 IP地址和埠號,如果是單機搭建,IP不要寫127.0.0.1,否則啟動不成功。
http.server_port=8888  #設定 http 埠號

# ln -s /usr/bin/fdfs_storaged /usr/local/bin
# service fdfs_storaged start #啟動storage,啟動會根據配置檔案的設定自動建立多級儲存目錄

檢視日誌,內容如下,表示啟動正常:

# cat /data/fastdfs-storage/logs/storaged.log
[2016-03-31 11:49:35] INFO - FastDFS v5.05, base_path=/data/fastdfs-storage, store_path_count=1, subdir_count_per_path=256, group_name=group1, run_by_group=, run_by_user=, connect_timeout=30s, network_timeout=60s, port=23000, bind_addr=, client_bind=1, max_connections=256, accept_threads=1, work_threads=4, disk_rw_separated=1, disk_reader_threads=1, disk_writer_threads=1, buff_size=256KB, heart_beat_interval=30s, stat_report_interval=60s, tracker_server_count=1, sync_wait_msec=50ms, sync_interval=0ms, sync_start_time=00:00, sync_end_time=23:59, write_mark_file_freq=500, allow_ip_count=-1, file_distribute_path_mode=0, file_distribute_rotate_count=100, fsync_after_written_bytes=0, sync_log_buff_interval=10s, sync_binlog_buff_interval=10s, sync_stat_file_interval=300s, thread_stack_size=512 KB, upload_priority=10, if_alias_prefix=, check_file_duplicate=0, file_signature_method=hash, FDHT group count=0, FDHTserver count=0, FDHT key_namespace=, FDHT keep_alive=0, HTTP server port=8888, domain name=, use_access_log=0, rotate_access_log=0, access_log_rotate_time=00:00, rotate_error_log=0, error_log_rotate_time=00:00, rotate_access_log_size=0, rotate_error_log_size=0, log_file_keep_days=0, file_sync_skip_invalid_record=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s
data path: /data/fastdfs-storage/data, mkdir sub dir...
mkdir data path: 00 ...
mkdir data path: 01 ...
mkdir data path: 02 ...
mkdir data path: 03 ...
。。。。。
data path: /data/fastdfs-storage/data, mkdir sub dir done.

確認啟動成功後,可以執行 fdfs_monitor 檢視 storage伺服器是否已經登記到 tracker伺服器。
# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2016-03-31 11:54:36] DEBUG - base_path=/data/fastdfs-storage, 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

server_count=1, server_index=0

tracker server is 192.168.116.145:22122

group count: 1

Group 1:
group name = group1
disk total space = 201586 MB
disk free space = 190886 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 8888
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

     Storage 1:
          id = 192.168.116.145
          ip_addr = 192.168.116.145  ACTIVE
          http domain =
          version = 5.05
          join time = 2016-03-31 11:49:35
          up time = 2016-03-31 11:49:35
          total storage = 201586 MB
          free storage = 190886 MB
          upload priority = 10
          store_path_count = 1
          subdir_count_per_path = 256
          storage_port = 23000
          storage_http_port = 8888
          current_write_path = 0
          source storage id =
          if_trunk_server = 0
          connection.alloc_count = 256
          connection.current_count = 0
          connection.max_count = 0
          total_upload_count = 0
          success_upload_count = 0
          total_append_count = 0
          success_append_count = 0
          total_modify_count = 0
          success_modify_count = 0
          total_truncate_count = 0
          success_truncate_count = 0
          total_set_meta_count = 0
          success_set_meta_count = 0
          total_delete_count = 0
          success_delete_count = 0
          total_download_count = 0
          success_download_count = 0
          total_get_meta_count = 0
          success_get_meta_count = 0
          total_create_link_count = 0
          success_create_link_count = 0
          total_delete_link_count = 0
          success_delete_link_count = 0
          total_upload_bytes = 0
          success_upload_bytes = 0
          total_append_bytes = 0
          success_append_bytes = 0
          total_modify_bytes = 0
          success_modify_bytes = 0
          stotal_download_bytes = 0
          success_download_bytes = 0
          total_sync_in_bytes = 0
          success_sync_in_bytes = 0
          total_sync_out_bytes = 0
          success_sync_out_bytes = 0
          total_file_open_count = 0
          success_file_open_count = 0
          total_file_read_count = 0
          success_file_read_count = 0
          total_file_write_count = 0
          success_file_write_count = 0
          last_heart_beat_time = 2016-03-31 11:54:35
          last_source_update = 1970-01-01 08:00:00
          last_sync_update = 1970-01-01 08:00:00
          last_synced_timestamp = 1970-01-01 08:00:00

看到“192.168.116.145  ACTIVE”即可確認 storage 執行正常。

五、給storage安裝nginx並配置

主要目的是為了提供 http 的訪問服務,同時解決 group 中 storage伺服器的同步延遲問題。 nginx需要新增fastdfs-nginx-module模組。 下載地址:http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz

# tar zxf fastdfs-nginx-module_v1.16.tar.gz
# tar zxf nginx-1.8.0.tar.gz
# cd nginx-1.8.0
# ./configure --prefix=/data/nginx --add-module=/data/soft/fastdfs-nginx-module/src
# make
# make install

make時報錯如下:

.......
/data/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:894: 錯誤:‘struct fdfs_http_context’沒有名為‘if_modified_since’的成員
/data/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:897: 錯誤:‘struct fdfs_http_context’沒有名為‘if_modified_since’的成員
/data/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:927: 錯誤:‘struct fdfs_http_context’沒有名為‘range’的成員
/data/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:933: 錯誤:‘struct fdfs_http_context’沒有名為‘if_range’的成員
/data/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:933: 錯誤:‘true’未宣告(在此函式內第一次使用)
make[1]: *** [objs/addon/src/ngx_http_fastdfs_module.o] 錯誤 1
make[1]: Leaving directory `/data/soft/nginx-1.8.0'
make: *** [build] 錯誤 2

解決辦法: 執行以下2條命令,然後重新make

# ln -sv /usr/include/fastcommon /usr/local/include/fastcommon
# ln -sv /usr/include/fastdfs /usr/local/include/fastdfs

拷貝相關檔案到/etc/fdfs目錄下:

# cp /data/soft/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
# cp /data/soft/FastDFS/conf/mime.types /etc/fdfs/
# cp /data/soft/FastDFS/conf/http.conf /etc/fdfs/
# cp /data/soft/FastDFS/conf/anti-steal.jpg /etc/fdfs/

修改nginx.conf的配置:

listen       9999;
location ~/group[1-3]/M00 {
            root /data/fastdfs-storage/data;
            ngx_fastdfs_module;
        }

修改/etc/fdfs/mod_fastdfs.conf的配置:

base_path=/data/fastdfs-storage  #儲存日誌目錄
tracker_server=192.168.116.145:22122  #tracker 伺服器的 IP 地址以及埠號
url_have_group_name = true #檔案 url 中是否有 group 名
store_path0=/data/fastdfs-storage # 儲存路徑
http.need_find_content_type=true  # 從副檔名查詢檔案型別 (nginx 時 為true)
group_count = 3  #設定組的個數,事實上這次只使用了group1
在檔案末新增如下內容:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs-storage

[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs-storage

[group3]
group_name=group3
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs-storage

建立 M00 至儲存目錄的符號連線:

# ln -s /data/fastdfs-storage/data /data/fastdfs-storage/data/M00

啟動nginx:

# /data/nginx/sbin/nginx

檢視nginx的error日誌,如果類似下以內容,表明配置成功:

六、給tracker安裝nginx並配置

安裝路徑為:/data/nginx2 安裝過程略。 nginx.conf配置如下:

upstream fdfs_group1 {
        server 127.0.0.1:9999;
    }

location /group1/M00 {
        proxy_pass http://fdfs_group1;
    }

啟動nginx:

# /data/nginx2/sbin/nginx

配置客戶端檔案

# cd /etc/fdfs/
# mv client.conf.sample client.conf  #修改內容如下:
base_path=/data/fastdfs-storage  #日誌存放路徑
tracker_server=192.168.116.145:22122  #tracker 伺服器 IP 地址和埠號
http.tracker_server_port=6666  # tracker 伺服器的 http 埠號,必須和tracker的設定對應起來

測試檔案上傳:

通過上圖可以看到檔案上傳後儲存的路徑為:group1/M00/00/00/wKh0kVb8xZuAdQGUAAfp5oSKTdo388.jpg

通過nginx訪問: