FastDFS存储

一、Fastdfs搭建

1.1 安装依赖
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel \
zlib zlib-devel openssl-devel wget vim -y
1.2 安装libevent
# download: https://libevent.org/
tar xf libevent-2.1.12-stable.tar.gz
cd libevent-2.1.12-stable/
./configure
make
make install
ls -al /usr/local/lib | grep libevent
1.3 安装LibFastCommon
# download: https://github.com/happyfish100/libfastcommon/releases
tar xf libfastcommon-1.0.43.tar.gz
cd libfastcommon-1.0.43/
./make.sh
./make.sh install
1.4 安装fastdfs
# download: https://github.com/happyfish100/fastdfs/releases
tar xf fastdfs-6.06.tar.gz
cd fastdfs-6.06/
./make.sh
./make.sh install
ll /usr/bin/fdfs_* |wc -l
ll /etc/fdfs/
ll /etc/init.d/fdfs_* |wc -l
cp conf/{anti-steal.jpg,http.conf,mime.types} /etc/fdfs/   #完善fdsf服务
1.5 创建目录
mkdir /data/resource/fastdfsdb             #根目录
mkdir -p /data/resource/fastdfsdb/storage1 #存储组目录
mkdir -p /data/resource/fastdfsdb/tracker  #跟踪器目录
mkdir -p /data/resource/fastdfsdb/client   #客户端目录
1.6 配置Tracker
# 生成配置文件
cp -a /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vim /etc/fdfs/tracker.conf

# 关注如下几个配置
disabled = false    #启用配置文件,如果是true则关闭
base_path=/data/resource/fastdfsdb/tracker  ## 存储数据和日志文件的基本路径
http.server_port=80   ## Http服务端口
bind_addr = 0.0.0.0  ## 监听地址
port=22122   ## 默认提供服务端口
connect_timeout = 5   ## 连接超时
max_connections = 10000  ## 最大连接数
network_timeout = 60  ## 网络超时

# 启动服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
netstat -ntlp |grep 22122
1.7 配置Storage
## 生成配置文件
cp -a /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vim /etc/fdfs/storage.conf

## 关注如下几个配置
disabled = false  #启用配置文件,如果是true则关闭
bind_addr = 0.0.0.0  ## 监听地址
base_path=/data/resource/fastdfsdb/storage1   ## storage存储data和log的跟路径
group_name=group1   ## 默认组名
port=23000          ## 默认端口,相同组的storage端口号必须一致
store_path0=/data/resource/fastdfsdb/storage1  ## 配置一个存储路径
tracker_server=10.0.0.26:22122            ## 配置跟踪器IP和端口,不要用127.0.0.1,否则起不来

## 启动服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
netstat -ntlp |grep 23000
1.8 配置Client测试
## 生成配置文件
cp -a /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf

## 关注如下几个配置
base_path=/data/resource/fastdfsdb/client  ## client数据和日志目录
tracker_server=10.0.0.26:22122  ## 配置跟踪器IP和端口

#客户端测试,返回文件上传的相对路径和编号为成功
touch /tmp/1.jpg
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /tmp/1.jpg

# 文件成功上传storage服务器,但是还无法查看下载,需要安装Nginx服务器用来支持Http方式访问文件;
1.9 安装nginx
# 准备好fastdfs-nginx模块
# https://github.com/happyfish100/fastdfs-nginx-module/releases
tar xf fastdfs-nginx-module-1.22.tar.gz -C /tmp/

# 编译nginx
wget -c http://nginx.org/download/nginx-1.18.0.tar.gz
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0/

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' \
--add-module=/tmp/fastdfs-nginx-module-1.22/src

make
make install

/usr/local/nginx/sbin/nginx -V
1.10 配置客户端
cp /tmp/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf

## 调整配置
connect_timeout=30   ## 客户端访问文件连接超时时长,单位秒
base_path=/data/resource/fastdfsdb/client  # 存储日志路径
tracker_server=10.0.0.26:22122  ## 配置跟踪器IP和端口
url_have_group_name = true      ## 路径包含group
group_name=group1 #和storage的groupname一一对应
store_path0=/data/resource/fastdfsdb/storage1  # 必须和storage配置相同
1.11 配置Nginx
vim /usr/local/nginx/conf/nginx.conf

#这个与tracker_server中定义的http.server_port一致
server {
    listen       80;
    location ~/group([0-9])/M00 {
        ngx_fastdfs_module;
    }
}

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
1.12 访问资源
  1. 先上传一个图片资源到服务器 ;
  2. 获取到返回的相对路径和编号,在浏览器拼接一下nginx的地址进行访问;
  3. 正常访问到资源表示成功。