转http://www.cnblogs.com/jym-sunshine/p/6397470.html

FastDFS安装全过程记录

1、安装准备

HA虚拟IP:192.168.1.208

HA软件:Keepalived

操作系统:CentOS 7

用户:root

数据目录:/data/fastdfs

安装包:

fastdfs-master-V5.05.zip:FastDFS源码

libfastcommon-master.zip:(从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库)

fastdfs-nginx-module-master.zip:storage节点http服务nginx模块

nginx-1.10.0.tar.gz:Nginx安装包

ngx_cache_purge-2.3.tar.gz:Nginx图片缓存清除模块

获取安装包的方式:

1> 从这里下载打包好的所有安装包:http://download.csdn.net/detail/xyang81/9667493

2> 从作者github官网挨个下载fastdfs源码及其依赖库:https://github.com/happyfish100 和 Nginx缓存清除模块:https://github.com/FRiCKLE/ngx_cache_purge
开始前,先将所有安装包下载到各个节点的/usr/local/src目录中。

1> 本文称节点IP最后一段就代表某个节点,如:192.168.1.206,文中提到206节点,就代表192.168.1.206。

2> 本文称tracker或跟踪服务器是同一个意思

3> 本文称storage或存储服务器是同一个意思

2、开始安装

1.安装所需依赖包

shell> yum install make cmake gcc gcc-c++

2.安装 FastDFS三步走

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# ls
fastdfs-master-V5.05.zip
fastdfs-nginx-module-master.zip
libfastcommon-master.zip
nginx-1.10.0.tar.gz
ngx_cache_purge-2.3.tar.gz
openssl-1.0.1t

2.1 安装 libfastcommon

解压 libfastcommon,命令:

[root@localhost src]# unzip libfastcommon-master.zip
[root@localhost src]# cd libfastcommon-master
[root@localhost libfastcommon-master]# ./make.sh
[root@localhost libfastcommon-master]# ./make.sh install

执行以上4步,安装完毕,即完成第一步。

2.2 安装 FastDFS

解压 libfastcommon,命令:

[root@localhost libfastcommon-master]# cd ..
[root@localhost src]# ls
fastdfs-master-V5.05.zip openssl-1.0.1t.tar.gz
fastdfs-nginx-module-master.zip pcre-8.39
libfastcommon-master pcre-8.39.tar.gz
libfastcommon-master.zip zlib-1.2.8
nginx-1.10.0.tar.gz zlib-1.2.8.tar.gz
ngx_cache_purge-2.3.tar.gz zlib-1.2.8.tar.gz.1
openssl-1.0.1t
[root@localhost src]# unzip fastdfs-master-V5.05.zip
[root@localhost src]# cd fastdfs-master-V5.05
-bash: cd: fastdfs-master-V5.05: 没有那个文件或目录
[root@localhost src]# ls
fastdfs-master openssl-1.0.1t
fastdfs-master-V5.05.zip openssl-1.0.1t.tar.gz
fastdfs-nginx-module-master.zip pcre-8.39
libfastcommon-master pcre-8.39.tar.gz
libfastcommon-master.zip zlib-1.2.8
nginx-1.10.0.tar.gz zlib-1.2.8.tar.gz
ngx_cache_purge-2.3.tar.gz zlib-1.2.8.tar.gz.1
[root@localhost src]# cd fastdfs-master
[root@localhost fastdfs-master]# ./make.sh
[root@localhost fastdfs-master]# ./make.sh install

执行以上几步,
显示:

[root@localhost fastdfs-master]# ./make.sh install
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O -DDEBUG_FLAG -c -o ../common/fdfs_global.o ../common/fdfs_global.c -I../ common -I/usr/include/fastcommon
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O -DDEBUG_FLAG -c -o tracker_proto.o tracker_proto.c -I../common -I/usr/ include/fastcommon
...
tracker_client.h storage_client.h storage_client1.h client_func.h client_global.h fdfs_client.h /usr/include/fastdfs
if [ ! -f /etc/fdfs/client.conf.sample ]; then cp -f ../conf/ client.conf /etc/fdfs/client.conf.sample; fi

即安装完毕!

3 配置 Tracker 服务

