Nginx && FastDFS实现分布式文件服务器

服务器 分布式
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。

 FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

1. 下载安装 libfastcommon

libfastcommon是从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库,基础环境,安装即可 。

1.1 下载:

  1. wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz 

1.2 解压:

  1. tar -zxvf V1.0.7.tar.gz 

1.3 安装:

  1. cd libfastcommon-1.0.7 
  2.  
  3. ./make.sh 
  4.  
  5. ./make.sh install 

1.4 复制文件:

解决FastDFS中lib配置文件路径问题。

  1. cp/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so  
  2. cp/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so 

2. 下载安装FastDFS

2.1 下载:

  1. wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz 

2.2 解压:

  1. tar -zxvf V5.05.tar.gz 

2.3 安装:

  1. cd fastdfs-5.05 
  2. ./make.sh   
  3. ./make.sh install 

安装完成后,默认配置文件目录为: /etc/fdfs/,默认命令放在 /usr/bin/中,以 fdfs_开头。

3. 配置FastDFS

首先将配置文件复制到 /etc/fdfs目录下。

  1. cd/home/fantj/download/fastdfs-5.05/conf 
  2.  
  3. cp*/etc/fdfs/ 
  4.  
  5. 如下: 
  6.  
  7. [root@s168 conf]#cd/etc/fdfs/ 
  8.  
  9. [root@s168 fdfs]#ls 
  10.  
  11. anti-steal.jpg  client.conf.sample  mime.types    storage.conf.sample  tracker.conf 
  12.  
  13. client.conf     http.conf           storage.conf  storage_ids.conf     tracker.conf.sample 

3.1. 配置tracker.conf

FastDFS跟踪器

修改如下配置:

  1. #Tracker 数据和日志目录地址(根目录必须存在,子目录会自动创建)  
  2. base_path=/fastdfs/tracker  
  3. port=22122 

3.1.1 创建该目录:

mkdir-p/fastdfs/tracker3.1.2 启动/关闭:

fdfs_trackerd/etc/fdfs/tracker.conf start/stop默认使用 /etc/fdfs下的配置文件,如有指定配置可在后面追加参数。如有防火墙,开放防火墙规则。

3.1.3 设置开机启动:

  1. # systemctl enable fdfs_trackerd 
  2.  
  3.  
  4. 或者: 
  5.  
  6. # vim /etc/rc.d/rc.local 
  7.  
  8. 加入配置: 
  9.  
  10. /etc/init.d/fdfs_trackerd start 

3.1.4 tracker server 目录及文件结构 :

Tracker服务启动成功后,会在base_path下创建data、logs两个目录。目录结构如下:

  1. ${base_path} 
  2.  
  3.  |__data 
  4.  
  5.  | |__storage_groups.dat:存储分组信息 
  6.  
  7.  | |__storage_servers.dat:存储服务器列表 
  8.  
  9.  |__logs 
  10.  
  11.  | |__trackerd.log:tracker server日志文件 

3.2. 配置storage

下面只是基本配置,如有更细微的需要,则请查看所有配置。

  1. group_name=fantj    #组名 
  2.  
  3. base_path=/fastdfs/storage  #日志目录 
  4.  
  5. store_path0=/fastdfs/storage    #存储目录 
  6.  
  7. tracker_server=192.168.27.168:22122   #tracker节点 
  8.  
  9. http.server_port=8888   #端口 

3.2.1 启动

  1. 可以用这种方式启动 
  2.  
  3. fdfs_storaged/etc/fdfs/storage.conf start 
  4.  
  5. 也可以用这种方式,后面都用这种 
  6.  
  7. # service fdfs_storaged start 

3.2.2 检查进程

netstat-unltp|grep fdfs3.2.3 Storage 开机启动

  1. # systemctl enable fdfs_storaged  
  2.  
  3. 或者: 
  4.  
  5. # vim /etc/rc.d/rc.local 
  6.  
  7. 加入配置: 
  8.  
  9. /etc/init.d/fdfs_storaged start 

3.2.4 Storage 目录

Storage 启动成功后,在base_path 下创建了data、logs目录,记录着 Storage Server 的信息。

