Linux下安装FastDFS

传统安装

准备

安装fastDFS需要分别安装fastdfs-nginx-module、fastdfs、nginx、libfastcommon。

本文所用版本:

官方的github下载地址:https://github.com/happyfish100/fastdfs/releases

官网:https://sourceforge.net/projects/fastdfs/files/

1、安装gcc(编译时需要)

yum install -y gcc gcc-c++

2、安装libevent(运行时需要)

yum -y install libevent

3、安装创建目录上传所有文件

mkdir -p /fileservice/fast
cd /fileservice/fast

安装libfastcommon

tar -zxvf libfastcommon-1.0.35.tar.gz	#解压
[root@localhost fast]# cd libfastcommon-1.0.35/

#编译并运行
[root@localhost libfastcommon-1.0.35]# ./make.sh 
[root@localhost libfastcommon-1.0.35]# ./make.sh install

安装fastdfs

1、安装相关依赖库

yum install perl
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel

2、安装fastdfs

进入fast目录:cd /fileservice/fast
解压文件:tar -zxvf fastdfs-5.11.tar.gz
进入解压后的目录:cd fastdfs-5.11
执行编译:./make.sh
安装:./make.sh install

成功之后

3、查看tracker和storage的可执行脚本

ll /etc/init.d/ | grep fdfs #后面配置需要用到

4、准备配置文件,默认在/etc/fdfs/下面

cd /etc/fdfs/

先把配置文件名中的sample去了。[可以复制一份]

cp client.conf.sample client.conf
cp storage.conf.sample storage.conf
cp storage_ids.conf.sample storage_ids.conf
cp tracker.conf.sample tracker.conf

然后修改tracker的存放数据和日志的目录

mkdir -p /home/coydone/fastdfs/tracker

配置和启动tracker

#切换目录到: /etc/fdfs/ 目录下
cd /etc/fdfs/

#修改tracker.conf
vim tracker.conf
22行 base_path=/home/yuqing/fastdfs改为: base_path=/home/coydone/fastdfs/tracker

#启动tracker
service fdfs_trackerd  start

注意:在/home/coydone/fastdfs/tracker 目录下生成两个目录, 一个是数据,一个是日志;

配置和启动storage

由于上面已经安装过FastDFS,这里只需要配置storage就好了。

#切换目录到: /etc/fdfs/ 目录下
cd /etc/fdfs/

#修改storage.conf
vim storage.conf
-------------------------------------- 
11行 group_name=group1 #配置组名
41行 base_path=/home/coydone/fastdfs/storage
#store存放文件的位置(store_path)
109行 store_path0=/home/yuqing/fastdfs改为: store_path0=/home/coydone/fastdfs/storage

#如果有多个挂载磁盘则定义多个store_path,如下
#store_path1=.....
#store_path2=......

#配置tracker服务器:IP
118行 tracker_server=192.168.81.130:22122
-------------------------------------------

#创建/home/coydone/fastdfs/storage 目录
mkdir -p /home/coydone/fastdfs/storage

#启动storage
service fdfs_storaged start

启动完成后进入 /home/coydone/fastdfs/storage/data 目录下,显示目录如下:

使用FastDFS自带工具测试

#切换目录到 /etc/fdfs/ 目录下
cd /etc/fdfs/

#修改client.conf 
vim client.conf

#修改基本路径和tracker_server如下:
第10行 base_path=/home/coydone/fastdfs/storage

第14行 tracker_server=192.168.81.130:22122

注意:若tracker有多个,可以配置多个,如下:
#tracker_server=......
#tracker_server=......

拷贝一张图片到Centos服务器上的/etc/fdfs目录下

进行测试:运行如下(运行测试程序,读取/etc/fdfs/client.conf 文件,上传/etc/fdfs/lolqinnv.jpg 文件)。

