Fork me on GitHub

CentOS 7 安裝FastDFS V6.0.3

目录

什么是FastDFS?

FastDFS是一个开源的分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

FastDFS是一个应用级分布式文件存储服务,其采用中心型结构(类似GFS、HDFS、TFS等),主要用于大中型网站存储资源文件。FastDFS具有轻量级,支持高并发放访问,负载均衡,可扩展等优点。而FastDFS最大的亮点就是对小文件的存储性能较好,这主要来自于其文件名策略。

1.小文件存储性能优化

小文件的性能瓶颈主要来自于对元数据服务器(如FastDFS中的TrackerServer或TFS中的NameServer)的访问,因为当文件本身大小很小时,元数据存储所占空间与文件内容存储所占空间的比例就变得较大,访问元数据所消耗资源与访问文件内容所消耗资源的比例也变得较大。因此,通常对小文件存储的优化方法主要有两大类思路:一是减少访问元数据的次数,比如Cache预取;二是减少元数据所占的存储空间,比如FastDFS使用的文件名策略。

2. FastDFS文件名策略

FastDFS中的文件名是在向StorageServer存储文件时由系统指定的,文件名中包含了VolumeID和FileID。也就是说,当客户要读取某个文件时,通过在客户端对文件名进行解析,就可以知道该文件存储在哪个Volume上和它在StorageServer中的FileID。但是此时用户还不能读取文件,因为他不知道Volume内各个StorageServer的ip地址,也不知道应该从Volume内的哪个StorageServer中读取。所以用户需手持欲访问的文件的VolumeID向TrackerServer询问,TrackerServe会均衡当前各StorageServer的IO负载状况,返回一个最佳的StorageServer的ip地址。最后用户与该StorageServer连接,出示欲访问文件的FileID,StorageServer上会维持一个FileID对应偏移量的表,从而得到欲访问文件的偏移量。

可见,FastDFS的文件名策略将文件存储位置信息隐含在文件名中,从而减少了元数据量,达到了优化小文件存储性能的作用。

CentOS 7 安裝FastDFS

FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。

上传交互过程编辑

  1. client询问tracker上传到的storage,不需要附加参数;

  2. tracker返回一台可用的storage;

  3. client直接和storage通讯完成文件上传。

FastDFS file download

下载交互过程编辑

  1. client询问tracker下载文件的storage,参数为文件标识(卷名和文件名);

  2. tracker返回一台可用的storage;

  3. client直接和storage通讯完成文件下载。

需要说明的是,client为使用FastDFS服务的调用方,client也应该是一台服务器,它对tracker和storage的调用均为服务器间的调用。

存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的key为width,value为1024。文件meta data是文件属性列表,可以包含多个键值对。

FastDFS系统结构如下图所示:

CentOS 7 安裝FastDFS

准备好四个文件

CentOS 7 安裝FastDFS

下载地址:

https://github.com/happyfish100

CentOS 7 FastDFS搭建

安装libfastcommon

上传到服务器,解压:

1
[root@localhost home]# unzip libfastcommon-master.zip

进入libfastcommon-1.0.36目录:

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost home]# cd libfastcommon-master/
[root@localhost libfastcommon-master]# ll
total 32
drwxr-xr-x. 2 root root 114 Dec 6 11:49 doc
-rw-r--r--. 1 root root 10179 Dec 6 11:49 HISTORY
-rw-r--r--. 1 root root 674 Dec 6 11:49 INSTALL
-rw-r--r--. 1 root root 1607 Dec 6 11:49 libfastcommon.spec
-rwxr-xr-x. 1 root root 3253 Dec 6 11:49 make.sh
drwxr-xr-x. 2 root root 191 Dec 6 11:49 php-fastcommon
-rw-r--r--. 1 root root 2776 Dec 6 11:49 README
drwxr-xr-x. 3 root root 4096 Dec 6 11:49 src
[root@localhost libfastcommon-master]#

用yum安装gcc:

Yum命令相当好用,是RedHad和CentOS从指定服务器下载RPM包并自动安装。我个人比较喜欢。

1
[root@localhost libfastcommon-master]# yum -y install gcc-c++

Complete! 执行完成了!