在 store_path0 目录下,创建了N*N个子目录:

  1. [root@s168 data]# pwd 
  2.  
  3. /fastdfs/storage/data 
  4.  
  5. [root@s168 data]# ls 
  6.  
  7. 00  07  0E  15  1C  23  2A  31  38  3F  46  4D  54  5B  62  69  70  77  7E  85  8C  93  9A  A1  A8  AF  B6  BD  C4  CB  D2  D9  E0  E7  EE  F5  FC 
  8.  
  9. 01  08  0F  16  1D  24  2B  32  39  40  47  4E  55  5C  63  6A  71  78  7F  86  8D  94  9B  A2  A9  B0  B7  BE  C5  CC  D3  DA  E1  E8  EF  F6  FD 
  10.  
  11. 02  09  10  17  1E  25  2C  33  3A  41  48  4F  56  5D  64  6B  72  79  80  87  8E  95  9C  A3  AA  B1  B8  BF  C6  CD  D4  DB  E2  E9  F0  F7  fdfs_storaged.pid 
  12.  
  13. 03  0A  11  18  1F  26  2D  34  3B  42  49  50  57  5E  65  6C  73  7A  81  88  8F  96  9D  A4  AB  B2  B9  C0  C7  CE  D5  DC  E3  EA  F1  F8  FE 
  14.  
  15. 04  0B  12  19  20  27  2E  35  3C  43  4A  51  58  5F  66  6D  74  7B  82  89  90  97  9E  A5  AC  B3  BA  C1  C8  CF  D6  DD  E4  EB  F2  F9  FF 
  16.  
  17. 05  0C  13  1A  21  28  2F  36  3D  44  4B  52  59  60  67  6E  75  7C  83  8A  91  98  9F  A6  AD  B4  BB  C2  C9  D0  D7  DE  E5  EC  F3  FA  storage_stat.dat 
  18.  
  19. 06  0D  14  1B  22  29  30  37  3E  45  4C  53  5A  61  68  6F  76  7D  84  8B  92  99  A0  A7  AE  B5  BC  C3  CA  D1  D8  DF  E6  ED  F4  FB  sync 

3.3. 配置client

  1. vim client.conf 
  2.  
  3. tracker_server=192.168.27.168:22122 #tracker节点 
  4.  
  5. base_path=/fastdfs/client  #日志路径 

3.4. 本地图片上传测试

  1. [root@s168 fdfs]#fdfs_upload_file/etc/fdfs/client.conf/home/test.png 
  2. fantj/M00/00/00/wKgbqFu7T7iAJh7lAABDYbhAMC4812.png 
  3. 或者: 
  4. [root@s168 fdfs]#fdfs_test/etc/fdfs/client.conf/home/test.png 
  5.  
  6. ThisisFastDFSclient test program v5.05 
  7. Copyright(C)2008,HappyFish/YuQing 
  8. FastDFSmay be copied only under the terms of the GNUGeneral 
  9.  
  10. PublicLicenseV3,which may be foundintheFastDFSsource kit. 
  11.  
  12. Pleasevisit theFastDFSHomePagehttp://www.csource.org/ 
  13.  
  14. formore detail. 
  15.  
  16. [2018-5-0820:41:15]DEBUG-base_path=/fastdfs/client,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 
  17. invalid operation:/home/test.png 

4. 安装nginx

之前有写过nginx的安装文章,在这里不重复。没有安装过的请点击:

点击查看我的文章:nginx安装

5. FastDFS 配置 Nginx 模块

fastdfs-nginx-module 可以重定向文件链接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误。

5.1 下载安装

  1. wget https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip 
  2.  
  3. unzip5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip 
  4.  
  5. mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1/fastdfs-nginx-module 

