搭建FastDFS+Nginx实现的文件服务器

搭服务器前请准备两台虚拟机,以分别安装FastDFS—tracker和FastDFS—storage

在10.245.36.133安装FastDFS—tracker

在10.245.36.134安装FastDFS—storage

准备安装包libfastcommonV1.0.7.tar.gz、FastDFS_v5.05.tar.gz、FastDFS-nginx-module_v1.16.tar.gz、nginx-1.8.0.tar.gz

  1. FastDFS--tracker安装

在10.245.36.133上安装tracker。

 

  1. 1下载

tracker和storage使用相同的安装包,下载地址:http://sourceforge.net/projects/FastDFS/ 或https://github.com/happyfish100/FastDFS(推荐)

 

本教程下载:FastDFS_v5.05.tar.gz

 

  1. 2.FastDFS安装环境

   FastDFS是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。

   安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++

yum -y install zlib zlib-devel

 

 

  1. 2.1 安装libevent

FastDFS依赖libevent库,需要安装:

yum -y install libevent

 

  1. 2.2 安装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

 

做软连接:

方法1:做如下软连接

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:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录所以需要将/usr/lib64下的库文件拷贝至/usr/lib下。

要拷贝的文件如下:

 

  1. 2.3 tracker编译安装

将FastDFS_v5.05.tar.gz拷贝至/usr/local/下

tar -zxvf FastDFS_v5.05.tar.gz

 

cd FastDFS

 

./make.sh

./make.sh install

 

修改 /etc/init.d/ fdfs_storaged和fdfs_trackerd两个文件

vim /etc/init.d/ fdfs_storaged

:%s+/usr/local/bin+/usr/bin

vim /etc/init.d/fdfs_trackerd

:%s+/usr/local/bin+/usr/bin

 

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

  1. 2.4 配置

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

 

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

cp tracker.conf.sample tracker.conf

 

修改tracker.conf

vi tracker.conf

base_path=/home/yuqing/FastDFS   系统默认

改为:

base_path=/home/FastDFS               放置到自己想要放的位置,/home下面是没有FastDFS    这个文件夹,所以得自己手动创建一个mkdir /home/FastDFS

 

修改防火墙设置:

vim /etc/sysconfig/iptables

增加22122端口:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT

 

自己玩可关闭防火墙,生产环境切勿关闭防火墙:

service iptables stop 关闭防火墙

chkconfig iptables off 禁止开机启动

 

  1. 2.5 启动

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

启动的日志显示先停止5619进程(实际环境不是5619)再启动,如下图:

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

 

  1. 2.6 设置开机自动启动。

 

[root@tracker FastDFS]# vim /etc/rc.d/rc.local

 

将运行命令行添加进文件:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

 

FastDFS--storage安装

在10.245.36.134上安装storage。

 

  1. 1.1 安装libevent

同tracker安装,同时也要安装gcc编译环境

  1. 2.1安装libfastcommon

将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

 

做软连接:

方法1:做如下软连接

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:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录所以需要将/usr/lib64下的库文件拷贝至/usr/lib下。

要拷贝的文件如下:

 

 

  1. 2.2 storage编译安装

将FastDFS_v5.05.tar.gz拷贝至/usr/local/下

tar -zxvf FastDFS_v5.05.tar.gz

 

cd FastDFS

 

./make.sh

./make.sh install

 

修改 /etc/init.d/ fdfs_storaged和fdfs_trackerd两个文件

vim /etc/init.d/ fdfs_storaged

:%s+/usr/local/bin+/usr/bin

vim /etc/init.d/fdfs_trackerd

:%s+/usr/local/bin+/usr/bin

 

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

  1. 2.3 配置

安装成功后进入/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

#如果有多个挂载磁盘则定义多个store_path,如下

#store_path1=.....

#store_path2=......
tracker_server=10.245.36.133:22122   #配置tracker服务器:IP

#如果有多个则配置多个tracker

tracker_server=10.245.36.136:22122

 

 

修改防火墙设置:

vim /etc/sysconfig/iptables

增加23000端口:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT

 

自己玩可关闭防火墙,生产环境切勿关闭防火墙:

service iptables stop 关闭防火墙

chkconfig iptables off 禁止开机启动

 

  1. 2.4 启动

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

启动的日志显示先停止8931进程(实际环境不是8931)再启动,如下图:

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

  1. 设置开机自动启动。

[root@storage1 FastDFS]# vim /etc/rc.d/rc.local

将运行命令行添加进文件:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

 

    1. 上传图片测试
      1. 通过fdfs_test程序

FastDFS安装成功可通过/usr/bin/fdfs_test测试上传、下载等操作。

 

修改/etc/fdfs/client.conf

 

base_path=/home/ FastDFS

tracker_server=10.245.36.133:22122

 

使用格式:

/usr/bin/fdfs_test 客户端配置文件地址  upload  上传文件

 

比如将/home下的图片上传到FastDFS中:

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.png

 

http://10.245.36.133/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png就是文件的下载路径。

对应storage服务器上的

/home/FastDFS/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件。

 

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

 

 

    1. FastDFS 和nginx整合
      1. 在tracker上安装nginx

10.245.36.133安装nginx

nginx的安装细节参考https://blog.csdn.net/a5331270/article/details/81476680

 

在每个tracker上安装nginx,的主要目的是做负载均衡及实现高可用。如果只有一台tracker服务器可以不配置nginx。

      1. 在Storage上安装nginx
        1. FastDFS-nginx-module

将FastDFS-nginx-module_v1.16.tar.gz传至/usr/local/下

cd /usr/local

tar -zxvf FastDFS-nginx-module_v1.16.tar.gz

cd FastDFS-nginx-module/src

修改config文件将/usr/local/路径改为/usr/

将标红这一行:

CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/lcoal/include/fastcommon/"

更改为:

CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

将FastDFS-nginx-module/src下的mod_FastDFS.conf拷贝至/etc/fdfs/下

cp mod_FastDFS.conf /etc/fdfs/

 

并修改mod_FastDFS.conf的内容:

vi /etc/fdfs/mod_FastDFS.conf

tracker_server=10.245.36.133:22122

#tracker_server=10.245.36.136:22122(多个tracker配置多行)

url_have_group_name=true             #url中包含group名称

store_path0=/home/FastDFS   #指定文件存储路径

 

做软连接:

ln -s /home/FastDFS/data /home/FastDFS/data/M00

 

也可将libfdfsclient.so拷贝至/usr/lib下

cp /usr/lib64/libfdfsclient.so /usr/lib/

 

nginx安装

 

添加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    ß这一行是添加FastDFS-nginx-module模块,路径是将FastDFS-nginx-module_v1.16.tar.gz传至/usr/local/下

/usr/local/fastdfs-nginx-module/src

 

 

 

make

make install

        1. nginx配置文件

添加server:

 

server {

        listen       8888;

        server_name  10.245.36.133;

 

        location /group1/M00/{

                #root /home/FastDFS/data;

                ngx_fastdfs_module;

        }

}

 

说明:

server_name指定本机ip

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

 

 

 

      1. 测试

通过java客户端上传文件,使用浏览器访问,比如访问上传图片测试的文件:

 

访问storage:

http://10.245.36.133/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值