目录
一、FastDFS的介绍
FastDFS是一款开源的分布式文件系统,功能主要包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了文件大容量存储和高性能访问的问题。FastDFS特别适合以文件为载体的在线服务,如图片、视频、文档等等服务。
FastDFS作为一款轻量级分布式文件系统,版本V6.01代码量6.3万行。FastDFS用C语言实现,支持Linux、FreeBSD、MacOS等类UNIX系统。FastDFS类似google FS,属于应用级文件系统,不是通用的文件系统,只能通过专有API访问,目前提供了C客户端和Java SDK,以及PHP扩展SDK。
FastDFS为互联网应用量身定做,解决大容量文件存储问题,实现高性能和高扩展性。FastDFS可以看做是基于文件的key value存储系统,key为文件ID,value为文件本身,因此称作分布式文件存储服务更为合适。
FastDFS的架构,如下图所示:
FastDFS特点:
(1)分组存储,简单灵活;
(2)对等结构,不存在单点;
(3)文件ID由FastDFS生成,作为文件访问凭证。FastDFS不需要传统的name server或meta server;
(4)大、中、小文件均可以很好支持,可以存储海量小文件;
(5)一台storage支持多块磁盘,支持单盘数据恢复;
(6)提供了nginx扩展模块,可以和nginx无缝衔接;
(7)支持多线程方式上传和下载文件,支持断点续传;
(8)存储服务器上可以保存文件附加属性。
二、使用的系统软件
名称 | 说明 |
---|---|
Linux发行版 | Ubuntu 18.04.6 |
libfastcommon-1.0.43 | FastDFS分离出的公用函数库 |
FastDFS-6.06 | FastDFS本体 |
fastdfs-nginx-module-1.22 | FastDFS和nginx的关联模块 |
Nginx | Nginx1.24.0 |
FastDFS的源码地址:
Githup地址:https://github.com/happyfish100/fastdfs
Gitee地址:https://gitee.com/fastdfs100/fastdfs
三、安装C/C++编译环境
sudo apt install g++
sudo apt install make
四、FastDFS的安装
1.下载安装libfastcommon
#下载命令
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
#解压命令
tar -zxvf V1.0.43.tar.gz
#进入libfastcommon-1.0.43目录下
cd libfastcommon-1.0.43/
#编译
./make.sh
#安装
./make.sh install
#设置软链接
ln -s /usr/lib64/libfastcommon.so /usr/local/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.下载安装fastdfs
#下载命令
wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
#解压命令
tar -zxvf V6.06.tar.gz
#进入fastdfs-6.06目录下
cd fastdfs-6.06/
#编译
./make.sh
#安装
./make.sh install
#设置软链接
ln -s /usr/bin/fdfs_trackerd /usr/local/bin
ln -s /usr/bin/fdfs_storaged /usr/local/bin
ln -s /usr/bin/stop.sh /usr/local/bin
ln -s /usr/bin/restart.sh /usr/local/bin
#复制fastdfs部分配置文件到/etc/fdfs/目录下
cp fastdfs-6.06/conf/http.conf /etc/fdfs/
cp fastdfs-6.06/conf/mime.types /etc/fdfs/
3.下载安装fastdfs-nginx-module
#下载命令
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
#解压命令
tar -zxvf V1.22.tar.gz
#复制mod_fastdfs.conf配置文件到/etc/fdfs/目录下
cp fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
五、Nginx的安装
1.下载安装nginx依赖插件
#nginx需要安装依赖插件如下,命令
apt-get install openssl libssl-dev -y
apt-get install libpcre3 libpcre3-dev -y
apt-get install zlib1g-dev -y
apt-get install build-essential -y
2.下载安装nginx
#下载命令
wget http://nginx.org/download/nginx-1.24.0.tar.gz
#解压命令
tar -zxvf nginx-1.24.0.tar.gz
#进入nginx-1.24.0目录下
cd nginx-1.24.0
#添加fastdfs-nginx-module模块
./configure --add-module=/usr/local/src/fastdfs-nginx-module-1.22/src/
#编译
make
#安装
make install
#查看模块是否添加成功命令
/usr/local/nginx/sbin/nginx -V
输出 configure arguments: --add-module=/usr/local/src/fastdfs-nginx-module-1.22/src/ 表示模块添加成功
六、FastDFS配置
1.1.tracker配置
#进入/etc/fdfs/目录下复制tracker.conf.sample并去除后缀.sample
cp tracker.conf.sample tracker.conf
#打开配置文件
vim tracker.conf
#需要修改的内容如下
port=22122 # tracker服务器端口(默认22122,一般不修改)
base_path=/home/data/fastdfs # 存储日志和数据的根目录,没有的话需要创建
2.storage配置
#进入/etc/fdfs/目录下复制storage.conf.sample并去除后缀.sample
cp storage.conf.sample storage.conf
#打开配置文件
vim /etc/fdfs/storage.conf
#需要修改的内容如下
port=23000 # storage服务端口(默认23000,一般不修改)
base_path=/home/data/fastdfs # 数据和日志文件存储根目录
store_path0=/home/data/fastdfs # 第一个存储目录
tracker_server=192.168.241.137:22122 # tracker服务器IP和端口
http.server_port=80 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
3.client配置
#进入/etc/fdfs/目录下复制client.conf.sample并去除后缀.sample
cp storage.conf.sample client.conf
#打开配置文件
vim /etc/fdfs/client.conf
#需要修改的内容如下
base_path=/home/data/fastdfs
tracker_server=192.168.241.137:22122 #tracker服务器IP和端口
七、测试
1.启动tracker服务
启动:fdfs_trackerd /etc/fdfs/tracker.conf start
停止:fdfs_trackerd /etc/fdfs/tracker.conf stop
2.启动storage服务
启动:fdfs_storaged /etc/fdfs/storage.conf start
停止:fdfs_storaged /etc/fdfs/storage.conf stop
启动后查看下端口监听,命令: netstat -unltp|grep fdfs 命令查看,有如下输出表示启动成功
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 2234/fdfs_trackerd
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 2246/fdfs_storaged
3.上传测试
#保存后测试,返回ID表示成功 如:group1/M00/00/00/xx.tar.gz
fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/nginx-1.24.0.tar.gz
#返回
group1/M00/00/00/wKjxiWZa-bqAFJlXABD5l7xtYLs.tar.gz
八、配置nginx访问
#打开配置fdfs的nginx模块的配置文件
vim /etc/fdfs/mod_fastdfs.conf
#需要修改的内容如下
tracker_server=192.168.241.137:22122 #tracker服务器IP和端口
url_have_group_name=true
store_path0=/home/data/fastdfs
#配置nginx.config
vim /usr/local/nginx/conf/nginx.conf
#添加如下配置
server {
listen 80; ## 该端口为storage.conf中的http.server_port相同
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
1.启动nginx
/usr/local/nginx/sbin/nginx
浏览器访问:192.168.241.137:80 出现 Welcome to nginx! 表示nginx启动成功。
2.测试下载
用外部浏览器访问刚才已传过的nginx安装包,引用返回的ID
#弹出下载单机部署全部跑通
http://192.168.241.137/group1/M00/00/00/wKjxiWZa-bqAFJlXABD5l7xtYLs.tar.gz
九、开机启动配置
1.查看所有服务
ls /lib/systemd/system,可以看到很多服务,这样不便于确认指定的服务是否存在
ls /lib/systemd/system | grep rc,找到我们关心的rc-local.service服务
2.修改服务配置
sudo vi /lib/systemd/system/rc-local.service
打开后可以看到,文件包含[Unit]和[Service]两个部分内容
一般启动文件需要三个部分:
[Unit] 启动顺序与依赖关系
[Service] 启动行为, 如何启动,启动类型
[Install] 定义如何安装这个配置文件,即怎样做到开机启动
在文件最后加入以下内容
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
3.创建执行文件
sudo vi /etc/rc.local
写入以下内容
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "this shows rc.local is start onboot" > /usr/local/rc-local-info.log
# add your scritp here
# 启动 fdfs_trackerd
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
# 启动 fdfs_storaged
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
# 启动 nginx
/usr/local/nginx/sbin/nginx
exit 0
为rc.local加执行权限
sudo chmod +x /etc/rc.local
4.建立软链接
systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接
ln -s src_file det_file
sudo ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
5.启用服务并启动
#加入开机自启
sudo systemctl enable rc-local
#启动服务
sudo systemctl start rc-local.service
#查看服务状态
sudo systemctl status rc-local.service
6.重启检查
cat /usr/local/rc-local-info.log
#输入内容
this shows rc.local is start onboot #这显示rc.local在启动时启动