这个时候分别执行./make.sh和./make.sh install,正常情况是可以成功的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[root@localhost libfastcommon-master]# ./make.sh
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o hash.o hash.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o chain.o chain.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o shared_func.o shared_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o ini_file_reader.o ini_file_reader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o logger.o logger.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o sockopt.o sockopt.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o base64.o base64.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o sched_thread.o sched_thread.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o http_func.o http_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o md5.o md5.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o pthread_func.o pthread_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o local_ip_func.o local_ip_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o avl_tree.o avl_tree.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o ioevent.o ioevent.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o ioevent_loop.o ioevent_loop.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_task_queue.o fast_task_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_timer.o fast_timer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o process_ctrl.o process_ctrl.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_mblock.o fast_mblock.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o connection_pool.o connection_pool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_mpool.o fast_mpool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_allocator.o fast_allocator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_buffer.o fast_buffer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o multi_skiplist.o multi_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o flat_skiplist.o flat_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o system_info.o system_info.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_blocked_queue.o fast_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o id_generator.o id_generator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o char_converter.o char_converter.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o char_convert_loader.o char_convert_loader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o common_blocked_queue.o common_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o multi_socket_client.o multi_socket_client.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o skiplist_set.o skiplist_set.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o json_parser.o json_parser.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o hash.lo hash.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o chain.lo chain.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o shared_func.lo shared_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o ini_file_reader.lo ini_file_reader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o logger.lo logger.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o sockopt.lo sockopt.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o base64.lo base64.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o sched_thread.lo sched_thread.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o http_func.lo http_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o md5.lo md5.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o pthread_func.lo pthread_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o local_ip_func.lo local_ip_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o avl_tree.lo avl_tree.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o ioevent.lo ioevent.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o ioevent_loop.lo ioevent_loop.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_task_queue.lo fast_task_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_timer.lo fast_timer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o process_ctrl.lo process_ctrl.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_mblock.lo fast_mblock.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o connection_pool.lo connection_pool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_mpool.lo fast_mpool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_allocator.lo fast_allocator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_buffer.lo fast_buffer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o multi_skiplist.lo multi_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o flat_skiplist.lo flat_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o system_info.lo system_info.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_blocked_queue.lo fast_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o id_generator.lo id_generator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o char_converter.lo char_converter.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o char_convert_loader.lo char_convert_loader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o common_blocked_queue.lo common_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o multi_socket_client.lo multi_socket_client.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o skiplist_set.lo skiplist_set.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o json_parser.lo json_parser.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -o libfastcommon.so -shared hash.lo chain.lo shared_func.lo ini_file_reader.lo logger.lo sockopt.lo base64.lo sched_thread.lo http_func.lo md5.lo pthread_func.lo local_ip_func.lo avl_tree.lo ioevent.lo ioevent_loop.lo fast_task_queue.lo fast_timer.lo process_ctrl.lo fast_mblock.lo connection_pool.lo fast_mpool.lo fast_allocator.lo fast_buffer.lo multi_skiplist.lo flat_skiplist.lo system_info.lo fast_blocked_queue.lo id_generator.lo char_converter.lo char_convert_loader.lo common_blocked_queue.lo multi_socket_client.lo skiplist_set.lo json_parser.lo -lm -ldl -lpthread
ar rcs libfastcommon.a hash.o chain.o shared_func.o ini_file_reader.o logger.o sockopt.o base64.o sched_thread.o http_func.o md5.o pthread_func.o local_ip_func.o avl_tree.o ioevent.o ioevent_loop.o fast_task_queue.o fast_timer.o process_ctrl.o fast_mblock.o connection_pool.o fast_mpool.o fast_allocator.o fast_buffer.o multi_skiplist.o flat_skiplist.o system_info.o fast_blocked_queue.o id_generator.o char_converter.o char_convert_loader.o common_blocked_queue.o multi_socket_client.o skiplist_set.o json_parser.o
[root@localhost libfastcommon-master]# ./make.sh install
mkdir -p /usr/lib64
mkdir -p /usr/lib
mkdir -p /usr/include/fastcommon
install -m 755 libfastcommon.so /usr/lib64
install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_define.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h fast_timer.h process_ctrl.h fast_mblock.h connection_pool.h fast_mpool.h fast_allocator.h fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h skiplist_common.h system_info.h fast_blocked_queue.h php7_ext_wrapper.h id_generator.h char_converter.h char_convert_loader.h common_blocked_queue.h multi_socket_client.h skiplist_set.h fc_list.h json_parser.h /usr/include/fastcommon
if [ ! -e /usr/lib/libfastcommon.so ]; then ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so; fi
[root@localhost libfastcommon-master]#

libfastcommon默认会被安装到/usr/lib64/libfastcommon.so但是FastDFS的主程序却在/usr/local/lib目录下