[root@localhost fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/lolqinnv.jpg
group1/M00/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg #输出图片路径表示搭建成功

[root@localhost fdfs]# ls /home/coydone/fastdfs/storage/data/00/00/
wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg	#发现在此目录下存在该图片

以上图中的文件地址:http://192.168.81.130/group1/M00/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg

对应storage服务器上的/home/coydone/fastdfs/storage/data/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg文件,由于现在还没有和nginx整合无法使用http下载。

FastDFS 和nginx整合

在tracker上安装 nginx

在每个tracker上安装nginx的主要目的是做负载均衡及实现高可用。如果只有一台tracker可以不配置nginx。

一个tracker对应多个storage,通过nginx对storage负载均衡;

在storage上安装nginx

上传fastdfs-nginx-module-1.20.tar.gz 到Centos服务器上,解压fastdfs-nginx-module-1.20.tar.gz 并移动到 /usr/local目录下。

cd /fileservice/fast/	#切换目录
tar -zxvf  fastdfs-nginx-module-1.20.tar.gz   #解压

#切换目录到: fastdfs-nginx-module-1.20/src 目录下
[root@localhost fast]# cd fastdfs-nginx-module-1.20/src/

#修改config文件,将文件中的所有 /usr/local/ 路径改为 /usr/
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon"

#将fastdfs-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下
[root@localhost src]# cp mod_fastdfs.conf /etc/fdfs/

#修改 /etc/fdfs/mod_fastdfs.conf 的内容
vim /etc/fdfs/mod_fastdfs.conf
------------------------------------------
40行 tracker_server=192.168.81.130:22122 #多个tracker配置多行
53行 url_have_group_name=true #url中包含group名称
62行 store_path0=/home/coydone/fastdfs/storage #指定文件存储路径(上面配置的store路径)
------------------------------------------
#进入之前解压的fastdfs目录的配置目录下(/fastdfs-5.11/conf),把http.conf、mime.conf配置文件移动至/etc/fdfs。
[root@localhost src]# cd /fileservice/fast/fastdfs-5.11/conf
[root@localhost conf]# cp http.conf mime.types /etc/fdfs/

Nginx的安装

cd /fileservice/fast/
tar -zxvf nginx-1.15.2.tar.gz 

[root@localhost fast]# cd nginx-1.15.2/

#加入模块配置命令
[root@localhost nginx-1.15.2]# ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/fileservice/fast/fastdfs-nginx-module-1.20/src

#编译并安装
[root@localhost nginx-1.15.2]# make && make install

#修改nginx配置
cd /opt/nginx/conf
vim nginx.conf

server_name  img.coydone.com;
location ~/group([0-9]) {
    # root   html;
    # index  index.html index.htm;
    ngx_fastdfs_module;
}

#启动nginx
cd /usr/bin/
[root@localhost bin]# ./nginx
ngx_http_fastdfs_set pid=117614 #看到有关fastdfs的输出

#需要关闭Linux防火墙,就可以通过浏览器访问了
#http://192.168.81.130/group1/M00/00/00/wKhRgl-eXhWAEhXVABa36ex1AZY605.jpg

使用Docker安装

# 拉取镜像并启动
docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.149.128 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs registry.cn-beijing.aliyuncs.com/tianzuo/fastdfs

其中-v {HOME}/fastdfs:/var/local/fdfs是指:将{HOME}/fastdfs这个目录挂载到容器里的/var/local/fdfs这个目录里。
所以上传的文件将被持久化到${HOME}/fastdfs/storage/data里,IP 后面是自己的服务器公网ip或者虚拟机ip,
-e WEB_PORT=80 指定nginx端口


#测试上传
#进入容器
docker exec -it fastdfs /bin/bash

#创建文件
echo "Hello FastDFS!">index.html

#测试文件上传
fdfs_test /etc/fdfs/client.conf upload index.html

#浏览器输入 http://192.168.149.128/group1/M00/00/00/wKiVgF3dfV6ANGAyAAAADwL5vO455_big.html访问

配置安全组

coydone的博客
原文地址:https://www.cnblogs.com/coydone/p/13921713.html