FastDFS 最新版本安装

2019-07-10  本文已影响0人  liuwei89757

声明

  1. 该文档是我边安装边写的,肯定是能行,但是不排除环境因素导致的问题出现。
  2. 有很多地方借鉴了 FastDFS 安装和配置,因为最新版有些地方还是不同的,所以总结出来供大家参,毕竟这种东西浪费时间而毫无意义。

安装环境

阿里云主机:Ubuntu 18.04 LTS

安装步骤

  1. 建立一个文件夹用来存放安装源码
mkdir  /mnt/install-packages
  1. 安装 libfastcommon 模块
# 进入安装目录
cd /mnt/install-packages

# 下载源码
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz

# 解压缩
tar zxf V1.0.39.tar.gz

# 进入源码目录
cd libfastcommon-1.0.39

# 构建
./make.sh

# 安装
./make.sh install

执行完以上命令一般就安装好了。

  1. 安装 fastdfs 模块
# 进入安装目录
cd /mnt/install-packages

# 下载源码
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz

# 解压缩
tar zxf V5.11.tar.gz

# 复制文件
cp -r libfastcommon-1.0.39/src/.   fastdfs-5.11/common/

# 进入源码目录
cd fastdfs-5.11

# 构建
./make.sh

# 安装
./make.sh install

这一环节中不要忘记把 libfastcommon-1.0.39/src/ 中的资源拷贝到 fastdfs-5.11/common/,不然会出现 no XXX.h 错误。

配置

  1. 创建一个用来保存数据的目录
mkdir -p /mnt/fastdfs/tracker storage cllient
  1. 复制配置模块
cp -r /mnt/install-packages/fastdfs-5.11/conf/.   /etc/fdfs/
  1. 配置 Tracker 服务
# 编辑配置文件
vim /etc/fdfs/tracker.conf

# 启用配置
disabled=false

# 存储数据跟日志的目录
base_path=/mnt/fastdfs/tracker

# 我的只是后台管理服务需要上传图片,最低配就好了
accept_threads=1
work_threads=1

其他的根据自己的需求来就好了。

  1. 配置 Storage 服务
# 编辑配置文件
vim /etc/fdfs/storage.conf

# 启用配置
disabled=false

# 分组名称
group_name=group

# 存储数据跟日志的目录
base_path=/mnt/fastdfs/storage

同上。

  1. 配置 Client
# 编辑配置文件
vim /etc/fdfs/client.conf

# 保存数据与日志的目录
base_path=/mnt/fastdfs/client

同上。

测试 FastDFS

  1. 启动服务
# tracker 服务
fdfs_trackerd /etc/fdfs/tracker.conf

# 查看服务进程
ps -aux | grep 'tracker'

# storage 服务
fdfs_storaged /etc/fdfs/storage.conf

# 查看服务进程
ps -aux | grep 'storage'

  1. 使用客户端测试
# 切换安装目录
cd /mnt/install-packages

# 下载一张测试图片
wget -O test.png https://pic1.zhimg.com/80/v2-29d099a8a083276da4a0be2e56dd5408_hd.png

# 测试上传
fdfs_test /etc/fdfs/client.conf upload test.png 

# output
[2019-07-10 02:21:02] DEBUG - base_path=/mnt/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

tracker_query_storage_store_list_without_group: 
    server 1. group_name=, ip_addr=xxxxx, port=xxxxx

group_name=group, ip_addr=xxxxxx, port=xxxxx
storage_upload_by_filename
group_name=group, remote_filename=M00/00/00/rBMuJV0k2w6Ac69fAAQP3juTv4s215.png
source ip address: xxxxx
file timestamp=2019-07-10 02:21:02
file size=266206
file crc32=999538571
example file url: http://ip:port/group/M00/00/00/rBMuJV0k2w6Ac69fAAQP3juTv4s215.png
storage_upload_slave_by_filename
group_name=group, remote_filename=M00/00/00/rBMuJV0k2w6Ac69fAAQP3juTv4s215_big.png
source ip address: 172.19.46.37
file timestamp=2019-07-10 02:21:02
file size=266206
file crc32=999538571
example file url: http://ip:port/group/M00/00/00/rBMuJV0k2w6Ac69fAAQP3juTv4s215_big.png

到这里我们的服务就搭建完毕了,但是没还没法通过 Http 访问,接下来需要安装 Nginx 模块。

