fastDFS单节点搭建

集群环境查看之前的文档

单机版搭建

安装依赖

1
# yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel -y

创建数据目录

1
# mkdir -p /data/fastdfs/{tracker,storage}

下载安装包

安装libfatscommon

1
2
git clone https://github.com/happyfish100/libfastcommon.git --depth 1
./make.sh && ./make.sh install

安装FastDFS

1
2
3
git clone https://github.com/happyfish100/fastdfs.git --depth 1
cd fastdfs/
./make.sh && ./make.sh install

配置文件生成

1
2
3
4
5
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf #tarcker服务配置文件
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf #storage服务配置文件
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客户端文件,测试用
cp conf/http.conf /etc/fdfs/ #供nginx访问使用
cp conf/mime.types /etc/fdfs/ #供nginx访问使用

安装fastdfs-nginx-module需要在编译nginx时候添加这个模块

1
2
git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

安装nginx

1
2
3
4
5
~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure --add-module=/root/fastdfs-nginx-module/src/
make && make instal

tracker配置

1
2
3
4
5
6
7
vim /etc/fdfs/tracker.conf
#需要修改的内容如下
port=22122 # tracker服务器端口(默认22122,一般不修改)
base_path=/data/fastdfs/tracker # 存储日志和数据的根目录
#保存后启动
/etc/init.d/fdfs_trackerd start #启动tracker服务
chkconfig fdfs_trackerd on #自启动tracker服务

storage配置

1
2
3
4
5
6
7
8
9
10
vim /etc/fdfs/storage.conf
#需要修改的内容如下
port=23000 # storage服务端口(默认23000,一般不修改)
base_path=/data/fastdfs/storage # 数据和日志文件存储根目录
store_path0=/data/fastdfs/storage/ # 第一个存储目录
tracker_server=192.168.1.38:22122 # tracker服务器IP和端口
http.server_port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)其实现在这版本不需要
#保存后启动
/etc/init.d/fdfs_storaged start #启动storage服务
chkconfig fdfs_storaged on

检查fdfs状态
fdfs_monitor /etc/fdfs/storage.conf

配置nginx访问

vim /etc/fdfs/mod_fastdfs.conf

1
2
3
4
需要修改的内容如下
tracker_server=192.168.1.38:22122
url_have_group_name=true
store_path0=/data/fastdfs/storage

1
2
3
4
5
6
7
8
9
10
11
12
13
vi /usr/local/nginx/conf/nginx.conf
#添加如下配置
server {
listen 8888; ## 该端口为storage.conf中的http.server_port相同 其实没必要
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

vim /etc/fdfs/client.conf

修改的内容如下

1
2
base_path=/data/fastdfs/tracker
tracker_server=192.168.1.38:22122 #tracker IP地址
1
2
3
4
[root@ecs-1d0e ~]# fdfs_upload_file  /etc/fdfs/client.conf /root/1.txt 
group1/M00/00/00/wKgBJlyrRtWAR5dVAAAAB4upymk232.txt
[root@ecs-1d0e ~]# curl http://192.168.1.38:8888/group1/M00/00/00/wKgBJlyrRtWAR5dVAAAAB4upymk232.txt
111111

FastDFS 防盗链开启

但是这样是不安全的,因为只要知道ip和文件路径,就能下载所需文件。因此采用Token方式防盗链。
cp /root/fastdfs/conf/anti-steal.jpg /etc/fdfs/
vim /etc/fdfs/http.conf

1
2
3
4
5
#开启token校验  
http.anti_steal.check_token=true
#设置校验失败后显示的警告图片
http.anti_steal.token_check_fail=/etc/fdfs/anti-steal.jpg
重启nginx