FastDFS安装

FastDFS安装
1.1 准备机器

根据实际情况准备多台Linux机器
1.2 FastDFS--tracker安装
分别在多个节点上安装tracker。
注:这里只演示安装一台tracker。
1.2.1 下载
tracker和storage使用相同的安装包,下载地址:https://github.com/happyfish100/FastDFS

1.2.2 FastDFS安装环境
FastDFS是C语言开发,建议在linux上运行,本教程使用Centos7作为安装环境。
安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:

yum install gcc-c++

1.2.3 安装libevent
FastDFS依赖libevent库,需要安装:

yum -y install libevent

1.2.4 安装libfastcommon

libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS运行所需要的一些基础库。
libfastcommon下载地址:https://github.com/happyfish100/libfastcommon


下载后将libfastcommon文件夹(里边包含以上文件)拷贝至/usr/local/下

cd /usr/local/libfastcommon
./make.sh
./make.sh install

注意:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录所以需要将/usr/lib64下的库文件拷贝至/usr/lib下,若/usr/lib下已存在所需文件就不需要拷贝了。
要拷贝的文件如下:

  1.2.5 tracker编译安装

将下载好的FastDFS拷贝至/usr/local/下
cd fastdfs
./make.sh 编译
./make.sh install 安装

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

1.2.6 配置

安装成功后进入/etc/fdfs目录:

  拷贝一份新的tracker配置文件:

cp tracker.conf.sample tracker.conf
修改tracker.conf
vi tracker.conf

base_path=/home/yuqing/FastDFS
改为:
base_path=/home/FastDFS
配置http端口:
http.server_port=80
1.2.7 启动
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
启动的日志显示先停止5619进程(实际环境不是5619)再启动,如下图:

  注意:如果没有显示上图要注意是否正常停止原有进程。

1.3 FastDFS--storage安装
分别在多台上安装storage。
注:这里演示只安装一台storage。

1.3.1 安装libevent
同tracker安装
1.3.2 安装libfastcommon
同tracker安装。
1.3.3 storage编译安装
同tracker编译安装。
1.3.4 配置
安装成功后进入/etc/fdfs目录:
拷贝一份新的storage配置文件:
cp storage.conf.sample storage.conf
修改storage.conf
vi storage.conf
group_name=group1
base_path=/home/yuqing/fastdfs改为:base_path=/home/fastdfs

store_path0=/home/yuqing/fastdfs
改为:store_path0=/home/fastdfs/fdfs_storage
#如果有多个挂载磁盘则定义多个store_path,如下
#store_path1=.....
#store_path2=...... tracker_server=192.168.101.3:22122 #配置tracker服务器:IP
#如果有多个则配置多个tracker
tracker_server=192.168.101.4:22122
#配置http端口
http.server_port=80
1.3.5 启动
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
启动的日志显示先停止8931进程(实际环境不是8931)再启动,如下图:

  注意:如果没有显示上图要注意是否正常停止原有进程。

1.4 上传图片测试
1.4.1 通过fdfs_test程序
FastDFS安装成功可通过/usr/bin/fdfs_test程序来测试上传、下载等操作。
修改/etc/fdfs/client.conf
tracker_server根据自己部署虚拟机的情况配置

  base_path=/home/fastdfs

  tracker_server=192.168.101.3:22122

  tracker_server=192.168.101.4:22122
使用格式:
/usr/bin/fdfs_test 客户端配置文件地址 upload 上传文件
比如将/home下的图片上传到FastDFS中:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.png
打印如下日志:

This is FastDFS client test program v6.02

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/
for more detail.

[2019-11-11 20:03:02] DEBUG - base_path=/home/fastdfs, connect_timeout=10, 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

tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.9.131, port=23000