安装 Nginx

  1. 回到安装目录
cd /mnt/install-packages
  1. 安装 PCRE
# 下载
wget https://sourceforge.net/projects/pcre/files/pcre/8.43/pcre-8.43.tar.gz/download

# 解压缩
tar zxf download

# 安装
cd pcre-8.43
./configure
make
make install
  1. 安装 OpenSSL
# 下载 OpenSSL
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1c.tar.gz

# 解压缩
tar zxf OpenSSL_1_1_1c.tar.gz

# 安装
cd openssl-OpenSSL_1_1_1c/
./config
make
make install
  1. 安装 zlib
# 下载
wget https://zlib.net/zlib-1.2.11.tar.gz

# 解压缩
tar zxf zlib-1.2.11.tar.gz

# 安装
./configure
make
make install
  1. 配置 fastdfs-nginx-module
# 下载 fastdfs-nginx-module
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz

# 解压缩
tar zxf V1.20.tar.gz

# 配置
vim fastdfs-nginx-module-1.20/src/config

ngx_addon_name=ngx_http_fastdfs_module

# output
if test -n "${ngx_module_link}"; then
    ngx_module_type=HTTP
    ngx_module_name=$ngx_addon_name
    ngx_module_incs="/usr/local/include"
    ngx_module_libs="-lfastcommon -lfdfsclient"
    ngx_module_srcs="$ngx_addon_dir/ngx_http_fastdfs_module.c"
    ngx_module_deps=
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
    . auto/module
else
    HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
    CORE_INCS="$CORE_INCS /usr/local/include"
    CORE_LIBS="$CORE_LIBS -lfastcommon -lfdfsclient"
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
fi

# 修改
 CORE_INCS="$CORE_INCS /usr/local/include"
# =>
 CORE_INCS="$CORE_INCS /usr/include"

在确定 CORE_INCS 位置的时候,可以自己使用下面的命令来确定,会输出 fastcommonfastdfs 就是正确的目录。

ls /usr/local/include | grep 'fast'

ls /usr/include | grep 'fast'
  1. 安装 Nginx
# 下载 Nginx 源码
wget https://nginx.org/download/nginx-1.17.1.tar.gz

# 解压缩
tar zxf nginx-1.17.1.tar.gz


# 进入 Nginx 源码目录
cd nginx-1.17.1

# 设置配置(支持的参数可以自己使用 ./configure --help 查看)
./configure \
--conf-path=/etc/nginx/nginx.conf    # 配置文件
--with-http_ssl_module  \            # HTTPS
--with-http_gzip_static_module  \    # GZIP,现在的前端资源都支持编译时自动生成 .gz 文件
--with-stream  \                     # TCP/UDP代理
--with-stream_ssl_module \           # 同上
--add-module= /mnt/install-packages/fastdfs-nginx-module-1.20/src   # 重要!添加 fdfs 的 nginx 模块 

# output
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/etc"
  nginx configuration file: "/etc/nginx"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"


# 安装
make
make install

Tips
  1. 复制命令的时候注意 \ 后面不要有 空格,因为 bash 默认 \ + 回车 是多行模式,中间是不能有空格的。
  2. 执行 ./configure 之后可以把输出内容记一下,这个就是你的 Nginx 配置信息。
  3. 如果 make 的时候提示 common_define.h not found,还是像上面一样,我们直接把 libfastcommon-1.0.39/src 中的头文件复制到 /usr/include/fastdfs/ (这个路径根据上一步的 CORE_INCS 来)。
# 复制头文件
cp -r /mnt/install-packages/libfastcommon-1.0.39/src/. /usr/include/fastdfs/

# 重新构建
make

# 安装
make install
  1. 设置全局命令
vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin/
export PATH

# 保存退出之后刷新配置
source ~/.bash_profile
Tips

.bashrc.bash_profile 的区别:

.bashrc 用于未登陆状态的 shell.bash_profile 用于已登录的 shell

.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

测试 Nginx

# 测试配置
nginx -t

# output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 启动
nginx

然后我们访问一下,发现 Nginx 无法访问?


哈哈,我们需要检查一下:

  1. 80 端口是否放开了
  2. Nginx 是否启动成功

因为我的主机是阿里云的,所以在 安全组 就能确认是放开了的;然后就检查一下服务启动情况:

# 查看报错日志
tail /usr/local/nginx/logs/error.log