这个时候我们就要建立一个软链接了,实际上也相当于windows上的快捷方式。

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

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

ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so

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

1
2
3
4
5
6
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln: failed to create symbolic link ‘/usr/lib/libfastcommon.so’: File exists
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
[root@localhost libfastcommon-master]#

安装FastDFS

解压FastDFS安装包

1
[root@localhost home]# unzip fastdfs-master.zip

解压后看到:

1
2
3
4
5
6
7
8
9
[root@localhost home]# ll
total 2144
drwx------. 2 dev1 dev1 62 Apr 11 2018 dev1
drwxr-xr-x. 12 root root 4096 Dec 8 10:17 fastdfs-master
-rw-r--r--. 1 root root 905173 Dec 10 20:24 fastdfs-master.zip
-rw-r--r--. 1 root root 22492 Dec 10 20:24 fastdfs-nginx-module-master.zip
drwxr-xr-x. 5 root root 153 Dec 10 20:37 libfastcommon-master
-rw-r--r--. 1 root root 218881 Dec 10 20:24 libfastcommon-master.zip
-rw-r--r--. 1 root root 1037527 Dec 10 20:24 nginx-1.17.6.tar.gz

进到刚解压的目录

1
2
3
[root@localhost home]# cd fastdfs-master/
[root@localhost fastdfs-master]# ./make.sh
[root@localhost fastdfs-master]# ./make.sh install

如果没有报错那么就成功了。安装log中会提示FastDFS安装到了/etc/fdfs目录下。

成功后查看安装目录:

1
2
3
4
5
6
7
8
[root@localhost fastdfs-master]# cd /etc/fdfs/
[root@localhost fdfs]# ll
total 28
-rw-r--r--. 1 root root 1909 Dec 10 20:40 client.conf.sample
-rw-r--r--. 1 root root 10246 Dec 10 20:40 storage.conf.sample
-rw-r--r--. 1 root root 620 Dec 10 20:40 storage_ids.conf.sample
-rw-r--r--. 1 root root 8128 Dec 10 20:40 tracker.conf.sample
[root@localhost fdfs]#

我们需要把这三个示例文件复制一份,去掉.sample。

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost fdfs]# cp client.conf.sample client.conf
[root@localhost fdfs]# cp storage.conf.sample storage.conf
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf
[root@localhost fdfs]# ll
total 52
-rw-r--r--. 1 root root 1909 Dec 10 20:40 client.conf
-rw-r--r--. 1 root root 1909 Dec 10 20:40 client.conf.sample
-rw-r--r--. 1 root root 10246 Dec 10 20:40 storage.conf
-rw-r--r--. 1 root root 10246 Dec 10 20:40 storage.conf.sample
-rw-r--r--. 1 root root 620 Dec 10 20:40 storage_ids.conf.sample
-rw-r--r--. 1 root root 8128 Dec 10 20:40 tracker.conf
-rw-r--r--. 1 root root 8128 Dec 10 20:40 tracker.conf.sample
[root@localhost fdfs]#

FastDFS安装结束。

安装tracker

创建tracker工作目录

这个目录可以自定义,用来保存tracker的data和log

根据个人习惯,我创建了下面的目录:

1
2
3
4
5
6
7
8
9
[root@localhost ~]# mkdir /data
[root@localhost ~]# cd /data/
[root@localhost data]# mkdir fastdfs
[root@localhost data]# cd fastdfs/
[root@localhost fastdfs]# mkdir fastdfs_tracker
[root@localhost fastdfs]# cd fastdfs_tracker/
[root@localhost fastdfs_tracker]# pwd
/data/fastdfs/fastdfs_tracker
[root@localhost fastdfs_tracker]#

配置tracker

1
2
3
[root@localhost fastdfs-5.11]# cd /etc/fdfs/
[root@localhost fastdfs-5.11]# vim tracker.conf
[root@localhost fastdfs-5.11]#

最小化的CentOS7是没有安装vim的,可以把vim tracker.conf命令改成vi tracker.conf,也可以去下载一个vim

yum -y install vim

打开后重点关注下面4个配置:

1
2
3
4
disabled = false
port=22122
base_path=/data/fastdfs/fastdfs_tracker
http.server_port=6666 # 默认8080 9901

启动tracker

保存配置后启动tracker,命令如下:

1
service fdfs_trackerd start

如果不能启动,或提示用systemctl可改用命令:

1
systemctl start fdfs_trackerd

成功后应该可以看到:

1
2
3
4
[root@localhost fdfs]# service fdfs_trackerd start
Reloading systemd: [ OK ]
Starting fdfs_trackerd (via systemctl): [ OK ]
[root@localhost fdfs]#

进行刚刚创建的tracker目录,发现目录中多了data和log两个目录

1
2
3
4
5
6
[root@localhost fdfs]# cd /data/fastdfs/fastdfs_tracker/
[root@localhost fastdfs_tracker]# ll
total 0
drwxr-xr-x. 2 root root 83 Dec 10 20:45 data
drwxr-xr-x. 2 root root 26 Dec 10 20:44 logs
[root@localhost fastdfs_tracker]#

最后我们需要给tracker加入开机启动

1
2
3
[root@localhost fastdfs_tracker]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 Feb 20 2019 /etc/rc.d/rc.local
[root@localhost fastdfs_tracker]#

发现并没有执行权限,需要加一下:

1
2
[root@localhost fastdfs_tracker]# chmod +x /etc/rc.d/rc.local
[root@localhost fastdfs_tracker]#

加完后应该是这样的:

1
2
3
[root@localhost fastdfs_tracker]# ll /etc/rc.d/rc.local      
-rwxr-xr-x. 1 root root 473 Feb 20 2019 /etc/rc.d/rc.local
[root@localhost fastdfs_tracker]#

修改rc.local

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost fastdfs_tracker]# vim /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
service fdfs_trackerd start

查看一下tracker的端口监听情况

1
2
3
[root@localhost fastdfs_tracker]# netstat -unltp|grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 20553/fdfs_trackerd
[root@localhost fastdfs_tracker]#

端口22122成功监听。

安装storage

storage的安装与tracker很类似。

为storage配置工作目录

与tracker不现的是,由于storage还需要一个目录用来存储数据,所以我另外多建了一个fasdfs_storage_data

下面是我的目录结构:

1
2
3
4
5
6
7
8
9
[root@localhost fastdfs]# cd /data/fastdfs
[root@localhost fastdfs]# mkdir fastdfs_storage
[root@localhost fastdfs]# mkdir fastdfs_storage_data
[root@localhost fastdfs]# ll
total 0
drwxr-xr-x. 2 root root 6 Dec 10 20:43 fastdfs_storage
drwxr-xr-x. 2 root root 6 Dec 10 20:43 fastdfs_storage_data
drwxr-xr-x. 4 root root 30 Dec 10 20:42 fastdfs_tracker
[root@localhost fastdfs]#

修改storage配置文件

修改storage.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost fastdfs]# vim /etc/fdfs/storage.conf
disabled=false
group_name=group1 #组名,根据实际情况修改
port=23000 #设置storage的端口号,默认是23000,同一个组的storage端口号必须一致
base_path=/data/fastdfs/fastdfs_storage #设置storage数据文件和日志目录
store_path_count=1 #存储路径个数,需要和store_path个数匹配

》》》》》store_path0=/data/fastdfs/fastdfs_storage_data #实际文件存储路径
》》》》》base_path0=/data/fastdfs/fastdfs_storage_data #实际文件存储路径
tracker_server=192.168.31.100:22122 #我CentOS7的ip地址
http.server_port=8888 #设置 http 端口号

[root@localhost fastdfs]#

修改保存后创建软引用

1
2
[root@localhost fastdfs]# ln -s /usr/bin/fdfs_storaged /usr/local/bin
[root@localhost fastdfs]#

启动storage

1
service fdfs_storaged start

如果不能启动,或提示用systemctl可改用命令:

systemctl start fdfs_storaged

成功后应该可以看到:

1
2
3
[root@localhost fastdfs]# service fdfs_storaged start
Starting fdfs_storaged (via systemctl): [ OK ]
[root@localhost fastdfs]#

同样的,设置开机启动:

修改rc.local

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
service fdfs_trackerd start
service fdfs_storaged start

查看一下服务是否启动

1
2
3
4
[root@localhost fastdfs]# netstat -unltp | grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 20553/fdfs_trackerd
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 20817/fdfs_storaged
[root@localhost fastdfs]#

校验整合

到这里,fastdfs的东西都已安装完成,最后我们还要确定一下,storage是否注册到了tracker中去。

查看命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
[root@localhost fastdfs]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2019-12-10 21:04:31] DEBUG - base_path=/data/fastdfs/fastdfs_storage, connect_timeout=5, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=1, 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.31.100:22122

group count: 1

