FastDFS安装教程

来源:互联网 发布:淘宝网瞄准镜 编辑:程序博客网 时间:2024/06/05 03:07

工作环境

  • 操作系统: CentOS 6.7 64bit
  • 编译工具: git, C/C++ 编译工具集(gcc/make等等)
  • libfastcommon: V1.24
  • FastDFS: V5.0.7
  • fastdfs-nginx-module
  • Nginx 1.8.1
  • 注明: 本教程三台linux服务器,安装两台storage服务,安装一台tracker服务.(测试在storage服务器上面测试)

安装

准备工作

  1. 创建fastdfs用户和组(给fastdfs用户sudo权限)

        ##设置root用户密码为root    sudo passwd root;    ##切换到root用户    su - root    ## 给fastdfs sudo权限    visudo    ##找到root ALL=(ALL) ALL行,在下面添加一行    fastdfs ALL=(ALL) ALL    ##退出到fastdfs用户
  2. 安装编译工具和必要的库(FastDFS安装包,nginx安装包,fastdfs-nginx-module源码包,)

# nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库sudo yum -y install gcc automake autoconf libtool make gcc-c++ pcre* zlib openssl openssl-devel
  1. 上传相关文件

安装libfastcommon

  1. fastdfs用户执行
 git clone https://github.com/happyfish100/libfastcommon.git cd libfastcommon ./make.sh sudo ./make.sh install

完成后,libfastcommon:

  • 库文件位置: /usr/lib64/libfastcommon.so
  • 头文件位置:/usr/include/fastcommon

安装FastDFS

本步骤在FastDFS服务端tracker或storage均需执行

  • FastDFS_v5.07.tar.gz下载地址: http://sourceforge.net/projects/fastdfs/files/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.07/FastDFS_v5.07.tar.gz/download
  • 查看FastDFS版本地址: https://sourceforge.net/projects/fastdfs/?source=typ_redirect
  1. fastdfs用户执行

     tar xvf FastDFS_v5.07.tar.gz cd FastDFS ./make.sh sudo ./make.sh install sudo cp /home/fastdfs/FastDFS/conf/http.conf /etc/fdfs/ sudo cp /home/fastdfs/FastDFS/conf/mime.types /etc/fdfs/

完成后,FastDFS安装位置:

  • 配置文件:/etc/fdfs/
  • 执行程序:/usr/bin/
  • 头文件: /usr/include/fastdfs

安装nginx以及fastdfs-nginx-module

本步骤仅在storage服务器执行

FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但是同组之间的服务器需要复制文件,有延迟的问题。fastdfs-nginx-module可以重定向连接到源服务器取文件,避免客户端由于复制延迟的问题,出现错误。

  1. 上传/下载fastdfs-nginx-module源码到/home/fastdfs 下载地址:git clone https://github.com/happyfish100/fastdfs-nginx-module.git
  2. fastdfs用户执行,安装nginx
cd /home/fastdfstar xvf nginx-1.8.1.tar.gzcd nginx-1.8.1./configure \--prefix=/home/fastdfs/nginx-1.8.1 \--sbin-path=/home/fastdfs/nginx-1.8.1/nginx \--conf-path=/home/fastdfs/nginx-1.8.1/nginx.conf \--pid-path=/home/fastdfs/nginx-1.8.1/nginx.pid \--with-http_ssl_module \--add-module=/home/fastdfs/fastdfs-nginx-module/srcmake -j ${NUM_OF_CPU} && make install

请检查fastdfs-nginx-module/src/config文件中的CORE_INCS路径,不同版本略有不同

