Ubuntu16.04 安装fastDFS

1、环境准备

  1. # 更新软件库
  2. sudo apt update
  1. # 安装gcc环境
  2. sudo apt install gcc
  3. # 编译安装需要make工具
  4. sudo apt install make
  1. # fastDFS依赖库
  2. sudo apt install libevent-dev

2、下载并安装FastDFS 5.x版本

1、为了方便管理,创建一个文件夹专门放所需软件

  1. sudo mkdir /usr/local/fastdfs

2、下载安装包(fastdfs源码包、libfastcommon源码包)

  1. wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
  2. wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

3、创建安装目录(-p是遍历创建目录)

  1. sudo mkdir -p /usr/local/fastdfs/{storage,tracker,client}

4、安装libfastcommon库(到指定文件夹下)

  1. sudo tar -xzvf V1.0.7.tar.gz -C /usr/local/fastdfs
  2. cd /usr/local/fastdfs/libfastcommon-1.0.7
  3. sudo ./make.sh
  4. sudo ./make.sh install

注意:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,所以可以设置软链接

  1. sudo ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
  2. sudo ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
  3. sudo ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
  4. sudo ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so

5、安装FastDFS

  1. sudo tar -zxvf V5.05.tar.gz
  2. cd usr/local/fastdfs/fastdfs-5.05
  3. sudo ./make.sh
  4. sudo ./make.sh install

/usr/bin 存放有编译出来的文件

/etc/fdfs 存放有配置文件

将配置文件复制一份用于编辑

  1. sudo cp storage.conf.sample storage.conf
  2. sudo cp tracker.conf.sample tracker.conf
  3. sudo cp client.conf.sample client.conf

6、配置tracker

  1. sudo vim /etc/fdfs/tracker.conf
  2. # 修改,修改前确保文件夹已存在
  3. base_path=/home/durian/fastdfs/tracker

7、启动tracker

  1. # 启动
  2. sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
  3. # 停止
  4. sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
  5. # 重启
  6. sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
  7. # 查看tracker进程
  8. ps -aux | grep tracker

8、配置storage

  1. sudo vim /etc/fdfs/storage.conf
  2. # 修改,修改前确保文件夹已存在
  3. base_path=/home/durian/fastdfs/storage
  4. group_name=group1
  5. # tracker服务地址,有多个tracker server则配置多个
  6. tracker_server=192.168.113.131:22122
  7. # 图片实际存放路径,如果有多个挂载,可以有配置多行
  8. store_path0=/usr/local/fastdfs/storage/images0
  9. store_path1=/usr/local/fastdfs/storage/images1
  10. store_path_count=2

9、启动storage

  1. # 启动
  2. sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
  3. # 停止
  4. sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
  5. # 重启
  6. sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
  7. # 查看tracker进程
  8. ps -aux | grep storage

10、配置client

  1. sudo vim /etc/fdfs/client.conf
  2. # 修改,修改前确保文件夹已存在
  3. base_path=/home/durian/fastdfs/client
  4. # 指定tracker server地址,有多个则配置多行
  5. tracker_server=192.168.113.131:22122

11、使用client测试

  1. sudo /usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/durian/test.jpg
  2. # 出现如下信息表示图片上传成功
  3. group_name=group1, remote_filename=M00/00/00/wKgchVqh9U6AClDVAACFzpgzGxg08920.h
  4. source ip address: 192.168.113.131
  5. file timestamp=2018-03-09 10:45:34
  6. file size=34254
  7. file crc32=2553486104
  8. example file url: http://192.168.113.131/group1/M00/00/00/wKgchVqh9U6AClDACFzpgz08920.jpg
  9. storage_upload_slave_by_filename

但是此时使用example file url不能访问到图片,需要搭配nginx。

安装Nginx和FastDFS模块

单独安装nginx代理服务,他的作用是代理访问storage上的文件,实现负载均衡。

1、下载Nginx的FastDFS模块

  1. wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz

2、解压Nginx的FastDFS模块带目录 /usr/local/fastdfs/fastdfs-nginx-module

  1. sudo tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fastdfs/fastdfs-nginx-module

3、安装nginx并添加fastDFS模块

  1. # 预设几个文件夹,方便等下安装的时候有些文件可以进行存放:
  2. sudo mkdir -p /usr/local/nginx /var/log/nginx /var/temp/nginx /var/lock/nginx
  3. cd /usr/local/nginx
  4. ./configure
  5. --prefix=/usr/local/nginx # 指定安装目录
  6. --pid-path=/var/local/nginx/nginx.pid
  7. --lock-path=/var/lock/nginx/nginx.lock
  8. --error-log-path=/var/log/nginx/error.log
  9. --http-log-path=/var/log/nginx/access.log
  10. --with-http_gzip_static_module
  11. --http-client-body-temp-path=/var/temp/nginx/client
  12. --http-proxy-temp-path=/var/temp/nginx/proxy
  13. --http-fastcgi-temp-path=/var/temp/nginx/fastcgi
  14. --http-uwsgi-temp-path=/var/temp/nginx/uwsgi
  15. --http-scgi-temp-path=/var/temp/nginx/scgi
  16. --add-module=usr/local/fastdfs/fastdfs-nginx-module/src # 添加fastDFS模块
  17. # 编译安装
  18. sudo make && sudo make install

4、配置FastDFS模块

  1. 4-1、复制**fastdfs-nignx**模块的配置文件 mod_fastdfs.conf /etc/fdfs/
  1. cd /usr/local/fastdfs/fastdfs-nginx-module/src
  2. sudo cp mod_fastdfs.conf /etc/fdfs/
  1. 4-2 复制**fastdfs**中的http.confmime.types文件到/etc/fdfs
  1. cd /usr/local/fastdfs/fastdfs-5.05
  2. sudo cp http.conf mime.types /etc/fdfs
  1. 4-3、修改mod_fastdfs.conf
  1. sudo vim /etc/fdfs/mod_fastdfs.conf
  2. # 日志记录路径
  3. base_path=/home/durian/fastdfs/nginx-fast-module
  4. # tracker server 地址
  5. tracker_server=192.168.113.131:22122
  6. # http带group名字
  7. url_have_group_name = true
  8. # 和storage.conf 一样
  9. store_path0=/home/durian/fastdfs/storage/images0
  10. store_path1=/home/durian/fastdfs/storage/images1
  11. # 参数值必须与store_path数量一致
  12. store_path_count=2
  1. 4-4、编辑 Nginx 模块的配置文件
  1. sudo vim /usr/local/fastdfs/fastdfs-nginx-module/src/config
  2. # 修改该行
  3. CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/
  4. CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/local/include/fastcommon/
  1. 4-5 修改storage.conf
  1. #端口号必须和nginx的端口好一样
  2. http.server_port=80
  1. 4-6、编辑nginx.conf
  1. vim /usr/local/nginx/conf/nginx.conf
  2. # 拦截包含 /group1/M00 请求,使用 fastdfs 这个 Nginx 模块进行转发
  3. location ~/group([0-9])/M00 {
  4. ngx_fastdfs_module;
  5. }

4-7、启动nginx

  1. sudo /usr/local/nginx/sbin/nginx