本指南详细阐述在Linux服务器上构建FastDFS分布式文件服务器的具体实施步骤。整个流程大致划分为九个关键环节,鉴于操作细节较多,以下将逐步引导完成部署。
FastDFS是由淘宝资深架构师余庆主导开源的分布式文件系统,采用C语言开发。该系统适用于中小企业场景,其特点是对文件不进行分块处理。
系统架构主要包含两个核心组件:负责协调管理的Tracker(跟踪器)和负责数据持久化的Storage(存储节点)。
文件访问路径遵循特定格式:组名 /虚拟盘符{M00/00/02} /文件名。

1.软件包
以下是进行FastDFS部署所需的安装包列表。
第一步:在浏览器地址栏输入https://sourceforge.net/projects/fastdfs/files,页面将显示如下图所示界面。

第二步:下载FastDFS的Server安装包,点击上图中的"FastDFS Server Source Code",进入新版本界面后,选择2014-12-02发布的版本,该版本被公认为最稳定的发布版。
第三步:在版本列表中,点击"FastDFS_v5.05.tar.gz"即可开始下载。

下载完成后,本地将获得相应的压缩包文件。

用户也可以自行从其他渠道获取所需资源。
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
wget http://nginx.org/download/nginx-1.12.1.tar.gz
2.安装gcc
首先检查系统中是否已预装GCC编译器。
gcc –version
若未安装,需执行GCC的安装命令。
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
可将gcc视为C语言编译器,g++作为C++语言编译器使用。(知乎)
3.安装libfastcommon
进入libfastcommon压缩包所在目录,依次执行解压、编译和安装操作。
tar -zxvf libfastcommonV1.0.7.tar.gz
进入解压生成的libfastcommon-1.0.7目录,执行编译命令。
./make.sh
执行安装命令以完成库文件的部署。
./make.sh install
最后,将libfastcommon.so文件复制到/usr/lib目录下,请根据实际文件路径调整复制命令中的源路径。
cp /data/fastDFS/software/libfastcommon-1.0.7/src/libfastcommon.so /usr/lib
4.安装FastDFS
解压FastDFS_v5.05.tar.gz,随后对FastDFS进行编译与安装。
tar -zxvf FastDFS_v5.05.tar.gz
./make.sh
./make.sh install
安装成功后,需要将目录conf内的配置文件拷贝至/etc/fdfs目录下。
cp /data/fastDFS/software/FastDFS/conf/* /etc/fdfs/
5.安装tracker
进入/etc/fdfs目录,编辑tracker.conf配置文件。若该文件不存在,则需将tracker.conf.sample复制并重命名为tracker.conf,随后进行修改。
base_path=/home/yuqing/fastdfs >>> base_path=/home/logs/fastdfs (注解:日志文件)
http.server_port=8080 >>> http.server_port=80 (注解:端口,80是方便默认)
store_group=group1 (注解:组名)
其中,/home/
logs
若/fastdfs目录不存在,请先创建该目录。
之后,启动tracker服务,并检查启动状态(出现如下提示表示启动成功):restart重启
fdfs_trackerd /etc/fdfs/tracker.conf start
netstat -unltp | grep tracker

6.安装storage
由于storage和tracker均运行FastDFS程序,每台服务器需部署一个FastDFS实例,分别承担tracker或storage角色。
修改storage.conf配置文件。若文件不存在,需将storage.conf.sample复制为storage.conf后再进行修改。
base_path=/home/yuqing/fastdfs >>> base_path=/home/logs/storage (日志路径)
store_path0=/home/yuqing/fastdfs >>> store_path0=/home/data/storage (实际储存文件路径,可以配置多个)
tracker_server=192.168.209.121:22122 >>> tracker_server=192.168.2.231:22122 (连接tracker服务器地址)
group_name=group1 (必须和tracker的组名相同)http.server_port=80 (这个端口也要改)
其中,若/home/logs/storage和/home/data/storage目录不存在,需手动创建这些目录。
然后,启动storage服务,并确认启动是否成功(出现如下提示表示启动成功)。
fdfs_storaged /etc/fdfs/storage.conf start
netstat -unltp | grep storage

最后,检查tracker和storage之间是否建立通信连接。
fdfs_monitor /etc/fdfs/storage.conf
若出现如下提示且显示ACTIVE,表明两者均正常启动,此时可进行文件上传测试。

7.测试图片上传
Tracker和Storage均已安装完毕,可使用命令测试文件上传功能。FastDFS提供文件上传命令:usr/bin/fdfs_test用于测试文件上传。测试上传需连接Tracker服务器和Storage服务器,因此需要指定client.conf配置文件,通过Client.conf连接Tracker服务器。
修改/etc/fdfs下的client.conf文件。
base_path=/home/logs/client (日志目录)
tracker_server=192.168.2.231:22122 (tracker端口)
执行测试命令。
/usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg
若返回地址如截图所示,则表示上传成功。

8.安装Nginx。
安装Nginx所需的其他依赖环境,gcc已在前面安装过。本人在安装时未重复此步骤。
#gcc安装
yum install gcc-c++
#PCRE pcre-devel 安装
yum install -y pcre pcre-devel
# zlib 安装
yum install -y zlib zlib-devel
#OpenSSL 安装
yum install -y openssl openssl-devel
执行解压、编译、安装操作。
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure
make
make install
9.安装fastdfs-nginx-module
首先解压fastdfs-nginx-module_v1.16.tar.gz,修改/fastdfs-nginx-module/src/config文件,去掉所有的local(三个)。

拷贝usr/lib64目录下的库文件libfdfsclient.so。
cp /usr/lib64/libfdfsclient.so /usr/lib
在nginx安装目录下执行如下命令:把module添加nginx中。通过设置安装参数方式添加模块。
cd nginx-1.12.1
./configure --add-module=../fastdfs-nginx-module/src
注意:我这里nginx-1.12.1和fastdfs-nginx-module在同一级目录,所以这样写

重新安装编译。
make && make install
查看Nginx的模块。
/usr/local/nginx/sbin/nginx -V
复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录, 并修改
cd fastdfs-nginx-module/src
cp mod_fastdfs.conf /etc/fdfs/
进入/etc/fdfs/修改mod_fastdfs.conf如下配置,其他默认
# 连接超时时间
connect_timeout=10
# Tracker Server
tracker_server=192.168.2.231:22122
# StorageServer 默认端口
storage_server_port=23000
# 如果文件ID的uri中包含/group**,则要设置为true
url_have_group_name = true
# Storage 配置的store_path0路径,必须和storage.conf中的一致
store_path0=/home/data/storage
# the base path to store log files
base_path=/home/logs/storage
配置iNginx ,进入/usr/local/nginx/conf目录下修改nginx.conf
注意:无论你在哪解压安装的。必须进这个目录/usr/local/nginx/conf下修改nginx.conf
vim nginx.conf
修改配置,其它的默认
在80端口下添加fastdfs-nginx模块
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
注意下面#user nobody;改成 #user root;
#user nobody;
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;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name 192.168.2.231;
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
注意:
listen 80 端口值是要与 /etc/fdfs/storage.conf 中的 http.server_port=80 (前面改成80了)相对应。如果改成其它端口,则需要统一,同时在防火墙中打开该端口。
location 的配置,如果有多个group则配置location ~/group([0-9])/M00 ,没有则不用配group。
在文件存储目录下创建软链接,将其链接到实际存放数据的目录,注意这个文件存储的位置后面多个data目录是系统自动生成的
ln -s /home/data/storage/data /home/data/storage/data/M00
启动nginx
#启动nginx
cd /usr/local/nginx/sbin/
./nginx 直接启动/usr/local/nginx/sbin/nginx
#设置开机启动
vim /etc/rc.local
/usr/local/nginx/sbin/nginx
# 设置执行权限
chmod 755 rc.local
查看Nginx是否启动
ps -ef | grep nginx
OK记得云服务的安全组 和防火墙设置需要方通 80、22122、 23000 这个不会的请参考腾讯云服务器tomcat端口无法访问 中的防火墙设置
在地址栏中访问成功 http://192.175.231.128/group1/M00/00/00/rBAACVzBeU2AQBKJAAOHDqS1H9o350.jpg

报错!不存在的。有问题110,120,119
注意:
1.group2同组的Storage2和Storage3 FastDFS服务端口必须一致: port=23000。
2.一台服务器可以装多个组(group)但不能装同组的多个Storage,日志会报错误,日志报错原因是"注意1"
3.Version 4.05之前fastdfs内部绑定了libevent作为http服务器.Version 4.05之后的版本删除了内置的web http服务,内置的web http服务是个累赘,不用也罢!
4.启动storage server时,一直处于僵死状态.启动storage server,storage将连接tracker server,如果连不上,将一直重试。直到连接成功,启动才算真正完成!如果集群中有2台tracker server,而
其中一台tracker没有启动,可能会导致storage server一直处于僵死状态。
到此这篇关于linux搭建FastDFS文件服务器的实现步骤的文章就介绍到这了,更多相关linux搭建FastDFS文件服务器内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!