centos7.2 FastDFS分布式文件系统单机搭建

本人这两天经历了FastDFS分布式文件系统的配置折磨,随将这个坑写下来,其实也很简单。
先来看看FastDFS的介绍,看看你是不是需要这个服务。

简单了解一下FastDFS分布式文件系统
本人的系统操作环境 centos 7.2 64位
截止2018/02这是一个最新版最稳定的教程,因为该项目2017/05 已经没有再更新了(也不知道会不会继续更新),版本的差异会使得安装配置出错,请及时检查github地址上的版本
这是余庆老师的github地址

本篇是单机配置教程,但已经是完整的FastDFS配置,与分布式配置的差异在于增加不同的组和更多的服务器。(如果你了解过fastdfs你应该知道 分组 的概念)如果对于linux不熟悉,请细心操作(面向百度编程)

一、安装 libfastcommon 工具包

首先,所有本篇的安装地址都在 /usr/local/下进行(本篇考虑Linux不熟的同学,如果你对Linux熟悉,安装路径随意)

[root@centos ~]# cd /usr/local/

下载 libfastcommon

[root@centos local]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.36.tar.gz

解压文件
[root@centos local]tar -zxvf V1.0.7.tar.gz
进入目录 libfastcommon-1.0.36
[root@centos local]# cd libfastcommon-1.0.36/
执行编译
[root@centos libfastcommon-1.0.36]# ./make.sh
安装
[root@centos libfastcommon-1.0.36]# ./make.sh install
这里告一段落(以上一般不会出现什么错误)。

二、安装FastDFS

让我们回到/usr/local/
[root@centos libfastcommon-1.0.36]# cd ..
接着下载FastDFS,截至目前(2018/02)5.11依然是最新版
[root@centos local]# wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
然后解压
[root@centos local]#tar -zxvf V5.11.tar.gz
进入目录fastfds-5.11
[root@centos local]#cd fastdfs-5.11
编译
[root@centos fastdfs-5.11]#./make.sh
安装
[root@centos fastdfs-5.11]#./make.sh install
进入conf/目录
[root@centos fastdfs-5.11]#cd conf/
将下面三个文件复制到 /etc/fdfs/目录下
[root@centos fastdfs-5.11]#cp <font color="#ff00ff;">anti-steal.jpg</font> <font color="#339966;">http.conf</font> <font color="#ff6600;">mime.types</font> /etc/fdfs/
这里再告一段落(以上一般不会出现什么错误)。

三、配置Tracker服务

上述安装成功后,在/etc/目录下会有一个fdfs的目录,进入它。会看到三个.sample后缀的文件,这是作者给我们的示例文件,我们需要把其中的tracker.conf.sample文件改为tracker.conf配置文件并修改它。(如果没有,可以从/usr/local/fastdfs-5.11/conf/文件夹下复制过去,指令 cp /usr/local/fastdfs-5.11/conf/filename /etc/fdfs/)
进入/etc/fdfs/目录
[root@centos fastdfs-5.11]#cd /etc/fdfs/
复制tracker.conf.sample文件
[root@centos fdfs]#cp tracker.conf.sample tracker.conf
编辑 tracker.conf文件
[root@centos fdfs]#vim tracker.conf
需要修改的有两处,请自行查找,这个配置文件很长

1
2
3
4
5
# the base path to store data and log files
base_path=/data/fastdfs

# HTTP port on this tracker server
http.server_port=8080

首先你要已经存在/data/fastdfs目录,没有的话请创建(这是一个绝对路径,我的路径是缺省了/home的),其余不建议修改,port=22122这个端口参数,除非你已经占用它了(按insert 或者 i 键进入编辑状态,按下esc键,再按下shift + ;键,输入wq保存退出)
防止有同学不清楚linux路径,这里给出创建/data/fastdfs的命令:↓

首先退回根目录

[root@centos fdfs]#cd /
创建文件
[root@centos /]#mkdir /data/fastdfs
还有就是查看端口占用的命令是<lsof -i加上端口号也可以不加,如查看80端口[root@centos /]#lsof -i:80>
或者是使用 netstat -anp|grep 80
现在,我们可以使用/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start来启动Tracker服务,也可以使用ln -s 建立软链接:(进行下面三个操作,此时没有提示是最好的提示,如果错误请检查命令和路径正确性)

1
2
3
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来启动服务了,当然你也可以通过netstat -unltp|grep fdfs来查看一下端口是否在监听。如果正常启动,就下显示:

1
2
[root@centos ~]# netstat -unltp|grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 17672/fdfs_trackerd

四、配置 Storage 服务

现在开始配置 Storage 服务,由于我这是单机器测试,你把 Storage 服务放在多台服务器也是可以的,它有 Group(组)的概念,同一组内服务器互备同步,这里不再演示。直接开始配置,依然是进入/etc/fdfs的目录操作,进入后。会看到三个.sample后缀的文件,我们需要把其中的storage.conf.sample文件改为storage.conf配置文件并修改它。
进入/etc/fdfs/目录
[root@centos /]#cd /etc/fdfs/
复制storage.conf.sample
[root@centos fdfs]#cp storage.conf.sample storage.conf
打开storage.conf文件后,找到以下参数进行修改:
[root@centos fdfs]#vim storage.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
# the base path to store data and log files
base_path=/data/fastdfs/storage

# store_path#, based 0, if store_path0 not exists, it&#039;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
# &quot;host:port&quot;, host can be hostname or ip address
tracker_server=192.168.1.111:22122
# 192.168.1.111是你的服务器公网IP!!!

同上 /data/fastdfs/storage 必须是已经存在的,没有的记得创建。不然会报错的,日志以及文件都会在这个下面,启动时候会自动生成许多文件夹。stroage的 port=23000 这个端口参数也不建议修改,默认就好,除非你已经占用它了。(同样,这里修改完后保存退出)
接着,建立软连接
[root@centos fdfs]#ln -s /usr/bin/fdfs_storaged /usr/local/bin
执行命令启动服务:
[root@centos fdfs]#service fdfs_storaged start
查看监听:
[root@centos fdfs]#netstat -unltp|grep fdfs
看到以下显示就是成功了:

1
2
3
[root@centos ~]# netstat -unltp|grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 17672/fdfs_trackerd
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 10475/fdfs_storaged

接着我们可以看看Tracker 和 Storage 服务是不是在通信,使用命令:
[root@centos ~]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf

FastDFS_5.11+Nginx_1.12.2 安装配置

出现图中的 ACTIVE 就 O J B K!

接着,继续配置 client.conf,让我们回到/etc/fdfs/
[root@centos fdfs]#cp client.conf.sample client.conf
[root@centos fdfs]#vim client.conf
自行查找,并修改以下参数,后保存

1
2
3
4
5
6
7
# the base path to store log files
base_path=/data/fastdfs
# 此处与Tracker服务的路径一致
# tracker_server can ocur more than once, and tracker_server format is
# &quot;host:port&quot;, host can be hostname or ip address
tracker_server=192.168.1.111:22122
# 192.168.1.111改为你的服务器公网IP

五、安装Nginx 和 fastdfs-nginx-module插件

这里的Nginx安装不讲,网上琳琅满目,直接百度 关键词 centos nginx安装配置 同样,nginx我也是安装在/usr/local/目录下
下载 fastdfs-nginx-module插件
[root@centos local]#wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip
解压文件,注意这里是zip文件,不要使用 tar 解压
[root@centos local]#unzip master.zip
OK,这样就可以了,现在进入Nginx的安装目录(这里我的Nginx是1.12.2版,请区别Nginx目录和Nginx-1.12.2目录)
[root@centos local]#cd nginx-1.12-2/
配置 nginx 安装,加入fastdfs-nginx-module模块
[root@centos nginx-1.12-2]# ./configure --add-module=../fastdfs-nginx-module-master/src/
上面是一个相对路径 (../fastdfs-nginx-module-master/src/)
因为此时的nginx-1.12.2和fastdfs-nginx-module-master是同级目录,
你也可以填写绝对路径,这里相对于教程 来说的绝对路径 是 /usr/local/fastdfs-nginx-module-master/src/

重新编译
[root@centos nginx-1.12-2]#make

重新安装

[root@centos nginx-1.12-2]#make install
如果安装成功可以看到下图(这里借用了其他老博客的图,目前为止,本人踩坑见过的 最详细最新的其他博客是2016/11发布的,时间关系导致教程版本差异是安装配置FastDFS的常见错误,也是这篇教程出世的原因之一)
FastDFS_5.11+Nginx_1.12.2 安装配置
现在执行 /usr/local/nginx/sbin/nginx -V 命令即可看到加入的模块 ,失败的话是看不到红框的内容的
FastDFS_5.11+Nginx_1.12.2 安装配置