Group 1:
group name = group1
disk total space = 51175 MB
disk free space = 48527 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 = 10.254.193.118
ip_addr = 10.254.193.118 (anantes-651-1-49-net.w2-0.abo.wanadoo.fr) ACTIVE
http domain =
version = 6.04
join time = 2019-12-10 21:01:47
up time = 2019-12-10 21:01:47
total storage = 51175 MB
free storage = 48527 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 = 2019-12-10 21:04:20
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
[root@localhost fastdfs]#

测试

前面已对FastDFS的安装和配置,做了比较详细的讲解。FastDFS的基础模块都搭好了,现在开始测试下载。

配置客户端

同样的,需要修改客户端的配置文件:

1
2
3
4
[root@localhost fastdfs]# vim /etc/fdfs/client.conf
base_path = /data/fastdfs/fastdfs_tracker
tracker_server = 192.168.31.100:22122
http.tracker_server_port = 6666 # 默认端口80

通过ftp上传图片到CentOS:

在我的windows上,我随便拖了一张图片上去。

1
2
3
4
5
6
7
8
9
10
11
[root@localhost fastdfs]# cd /home/
[root@localhost home]# ll
total 1148
-rw-r--r--. 1 root root 18832 Oct 12 12:58 123.png
drwx------. 2 dev1 dev1 62 Apr 11 2018 dev1
drwxr-xr-x. 12 root root 4096 Dec 8 10:17 fastdfs-5.11
-rw-r--r--. 1 root root 905173 Dec 10 11:38 fastdfs-5.11.zip
-rw-r--r--. 1 root root 22492 Dec 10 11:38 fastdfs-nginx-module-master.zip
drwxr-xr-x. 5 root root 153 Dec 10 19:48 libfastcommon-1.0.36
-rw-r--r--. 1 root root 218881 Dec 10 11:36 libfastcommon-1.0.36.zip
[root@localhost home]#

模拟上传

确定图片位置后,我们输入上传图片命令:

1
2
3
[root@localhost home]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/123.jpg 
group1/M00/00/00/wKgfZF3vl6WAJDW5AAvWFlS1kOw230.jpg
[root@localhost home]#

成功后会返回图片的路径:

[root@localhost~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/123.png

group1/M00/00/00/wKgfZF3vl6WAJDW5AAvWFlS1kOw230.jpg

组名:group1

磁盘:M00

目录:00/00

文件名称:wKgfZF3vl6WAJDW5AAvWFlS1kOw230.png

我们上传的图片会被上传到我们创建的storage_data目录下,让我们去看看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost fastdfs_storage_data]# cd /data/fastdfs/fastdfs_storage_data/data/
[root@localhost data]# ls
00 06 0C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A 60 66 6C 72 78 7E 84 8A 90 96 9C A2 A8 AE B4 BA C0 C6 CC D2 D8 DE E4 EA F0 F6 FC
01 07 0D 13 19 1F 25 2B 31 37 3D 43 49 4F 55 5B 61 67 6D 73 79 7F 85 8B 91 97 9D A3 A9 AF B5 BB C1 C7 CD D3 D9 DF E5 EB F1 F7 FD
02 08 0E 14 1A 20 26 2C 32 38 3E 44 4A 50 56 5C 62 68 6E 74 7A 80 86 8C 92 98 9E A4 AA B0 B6 BC C2 C8 CE D4 DA E0 E6 EC F2 F8 FE
03 09 0F 15 1B 21 27 2D 33 39 3F 45 4B 51 57 5D 63 69 6F 75 7B 81 87 8D 93 99 9F A5 AB B1 B7 BD C3 C9 CF D5 DB E1 E7 ED F3 F9 FF
04 0A 10 16 1C 22 28 2E 34 3A 40 46 4C 52 58 5E 64 6A 70 76 7C 82 88 8E 94 9A A0 A6 AC B2 B8 BE C4 CA D0 D6 DC E2 E8 EE F4 FA
05 0B 11 17 1D 23 29 2F 35 3B 41 47 4D 53 59 5F 65 6B 71 77 7D 83 89 8F 95 9B A1 A7 AD B3 B9 BF C5 CB D1 D7 DD E3 E9 EF F5 FB
[root@localhost data]# cd 00/
[root@localhost 00]# ls
00 06 0C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A 60 66 6C 72 78 7E 84 8A 90 96 9C A2 A8 AE B4 BA C0 C6 CC D2 D8 DE E4 EA F0 F6 FC
01 07 0D 13 19 1F 25 2B 31 37 3D 43 49 4F 55 5B 61 67 6D 73 79 7F 85 8B 91 97 9D A3 A9 AF B5 BB C1 C7 CD D3 D9 DF E5 EB F1 F7 FD
02 08 0E 14 1A 20 26 2C 32 38 3E 44 4A 50 56 5C 62 68 6E 74 7A 80 86 8C 92 98 9E A4 AA B0 B6 BC C2 C8 CE D4 DA E0 E6 EC F2 F8 FE
03 09 0F 15 1B 21 27 2D 33 39 3F 45 4B 51 57 5D 63 69 6F 75 7B 81 87 8D 93 99 9F A5 AB B1 B7 BD C3 C9 CF D5 DB E1 E7 ED F3 F9 FF
04 0A 10 16 1C 22 28 2E 34 3A 40 46 4C 52 58 5E 64 6A 70 76 7C 82 88 8E 94 9A A0 A6 AC B2 B8 BE C4 CA D0 D6 DC E2 E8 EE F4 FA
05 0B 11 17 1D 23 29 2F 35 3B 41 47 4D 53 59 5F 65 6B 71 77 7D 83 89 8F 95 9B A1 A7 AD B3 B9 BF C5 CB D1 D7 DD E3 E9 EF F5 FB
[root@localhost 00]# cd 00
[root@localhost 00]# ls
wKgfZF3vl6WAJDW5AAvWFlS1kOw230.png
[root@localhost 00]#

