搭建fastdfs文件服务器

一、安装FastDFS环境

1、跟踪服务器(Tracker Server)

tracker1:192.168.2.134

tracker2:192.168.2.135
2、存储服务器(Storage Server)

storage1:192.168.2.136

storage2:192.168.2.137

storage3:192.168.2.138

storage4:192.168.2.139
3、操作系统:CentOS7
用户:root
数据存储目录:/home/fastdfs

我把所有的安装包下载到/usr/local/softpackages/下,解压到当前目录

[root@tracker1 ~]# ls /usr/local/softpackages/
5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip  fastdfs-5.05  fastdfs-nginx-module-master  libfastcommon-1.0.7  nginx-1.12.1  nginx-1.12.1.tar.gz  V1.0.7.tar.gz  V5.05.tar.gz

二、FastDFS基础配置

1、下载安装 libfastcommon

libfastcommon是从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库,基础环境,安装即可 。

① 下载libfastcommon

# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

② 解压

# tar -zxvf V1.0.7.tar.gz

# cd libfastcommon-1.0.7

绝对路径

# cd /usr/local/softpackages/libfastcommon-1.0.7

授权

# chmod 755 *

③ 编译、安装

# ./make.sh

# ./make.sh install

可以一条语句:

# ./make.sh && ./make.sh install

[root@tracker1 libfastcommon-1.0.7]# pwd
/usr/local/softpackages/libfastcommon-1.0.7
[root@tracker1 libfastcommon-1.0.7]#
[root@tracker1 libfastcommon-1.0.7]# ll
total 24
-rw-r--r--. 1 root root 2170 Jun  3 23:47 HISTORY
-rw-r--r--. 1 root root  582 Jun  3 23:47 INSTALL
-rw-r--r--. 1 root root 1341 Jun  3 23:47 libfastcommon.spec
-rw-r--r--. 1 root root 2151 Jun  3 23:47 make.sh
-rw-r--r--. 1 root root  617 Jun  3 23:47 README
drwxr-xr-x. 2 root root 4096 Jun  3 23:47 src
[root@tracker1 libfastcommon-1.0.7]#
[root@tracker1 libfastcommon-1.0.7]# chmod 755 *
[root@tracker1 libfastcommon-1.0.7]#
[root@tracker1 libfastcommon-1.0.7]# ll
total 24
-rwxr-xr-x. 1 root root 2170 Jun  3 23:47 HISTORY
-rwxr-xr-x. 1 root root  582 Jun  3 23:47 INSTALL
-rwxr-xr-x. 1 root root 1341 Jun  3 23:47 libfastcommon.spec
-rwxr-xr-x. 1 root root 2151 Jun  3 23:47 make.sh
-rwxr-xr-x. 1 root root  617 Jun  3 23:47 README
drwxr-xr-x. 2 root root 4096 Jun  3 23:47 src
[root@tracker1 libfastcommon-1.0.7]#
[root@tracker1 libfastcommon-1.0.7]# ./make.sh
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o hash.lo hash.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o chain.lo chain.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o shared_func.lo shared_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o ini_file_reader.lo ini_file_reader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o logger.lo logger.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o sockopt.lo sockopt.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o base64.lo base64.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o sched_thread.lo sched_thread.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o http_func.lo http_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o md5.lo md5.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o pthread_func.lo pthread_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o local_ip_func.lo local_ip_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o avl_tree.lo avl_tree.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o ioevent.lo ioevent.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o ioevent_loop.lo ioevent_loop.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o fast_task_queue.lo fast_task_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o fast_timer.lo fast_timer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o process_ctrl.lo process_ctrl.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o fast_mblock.lo fast_mblock.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -fPIC -o connection_pool.lo connection_pool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o hash.o hash.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o chain.o chain.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o shared_func.o shared_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o ini_file_reader.o ini_file_reader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o logger.o logger.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o sockopt.o sockopt.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o base64.o base64.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o sched_thread.o sched_thread.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o http_func.o http_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o md5.o md5.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o pthread_func.o pthread_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o local_ip_func.o local_ip_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o avl_tree.o avl_tree.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o ioevent.o ioevent.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o ioevent_loop.o ioevent_loop.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o fast_task_queue.o fast_task_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o fast_timer.o fast_timer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o process_ctrl.o process_ctrl.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o fast_mblock.o fast_mblock.c
cc -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o connection_pool.o connection_pool.c
ar rcs libfastcommon.a hash.o
[root@tracker1 libfastcommon-1.0.7]#
[root@tracker1 libfastcommon-1.0.7]# ./make.sh install
mkdir -p /usr/lib64
install -m 755 libfastcommon.so /usr/lib64
mkdir -p /usr/include/fastcommon
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_bits.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 /usr/include/fastcommon
[root@tracker1 libfastcommon-1.0.7]#

