欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fastdfs安装配置(整合nginx和fastdfs)

程序员文章站 2022-05-30 12:04:39
...

fastdfs安装配置、

一、简介

1.1 环境

名称 资源地址 介绍
fastdfs https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz 文件服务器
nginx http://nginx.org/download/nginx-1.11.8.tar.gz 反向代理服务器
libfastcommon https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
fastdfs-nginx-module http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz nginx插件,用来整合fastdfs

资源我全部下载到/opt/resources/目录下,解压到/opt/目录下

1.2 Fastdfs架构

FastDFS包括Tracker server和Storage server, 客户端请求Tracker server进行文件上传、下载,通过Tracker server调度最终由Storage server完成文件上传和下载。 Tracker server 的角色类似于dubbo的registry和moniter、并不直接提供服务、而是storage server启动时注册到tracker server, client通过tracker server连接storage server, client不知道自己连接的是哪一台storage server, 连接完成后、上传和下载是client直接请求storage server, 可类比于 dubbo consumer通过registry连接dubbo service ,但连接完成之后是consumer和service直接通信
fastdfs安装配置(整合nginx和fastdfs)

  • 文件上传流程
    fastdfs安装配置(整合nginx和fastdfs)

  • 文件下载流程
    fastdfs安装配置(整合nginx和fastdfs)

二、Fastdfs安装配置

2.1 安装libfastcommon

cd /opt/resources
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
//修改名字
mv V1.0.7 libfastcommon-1.0.7.tar.gz
tar -zxvf libfastcommon-1.0.7.tar.gz -C /opt/
cd /opt/libfastcommon-1.0.7/
//编译
./make.sh
//安装
./make.sh install

另外:
设置几个软链接、方便后续扩展nginx时使用:
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/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.2 tracker安装

//下载资源到/opt/resources
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
mv V5.05 FastDFS_v5.05.tar.gz
//解压
tar -zxvf FastDFS_v5.05.tar.gz -C /opt
cd /opt/fastdfs-5.05/
//编译
./make.sh
//安装
./make.sh install
  • 修改tracker配置文件
安装完成后、在/etd/fdfs下有tracker的配置文件
复制一份:cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
mkdir -p /usr/local/fastdfs/  (此处可以根据自己的情况和习惯存放)
vi /etc/fdfs/tracker.conf
------------------------------------------------------------
//调整base_path
base_path= /usr/local/fastdfs/
------------------------------------------------------------

//启动 tracker 服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
//重启 tracker 服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
//查看是否有 tracker 进程
ps aux | grep tracker

2.3 storage(存储节点)服务部署

一般 storage 服务我们会单独部署到一台服务器上,但是这里为了方便(我只有一台服务器)就安装在同一台上了
如果单独部署到一台服务器上、上边tracker的部署步骤重新来一遍即可
这里是同一台server、只修改配置

//创建目录
mkdir  /usr/local/fastdfs/storage/
//复制一份配置
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vim /etc/fdfs/storage.conf
------------------------------------------------------------
base_path= /usr/local/fastdfs/

store_path0= /usr/local/fastdfs/storage/

//图片实际存放路径,如果有多个,这里可以有多行(要创建多个目录):
//store_path0=/opt/fastdfs/storage/
//store_path1=/opt/fastdfs/storage1/

//是用来配置目录个数的、如果只是练习不做实际存储服务、可改小一点儿
subdir_count_per_path=256 

//指定 tracker 服务器的 IP 和端口
//(39.96.46.193)是你的server服务器ip、本机也可以使用(0.0.0.0:22122)、记得不可使用127.0.0.1
tracker_server=39.96.46.193:22122 
------------------------------------------------------------

如果用的是阿里云服务器,需要配置安全组开放22122、23000端口

  • 启动 storage 服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf,首次启动会很慢,因为它在创建预设存储文件的目录
//重启 storage 服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
  • 查看storage tracker进程
ps aux | grep fdfs
------------------------------------------------------------------------------------
[aaa@qq.com fdfs]# ps aux | grep fdfs
root      3083  0.0  0.1 144548  2576 ?        Sl   Mar21   0:11 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
root      3484  0.0  3.5 278252 66620 ?        Sl   Mar21   0:38 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
root     13833  0.0  0.0 112704   972 pts/1    S+   16:48   0:00 grep --color=auto fdfs
-----------------------------------------------------------------------------------
  • 查看tracker是否可以正常与storage通信