果然通过刚刚返回的路径,我们成功找到了图片。

我们仔细看一下,实际文件存储路径下有创建好的多级目录。data下有256个1级目录,每级目录下又有256个2级子目录,总共65536个文件,新写的文件会以hash的方式被路由到其中某个子目录下,然后将文件数据直接作为一个本地文件存储到该目录中。

HTTP访问文件

我们去浏览器用http请求访问一下刚刚的图片:

这里写图片描述

我们发现,http不能直接访问到图片。这是为什么呢。

我去官网看了一原码,在HISTORY中发现,原来早在4.05的时候,就remove embed HTTP support

Version 4.05 2012-12-30

* client/fdfs_upload_file.c can specify storage ip port and store path index

* add connection pool

* client load storage ids config

* common/ini_file_reader.c does NOT call chdir

* keep the mtime of file same

* use g_current_time instead of call time function

* remove embed HTTP support

HTTP请求不能访问文件的原因

我们在使用FastDFS部署一个分布式文件系统的时候,通过FastDFS的客户端API来进行文件的上传、下载、删除等操作。同时通过FastDFS的HTTP服务器来提供HTTP服务。但是FastDFS的HTTP服务较为简单,无法提供负载均衡等高性能的服务,所以FastDFS的开发者——淘宝的架构师余庆同学,为我们提供了Nginx上使用的FastDFS模块(也可以叫FastDFS的Nginx模块)。

FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但是同组之间的服务器需要复制文件,有延迟的问题.假设Tracker服务器将文件上传到了192.168.128.131,文件ID已经返回客户端,这时,后台会将这个文件复制到192.168.128.131,如果复制没有完成,客户端就用这个ID在192.168.128.131取文件,肯定会出现错误。这个fastdfs-nginx-module可以重定向连接到源服务器取文件,避免客户端由于复制延迟的问题,出现错误。

正是这样,FastDFS需要结合nginx,所以取消原来对HTTP的直接支持。

FastDFS的nginx模块安装

安装nginx准备

将nginx-1.17.6.tar.gz上传至linux

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost 00]# cd /home/
[root@lml74xunyuanfuwuqi home]# ll
总用量 2164
-rw-r--r-- 1 root root 18832 10月 12 12:58 123.png
drwx------ 2 dceq dceq 62 11月 15 18:14 dceq
drwxr-xr-x 12 root root 4096 12月 8 10:17 fastdfs-master
-rw-r--r-- 1 root root 905173 12月 10 11:38 fastdfs-master.zip
-rw-r--r-- 1 root root 22492 12月 10 11:38 fastdfs-nginx-module-master.zip
drwxr-xr-x 5 root root 153 12月 11 15:07 libfastcommon-master
-rw-r--r-- 1 root root 218881 12月 10 11:36 libfastcommon-master.zip
-rw-r--r-- 1 root root 1037527 12月 10 13:38 nginx-1.17.6.tar.gz
drwxr-xr-x 3 root root 21 12月 11 09:03 work
[root@localhost home]#