④ 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
# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

2、下载安装FastDFS

① 下载FastDFS

# wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz

② 解压

# tar -zxvf V5.05.tar.gz

# cd fastdfs-5.05

绝对路径

# cd /usr/local/softpackages/fastdfs-5.05

授权

# chmod 755 *

③ 编译、安装

# ./make.sh

# ./make.sh install

[root@tracker1 softpackages]# cd fastdfs-5.05
[root@tracker1 fastdfs-5.05]#
[root@tracker1 fastdfs-5.05]# pwd
/usr/local/softpackages/fastdfs-5.05
[root@tracker1 fastdfs-5.05]#
[root@tracker1 fastdfs-5.05]# ll
total 128
drwxr-xr-x. 3 root root  4096 Jun  3 23:47 client
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 common
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 conf
-rw-r--r--. 1 root root 35067 Jun  3 23:47 COPYING-3_0.txt
-rw-r--r--. 1 root root  2802 Jun  3 23:47 fastdfs.spec
-rw-r--r--. 1 root root 31386 Jun  3 23:47 HISTORY
drwxr-xr-x. 2 root root    46 Jun  3 23:47 init.d
-rw-r--r--. 1 root root  7755 Jun  3 23:47 INSTALL
-rw-r--r--. 1 root root  5813 Jun  3 23:47 make.sh
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 php_client
-rw-r--r--. 1 root root  2380 Jun  3 23:47 README.md
-rw-r--r--. 1 root root  1768 Jun  3 23:47 restart.sh
-rw-r--r--. 1 root root  1680 Jun  3 23:47 stop.sh
drwxr-xr-x. 4 root root  4096 Jun  3 23:47 storage
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 test
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 tracker
[root@tracker1 fastdfs-5.05]#
[root@tracker1 fastdfs-5.05]# chmod 755 *
[root@tracker1 fastdfs-5.05]#
[root@tracker1 fastdfs-5.05]# ll
total 128
drwxr-xr-x. 3 root root  4096 Jun  3 23:47 client
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 common
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 conf
-rwxr-xr-x. 1 root root 35067 Jun  3 23:47 COPYING-3_0.txt
-rwxr-xr-x. 1 root root  2802 Jun  3 23:47 fastdfs.spec
-rwxr-xr-x. 1 root root 31386 Jun  3 23:47 HISTORY
drwxr-xr-x. 2 root root    46 Jun  3 23:47 init.d
-rwxr-xr-x. 1 root root  7755 Jun  3 23:47 INSTALL
-rwxr-xr-x. 1 root root  5813 Jun  3 23:47 make.sh
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 php_client
-rwxr-xr-x. 1 root root  2380 Jun  3 23:47 README.md
-rwxr-xr-x. 1 root root  1768 Jun  3 23:47 restart.sh
-rwxr-xr-x. 1 root root  1680 Jun  3 23:47 stop.sh
drwxr-xr-x. 4 root root  4096 Jun  3 23:47 storage
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 test
drwxr-xr-x. 2 root root  4096 Jun  3 23:47 tracker
[root@tracker1 fastdfs-5.05]#
[root@tracker1 fastdfs-5.05]# ./make.sh
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -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 -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o tracker_proto.o tracker_proto.c  -I../common -I/usr/include/fastcommon
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o tracker_mem.o tracker_mem.c  -I../common -I/usr/include/fastcommon
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o tracker_service.o tracker_service.c  -I../common -I/usr/include/fastcommon
tracker_service.c: In function ‘tracker_deal_reselect_leader’:
tracker_service.c:1743:21: warning: variable ‘pClientInfo’ set but not used [-Wunused-but-set-variable]
  TrackerClientInfo *pClientInfo;