上述安装成功后,在/etc/目录下会有一个fdfs的目录,进入它。会看到三个.sample后缀的文件,这是作者给我们的示例文件,我们需要把其中的tracker.conf.sample文件改为tracker.conf配置文件并修改它。看命令:

[root@localhost fastdfs-master]# cd /etc/fdfs
[root@localhost fdfs]# ls
client.conf.sample storage.conf.sample tracker.conf.sample
---
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf
[root@localhost fdfs]# vim tracker.conf

打开tracker.conf文件,只需要找到你只需要该这两个参数就可以了。

# the base path to store data and log files
base_path=/data/fastdfs
# HTTP port on this tracker server
http.server_port=80

当然前提是你要有或先创建了/data/fastdfs目录。port=22122这个端口参数不建议修改,除非你已经占用它了。
修改完成保存并退出 vim ,这时候我们可以使用/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start来启动 Tracker服务,但是这个命令不够优雅,怎么做呢?使用ln -s 建立软链接:

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服务了,是不是比刚才带目录的命令好记太多了(懒是社会生产力)。你也可以启动过服务看一下端口是否在监听,命令:

启动服务:service fdfs_trackerd start
查看监听:netstat -unltp|grep fdfs

启动命令如下:

[root@localhost fdfs]# mkdir /data
[root@localhost fdfs]# mkdir /data/fastdfs
[root@localhost fdfs]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start

看到22122端口正常被监听后,这时候就算 Tracker服务安装成功啦!

[root@localhost fdfs]# netstat -unltp | grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:*
LISTEN 5895/fdfs_trackerd

4、配置 Storage 服务

现在开始配置 Storage 服务,由于我这是单机器测试,你把 Storage 服务放在多台服务器也是可以的,它有 Group(组)的概念,同一组内服务器互备同步,这里不再演示。直接开始配置,依然是进入/etc/fdfs的目录操作,首先进入它。会看到三个.sample后缀的文件,我们需要把其中的storage.conf.sample文件改为storage.conf配置文件并修改它。还看命令:

cp storage.conf.sample storage.conf
vim storage.conf

指令操作:

[root@localhost fdfs]# cd /etc/fdfs
[root@localhost fdfs]# cp storage.conf.sample storage.conf
[root@localhost fdfs]# vim storage.conf

打开storage.conf文件后,找到这两个参数进行修改:

# the base path to store data and log files
base_path=/data/fastdfs/storage
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/data/fastdfs/storage
#store_path1=/home/yuqing/fastdfs2
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.198.129:22122

当然你的/data/fastdfs目录下要有storage文件夹,没有就创建一个,不然会报错的,日志以及文件都会在这个下面,启动时候会自动生成许多文件夹。stroage的port=23000这个端口参数也不建议修改,默认就好,除非你已经占用它了。
修改完成保存并退出 vim ,这时候我们依然想优雅地启动 Storage服务,带目录的命令不够优雅,这里还是使用ln -s 建立软链接:

ln -s /usr/bin/fdfs_storaged /usr/local/bin

执行命令启动服务:

service fdfs_storaged start

出现了一个大大的error啦!!!要仔细看,错误提示是找不到文件夹,这就好办了嘛。创建一个文件夹再次启动看看。

[root@localhost fdfs]# /usr/bin/fdfs_storaged start
[2017-02-08 14:48:00] ERROR - file: shared_func.c, line: 968, /etc/fdfs/start is not a regular file
[2017-02-08 14:48:00] ERROR - file: process_ctrl.c, line: 230, load conf file "start" fail, ret code: 22

这次启动成功,没有错误了。查看一下监听:

netstat -unltp|grep fdfs

监听如下:

[root@localhost fdfs]# netstat -unltp|grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 5895/fdfs_trackerd
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 6001/fdfs_storaged

很好,22122 和 23000端口都在监听了,这个时候你去/data/fastdfs/storage文件夹下看的话,会出现一大堆文件夹,而且进去还有一大堆,哈哈,这就是存放文件的啦!

5服务监听测试

我们安装配置并启动了 Tracker 和 Storage 服务,也没有报错了。那他俩是不是在通信呢?我们可以监视一下:

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