如果执行失败……,首先默哀三分钟,然后来看看你是不是和我一样的错误。
1、上上步骤中的编译安装
FastDFS_5.11+Nginx_1.12.2 安装配置
出现以下错误提示,有以下可能。
①模块和FastDFS版本不对(这是小几率发生的,因为本博客教程中全部是最新版本!截止2018/02:-)
②模块配置错误,执行以下命令

1
2
[root@centos ~]# cd /usr/local/fastdfs-nginx-module-master/src/
[root@centos src]# vim config

FastDFS_5.11+Nginx_1.12.2 安装配置
查看你配置文件中 红框的配置 与 我的差异,多的删除,少的补上,特别是多出的 local/
③其他错误,萌新请将 Error 复制到百度中查询,(若是校工作室同学,2018-2019年期间可以请教大刀,我就是大刀帮解决的……2333 || 也可以留言下方讨论,没错是讨论,我linux不熟 : -)
如以上的错误的百度连接(点击此处)。
④如下所示

1
2
3
4
5
6
sed -e &quot;s|%%PREFIX%%|/usr/local/nginx|&quot; \
-e &quot;s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|&quot; \
-e &quot;s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|&quot; \
-e &quot;s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|&quot; \
&lt; man/nginx.8 &gt; objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.10.3&#039;

实际上我在出现上述情况后去查看了 [root@centos nginx-1.12-2]cd /usr/local/nginx/logs/error.log 的日志文件
如果日志和下图类似,则无视,继续安装 执行make install即可。
FastDFS_5.11+Nginx_1.12.2 安装配置

六、配置 fastdfs-nginx-module 和 Nginx

首先进入目录/fastdfs-nginx-module-master/src/
[root@centos /]# cd /usr/local/fastdfs-nginx-module-master/src/
修改模块中的配置
[root@centos src]# vim mod_fastdfs.conf
需要修改的有三处,请仔细查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# FastDFS tracker_server can ocur more than once, and tracker_server format is
# &quot;host:port&quot;, host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=111.11.11.111:22122
# 111.11.11.111是你服务器的公网IP !!

# 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&#039;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

七、配置 Nginx

进入/nginx/conf目录 (一段废话:很多时候,教程的使用的都是绝对路径,如果你(萌新)不清楚操作到什么步骤,也请用绝对路径切换各个文件夹解决问题,避免不懂路径问题 和 不熟悉linux文件路径造成问题,及时的使用tab键补全 和 指令tab ls 来查看是不是自己要去的文件夹,因为可能存在很多同名不同级的文件夹)
[root@centos src]#cd /usr/local/nginx/conf
编辑nginx配置文件(记得保存)
[root@centos src]#vim nginx.conf
找到server{ ……} 在其中添加以下内容

1
2
3
4
location /group1/M00 {
root /data/fastdfs/storage/;
ngx_fastdfs_module;
}

FastDFS_5.11+Nginx_1.12.2 安装配置
由于我们配置了group1/M00的访问,我们需要建立一个group1文件夹,并建立M00到data的软链接。
[root@centos src]#mkdir /data/fastdfs/storage/data/group1 [root@centos src]#ln -s /data/fastdfs/storage/data /data/fastdfs/storage/data/group1/M00
现在,重启或者启动你的 nginx,启动 Nginx ,会打印出fastdfs模块的pid,看看日志是否报错,正常不会报错的。(如果是端口占用,自行从上面的配置文件改,默认Tracker服务22122,Storage 服务23000,Nginx 80)
打开浏览器,访问呢你服务器的80端口(默认是80端口),成功如下图:
FastDFS_5.11+Nginx_1.12.2 安装配置
现在,让我们来测试一下,文件上传。
依然在linux系统中操作,输入一下命令:/usr/bin/fdfs_test /etc/fdfs/client.conf upload加上你要上传的文件
例如
[root@centos ~]# /usr/bin/fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/anti-steal.jpg
上传成功后你会得到一个网址
FastDFS_5.11+Nginx_1.12.2 安装配置
复制这网址,打开浏览器见证奇迹!
FastDFS_5.11+Nginx_1.12.2 安装配置
至此,结束。顺便说一下,你接下来要考虑的问题
①文件重复上传问题->fastdfs作者提供了类似重复上传模块的插件(每一个文件都有一个MD5)
②如何使用这个文件服务器->官网提供了FastDFS的jar,具体操作请参见API文档
最后,补充一句,以上的步骤都是靠经验复现出来的(并不是在服务器上重新装一遍),如果我的操作有误,请留言告知。

zcolder wechat
写得不好?加我QQ开始喷我!