linux下搭建FastDFS文件服务器

linux下搭建FastDFS文件服务器

三月 12, 2020 阅读数(请刷新)

1.1、安装gcc环境

FastDFS是C语言开发,安装FastDFS需要先下载源码,然后进行编译,编译需要gcc 环境,所以需要安装gcc环境
打开xshell连接linux系统

1
2
# 查看gcc环境
gcc -version
1
2
# 安装gcc环境
yum -y install gcc-c++


安装FastDFS依赖的libevent库

1
2
# 安装libevent库
yum -y install libevent

1.2、上传安装文件到linux


下载:FastDFS安装文件
提取码:jndg
使用Xftp将文件传到linux下(/usr/local/FastDFS)

或者通过Xshell进入对应目录后使用rz命令传文件

1.3、安装libfastcommon

libfastcommon包含了FastDFS运行所需要的一些基础库
进入刚才安装文件所在的盘符安装解压libfastcommon
执行以下指令

1
2
3
4
5
6
7
8
9
10
11
# 进入盘符
cd /usr/local/FastDFS
# 查看文件
ll
# 解压文件
tar -zxf libfastcommon-1.0.35.tar.gz
cd libfastcommon-1.0.35
ll
# 编译
./make.sh
./make.sh install



libfastcommon安装好后,会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用/usr/lib目录,所以需要将/usr/lib64下的库文件拷贝至/usr/lib下

1
2
3
cd /usr/lib64
# 将文件拷贝至/usr/lib
cp libfastcommon.so /usr/lib

1.4、安装FastDFS(tracker+storage)

将tracker和storage安装在同一服务器下,正常情况应是分别安装

1
2
3
4
5
6
cd /usr/local/FastDFS
tar -zxf FastDFS_v5.08.tar.gz
cd FastDFS
./make.sh
# 安装
./make.sh install


安装成功后将安装目录下的conf文件拷贝至/etc/fdfs下

1
2
cd /usr/local/FastDFS/FastDFS/conf
cp * /etc/fdfs

1.4.1 配置Tracker服务

修改/etc/fdfs/tracker.conf文件

1
2
3
cd /usr/local/FastDFS/FastDFS/tracker
# 修改文件
vim /etc/fdfs/tracker.conf


修改base_path路径为:base_path=/usr/local/FastDFS/FastDFS/tracker
为防止冲突将端口修改 http.server_port=9080(可以不修改)

1
2
# 保存并退出
:wq

启动tracker

1
2
3
4
5
6
# 启动
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
# 重启
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# 关闭服务
service fdfs_trackerd stop

配置开机自启动

1
2
# 编辑文件
vim /etc/rc.d/rc.local

加入/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

1.4.2 配置Storage服务

修改/etc/fdfs/storage.conf文件

1
2
3
cd /usr/local/FastDFS/FastDFS/storage
# 修改文件
vim /etc/fdfs/storage.conf

修改base_path路径为:base_path=/usr/local/FastDFS/FastDFS/storage
修改store_path0路径为:store_path0=/usr/local/FastDFS/FastDFS/storage
因为Tracker(跟踪器)和Storage(存储器)在配置在同一服务器下
所以将 tracker_server=本机ip:22122
为防止冲突将端口修改 http.server_port=9888(可以不修改)

1
2
# 保存并退出
:wq
1
2
3
4
5
6
# 启动
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
# 重启
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# 关闭服务
service fdfs_storaged stop

查看是否启动成功

1
2
netstat -unltp|grep fdfs
# 22122 23000 端口都被监听表示tracker和storage服务都启动成功

配置开机自启动

1
2
# 编辑文件
vim /etc/rc.d/rc.local

加入/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

防火墙打开tracker和storage端口

1
2
3
4
5
6
7
8
# 打开防火墙
vim /etc/sysconfig/iptables
添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT
# 重启防火墙:
service iptables restart

1.5、配置客户端

将/usr/local/FastDFS/client里面的libfdfsclient.so文件拷贝到 /usr/lib下

1
2
3
4
cd /usr/local/FastDFS/FastDFS/client
cp libfdfsclient.so /usr/lib
# 修改文件
vim /etc/fdfs/client.conf

修改base_path路径为:base_path=/usr/local/FastDFS/FastDFS/client
将 tracker_server=本机ip:22122

测试:
在root目录下新建html文件作为测试文件上传

1
vim /root/test.html

上传文件

1
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/test.html


上传成功
如果报错:
ERROR - file: tracker_proto.c, line: 48, server: 192.168.110.128:22122, response status 2 != 0
tracker_query_storage fail, error no: 2, error info: No such file or directory
解决方案:

1
2
3
pkill -9 fdfs
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

查看文件保存目录

1
2
cd /usr/local/FastDFS/FastDFS/storage/data/00/00
ll

1.6、安装Nginx和Nginx插件,允许http访问

1.6.1、安装依赖:

1
2
3
4
5
6
7
8
# gcc,前面已安装
yum install gcc-c++
# PCRE
yum install -y pcre pcre-devel
# zlib
yum install -y zlib zlib-devel
# openssl
yum install -y openssl openssl-devel

1.6.2、解压安装Nginx插件

1
2
3
cd /usr/local/FastDFS
tar -zxf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module

修改fastdfs-nginx-module/src/config文件,去掉local

1
2
# 将local替换为空
:%s/local\///g

将fastdfs-nginx-module/src/mod_fastdfs.conf文件复制到/etc/fdfs目录下,并编辑该文件

1
2
cp mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf

将tracker_server=你的ip:22122
将url_have_group_name = true
将store_path0=/usr/local/FastDFS/FastDFS/storage

1.6.3、解压安装Nginx

1
2
3
4
cd /usr/local/FastDFS/
# 解压Nginx
tar -zxf nginx-1.11.5.tar.gz
cd nginx-1.11.5

对Nginx重新config

1
./configure --add-module=/usr/local/FastDFS/fastdfs-nginx-module/src


1
2
# 编译
make

1
2
# 安装
make install


对Nginx进行配置

1
2
cd /usr/local/nginx/conf
vim nginx.conf


在sever里添加:

1
2
3
location /group1/M00/{
ngx_fastdfs_module;
}

1
2
# 保存退出
:wq

启动nginx进程

1
2
3
4
5
cd ..
cd sbin
./nginx
# 查看进程是否启动
ps -aux | grep nginx


访问路径:

设置Nginx开机自启

1
2
# 编辑文件
vim /etc/rc.d/rc.local

加入/usr/local/nginx/sbin/ngin