看到我横线处ACTIVE这样就 ok 啦!

    [root@localhost fdfs]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2017-02-08 14:51:16] 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=600s, use_storage_id=0, storage server id count: 0 server_count=1, server_index=0 tracker server is 192.168.160.130:22122 group count: 1 Group 1:
group name = group1
disk total space = 17878 MB
disk free space = 10198 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.160.130
ip_addr = 192.168.160.130 (localhost.localdomain) ACTIVE
http domain =
version = 5.08
join time = 2017-02-08 14:49:53
up time = 2017-02-08 14:49:53

其实这个时候你就可以进行上传测试了,但可能会下载不了,

6 安装 Nginx 和 fastdfs-nginx-module

解压 fastdfs-nginx-module ,记着这时候别用tar解压了,因为是 .zip 文件,正确命令:

unzip master.zip

1)配置 nginx 安装,加入fastdfs-nginx-module模块

这是和普通 Nginx 安装不一样的地方,因为加载了模块。
若nginx未安装,根据相关教程进行安装,若已安装,查看已安装的nginx的版本,找到源码的存放位置,然后配置fastdfs-nginx-module模块

[root@localhost src]# cd /home/roo/下载/nginx-1.11.6
[root@localhost nginx-1.11.6]# ./configure --add-module=/usr/local/src/fastdfs-nginx-module-master/src/
checking for OS
+ Linux 3.10.0-327.36.3.el7.x86_64 x86_64
checking for C compiler ... found
[root@localhost nginx-1.11.6]# make && make install

这时候,我们可以看一下 Nginx 下安装成功的版本及模块,命令:

/usr/local/nginx/sbin/nginx -V

配置 fastdfs-nginx-module 和 Nginx

1.配置mod-fastdfs.conf,并拷贝到/etc/fdfs文件目录下。

cd /usr/local/src/fastdfs-nginx-module-master/src/
vim mod_fastdfs.conf
cp mod_fastdfs.conf /etc/fdfs

修改mod-fastdfs.conf配置只需要修改我标注的这三个地方就行了,其他不需要也不建议改变。

# FastDFS tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=192.168.198.129:22122
# if the url / uri including the group name
# set to false when uri like /M00/00/00/xxx
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/data/fastdfs/storage
#store_path1=/home/yuqing/fastdfs1 接着我们需要把fastdfs-5.05下面的配置中还没有存在/etc/fdfs中的拷贝进去 cd /usr/local/src/fastdfs-5.05/conf
cp anti-steal.jpg http.conf mime.types /etc/fdfs/

2.配置 Nginx。编辑nginx.conf文件:

cd /usr/local/nginx/conf
vi nginx.conf

在配置文件中加入:

location /group1/M00 {
root /data/fastdfs/storage/;
ngx_fastdfs_module;
}

由于我们配置了group1/M00的访问,我们需要建立一个group1文件夹,并建立M00到data的软链接。

mkdir /data/fastdfs/storage/data/group1
ln -s /data/fastdfs/storage/data /data/fastdfs/storage/data/group1/M00

启动 Nginx ,会打印出fastdfs模块的pid,看看日志是否报错,正常不会报错的

/usr/local/nginx/sbin/nginx

打开浏览器,访问一下发现并不能访问,也并没有报错,但显示如下画面。糟糕了,怎么办?对了,我好像没关闭防火墙。

开放80端口访问权限。在iptables中加入重启就行,或者你直接关闭防火墙,本地测试环境可以这么干,但到线上万万不能关闭防火墙的。

vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启防火墙,使设置生效:

service iptables restart

再次刷新浏览器,可以看到如下画面,说明我们 Nginx 结合 fastdfs-nginx-module 模块安装并配置成功啦!

我最后说一下怎么在已经安装过 Nginx 的服务器上安装配置 fastdfs-nginx-module 模块? 因为,一般我们线上服务器都是已经安装过 Nginx 的,所以这个时候,我们就直接进入 Nginx 的存放目录,进行配置后编译,就不需要执行最后安装make install这一步了,接着重启就行了。

上传测试

完成上面的步骤后,我们已经安装配置完成了全部工作,接下来就是测试了。因为执行文件全部在/usr/bin目录下,我们切换到这里,并新建一个test.txt文件,随便写一点什么,我写了This is a test file. by:mafly这句话在里边。然后测试上传:

cd /usr/bin
vim test.txt
fdfs_test /etc/fdfs/client.conf upload test.txt

很不幸,并没有成功,报错了。

ERROR - file: shared_func.c, line: 960, open file /etc/fdfs/client.conf fail, errno: 2, error info: No such file or directory
ERROR - file: ../client/client_func.c, line: 402, load conf file "/etc/fdfs/client.conf" fail, ret code: 2

一般什么事情第一次都不是很顺利,这很正常,通过错误提示我看到,好像没有找到client.conf这个文件,现在想起来的确没有配置这个文件,那我们现在去配置一下图中的两个参数:

cd /etc/fdfs
cp client.conf.sample client.conf
vim client.conf

怎么还依然报错阿???

upload file fail, error no: 2, error info: No such file or directory

哈哈,你是不是测试上传命令中要上传的test.txt文件路径有问题,嗯,那我改一下命令:

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /usr/bin/test.txt

成功啦!!! 返回文件信息及上传后的文件 HTTP 地址,你打开浏览器访问一下试试

7.总结一下

接下来其实还有更多关于文件的工作,比如防盗链、图片切图、视频处理等等。

1.防盗链的常用方案

>1.加token验证
>2.将FastDFS返回的地址,转化为该地址的映射,通过中间件来进行转换访问

2.关于Storage集群分组存储方式

1.Storage server会连接集群中所有的Tracker server,定时向他们报告自己的状态,包括磁盘剩余空间、文件同步状况、文件上传下载次数等统计信息。

2.storage集群由一个或多个组构成,集群存储总容量为集群中所有组的存储容量之和。一个组由一台或多台存储服务器组成,组内的Storage server之间是平等关系,不同组的Storage server之间不会相互通信,同组内的Storage server之间会相互连接进行文件同步,从而保证同组内每个storage上的文件完全一致的。一个组的存储容量为该组内存储服务器容量最小的那个,由此可见组内存储服务器的软硬件配置最好是一致的。采用分组存储方式的好处是灵活、可控性较强。比如上传文件时,可以由客户端直接指定上传到的组也可以由tracker进行调度选择。一个分组的存储服务器访问压力较大时,可以在该组增加存储服务器来扩充服务能力(纵向扩容)。当系统容量不足时,可以增加组来扩充存储容量(横向扩容)。

3.关于上传的文件备注信息NameValuePair(源文件细息)的存储

原文件信息的存储:

>1.在上传文件时,写入FastDfs中的该文件NameValuePair[]中,但此方式只能根据文件查看文件的备注信息,FastDfs没有提供逆向查询的方式;
>2.在关系型数据库创建一张表,用于存储该group下的所有文件信息,此方式可以解决第一种方式的逆向查询问题,同时可以存储FastDfs的文件信息;

8.FastDfs常见异常汇总

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

返回错误码28,表示磁盘空间不足。注意FastDFS中有
预留空间的概念,在tracker.conf中设置,配置项为
:reserved_storage_space,缺省值为4GB,即预留
4GB的空间。请酌情设置reserved_storage_space这个
参数,比如可以设置为磁盘总空间的20%左右。

2. nginx扩展模块,不能正常显示图片的问题

在配置文件/etc/fdfs/mod_fastdfs.conf中,缺省的
设置是这样的:http.need_find_content_type=false
这个参数在nginx中需要设置为true,apache中应该设置为false

3. 如何删除无效的storage server?

可以使用fdfs_monitor来删除。命令行如下:
/usr/local/bin/fdfs_monitor delete
例如:
/usr/local/bin/fdfs_monitor
/etc/fdfs/client.conf delete group1
192.168.0.100
注意:如果被删除的storage server的状态是ACTIVE
,也就是该storage server还在线上服务的情况下,
是无法删除掉的。

4. 执行fdfs_test或fdfs_test1上传文件时,服务器

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

若转载请注明出处!若有疑问,请回复交流!

