又是四个月没有写过博客了。最近看到网上有人做的仿微信的全栈项目,其中用到了FastDFS作为图片服务器,学习了以下,以下是学习的过程。
FastDFS,是一个使用c语言开发的分布式文件系统,个人理解就是一种轻量级的分布式文件系统(比起HDFS来讲),由tracker_server和storage_server两部分组成。
FastDFS可以和nginx配合,直接通过浏览器就可以访问分布式文件系统中存储的文件。
配置过程:
1.安装libfastcommon
这是一个库,FastDFS需要使用它才能运行,需要注意的是安装完成后,这个库的路径和其它库的路径有所不同,需要将.so文件和.h文件拷贝到/usr/local/lib和/usr/local/include目录下,避免运行时报错库找不到或者头文件找不到。
注意,此处默认安装好了make和gcc
(1)获取安装源文件
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.38.tar.gz
(2)解压
tar -zxvf V1.0.38.tar.gz
(3)进入目录
cd libfastcommon-1.0.38
(4)编译
./make.sh
(5)安装
./make.sh install
2.安装FastDFS
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
tar -zxvf V5.11.tar.gz
cd fastdfs-5.11
./make.sh
./make.sh install
此时,就能在/usr/bin和/usr/sbin目录下,看到一系列fdfs开头的可执行文件,以/usr/sbin为例:
3.配置tracker服务
(1)进入/etc/fdfs目录,有很多.sample后缀的文件,通过cp命令拷贝tracker.conf.sample,删除.sample后缀作为正式文件。
(2)vim tracker.conf,修改相关参数
base_path=/home/mm/fastdfs/tracker #tracker存储data和log的跟路径,必须提前创建好 port=22122 #tracker默认22122
http.server_port=8092 #http端口,需要和nginx监听的端口相同
(3)启动tracker
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
(4)查看启动日志
进入刚刚指定的base_path(/home/mm/fastdfs/tracker)中有个logs目录,查看tracker.log文件,看启动过程是否有报错。
4.配置storage服务
(1)进入/etc/fdfs目录,有cp命令拷贝storage.conf.sample,删除.sample后缀作为正式文件
(2)vim storage.conf,修改相关参数
base_path=/home/mm/fastdfs/storage #storage存储data和log的跟路径,必须提前创建好 port=23000 #storge默认23000,同一个组的storage端口号必须一致
group_name=group1 #默认组名,可以理解为公司名称,可以修改,修改后nginx访问路径会不一样
store_path_count=1 #存储路径个数,需要和store_path个数匹配 store_path0=/home/mm/fastdfs/storage #如果为空,则使用base_path tracker_server=192.168.1.199:22122 #配置该storage监听的tracker的ip和port
(3)启动storage
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
(4)查看启动日志
进入刚刚指定的base_path(/home/mm/fastdfs/storage)中有个logs目录,查看tracker.log文件,看启动过程是否有报错。
(5)通过monitor来查看storage是否成功绑定
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
5.安装nginx和fastdfs-nginx-module模块
(1)下载
wget http://nginx.org/download/nginx-1.15.2.tar.gz
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
(2)解压
tar -zxvf nginx-1.15.2.tar.gz
tar -xvf V1.20.tar.gz
(3)安装nginx依赖的库
apt-get install libpcre3 libpcre3-dev openssl libssl-dev libperl-dev
(4)进入nginx目录,配置,并加载fastdfs-nginx-module模块:
cd nginx-1.10.1
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module-1.20/src/
(5)编译并且安装
make & make install
(6)进入nginx,配置nginx.conf
cd /usr/local/nginx/conf
vim nginx.conf
在server的下层目录,加入以下内容
location ~/M00{
root /home/fastdfs/storage/data;
ngx_fastdfs_module;
}
(7)拷贝fastdfs解压目录下的mime.types和http.conf两个文件至/etc/fdfs
cp mime.types http.conf /etc/fdfs/
(8)进入/etc/fdfs,vim mod-fastdfs.conf
base_path=/home/mm/fastdfs
tracker_server=192.168.1.199:22122 #tracker的地址
url_have_group_name=true #url是否包含group名称
storage_server_port=23000 #需要和storage配置的相同
store_path_count=1 #存储路径个数,需要和store_path个数匹配 store_path0=/home/mm/fastdfs/storage #文件存储的位置
(9)启动nginx
cd /usr/local/nginx/sbin
./nginx
6. 测试
(1)进入/etc/fdfs目录,用cp命令拷贝client.conf.sample,删除.sample后缀作为正式文件
(2)vim client.conf
base_path=/home/mm/fastdfs/tracker #tracker服务器文件路径
tracker_server=192.168.1.199:22122 #tracker服务器IP地址和端口号 http.tracker_server_port=8092 #nginx服务器的http端口号,必须和tracker的设置对应
(3)新建一个文件1.txt,随便写入一些内容
(4)执行命令
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf 1.txt
(5)返回了一个字符串:
group1/M00/00/00/AMSV01hahabAbqN0bcAArL12345677.txt
(6)进入storage的对应目录,就可以看到这个文件
(7)通过http形式查看文件
wget http://192.168.1.199:8092/group1/M00/00/00/AMSV01hahabAbqN0bcAArL12345677.txt
当然,也可以通过浏览器打开
(8)通过命令行下载文件
/usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/AMSV01hahabAbqN0bcAArL12345677.txt a.txt
(9)通过命令行删除文件
/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/AMSV01hahabAbqN0bcAArL12345677.txt
踩过的坑:
1. nginx启动后,只有master进程而没有worker进程
我碰到的这种情况,是tracker和storage的配置文件中,有路径配错了,然后导致nginx的worker进程无法启动。
看网上说,有其它错误的可能性,但是我在学习的过程中没有碰到。
2.网上发现的,在编译nginx时,报错
/usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No such file or directory
解决方案:修改fastdfs-nginx-module-1.20/src/config文件,然后重新第7步开始
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/" CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"