fdfs_monitor /etc/fdfs/storage.conf
-----------------------------------------------------------------------------------
Storage 1:
        id = 39.96.46.193
        ip_addr = 39.96.46.193  ACTIVE --若看到ACTIVE这个字样、代表可以正常通信
-----------------------------------------------------------------------------------
查看storage和tracker是否正常启动:

2.4 配置fdfs_client

复制一份配置:cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
-----------------------------------------------------------------------------------
base_path= /usr/local/fastdfs/

//指定 tracker 服务器的 IP 和端口
tracker_server=39.96.46.193:22122
log_level=info
-----------------------------------------------------------------------------------
// 文件上传测试
echo asasasa > ~/test.txt
fdfs_test /etc/fdfs/client.conf upload ~/test.txt 
可以看到如下图所示、就是上传成功了

fastdfs安装配置(整合nginx和fastdfs)
上传成功之后也无法根据url地址访问资源,只有整合好nginx之后才能访问

2.5 安装nginx以及插件整合fastdfs

如果Nginx已经安装过,则仅需要fastdfs-nginx-module_v1.16.tar.gz
cd /opt/resources
//下载nginx
wget http://nginx.org/download/nginx-1.11.8.tar.gz
//下载Nginx插件
wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
//解压 Nginx 模块:
tar zxvf fastdfs-nginx-module_v1.16.tar.gz -C /opt
//进入解压后的目录 
cd /opt/fastdfs-nginx-module
vim src/config
-----------------------------------------------------------------------------------
修改:去掉local、因为实际安装fastdfs时、是放到了/usr/include下
 - CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
-> CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

 -  CORE_LIBS="$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient"
-> CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
-----------------------------------------------------------------------------------
//回到nginx的解压目录
cd /opt/nginx-1.11.8
sudo ./configure  --prefix=/usr/local/nginx --sbin-path=/usr/local/bin/nginx --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --error-log-path=/usr/local/var/log/nginx/^Cror.log --http-log-path=/usr/local/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-file-aio --add-module=/opt/fastdfs-nginx-module/src
//编译
make  
//安装
make install (若是有权限的账户、可以不用加sudo、我使用的是普通用户)
  • 整合nginx和fastdfs插件
//copy fastdfs-nginx-module的配置文件到 /etc/fdfs下、方便查找
cp /opt/fastdfs-nginx-module/src/mod_fdfs.conf /etc/fdfs
vi /etc/fdfs/mod_fdfs.conf
-----------------------------------------------------------------------------------
base_path=/usr/local/fastdfs
tracker_server=39.96.46.193:22122
url_have_group_name = true
store_path0=/usr/local/fastdfs/storage
-----------------------------------------------------------------------------------
  • 配置Nginx
vi /usr/local/nginx/conf/nginx.conf
-----------------------------------------------------------------------------------
server {
        listen       80;
        server_name  localhost;
        ...
         # 配置fastdfs的访问路径
        location /group1/M00 {
            ngx_fastdfs_module;
        }
        ...
    }
    -----------------------------------------------------------------------------------
//启动nginx
/usr/local/nginx/sbin/nginx 
// 重启
/usr/local/nginx/sbin/nginx -s reload
// 停止
/usr/local/nginx/sbin/nginx -s stop

这里我启动nginx之后,ps -ef|grep nginx只查看master进程,/usr/local/nginx/logs/error.log查看日志,报错如下:

ERROR - file: ini_file_reader.c, line: 315, include file "http.conf" not exists, line: "#include http.conf"
[2020-03-21 15:54:32] ERROR - file: /opt/fastdfs-nginx-module/src/common.c, line: 155, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code: 2
ERROR - file: shared_func.c, line: 960, open file /etc/fdfs/mime.types fail, errno: 2, error info: No such file or directory
2020/03/21 16:04:46 [alert] 778#0: worker process 2190 exited with fatal code 2 and cannot be respawned
/opt/fastdfs-5.05/conf

解决方法:
cp /opt/fastdfs-5.05/conf/http.conf /etc/fdfs/
cp /opt/fastdfs-5.05/confhttp.conf /etc/fdfs/

之后重启所有服务
  • 用浏览器访问前面上传的文件
    fastdfs安装配置(整合nginx和fastdfs)
    至此,安装配置完成
相关标签: 技术文档