………………………………
………………………………
………………………………
[root@tracker1 fastdfs-5.05]# ./make.sh install
mkdir -p /usr/bin
mkdir -p /etc/fdfs
cp -f fdfs_trackerd /usr/bin
if [ ! -f /etc/fdfs/tracker.conf.sample ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf.sample; fi
mkdir -p /usr/bin
mkdir -p /etc/fdfs
cp -f fdfs_storaged  /usr/bin
if [ ! -f /etc/fdfs/storage.conf.sample ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf.sample; fi
mkdir -p /usr/bin
mkdir -p /etc/fdfs
mkdir -p /usr/lib64
cp -f fdfs_monitor fdfs_test fdfs_test1 fdfs_crc32 fdfs_upload_file fdfs_download_file fdfs_delete_file fdfs_file_info fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_upload_appender /usr/bin
if [ 0 -eq 1 ]; then cp -f libfdfsclient.a /usr/lib64; fi
if [ 1 -eq 1 ]; then cp -f libfdfsclient.so /usr/lib64; fi
mkdir -p /usr/include/fastdfs
cp -f ../common/fdfs_define.h ../common/fdfs_global.h ../common/mime_file_parser.h ../common/fdfs_http_shared.h ../tracker/tracker_types.h ../tracker/tracker_proto.h ../tracker/fdfs_shared_func.h ../storage/trunk_mgr/trunk_shared.h 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

④ 默认安装方式安装后的相应文件与目录
A、服务脚本:

/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_tracker

B、配置文件(这三个是作者给的样例配置文件) :

/etc/fdfs/client.conf.sample
/etc/fdfs/storage.conf.sample
/etc/fdfs/tracker.conf.sample

C、命令工具在 /usr/bin/ 目录下:

fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file
stop.sh
restart.sh 

⑤ FastDFS 服务脚本设置的 bin 目录是 /usr/local/bin, 但实际命令安装在 /usr/bin/ 下

两种方式:

  》 一是修改FastDFS 服务脚本中相应的命令路径,也就是把 /etc/init.d/fdfs_storaged 和 /etc/init.d/fdfs_tracker 两个脚本中的 /usr/local/bin 修改成 /usr/bin。

     # vim fdfs_trackerd
    使用查找替换命令进统一修改:%s+/usr/local/bin+/usr/bin
    # vim fdfs_storaged
    使用查找替换命令进统一修改:%s+/usr/local/bin+/usr/bin

# vi /etc/init.d/fdfs_storaged

》 二是建立 /usr/bin 到 /usr/local/bin 的软链接,我是用这种方式。

# ln -s /usr/bin/fdfs_trackerd   /usr/local/bin
# ln -s /usr/bin/fdfs_storaged   /usr/local/bin
# ln -s /usr/bin/stop.sh         /usr/local/bin
# ln -s /usr/bin/restart.sh      /usr/local/bin

三、配置FastDFS跟踪器(Tracker)

配置文件详细说明参考:FastDFS 配置文件详解

① 进入 /etc/fdfs,复制 FastDFS 跟踪器样例配置文件 tracker.conf.sample,并重命名为 tracker.conf。

# cd /etc/fdfs

# cp tracker.conf.sample tracker.conf

# vim tracker.conf

[root@tracker1 ~]# cd /etc/fdfs
[root@tracker1 fdfs]#
[root@tracker1 fdfs]# ls
client.conf.sample  storage.conf.sample  tracker.conf.sample
[root@tracker1 fdfs]#
[root@tracker1 fdfs]# cp tracker.conf.sample tracker.conf
[root@tracker1 fdfs]#

② 编辑tracker.conf ,标红的需要修改下,其它的默认即可。

# vi tracker.conf

# 配置文件是否不生效,false 为生效
disabled=false

# 提供服务的端口
port=22122

# Tracker 数据和日志目录地址(根目录必须存在,子目录会自动创建)
base_path=/home/fastdfs/tracker

# HTTP 服务端口
http.server_port=80

③ 创建tracker基础数据目录,即base_path对应的目录

# mkdir -p /home/fastdfs/tracker

 ④ 防火墙中打开跟踪端口(默认的22122)

# vim /etc/sysconfig/iptables

添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT

重启防火墙:
# service iptables restart

⑤ 启动Tracker

初次成功启动,会在 /home/fdfsdfs/tracker/ (配置的base_path)下创建 data、logs 两个目录。

注意:

启动前先授权

# chmod 755 /etc/init.d/fdfs_trackerd

# chmod 755 -R /usr/local/bin/fdfs_trackerd

[root@tracker2 fdfs]# /etc/init.d/fdfs_trackerd start
-bash: /etc/init.d/fdfs_trackerd: Permission denied
[root@tracker2 fdfs]#
[root@tracker2 fdfs]# chmod 755 /etc/init.d/fdfs_trackerd
[root@tracker2 fdfs]#
[root@tracker2 fdfs]# /etc/init.d/fdfs_trackerd start
Reloading systemd:                                         [  OK  ]
Starting fdfs_trackerd (via systemctl):                    [  OK  ]
[root@tracker2 fdfs]#
[root@tracker2 fdfs]# service fdfs_trackerd status
● fdfs_trackerd.service - LSB: FastDFS tracker server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_trackerd)
   Active: active (exited) since Tue 2019-06-04 18:47:32 CST; 14s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3083 ExecStart=/etc/rc.d/init.d/fdfs_trackerd start (code=exited, status=0/SUCCESS)

Jun 04 18:47:32 tracker2 systemd[1]: Starting LSB: FastDFS tracker server...
Jun 04 18:47:32 tracker2 fdfs_trackerd[3083]: Starting FastDFS tracker server:
Jun 04 18:47:32 tracker2 systemd[1]: Started LSB: FastDFS tracker server.
Jun 04 18:47:32 tracker2 fdfs_trackerd[3083]: /etc/rc.d/init.d/fdfs_trackerd: line 43: /usr/local/bin/fdfs_trackerd: Permission denied
[root@tracker2 fdfs]#
[root@tracker2 fdfs]# chmod 755 -R /usr/local/bin/fdfs_trackerd
[root@tracker2 fdfs]#
[root@tracker2 fdfs]# service fdfs_trackerd restart
Restarting fdfs_trackerd (via systemctl):                  [  OK  ]
[root@tracker2 fdfs]#
[root@tracker2 fdfs]# service fdfs_trackerd status
● fdfs_trackerd.service - LSB: FastDFS tracker server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_trackerd)
   Active: active (running) since Tue 2019-06-04 18:48:08 CST; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3127 ExecStop=/etc/rc.d/init.d/fdfs_trackerd stop (code=exited, status=0/SUCCESS)
  Process: 3150 ExecStart=/etc/rc.d/init.d/fdfs_trackerd start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/fdfs_trackerd.service
           └─3154 /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf

Jun 04 18:48:08 tracker2 systemd[1]: Starting LSB: FastDFS tracker server...
Jun 04 18:48:08 tracker2 fdfs_trackerd[3150]: Starting FastDFS tracker server:
Jun 04 18:48:08 tracker2 systemd[1]: Started LSB: FastDFS tracker server.
[root@tracker2 fdfs]#

可以用这种方式启动

# /etc/init.d/fdfs_trackerd start

也可以用这种方式启动,前提是上面创建了软链接,后面都用这种方式

# service fdfs_trackerd start

查看 FastDFS Tracker 是否已成功启动 ,22122端口正在被监听,则算是Tracker服务安装成功。

# netstat -tunlp | grep fdfs

[root@tracker1 ~]# service fdfs_trackerd start
Starting fdfs_trackerd (via systemctl):                    [  OK  ]
[root@tracker1 ~]#
[root@tracker1 ~]#
[root@tracker1 ~]# service fdfs_trackerd status
● fdfs_trackerd.service - LSB: FastDFS tracker server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_trackerd)
   Active: active (running) since Tue 2019-06-04 00:45:46 CST; 4s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2822 ExecStart=/etc/rc.d/init.d/fdfs_trackerd start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/fdfs_trackerd.service
           └─2825 /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf

Jun 04 00:45:46 tracker1 systemd[1]: Starting LSB: FastDFS tracker server...
Jun 04 00:45:46 tracker1 fdfs_trackerd[2822]: Starting FastDFS tracker server:
Jun 04 00:45:46 tracker1 systemd[1]: Started LSB: FastDFS tracker server.
[root@tracker1 ~]#
[root@tracker1 ~]#
[root@tracker1 ~]# netstat -tunlp | grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      2825/fdfs_trackerd
[root@tracker1 ~]#
[root@tracker1 ~]# ls /home/fastdfs/tracker/
data  logs