在安装nginx之前要安装nginx所需的依赖lib:

1
2
3
[root@localhost home]# yum -y install pcre pcre-devel 
[root@localhost home]# yum -y install zlib zlib-devel
[root@localhost home]# yum -y install openssl openssl-devel

安装nginx并添加fastdfs-nginx-module

解压nginx,和fastdfs-nginx-module:

1
2
[root@localhost home]# tar -zxvf nginx-1.17.6.tar.gz 
[root@localhost home]# unzip fastdfs-nginx-module-master.zip

解压后进入nginx目录编译安装nginx,并添加fastdfs-nginx-module:

1
2
3
4
[root@lml74xunyuanfuwuqi home]# cd nginx-1.17.6
[root@localhost nginx-1.17.6]# ./configure --prefix=/usr/local/nginx --add-module=/home/fastdfs-nginx-module-master/src

#解压后fastdfs-nginx-module所在的位置

如果配置不报错的话,就开始编译:

1
2
[root@localhost nginx-1.17.6]# make
[root@localhost nginx-1.17.6]# make install

如果报错的话,很可能是版本的原因,在我的第二篇博文中提供了我测试成功不报错的版本下载。

nginx的默认目录是/usr/local/nginx,安装成功后查看:

1
2
3
4
5
6
7
8
9
10
[root@localhost nginx-1.17.6]# cd /usr/local/nginx/
[root@localhost nginx]# ll
total 4
drwxr-xr-x. 2 root root 4096 Dec 10 21:06 conf
drwxr-xr-x. 2 root root 40 Dec 10 21:06 html
drwxr-xr-x. 2 root root 6 Dec 10 21:06 logs
drwxr-xr-x. 2 root root 19 Dec 10 21:06 sbin
[root@lml74xunyuanfuwuqi nginx]# cd conf/
[root@localhost nginx]#
[root@lml74xunyuanfuwuqi conf]# vi nginx.conf

配置storage nginx

修改nginx.conf:

修改监听端口 listen 9999, 新增location

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
[root@localhost nginx]# cd conf/
[root@localhost conf]# ll
total 68
-rw-r--r--. 1 root root 1077 Dec 10 21:06 fastcgi.conf
-rw-r--r--. 1 root root 1077 Dec 10 21:06 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Dec 10 21:06 fastcgi_params
-rw-r--r--. 1 root root 1007 Dec 10 21:06 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Dec 10 21:06 koi-utf
-rw-r--r--. 1 root root 2223 Dec 10 21:06 koi-win
-rw-r--r--. 1 root root 5231 Dec 10 21:06 mime.types
-rw-r--r--. 1 root root 5231 Dec 10 21:06 mime.types.default
-rw-r--r--. 1 root root 2656 Dec 10 21:06 nginx.conf
-rw-r--r--. 1 root root 2656 Dec 10 21:06 nginx.conf.default
-rw-r--r--. 1 root root 636 Dec 10 21:06 scgi_params
-rw-r--r--. 1 root root 636 Dec 10 21:06 scgi_params.default
-rw-r--r--. 1 root root 664 Dec 10 21:06 uwsgi_params
-rw-r--r--. 1 root root 664 Dec 10 21:06 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Dec 10 21:06 win-utf
[root@localhost conf]# vi nginx.conf

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

server {
listen 9999;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

location ~/group1/M00 {
root /data/fastdfs/fastdfs_storage_data/data;
ngx_fastdfs_module;
}


#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;
# }
#}

}
[root@localhost conf]#
[root@localhost conf]#

然后进入FastDFS安装时的解压过的目录,将http.conf和mime.types拷贝到/etc/fdfs目录下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost conf]# cd /home/fastdfs-master/conf/
[root@localhost conf]# ll
total 88
-rw-r--r--. 1 root root 23981 Dec 8 10:17 anti-steal.jpg
-rw-r--r--. 1 root root 1909 Dec 8 10:17 client.conf
-rw-r--r--. 1 root root 965 Dec 8 10:17 http.conf
-rw-r--r--. 1 root root 31172 Dec 8 10:17 mime.types
-rw-r--r--. 1 root root 10246 Dec 8 10:17 storage.conf
-rw-r--r--. 1 root root 620 Dec 8 10:17 storage_ids.conf
-rw-r--r--. 1 root root 8128 Dec 8 10:17 tracker.conf
[root@localhost conf]#
[root@localhost conf]# cp http.conf /etc/fdfs/
[root@localhost conf]# cp mime.types /etc/fdfs/
[root@localhost conf]#