5.2 配置Nginx

  1. ngix-s stop 
  2.  
  3. cd nginx-1.12.1 
  4.  
  5. #添加模块 
  6.  
  7. ./configure--add-module=../fastdfs-nginx-module/src 
  8.  
  9. (如果你是yum安装的nginx,我暂时还没找到解决办法,目前的方法是nginx-V查看nginx版本,然后下载一个同版本的nginx, 
  10.  
  11. 然后将/etc/nginx下数据整体备份,make&&make install完成后再做恢复) 
  12.  
  13. #重新编译和安装 
  14.  
  15. make 
  16.  
  17. make install 
  18.  
  19. #拷贝配置文件 
  20.  
  21. [root@s168 src]#pwd 
  22.  
  23. /home/fantj/download/fastdfs-nginx-module/src 
  24.  
  25. [root@s168 src]#cp mod_fastdfs.conf/etc/fdfs/ 

配置 /etc/fdfs/mod_fastdfs.conf文件

  1. #配置 /etc/fdfs/mod_fastdfs.conf文件 
  2.  
  3. base_path=/fastdfs/tmp  #日志目录 
  4.  
  5. store_path0=/fastdfs/storage 
  6.  
  7. tracker_server=192.168.27.168:22122 
  8.  
  9. # the group name of the local storage server 
  10.  
  11. group_name=fantj  #和storage的groupname一一对应 
  12.  
  13. url_have_group_name=true #开启url中附带group_name 

配置nginx

  1. # 配置nginx 
  2.  
  3. [root@s168 fdfs]#cd/usr/local/nginx/conf/ 
  4.  
  5. [root@s168 conf]#vim nginx.conf 
  6.  
  7. #添加如下配置 
  8.  
  9. server{ 
  10.  
  11.    listen88; 
  12.  
  13.    server_name192.168.27.168; 
  14.  
  15.    location/fantj/M00{ 
  16.  
  17.            ngx_fastdfs_module; 
  18.  
  19.    } 
  20.  
  21.  
  22.  
  23. # 进入/usr/local/nginx/sbin目录执行配置检测 
  24.  
  25. [root@s168 sbin]#./nginx  -t 
  26.  
  27. ngx_http_fastdfs_set pid=6431 
  28.  
  29. nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntaxisok 
  30.  
  31. nginx:configuration file/usr/local/nginx/conf/nginx.conf testissuccessful 
  32.  
  33. # 启动nginx 
  34.  
  35. [root@s168 sbin]#./nginx 
  36.  
  37. ngx_http_fastdfs_set pid=6436 

服务重启并测试

  1. [root@s168 fdfs]#fdfs_trackerd tracker.conf restart 
  2.  
  3. waitingforpid[1202]exit... 
  4.  
  5. starting... 
  6.  
  7. [root@s168 fdfs]#fdfs_storaged storage.conf restart 
  8.  
  9. waitingforpid[1211]exit... 
  10.  
  11. starting... 
  12.  
  13. [root@s168 fdfs]#/usr/local/nginx/sbin/nginx-s reload 
  14.  
  15. ngx_http_fastdfs_set pid=6463 

6. 配置成功测试

 

 

责任编辑:武晓燕 来源: Java知音
相关推荐

2019-03-27 08:43:17

Nginx负载均衡服务器

2022-08-24 08:42:59

Minio存储Golang

2021-09-10 10:07:17

Nginx虚拟主机服务器

2023-10-12 10:32:51

2012-10-09 16:43:47

FastDFS分布式文件系统

2012-05-10 14:04:07

分布式文件系统架构

2018-03-23 14:48:27

静态服务器实现

2018-11-07 09:23:21

服务器分布式机器学习

2012-10-11 14:03:56

FastDFS分布式文件系统

2012-10-11 14:31:57

FastDFSMogileFS

2019-07-12 08:37:22

DockerNginx程序员

2012-07-06 09:27:02

云计算分布式服务器负载均衡

2017-03-13 14:02:10

分布式聊天服务器

2018-05-31 09:27:38

服务器架构原理

2018-01-18 17:14:58

分布式文件系统FastDFS

2020-03-09 08:24:06

TengineWeb代理服务器

2023-05-29 14:07:00

Zuul网关系统

2010-07-22 14:02:44

远程文件服务器加密EFS

2011-09-13 09:31:07

文件服务器云存储云计算

2009-02-11 00:09:00

点赞
收藏

51CTO技术栈公众号