代码改变世界

Ubuntu 22.04.1 LTS 安装FastFDS

2023-04-25 15:28  猎手家园  阅读(766)  评论(1编辑  收藏  举报

安装过程一波三折,差点被坑死!

一、简单介绍

1、FastFDS是什么?

FastDFS是阿里余庆用C语言编写的一款开源的分布式文件系统。FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

 

2、FastFDS的三个角色

FastFDS的三个角色:跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)。客户端请求Tracker server进行文件上传、下载,通过Tracker Server调度最终由Storage Server完成文件上传和下载。

  • Tracker Server 作用是负载均衡和调度,负责管理所有的Storage Server和group,每个Storage在启动后会连接Tracker Server,告知自己所属group等信息,并保持周期性心跳。通过Tracker Server在文件上传时可以根据一些策略找到Storage Server提供文件上传服务。可以将Tracker`称为追踪服务器或调度服务器。
  • Storage Server 作用是文件存储,主要提供容量和备份服务;以group为单位,每个group内可以有多台 Storage Server,数据互为备份。客户端上传的文件最终存储在Storage服务器上,Storage Server没有实现自己的文件系统而是利用操作系统的文件系统来管理文件。可以将Storage称为存储服务器。
  • Client 是上传下载数据的服务器,也就是我们自己的项目所部署在的服务器,称为客户端。

 

二、切换数据源

为了避免没必要的坑,强烈建议安装前切换到国内源,推荐阿里源。

直通车:https://www.cnblogs.com/hunttown/p/17352465.html

 

三、准备工作

FastDFS官方开源地址:happyfish100 (YuQing) (github.com)

官方安装文档:Home · happyfish100/fastdfs Wiki (github.com)

 

1、首页配置一个host,原因就是为了避免以后改IP带来的麻烦。

vim /etc/hosts

#host 随便配
127.0.0.1 file.myfastdfs.com

 

2、安装环境变量

apt-get -y install git gcc g++ make automake autoconf libtool pcre2-utils libpcre2-dev zlib1g zlib1g-dev openssl libssh-dev

 

3、先准备几个目录

目录名根据你的喜好随便命名,如果你不想费脑细胞,就按我的来!

因为是测试环境,全部放到了root下

#安装目录
mkdir -p /usr/local/softs

#跟踪器目录
mkdir -p /root/fdfs/tracker

#存储目录
mkdir -p /root/fdfs/storage

#客户端目录
mkdir -p /root/fdfs/client

#文件目录
mkdir -p /root/fdfs/file

 

四、开始安装

1、安装libfastcommon

git clone https://github.com/happyfish100/libfastcommon.git --depth 1
cd libfastcommon/

#编译安装
./make.sh
./make.sh install

 

说明一下 --depth 1 作用:

一般仓库文件不大时,我们都可以用这个方法git clone仓库,但问题是有时候,在仓库历史的某次commit时,有人不小心提交了1G的文件,虽然后面的commit中他把这个文件删除了,但是在.git文件夹中仍然存储着这个文件,所以如果我们克隆仓库这个仓库,会把所有的历史协作记录都clone下来,这样整个文件会非常大,其实对于我们直接使用仓库,而不是参与仓库工作的人来说,只要把最近的一次commit给clone下来就好了。这就好比一个产品有很多个版本,我们只要clone最近的一个版本来使用就行了。实现这个功能就需要用到git clone --depth=1命令

  • 用 git clone --depth=1 的好处是限制 clone 的深度,不会下载 Git 协作的历史记录,这样可以大大加快克隆的速度
  • depth 用于指定克隆深度,为1即表示只克隆最近一次commit
  • 适合用 git clone --depth=1 的场景:你只是想clone最新版本来使用或学习,而不是参与整个项目的开发工作

 

2、安装libserverframe

git clone https://github.com/happyfish100/libserverframe.git --depth 1
cd libserverframe/

#编译安装
./make.sh
./make.sh install

 

3、安装FastDFS

git clone https://github.com/happyfish100/fastdfs.git --depth 1
cd fastdfs/

#编译安装
./make.sh
./make.sh install

#配置文件准备
cp /usr/local/softs/fastdfs/conf/http.conf /etc/fdfs/  #供nginx访问使用
cp /usr/local/softs/fastdfs/conf/mime.types /etc/fdfs/ #供nginx访问使用

 

4、安装fastdfs-nginx-module

git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1

cp /usr/local/softs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs

 

五、配置(以下配置均为单机部署)

1、配置FastDFS跟踪器(Tracker)

cd /etc/fdfs
vim tracker.conf
# 提供服务的端口
port=22122

# Tracker 数据和日志目录地址(根目录必须存在,子目录会自动创建)
base_path=/root/fdfs/tracker

# HTTP 服务端口
http.server_port=80

# 其它配置默认即可

 

启动 Tracker

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

查看是否启动成功

netstat -tunpl | grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      2543/fdfs_trackerd

 

2、配置FastDFS存储(Storage)

cd /etc/fdfs
vim storage.conf
# storage server 服务端口
port=23000

# Storage 数据和日志目录地址(根目录必须存在,子目录会自动生成)
base_path=/root/fdfs/storage

# 如果不配置 store_path0,那它就和 base_path 对应的路径一样。
store_path0=/root/fdfs/file

# tracker_server 的列表 ,会主动连接 tracker_server
# 有多个 tracker server 时,每个 tracker server 写一行
tracker_server=file.myfastdfs.com:22122

# 访问端口
http.server_port=80

# 其它配置默认即可

 

启动 Storage

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

查看是否启动成功

netstat -tunpl | grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      2543/fdfs_trackerd  
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      2629/fdfs_storaged

 

查看Storage和Tracker是否在通信

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

server_count=1, server_index=0

tracker server is 127.0.1.1:22122

group count: 1

Group 1:
group name = group1
disk total space = 18,974 MB
disk free space = 8,944 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 80
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

Storage 1:
id = 127.0.0.1
ip_addr = 127.0.0.1 ACTIVE
http domain =
version = 6.9.4
join time = 2023-04-25 03:07:04
up time = 2023-04-25 03:07:04
total storage = 18,974 MB
free storage = 8,944 MB
upload priority = 10

注意查看上文红色加粗的部分!

 

3、配置客户端(Client)

cd /etc/fdfs
vim client.conf
# Client 的数据和日志目录
base_path=/root/fdfs/client

# Tracker端口
tracker_server=file.myfastdfs.com:22122

# 其它配置默认即可

 

4、文件上传测试

 我们先在 /root/mysource 目录下上传一张名为 1001688.jpg 的图片

/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/mysource/1001688.jpg

上传完成后返回结果:group1/M00/00/00/fwAAAWRHRsOAeZKaAAjHBJUWbtk118.jpg

文件ID由:group、存储目录、两级子目录、file_id、文件后缀名(由客户端指定,主要用于区分文件类型)拼接而成。

文件保存位置:/root/fdfs/file/data/00/00

文件最终访问路径:http://file.myfastdfs.com/group1/M00/00/00/fwAAAWRHRsOAeZKaAAjHBJUWbtk118.jpg

但是在浏览器中还不能够直接访问文件,需要配置nginx。

 

六、安装nignx

nginx安装直通车:https://www.cnblogs.com/hunttown/p/17352390.html 

先看完这里再安装nginx

FastFDS有一个nginx模块 fastdfs-nginx-module

它的作用是:

FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但是同组存储服务器之间需要进行文件复制,有同步延迟的问题。

假设Tracker服务器将文件上传到了192.168.30.1,上传成功后文件ID已经返回给客户端。

此时FastDFS存储集群机制会将这个文件同步到同组存储192.168.30.2,在文件还没有复制完成的情况下,客户端如果用这个文件ID在192.168.30.2上取文件,就会出现文件无法访问的错误。

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

因此,在编译 nginx 时,要加入这个配置:--add-module=/usr/local/softs/fastdfs-nginx-module/src/

完整编译参数:

./configure --prefix=/usr/local/nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module \
--add-module=/usr/local/softs/fastdfs-nginx-module/src/

 

其它的都一样!

 

七、测试

1、在启动 nginx 前先配置一下:mod_fastdfs

cd /etc/fdfs
vim mod_fastdfs.conf
# 连接超时时间
connect_timeout=10

# Tracker Server
tracker_server=file.myfastdfs.com:22122

# StorageServer 默认端口
storage_server_port=23000

# 如果文件ID的uri中包含/group**,则要设置为true
url_have_group_name = true

# Storage 配置的store_path0路径,必须和storage.conf中的一致
store_path0=/root/fdfs/file

# 其它配置默认即可

 

2、配置 nginx.conf

在80端口下添加fastdfs-nginx模块:

location ~/group([0-9])/ {
    ngx_fastdfs_module;
}

注意:listen 80端口值是要与 /etc/fdfs/storage.conf 中的 http.server_port=80 相对应。如果改成其它端口,则需要统一。

有一点要特别注意:

如果你是在 root 下运行的,那么nginx启动权限要设置成 root,否则会报:Nginx 13: Permission denied 的错误

附:nginx.conf

user  root;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  65;
    
    server {
        listen       80;
        server_name  file.myfastdfs.com;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~/group([0-9])/ {
            ngx_fastdfs_module;
        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
            root   html;
        }
    }
}

 

3、启动 nginx

/usr/local/nginx/sbin/nginx

如果下方打印有:ngx_http_fastdfs_set pid=xxx 的日志,说明配置成功!

 

4、浏览器访问

由于域名 file.myfastdfs.com 是我们随意写的,所以需要在我们的windows系统中配置一下hosts

如果你是在ubuntu系统中直接访问可以忽略这步。因为我的ubuntu系统只是一个服务器,不带桌面功能。

# 192.168.30.244 是ubuntu内网ip
192.168.30.244        file.myfastdfs.com

然后在浏览器中输入上面的地址即可!

http://file.myfastdfs.com/group1/M00/00/00/fwAAAWRHRsOAeZKaAAjHBJUWbtk118.jpg

 

到此,全部结束!