另外还需要把fastdfs-nginx-module安装目录中src目录下的mod_fastdfs.conf也拷贝到/etc/fdfs目录下:

1
2
[root@localhost conf]# cp /home/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
[root@localhost conf]#

对刚刚拷贝的mod_fastdfs.conf文件进行修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@localhost conf]# vim /etc/fdfs/mod_fastdfs.conf
! base_path=/data/fastdfs/fastdfs_storage #保存日志目录
! tracker_server=192.168.31.100:22122 #tracker服务器的IP地址以及端口号
! storage_server_port=23000 #storage服务器的端口号
! url_have_group_name = true #文件 url 中是否有 group 名
! store_path0=/data/fastdfs/fastdfs_storage_data #存储路径
group_count = 3 #设置组的个数,事实上这次只使用了group1
在文件的最后,设置group

[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/fastdfs_storage_data
store_path1=/data/fastdfs/fastdfs_storage_data

# group settings for group #2
# # since v1.14
# # when support multi-group, uncomment following section as neccessary
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/fastdfs_storage_data

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

[root@localhost conf]#

创建M00至storage存储目录的符号连接:

1
2
[root@localhost conf]# ln -s /data/fastdfs/fastdfs_storage_data/data/ /data/fastdfs/fastdfs_storage_data/data/M00
[root@localhost conf]#

启动nginx:

1
2
3
[root@localhost conf]# /usr/local/nginx/sbin/nginx 
ngx_http_fastdfs_set pid=31687
[root@localhost conf]#

成功启动:

1
2
3
[root@localhost conf]# /usr/local/nginx/sbin/nginx 
ngx_http_fastdfs_set pid=31687
[root@localhost conf]#

CentOS 7 安裝FastDFS

恭喜你,storage的nginx已配置成功。接下来,我们还要继续配置tracker的nginx。

配置tracker nginx

再解压一个nginx:

我在我自己的工作下再建了一个nginx2,把原来的nginx-1.12.0.tar.gz又解压了一份到里面

1
2
3
4
5
[root@localhost home]# mkdir nginx2
[root@localhost home]# tar -zxvf nginx-1.17.6.tar.gz -C nginx2/
[root@localhost home]# cd nginx2/nginx-1.17.6/
[root@localhost nginx-1.17.6]# ./configure --prefix=/usr/local/nginx2 --add-module=/home/fastdfs-nginx-module-master/src
[root@localhost nginx-1.17.6]# make && make install

接下来,一样的还是修改nginx.conf,端口号可以不改,用80的。需将upstream指向tracker的nginx地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost nginx-1.17.6]# vim /usr/local/nginx2/conf/nginx.conf

upstream fdfs_group1 {
server 127.0.0.1:9999;
}
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location /group1/M00 {
proxy_pass http://fdfs_group1;
}
#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;
}
}

启动nginx2:

1
[root@localhost nginx-1.17.6]# /usr/local/nginx2/sbin/nginx

CentOS 7 安裝FastDFS

可以成功访问。

防火墙端口设置
成功了,为什么还要讲这个呢。因为有些同学到这里,还是不到访问,很可能是防火墙没有开启相应的端口。防火墙这个东西我建议大家还是不要关闭,虽然麻烦了一点。

查看已开启的端口:

1
2
3
[root@localhost nginx-1.13.8]# firewall-cmd --zone=public --list-ports
20880/tcp 80/tcp 2181/tcp 23000/tcp 22122/tcp 9999/tcp
[root@localhost nginx-1.13.8]#

在我的CentOS上这些端口都是开放的。
storage:20880
tracker:23000
这两个端口要开启,到时候下一篇讲fastdfs-client-javas可能会造成无法连接。
9999和80端口是提供给nginx访问的。
开放端口号命令:–permanent表示永久生效,不加的话,重启后不生效

1
firewall-cmd --zone=public --add-port=23000/tcp --permanent #开户端口号

CentOS7 防火墙相关命令:

1
2
3
systemctl enable firewalld.service    #开启防火墙
systemctl stop firewalld.service #关闭防火墙(开机会仍会启动)
systemctl disable firewalld.service #禁用防火墙(开机后不再启动)

HTTP测试

现在我们再去访问一下,原来我们上传过的文件:

CentOS 7 安裝FastDFS

这个时候已经能成功访问。

相关文章

微信打赏

赞赏是不耍流氓的鼓励

评论系统未开启,无法评论!