执行完上述命令,有可能会报错/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:812: 错误:‘fdfs_send_reply_chunk’ 未声明 (在此函数内第一次使用)/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:813: 错误:‘struct fdfs_http_context’ 没有名为 ‘proxy_handler’ 的成员/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:819: 错误:‘struct fdfs_http_context’ 没有名为 ‘if_modified_since’ 的成员/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:822: 错误:‘struct fdfs_http_context’ 没有名为 ‘if_modified_since’ 的成员/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:852: 错误:‘struct fdfs_http_context’ 没有名为 ‘range’ 的成员/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:858: 错误:‘struct fdfs_http_context’ 没有名为 ‘if_range’ 的成员/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:858: 错误:‘true’ 未声明 (在此函数内第一次使用)make[1]: *** [objs/addon/src/ngx_http_fastdfs_module.o] 错误 1make[1]: Leaving directory `/usr/local/src/nginx-1.4.7'make: *** [build] 错误 2cd fastdfs-nginx-module/srcvi config去掉/local  CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
  1. 复制fastdfs-nginx-module源码中的配置文件到/etc/fdfs
sudo cp /home/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs

配置步骤

配置文件说明:

  • /etc/fdfs/tracker.conf.sample 服务端tracker配置文件
  • /etc/fdfs/storage.conf.sample 服务端storage配置文件
  • /etc/fdfs/storage.conf.sample client端配置文件

配置项说明:

  1. 通用配置项:
配置名称值类型默认值必输备注base_pathstring Y存放数据和日志文件的根目录(必须已存在),数据data和日志logs目录如果不存在会自动创建disabledbooleanfalseN本配置文件是否已被禁用bind_addrstring N绑定的IP地址(常用于服务器有多个IP但只希望一个IP提供服务),为空表示本机所有可用地址connect_timeoutint30(s)N连接超时时间,针对socket套接字函数connectnetwork_timeoutint30(s)N网络超时时间,发送或接收数据时,如果在超时时间后还不能发送或接收数据,则本次网络通信失败max_connectionsint256N最大客户端连接数work_threadsint4N工作线程数,通常设置为CPU数log_levelstringinfoN日志级别,标准同syslog;可选值为:emerg,alert,crit,error,warn,notice,info,debug (大小写不敏感)run_by_groupstring N执行程序的用户组,默认为当前用户所在组run_by_userstring N执行程序的用户,默认为当前用户allow_hostsstring*N允许连接的客户端地址,默认为所有,可以使用ip地址范围或CIDR,如:10.0.1.[1-15,20]sync_log_buff_intervalint10(s)N日志缓存刷新同步间隔thread_stack_sizestring64KN线程栈内存大小use_connection_poolbooleanfalseN是否了使用连接线程池,默认为否
  1. tracker服务端配置项:
配置名称值类型默认值必输备注portint22122Ntracker服务器监听端口store_lookupint2N上传文件时的分组选取方式,0-轮询;1-指定组;2-负载均衡(选取剩余空间最大的分组)store_groupstring N在store_lookup为1时生效,指定上传文件的分组名称store_serverint0N上传文件时服务器的选取方式,0-轮询;1-ip地址最小的服务器;2-优先级最高(上传优先级由storageserver来设置,参数upload_priority值最小)的服务器store_pathint0N上传文件时目录的选取方式,0-轮询;2-选择剩余空间最大的目录(storageserver可以有多个base_path)来存放文件download_serverint0N下载文件时服务器的选取方式,0-轮询;1-文件上传时指定的服务器reserved_storage_spacestring1GBN为系统和其他应用保留的硬盘空间(避免耗尽磁盘空间,单位是G M K )storage_ip_changed_auto_adjustboolean N自动适应存储服务器IP改变storage_sync_file_max_delayint86400(s)N最大文件同步延迟(默认为1天)http.server_portint8080Ntracker服务器web端口
  1. Storage服务端配置项:
配置名称值类型默认值必输备注group_namestring Y服务器所属分组tracker_serverstring YTracker服务器地址(服务器地址:端口),可以配置多行指定多个tracker服务器portint23000Nstorage服务器监听端口heart_beat_intervalint30(s)N心跳检查时间间隔stat_report_intervalint300(s)N磁盘使用信息报告时间间隔sync_wait_msecint50(ms)N读取binlog进行同步前的等待时间sync_intervalint0(ms)N同步时间间隔sync_start_timestring00:00N同步开始时间sync_end_timestring23:59N同步结束时间store_path_countint1N存储文件的目录位置store_path0string N第一个存储文件目录位置(如果store_path_count!=1,则还需根据值配置store_path1、store_path2等)subdir_count_per_pathint256N子文件夹数目限制check_file_duplicateboolean0(no)N是否检查文件重复,0(no)-不检查,1(yes)-检查key_namespacestringFastDFSN文件索引的命名空间,如果check_file_duplicate=1则必须设置keep_aliveboolean0N是否使用长连接连接FastDHT服务器,0-不使用;1-允许http.server_portint8888NStroage服务器WEB服务端口

配置步骤

1.tracker服务器:

 #创建数据目录和软链接,便于文件管理 mkdir -p /home/fastdfs/fastdfs-root/data sudo cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf sudo vi /etc/fdfs/tracker.conf

tracker.conf 推荐修改配置:

 base_path=/home/fastdfs/fastdfs-root accept_threads=4 store_path=1 download_server=1 thread_stack_size = 1MB  use_connection_pool = true
  1. storage服务器
 #创建数据目录和软链接,便于文件管理 mkdir -p /home/fastdfs/fastdfs-root/data sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf sudo vi /etc/fdfs/storage.conf

storage.conf 推荐修改配置:

group_name=group1base_path=/home/fastdfs/fastdfs-rootstore_path0=/home/fastdfs/fastdfs-roottracker_server=${TRACKER_IP}:22122#与nginx一致http.server_port=8080 
  1. fastdfs-nginx-module配置
sudo vi /etc/fdfs/mod_fastdfs.conf

修改配置项:

group_count=1[group1]group_name=group1tracker_server=${TRACKER_IP}:22122store_path0=/home/fastdfs/fastdfs-rootbase_path=/home/fastdfs/fastdfs-root#url中带有group名称url_have_group_name = true 
  1. nginx配置
#创建数据目录和软链接,便于文件管理mkdir -p /home/fastdfs/fastdfs-root/dataln -s /home/fastdfs/fastdfs-root/data/ /home/fastdfs/fastdfs-root/data/M00vi /home/fastdfs/nginx-1.8.1/nginx.conf

在server配置中修改:

Listen 8080;location /group1/M00 {  root /home/fastdfs/fastdfs-root/data;  ngx_fastdfs_module;}

启动&退出

  1. tracker
#启动tracker服务service fdfs_trackerd start ps -elf | grep fdfsnetstat -an | grep 22122
  1. nginx
##启动nginx/home/fastdfs/nginx-1.8.1/nginx 
  1. storage
##启动storage服务service fdfs_storaged startps -elf | grep fdfs##停止storage服务service fdfs_storaged stop

注册是否成功

fdfs_monitor /etc/fdfs/storage.conf

tracker 配置反向代理

#user  nobody;worker_processes  1;#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;    upstream fdfs_group1 {         server  store服务器1 :8080 weight=1 max_fails=2 fail_timeout=30s;         server store服务器2:8080 weight=1 max_fails=2 fail_timeout=30s;    }    server {    listen       8080;    server_name  localhost;    #charset koi8-r;    #access_log  logs/host.access.log  main;    location /group1/M00 {        proxy_pass http://fdfs_group1;    }        #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;    #    }    #}}

测试步骤

  1. fastdfs用户在storage服务器上执行
sudo cp /etc/fdfs/client.conf.sample /etc/fdfs/client.confsudo vi /etc/fdfs/client.conf
  1. 修改配置:
base_path=/home/fastdfs/fastdfs-root/tracker_server=${TRACKER_IP}:22122
  1. 在storage上做上传文件测试
#创建测试文件,随便输入内容vim hello.txtfdfs_test /etc/fdfs/client.conf upload hello.txt# 获取fileID, 下载 fdfs_test /etc/fdfs/client.conf download  group1 M00/00/00/CiADRlhbSaiAAGsOAAAADXj9r-s563_big.txt
  1. 删除 storage 服务器

    fdfs_monitor /etc/fdfs/client.conf delete 组名 需要删除的IP

安装fastdfs-nginx-module异常

官方FAQ

资料来源:官方论坛

FastDFS需要的编译和运行环境是怎样的?

FastDFS Server仅支持unix系统,在Linux和FreeBSD测试通过。在Solaris系统下网络通信方面有些问题。

编译需要的其他库文件有pthread,V5.0以前的版本还依赖libevent;V5.0以后,不再依赖libevent。

v5.04开始依赖libfastcommon. github地址

v5版本从v5.05开始才是稳定版本,请使用v5版本的同学尽快升级到v5.05或更新的版本,建议升级到v5.07。

pthread使用系统自带的即可。

对libevent的版本要求为1.4.x,建议使用最新的stable版本,如1.4.14b。

注意,千万不要使用libevent 2.0非stable版本。

在64位系统下,可能需要自己在/usr/lib64下创建libevent.so的符号链接。比如:

 ln -s /usr/lib/libevent.so /usr/lib64/libevent.so

启动storage server时,一直处于僵死状态。

A:启动storage server,storage将连接tracker server,如果连不上,将一直重试。直到连接成功,启动才算真正完成。

出现这样情况,请检查连接不上tracker server的原因。

友情提示:从V2.03以后,多tracker server在启动时会做时间上的检测,判断是否需要从别的tracker server同步4个系统文件。

触发时机是第一个storage server连接上tracker server后,并发起join请求。

如果集群中有2台tracker server,而其中一台tracker没有启动,可能会导致storage server一直处于僵死状态。

注:这个问题v5.07解决了。

执行fdfs_test或fdfs_test1上传文件时,服务器返回错误号2

错误号表示没有ACTIVE状态的storage server。可以执行fdfs_monitor查看服务器状态。

如何让server进程退出运行?

直接kill即可让server进程正常退出,可以使用killall命令,例如:

 killall fdfs_trackerd killall fdfs_storaged

也可以使用如下命令:

 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop

千万不要使用-9参数强杀,否则可能会导致binlog数据丢失的问题。

如何重启server进程?

直接使用:

 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

跨运营商通信异常问题

比如电信和网通机房相互通信,可能会存在异常,有两种表现:

  • 不能建立连接,这个比较直接,肯定是网络连接的问题
  • 可以正常建立连接,但接收和发送数据失败,这个问题比较隐蔽,正常网络环境下,不应该出现此类问题。

还有人碰到过从一个方向建立连接可以正常通信,但从另外一个方向就不能正常通信的情况。

解决办法:

尝试将服务端口改小,建议将端口修改为1024以下。比如将storage服务端口由23000修改为873等,也可以试试修改为8080; 如果问题还不能解决,请联系你的网络(机房)服务商。

fdfs_test和fdfs_test1是做什么用的?

这两个是FastDFS自带的测试程序,会对一个文件上传两次,分别作为主文件和从文件。返回的文件ID也是两个。

并且会上传文件附加属性,storage server上会生成4个文件。

这两个程序仅用于测试目的,请不要用作实际用途。

什么是主从文件?

主从文件是指文件ID有关联的文件,一个主文件可以对应多个从文件。

主文件ID = 主文件名 + 主文件扩展名 从文件ID = 主文件名 + 从文件后缀名 + 从文件扩展名

使用主从文件的一个典型例子:以图片为例,主文件为原始图片,从文件为该图片的一张或多张缩略图。

FastDFS中的主从文件只是在文件ID上有联系。FastDFS server端没有记录主从文件对应关系,因此删除主文件,FastDFS不会自动删除从文件。

删除主文件后,从文件的级联删除,需要由应用端来实现。

主文件及其从文件均存放到同一个group中。

主从文件的生成顺序:

  1. 先上传主文件(如原文件),得到主文件ID
  2. 然后上传从文件(如缩略图),指定主文件ID和从文件后缀名(当然还可以同时指定从文件扩展名),得到从文件ID。

如何删除无效的storage server?

可以使用fdfs_monitor来删除。命令行如下:

 /usr/bin/fdfs_monitor <config_filename> delete <group_name> <storage_id>

例如:

 /usr/bin/fdfs_monitor /etc/fdfs/client.conf delete group1 192.168.0.100

注意:如果被删除的storage server的状态是ACTIVE,也就是该storage server还在线上服务的情况下,是无法删除掉的。

storage_id参数:如果使用默认的ip方式,填写storage server IP地址,否则使用对应的server id。

FastDFS扩展模块升级到V1.06及以上版本的注意事项

apache和nginx扩展模块版本v1.06及以上版本,需要在配置文件/etc/fdfs/fastdfs_mod.conf中设置storage server的存储路径信息。

一个示例如下所示:

 store_path_count=1 store_path0=/home/yuqing/fastdfs

store_path_count和store_path#均需要正确设置,必须和storage.conf中的相应配置完全一致,否则将导致文件不能正确下载!

nginx和apache扩展模块与FastDFS server版本对应关系

扩展模块1.05: 针对FastDFs server v2.x,要求server版本大于等于v2.09

扩展模块1.07及以上版本: 针对FastDFs server v3.x

具体的版本匹配情况,参阅扩展模块源码下的HISTORY文件

上传文件失败,返回错误码28,这是怎么回事?

返回错误码28,表示磁盘空间不足。注意FastDFS中有预留空间的概念,在tracker.conf中设置,配置项为:reserved_storage_space,缺省值为4GB,即预留4GB的空间。

请酌情设置reserved_storage_space这个参数,比如可以设置为磁盘总空间的20%左右。

fdfs_trackerd或者fdfs_storaged的日志中出现:malloc task buff failed字样的错误,这是怎么回事?

出现此类信息表示已经达到最大连接数。server端支持的最大连接数可以通过max_connections这个参数来设置。

出现这样的问题,需要排查一下是否客户端使用不当导致的,比如客户端没有及时关闭无用的连接。

FastDFS的文件ID中可以反解出哪些字段?

文件ID中除了包含group name和存储路径外,文件名中可以反解出如下几个字段: 1. 文件创建时间(unix时间戳,32位整数) 2. 文件大小 3. 上传到的源storage server IP地址(32位整数) 4. 文件crc32校验码 5. 随机数(这个字段用来避免文件重名)

为什么生成的token验证无法通过?

出现这样的问题,请进行如下两项检查:

  1. 确认调用token生成函数,传递的文件ID中没有包含groupname。传递的文件ID格式形如:M00/00/1B/wKgnVE84utyOG9hEAAATz5-S0SI99.java
  2. 确认服务器时间基本是一致的,注意服务器时间不能相差太多,不要相差到分钟级别。
0 0
原创粉丝点击