fast-dfs的更多相关文章

  1. spring boot集成FastDFS

    官方文档:https://github.com/happyfish100/fastdfs-client-java 一.首先,maven工程添加依赖 <!--fastdfs--> <d ...

  2. (转)Spring Boot(十八):使用 Spring Boot 集成 FastDFS

    http://www.ityouknow.com/springboot/2018/01/16/spring-boot-fastdfs.html 上篇文章介绍了如何使用 Spring Boot 上传文件 ...

  3. Spring Boot(十八):使用 Spring Boot 集成 FastDFS

    上篇文章介绍了如何使用 Spring Boot 上传文件,这篇文章我们介绍如何使用 Spring Boot 将文件上传到分布式文件系统 FastDFS 中. 这个项目会在上一个项目的基础上进行构建. ...

  4. Spring Boot(十八):使用Spring Boot集成FastDFS

    Spring Boot(十八):使用Spring Boot集成FastDFS 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 功能:使用Spring Boot将文 ...

  5. spring boot(十八)集成FastDFS文件上传下载

    上篇文章介绍了如何使用Spring Boot上传文件,这篇文章我们介绍如何使用Spring Boot将文件上传到分布式文件系统FastDFS中. 这个项目会在上一个项目的基础上进行构建. 1.pom包 ...

  6. django-dailyfresh

    Hold on ,learn by myself! redis nosql - 不支持sql语法 - 存储数据都是KV形式 - Mongodb - Redis - Hbase hadoop - Cas ...

  7. P1054 等价表达式

    题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数 ...

  8. 使用Spring Boot集成FastDFS

    原文:http://www.cnblogs.com/ityouknow/p/8298358.html#3893468 上篇文章介绍了如何使用Spring Boot上传文件,这篇文章我们介绍如何使用Sp ...

  9. SpringBoot2.0 整合 FastDFS 中间件,实现文件分布式管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.FastDFS简介 1.FastDFS作用 FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步 ...

  10. django-自定义文件上传存储类

    文件储存API:https://yiyibooks.cn/xx/django_182/ref/files/storage.html 编写自定义存储系统:https://yiyibooks.cn/xx/ ...

随机推荐

  1. zookeeper和Kafka集群安装配置

    3个虚拟机,首先关闭防火墙,在进行下面操作 一.java环境 yum list java* yum -y install java-1.8.0-openjdk* 查看Java版本 Java -vers ...

  2. 你以为在用SharePoint但其实不是

    博客地址 http://blog.csdn.net/foxdave 原文链接:http://www.techrepublic.com/blog/tech-decision-maker/you-thin ...

  3. react native 获取 软键盘高度 和 新增软键盘的组件

    import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Keyboard, Te ...

  4. How to convert a QString to unicode object in python 2?

    How to convert a QString to unicode object in python 2? I had this problem to solve, and I tried to ...

  5. jQuery因mouseover,mouseout冒泡产生的闪烁问题

    由于浏览器的冒泡行为.造成如果在一个DIV元素上同时定义了mouseover,mouseout的时候,当鼠标移动到DIV中的child子元素的时候,就会同时执行了两个操作mouseover和mouse ...

  6. 如何重启 Windows 10 子系统(WSL) ubuntu

    如何重启 Windows 10 子系统(WSL) ubuntu WSL 子系统是基于 LxssManager 服务运行的. 只需要将 LxssManager 重启即可. 可以做成一个 bat 文件. ...

  7. js setInterval每隔一段时间执行一次

    js setInterval每隔一段时间执行一次setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval() 方法会不停地调用函数,直到 clearI ...

  8. HBase,region以及HFile概念

    什么是HBase的Region? 大家一定对一个词不陌生:域分区,这个域就是Region:Region定义为key的一个取值范围的子集的数据载体:比如常见的域分区有固定大小分区,比如1-10一个reg ...

  9. c# 爬虫(二) 模拟登录

    有了上一篇的介绍,这次我们来说说模拟登录,上一篇见 :c# 爬虫(一) HELLO WORLD 原理 我们知道,一般需要登录的网站,服务器和客户端都会有一段时间的会话保持,而这个会话保持是在登录时候建 ...

  10. 《快学Scala》

    Robert Peng's Blog - https://mr-dai.github.io/ <快学Scala>Intro与第1章 - https://mr-dai.github.io/S ...