关闭Tracker命令:

# service fdfs_trackerd stop

⑥ 设置Tracker开机启动

# chkconfig fdfs_trackerd on

或者:
# vim /etc/rc.d/rc.local
加入配置:
/etc/init.d/fdfs_trackerd start 

⑦ tracker server 目录及文件结构

Tracker服务启动成功后,会在base_path下创建data、logs两个目录。目录结构如下:

${base_path}
  |__data
  |   |__storage_groups.dat:存储分组信息
  |   |__storage_servers.dat:存储服务器列表
  |__logs
  |   |__trackerd.log: tracker server 日志文件 

四、配置FastDFS 存储 (Storage)

① 进入 /etc/fdfs 目录,复制 FastDFS 存储器样例配置文件 storage.conf.sample,并重命名为 storage.conf

# cd /etc/fdfs
# cp storage.conf.sample storage.conf

② 编辑storage.conf

标红的需要修改,其它的默认即可。

# vi storage.conf

# 配置文件是否不生效,false 为生效
disabled=false 

# 指定此 storage server 所在 组(卷)
group_name=group1

# storage server 服务端口
port=23000

# 心跳间隔时间,单位为秒 (这里是指主动向 tracker server 发送心跳)
heart_beat_interval=30

# Storage 数据和日志目录地址(根目录必须存在,子目录会自动生成)
base_path=/home/fastdfs/storage

# 存放文件时 storage server 支持多个路径。这里配置存放文件的基路径数目,通常只配一个目录。
store_path_count=1


# 逐一配置 store_path_count 个路径,索引号基于 0。
# 如果不配置 store_path0,那它就和 base_path 对应的路径一样。
store_path0=/home/fastdfs/file

# FastDFS 存储文件时,采用了两级目录。这里配置存放文件的目录个数。 
# 如果本参数只为 N(如: 256),那么 storage server 在初次运行时,会在 store_path 下自动创建 N * N 个存放文件的子目录。
subdir_count_per_path=256

# tracker_server 的列表 ,会主动连接 tracker_server
# 有多个 tracker server 时,每个 tracker server 写一行
tracker_server=192.168.2.134:22122
tracker_server=192.168.2.135:22122
# 允许系统同步的时间段 (默认是全天) 。一般用于避免高峰同步产生一些问题而设定。 sync_start_time=00:00 sync_end_time=23:59 # 访问端口 http.server_port=80

③ 创建Storage基础数据目录,对应base_path目录

# mkdir -p /home/fastdfs/storage

这是配置的store_path0路径

# mkdir -p /home/fastdfs/file

④ 防火墙中打开存储器端口(默认的 23000)

# vim /etc/sysconfig/iptables

添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT

重启防火墙:
# service iptables restart

⑤ 启动 Storage

启动Storage前确保Tracker是启动的。初次启动成功,会在 /home/fastdfs/storage 目录下创建 data、 logs 两个目录。

注意:

启动前先授权

# chmod 755 /etc/init.d/fdfs_storaged
# chmod 755 /usr/local/bin/fdfs_storaged

可以用这种方式启动

# /etc/init.d/fdfs_storaged start

也可以用这种方式,后面都用这种

# service fdfs_storaged start

[root@storage1 ~]# service fdfs_storaged start
env: /etc/init.d/fdfs_storaged: Permission denied
[root@storage1 ~]#
[root@storage1 ~]# chmod 755 /etc/init.d/fdfs_storaged
[root@storage1 ~]#
[root@storage1 ~]# service fdfs_storaged start
Starting fdfs_storaged (via systemctl):                    [  OK  ]
[root@storage1 ~]#
[root@storage1 ~]# service fdfs_storaged status
● fdfs_storaged.service - LSB: FastDFS storage server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_storaged)
   Active: active (exited) since Tue 2019-06-04 02:28:10 CST; 50s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 16929 ExecStart=/etc/rc.d/init.d/fdfs_storaged start (code=exited, status=0/SUCCESS)

