1 FastDFS 安装
1.1 安装 VMware 虚拟机
安装 CentOS7 虚拟机。
1.2 FastDFS--tracker 安装
分别在 192.168.101.3 和 192.168.101.4 上安装 tracker。
注:初次安装可只安装一台 tracker,快速体验。
1.2.1 下载
tracker 和 storage 使 用 相 同 的 安 装 包 , 下 载 地 址 :
https://github.com/happyfish100/FastDFS
本教程下载:FastDFS_v5.05.tar.gz
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 运行所需
要的一些基础库。
将 libfastcommonV1.0.7.tar.gz 拷贝至/usr/local/下
cd /usr/local tar -zxvf libfastcommonV1.0.7.tar.gz cd libfastcommon-1.0.7
./make.sh
./make.sh install
注意:
libfastcommon 安装好后会自动将库文件拷贝至/usr/lib64 下,
由于 FastDFS 程序引用 usr/lib 目录所以需要将/usr/lib64 下的库文件拷贝至/usr/lib 下。
要拷贝的文件如下:
1.2.5 tracker 编译安装
将 FastDFS_v5.05.tar.gz 拷贝至/usr/local/下
tar -zxvf FastDFS_v5.05.tar.gz 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)再启动,如下图:
注意:如果没有显示上图要注意是否正常停止原有进程。(第一次启动不显示 exit...,第二次启动才显示)
1.3 FastDFS--storage 安装
分别在 192.168.101.5、192.168.101.6、192.168.101.7、192.168.101.8 上安装 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)再启动,如下图:
注意:如果没有显示上图要注意是否正常停止原有进程。(第一次启动不显示exit...,我第一次就被误导了,第二次启动才有)
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 v5.05
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.
[2015-05-18 02:07:10] DEBUG - base_path=/home/FastDFS, 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
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.101.5, port=23000 server 2. group_name=, ip_addr=192.168.101.6, port=23000
group_name=group1, ip_addr=192.168.101.5, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485.png
source ip address: 192.168.101.5 file timestamp=2015-05-18 02:07:11 file size=5103
file crc32=3979441827
example file url:
http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485.
png storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png
source ip address: 192.168.101.5 file timestamp=2015-05-18 02:07:11 file size=5103 file crc32=3979441827
example file url:
http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_ big.png
http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png 就是文件的下载路径。
对应 storage 服务器上的
/home/fastdfs/fdfs_storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_b ig.png 文件。
由于现在还没有和 nginx 整合无法使用 http 下载。
1.5 FastDFS 和 nginx 整合
1.5.1 nginx 代理 (代理到storage服务器上的nginx)
单独安装 nginx 代理服务,它的作用是代理访问 storage 上的文件,实现负载均衡。 nginx 的安装细节参考 nginx 文档,这里使用单机 nginx,也可以使用两台 nginx 组成高可用或者采用 lvs+nginx 访问 Storage 上的 nginx。
创建 nginx-fdfs.conf 配置文件:
events {
worker_connections 1024;
}http {
#storage群group1组
upstream storage_server_group1{
server192.168.101.5:80 weight=10;
server 192.168.101.6:80 weight=10;
}
#storage群group2组
upstream storage_server_group2{
server 192.168.101.7:80 weight=10;
server 192.168.101.8:80 weight=10;
}
server {
listen 80;
server_name ccc.test.com;
location /group1{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://storage_server_group1;
}
location /group2{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://storage_server_group2;
}
}
}
1.5.2在Storage上安装nginx
在storage server上安装nginx的目的是对外通过http访问storage server上的文件。使用nginx的模块FastDFS-nginx-module的作用是通过http方式访问storage中
的文件,当storage本机没有要找的文件时向源storage主机代理请求文件。
1.5.2.1FastDFS-nginx-module
将FastDFS-nginx-module_v1.16.tar.gz传至fastDFS的storage服务器的/usr/local/下,执行如下命令:
cd /usr/local
tar-zxvfFastDFS-nginx-module_v1.16.tar.gz
cd 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.2nginx安装
将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
make
make install
1.5.2.3 nginx 配置文件
storage服务器上的nginx配置,不是图片服务器nginx的配置
新建一个 nginx 配置文件 nginx-fdfs.conf
添加虚拟主机:
events {
worker_connections 1024;
}http {
server {
listen 80;
server_name 192.168.101.65;
location /group1/M00/{
root /home/FastDFS/fdfs_storage/data;
ngx_FastDFS_module; #如果说 ngx_FastDFS_module 这个报错,改为: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、启动tracker:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
2、启动storage:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
3、启动storage上的nginx:/usr/local/nginx/sbin/nginx ‐c /usr/local/nginx/conf/nginx‐fdfs.conf4、启动图片虚拟主机nginx
1.5.3测试
通过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