group_name=group1, ip_addr=192.168.9.131, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKgJg13KLvaAXn65AABdreSfEnY403.jpg
source ip address: 192.168.9.131
file timestamp=2019-11-11 20:03:02
file size=23981
file crc32=3835630198
example file url: http://192.168.9.131/group1/M00/00/00/wKgJg13KLvaAXn65AABdreSfEnY403.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgJg13KLvaAXn65AABdreSfEnY403_big.jpg
source ip address: 192.168.9.131
file timestamp=2019-11-11 20:03:02
file size=23981
file crc32=3835630198
example file url: http://192.168.9.131/group1/M00/00/00/wKgJg13KLvaAXn65AABdreSfEnY403_big.jpg

http://192.168.9.131/group1/M00/00/00/wKgJg13KLvaAXn65AABdreSfEnY403_big.jpg就是文件的下载路径。

对应storage服务器上的
/home/fastdfs/fdfs_storage/data/00/00/wKgJg13KLvaAXn65AABdreSfEnY403_big.png文件。

由于现在还没有和nginx整合无法使用http下载。

1.5 FastDFS 和nginx整合
1.5.1 在Storage上安装nginx
在storage server上安装nginx的目的是对外通过http访问storage server上的文件。使用nginx的模块FastDFS-nginx-module的作用是通过http方式访问storage中的文件,当storage本机没有要找的文件时向源storage主机代理请求文件。
1.5.1.1 FastDFS-nginx-module

下载地址:https://github.com/happyfish100/fastdfs-nginx-module

将下载的fastdfs-nginx-module文件包传至fastDFS的storage服务器的/usr/local/下,然后
cd /usr/local/fastdfs-nginx-module/src
修改config文件将/usr/local/路径改为/usr/
将FastDFS-nginx-module/src下的mod_FastDFS.conf拷贝至/etc/fdfs/下
cp mod_FastDFS.conf /etc/fdfs/
并修改mod_FastDFS.conf的内容:
vi /etc/fdfs/mod_FastDFS.conf
base_path=/home/fastdfs

tracker_server=192.168.101.3:22122
tracker_server=192.168.101.4:22122
url_have_group_name=true #url中包含group名称
store_path0=/home/fastdfs/fdfs_storage #指定文件存储路径
#如果有多个


将libfdfsclient.so拷贝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/


创建nginx/client目录
mkdir -p /var/temp/nginx/client
1.5.2.2 nginx安装
将nginx-1.8.0.tar.gz拷贝到/usr/local下
解压nginx-1.8.0.tar.gz
进入nginx-1.8.0目录,执行如下配置命令:
下边红色部分表示添加fastdfs-nginx-module模块

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs-nginx-module/src

执行以上命令过程可能会由于缺少zlib库或pcre库产生错误,可按以下方式安装相应的库解决

yum -y install pcre-devel

yum install zlib-devel

然后

make
make install
1.5.2.3 nginx配置文件
新建一个nginx配置文件nginx-fdfs.conf.
添加虚拟主机:

server {
    listen 80;
    server_name 192.168.101.65;
    location /group1/M00/{
        root /home/fastdfs/fdfs_storage/data;
        ngx_fastdfs_module;
    }
}

说明:

server_name指定本机ip
location /group1/M00/:group1为nginx 服务FastDFS的分组名称,M00是FastDFS自动生成编号,对应store_path0=/home/FastDFS/fdfs_storage,如果FastDFS定义store_path1,这里就是M01
1.5.3 测试

启动nginx

nginx -c /usr/local/nginx/conf/nginx.conf

通过java客户端上传文件,使用浏览器http访问文件,这里访问上传图片测试的文件:
访问storage:
http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

ip地址改为192.168.101.6也可以访问到文件,因为同一个分组的storage文件互相同步。
访问tracker:
http://192.168.101.3/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png
ip地址改为192.168.101.4也可以访问到文件。
使用域名访问 (推荐):
nginx对外由vip提供服务,使用域名访问如下:
比如vip对应的域名为img.test.com:
http://img.test.com/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

posted @ 2019-11-12 14:18  搬砖的小伙儿  阅读(529)  评论(0编辑  收藏  举报
Fork me on GitHub