Jun 04 02:28:10 storage1 systemd[1]: Starting LSB: FastDFS storage server...
Jun 04 02:28:10 storage1 fdfs_storaged[16929]: Starting FastDFS storage server:
Jun 04 02:28:10 storage1 systemd[1]: Started LSB: FastDFS storage server.
Jun 04 02:28:10 storage1 fdfs_storaged[16929]: /etc/rc.d/init.d/fdfs_storaged: line 43: /usr/local/bin/fdfs_storaged: Permission denied
[root@storage1 ~]#
[root@storage1 ~]# chmod 755 /usr/local/bin/fdfs_storaged
[root@storage1 ~]#
[root@storage1 ~]# service fdfs_storaged restart
Restarting fdfs_storaged (via systemctl):                  [  OK  ]
[root@storage1 ~]#
[root@storage1 ~]# service fdfs_storaged status
● fdfs_storaged.service - LSB: FastDFS storage server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_storaged)
   Active: active (running) since Tue 2019-06-04 02:29:30 CST; 4s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 16973 ExecStop=/etc/rc.d/init.d/fdfs_storaged stop (code=exited, status=0/SUCCESS)
  Process: 16996 ExecStart=/etc/rc.d/init.d/fdfs_storaged start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/fdfs_storaged.service
           └─17001 /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf

Jun 04 02:29:30 storage1 systemd[1]: Starting LSB: FastDFS storage server...
Jun 04 02:29:30 storage1 fdfs_storaged[16996]: Starting FastDFS storage server:
Jun 04 02:29:30 storage1 systemd[1]: Started LSB: FastDFS storage server.
[root@storage1 ~]# netstat -tunlp | grep fdfs
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      17001/fdfs_storaged

关闭Storage命令:

# service fdfs_storaged stop

查看Storage和Tracker是否在通信:

# chmod 755 /usr/bin/fdfs_monitor

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

[root@storage1 ~]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
bash: /usr/bin/fdfs_monitor: Permission denied
[root@storage1 ~]#
[root@storage1 ~]# chmod 755 /usr/bin/fdfs_monitor
[root@storage1 ~]#
[root@storage1 ~]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2019-06-04 02:36:19] DEBUG - base_path=/home/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.2.134:22122

group count: 1

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

        Storage 1:
                id = 192.168.2.136
                ip_addr = 192.168.2.136  ACTIVE
                http domain =
                version = 5.05
                join time = 2019-06-04 02:29:30
                up time = 2019-06-04 02:29:30
                total storage = 79833 MB
                free storage = 78514 MB
                upload priority = 10
                store_path_count = 1
                subdir_count_per_path = 256
                storage_port = 23000
                storage_http_port = 80
                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-06-04 02:36:02
                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@storage1 ~]#

⑥ 设置 Storage 开机启动

# chkconfig fdfs_storaged on

或者:
# vim /etc/rc.d/rc.local
加入配置:
/etc/init.d/fdfs_storaged start

⑦ Storage 目录

同 Tracker,Storage 启动成功后,在base_path 下创建了data、logs目录,记录着 Storage Server 的信息。

在 store_path0 目录下,创建了N*N个子目录:

# ls /home/fastdfs/file/data/

五、文件上传测试

① 修改 Tracker 服务器中的客户端配置文件

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

修改如下配置即可,其它默认。

# Client 的数据和日志目录
base_path=/home/fastdfs/client

# Tracker端口
tracker_server=192.168.2.134:22122

创建client基础数据目录,对应base_path目录

# mkdir -p /home/fastdfs/client

② 上传测试

 在linux内部执行如下命令上传test.jpg 图片

# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg

[root@tracker1 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg
-bash: /usr/bin/fdfs_upload_file: Permission denied
[root@tracker1 ~]#
[root@tracker1 ~]# chmod 755 /usr/bin/fdfs_upload_file
[root@tracker1 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg
group1/M00/00/00/wKgCiFz1bm-ATQNlAACLwhA7j18254.jpg

上传成功后返回文件ID号:group1/M00/00/00/wKgCiFz1bm-ATQNlAACLwhA7j18254.jpg

返回的文件ID由group、存储目录、两级子目录、fileid、文件后缀名(由客户端指定,主要用于区分文件类型)拼接而成。

六、安装Nginx

上面将文件上传成功了,但我们无法下载。因此安装Nginx作为服务器以支持Http方式访问文件。同时,后面安装FastDFS的Nginx模块也需要Nginx环境。

Nginx只需要安装到StorageServer所在的服务器即可,用于访问文件。

1、安装nginx所需环境  

① gcc 安装

# yum install gcc-c++ -y

 

② PCRE pcre-devel 安装

# yum install -y pcre pcre-devel

③ zlib 安装

# yum install -y zlib zlib-devel

 

④ OpenSSL 安装

# yum install -y openssl openssl-devel

 

2、安装Nginx

① 下载nginx

# wget -c https://nginx.org/download/nginx-1.12.1.tar.gz

② 解压

# tar -zxvf nginx-1.12.1.tar.gz
# cd nginx-1.12.1

 

③ 使用默认配置

# ./configure

 

实例操作如下:

[root@storage1 softpackages]# chmod 755 -R nginx-1.12.1
[root@storage1 softpackages]#
[root@storage1 softpackages]# cd nginx-1.12.1
[root@storage1 nginx-1.12.1]#
[root@storage1 nginx-1.12.1]# pwd
/usr/local/softpackages/nginx-1.12.1
[root@storage1 nginx-1.12.1]#
[root@storage1 nginx-1.12.1]# ll
total 716
drwxr-xr-x. 6 root root   4096 Jun  3 23:48 auto
-rwxr-xr-x. 1 root root 277349 Jun  3 23:48 CHANGES
-rwxr-xr-x. 1 root root 422542 Jun  3 23:48 CHANGES.ru
drwxr-xr-x. 2 root root   4096 Jun  3 23:48 conf
-rwxr-xr-x. 1 root root   2481 Jun  3 23:48 configure
drwxr-xr-x. 4 root root     68 Jun  3 23:48 contrib
drwxr-xr-x. 2 root root     38 Jun  3 23:48 html
-rwxr-xr-x. 1 root root   1397 Jun  3 23:48 LICENSE
-rwxr-xr-x. 1 root root    376 Jun  3 23:48 Makefile
drwxr-xr-x. 2 root root     20 Jun  3 23:48 man
drwxr-xr-x. 4 root root   4096 Jun  3 23:48 objs
-rwxr-xr-x. 1 root root     49 Jun  3 23:48 README
drwxr-xr-x. 9 root root     84 Jun  3 23:48 src
[root@storage1 nginx-1.12.1]#
[root@storage1 nginx-1.12.1]# ./configure
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@storage1 nginx-1.12.1]#

 

④ 编译、安装

# make
# make install

⑤ 启动nginx

# cd /usr/local/nginx/sbin/
# ./nginx 

其它命令
# ./nginx -s stop
# ./nginx -s quit
# ./nginx -s reload

⑥ 设置开机启动

 

# vim /etc/rc.local

添加一行:
/usr/local/nginx/sbin/nginx

# 设置执行权限
# chmod 755 rc.local

 

⑦ 查看nginx的版本及模块

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

[root@storage1 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments:

 

⑧ 防火墙中打开Nginx端口(默认的 80) 

添加后就能在本机使用80端口访问了。

# vim /etc/sysconfig/iptables

添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启防火墙:
# service iptables restart

 

3、访问文件

简单的测试访问文件

① 修改nginx.conf

# vim /usr/local/nginx/conf/nginx.conf

添加如下行,将 /group1/M00 映射到 /home/fastdfs/file/data
location /group1/M00 {
    alias /home/fastdfs/file/data;
}

# 重启nginx
# /usr/local/nginx/sbin/nginx -s reload

查看80端口是否监听

# netstat -tunlp | grep 80

[root@storage1 sbin]# netstat -tunlp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      31104/nginx: master

② 在浏览器访问之前上传的图片、成功。

http://192.168.2.136/group1/M00/00/00/wKgCiFz1bm-ATQNlAACLwhA7j18254.jpg

七、FastDFS 配置 Nginx 模块

1、安装配置Nginx模块

① fastdfs-nginx-module 模块说明

  FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储, 但是同组存储服务器之间需要进行文件复制, 有同步延迟的问题。

  假设 Tracker 服务器将文件上传到了 192.168.2.136,上传成功后文件 ID已经返回给客户端。

  此时 FastDFS 存储集群机制会将这个文件同步到同组存储 192.168.2.138,在文件还没有复制完成的情况下,客户端如果用这个文件 ID 在 192.168.2.138 上取文件,就会出现文件无法访问的错误。

  而 fastdfs-nginx-module 可以重定向文件链接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误。

② 下载 fastdfs-nginx-module、解压

# 这里为啥这么长一串呢,因为最新版的master与当前nginx有些版本问题。
# wget https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip

# 解压
# unzip 5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip

# 重命名
# mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1  fastdfs-nginx-module-master

③ 配置Nginx

在nginx中添加模块

# 先停掉nginx服务
# /usr/local/nginx/sbin/nginx -s stop

进入解压包目录
# cd /usr/local/softpackages/nginx-1.12.1

# 添加模块
# ./configure --add-module=../fastdfs-nginx-module-master/src

重新编译、安装
# make && make install

④ 查看Nginx的模块

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

有下面这个就说明添加模块成功

[root@storage1 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --add-module=../fastdfs-nginx-module-master/src
[root@storage1 nginx-1.12.1]#

⑤ 复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录, 并修改

[root@storage1 ~]# cd /usr/local/softpackages/fastdfs-nginx-module-master/src/
[root@storage1 src]#
[root@storage1 src]# cp mod_fastdfs.conf /etc/fdfs/

修改如下配置,其它默认

# vi /etc/fdfs/mod_fastdfs.conf

# 连接超时时间
connect_timeout=10

# Tracker Server
tracker_server=192.168.2.134:22122

# StorageServer 默认端口
storage_server_port=23000

# 如果文件ID的uri中包含/group**,则要设置为true
url_have_group_name = true

# Storage 配置的store_path0路径,必须和storage.conf中的一致
store_path0=/home/fastdfs/file

⑥ 复制 FastDFS 的部分配置文件到/etc/fdfs 目录

[root@storage1 softpackages]# cd fastdfs-5.05/conf/
[root@storage1 conf]#
[root@storage1 conf]# pwd
/usr/local/softpackages/fastdfs-5.05/conf
[root@storage1 conf]#
[root@storage1 conf]# cp anti-steal.jpg http.conf mime.types /etc/fdfs/

⑦ 配置nginx,修改nginx.conf

# vim /usr/local/nginx/conf/nginx.conf

修改配置,其它的默认

在80端口下添加fastdfs-nginx模块

location ~/group([0-9])/M00 {
    ngx_fastdfs_module;
}

注意:

  listen 80 端口值是要与 /etc/fdfs/storage.conf 中的 http.server_port=80 (前面改成80了)相对应。如果改成其它端口,则需要统一,同时在防火墙中打开该端口。

  location 的配置,如果有多个group则配置location ~/group([0-9])/M00 ,没有则不用配group。

⑧ 在/home/fastdfs/file 文件存储目录下创建软连接,将其链接到实际存放数据的目录,这一步可以省略

# ln -s /home/fastdfs/file/data/ /home/fastdfs/file/data/M00 

⑨ 启动nginx

# /usr/local/nginx/sbin/nginx

打印处如下就算配置成功

[root@storage1 ~]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=34082
[root@storage1 ~]#

⑩ 在地址栏访问。

能下载文件就算安装成功。注意和第三点中直接使用nginx路由访问不同的是,这里配置 fastdfs-nginx-module 模块,可以重定向文件链接到源服务器取文件。

http://192.168.2.136/group1/M00/00/00/wKgCiFz1bm-ATQNlAACLwhA7j18254.jpg

参考博客:
用FastDFS一步步搭建文件管理系统

https://www.cnblogs.com/chiangchou/p/fastdfs.html

https://blog.csdn.net/yin_zh0522/article/details/81232041

posted on 2019-06-04 10:17  阿小杜  阅读(1136)  评论(0编辑  收藏  举报