# output
[2019-07-10 11:41:31] ERROR - file: shared_func.c, line: 968, file /etc/fdfs/mod_fastdfs.conf not exist
[2019-07-10 11:41:31] ERROR - file: /mnt/install-packages/fastdfs-nginx-module-1.20/src/common.c, line: 163, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code:2
2019/07/10 11:41:31 [alert] 10523#0: worker process 10524 exited with fatal code 2 and cannot be respawned
2019/07/10 12:02:04 [notice] 10606#0: signal process started

可以看到是 fastdfs-nginx-module 报错了,导致 Nginx 没有启动起来。

解决报错

从错误日志可以看到是由于缺少 mod_fastdfs.conf ,所以我们就复制一份:

# 复制配置文件
 cp /mnt/install-packages/fastdfs-nginx-module-1.20/src/mod_fastdfs.conf   /etc/fdfs/

# 配置 mod_fastdfs
vim /etc/fdfs/mod_fastdfs.conf

# storage 服务端口,对应 storage.conf # http.server_port
storage_server_port=xxx

# 保存日志的目录
base_path=/mnt/fastdfs/nginx

# 重要,我们的地址是带 group 路径的,所以需要设置为 true
url_have_group_name = true

# 把以下配置改成跟 storage.conf 一致
group_name=xxx
tracker_server=xxx
store_path0==xxx

# 修改完了记得新建个 nginx 目录
mkdir /mnt/fastdfs/nginx

最后,我们重启以下 Nginx:

# 删除错误日志
rm /usr/local/nginx/logs/error.log

# 重启
nginx -s reload

# 查看是否报错
tail /usr/local/nginx/logs/error.log

# output
signal process started
ngx_http_fastdfs_process_init pid=10906
[2019-07-10 12:26:34] INFO - fastdfs apache / nginx module v1.15, response_mode=proxy, base_path=/mnt/fastdfs/nginx, url_have_group_name=0, group_name=group, storage_server_port=10001, path_count=1, store_path0=/mnt/fastdfs/storage/files, connect_timeout=2, network_timeout=30, tracker_server_count=1, if_alias_prefix=, local_host_ip_count=2, anti_steal_token=0, token_ttl=0s, anti_steal_secret_key length=0, token_check_fail content_type=, token_check_fail buff length=0, load_fdfs_parameters_from_tracker=1, storage_sync_file_max_delay=86400s, use_storage_id=0, storage server id count=0, flv_support=1, flv_extension=flv

好啦,这次 Nginx 肯定是好了!!!

配置 Nginx 访问 FastDFS

  1. 改造一下 Nginx,把 /etc/nginx/nginx.conf 替换为:
user  root;
worker_processes  1;

error_log  /usr/local/nginx/logs/error.log warn;
pid        /usr/local/nginx/logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/nginx/logs/access.log  main;

    sendfile        on;
    
    #tcp_nopush     on;
    
    keepalive_timeout  65;
    
    gzip  on;
    
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    
    include /etc/nginx/conf.d/*.conf;                           
 }
  1. 添加 FastDFS 配置
# 新建配置目录
mkdir /etc/nginx/conf.d

# 新建 fastdfs 配置
vim /etc/nginx/conf.d/fastdfs.conf

server {
    listen       80;
    server_name  localhost; # 一般使用一个特定的 host

    location /{
      root /mnt/fastdfs/storage/files/data;
      ngx_fastdfs_module;
   }

}
  1. 重启 Nginx,测试
# 重启
nginx -s reload

# output
ngx_http_fastdfs_set pid=11271

# 上传图片测试
fdfs_test /etc/fdfs/client.conf upload test.png

最后,在浏览器中把 http://ip:port/group/M00/00/00/rBMuJV0lhryACmTvAAQP3juTv4s674.png 换成你自己的 ip 就可以访问到上传的图片了。

结束

哎呀,终于写完了,弄了大半天!!!


还是要提醒各位,遇到问题了,先不要去急着百度!!!
哪个环节出问题就看对应模块的 error.log (当然,不是每个模块的日志都是这个文件):

  1. tracker: /mnt/fastdfs/tracker/logs/trackerd.log
  2. storage: /mnt/fastdfs/storage/logs/storaged.log
  3. nginx: /usr/local/nginx/logs/error.log
上一篇下一篇

猜你喜欢

热点阅读