天天看点

FastDFS配合Nginx服务的安装配置1、FastDFS简介2、安装3、启动FDFS4、测试上传5、配置fastdfs-nginx-module6、安装Nginx7、重启所有服务

文章目录

  • 1、FastDFS简介
  • 2、安装
    • 2.1、获取安装包
    • 2.2、安装FastDFS依赖库
    • 2.3、安装 fastdfs
    • 2.4、修改配置
      • 2.4.1、建立项目目录
      • 2.4.2、tracker.conf
      • 2.4.3、storage.conf
      • 2.4.4、client.conf
      • 2.4.5、mod_fastdfs.conf
  • 3、启动FDFS
  • 4、测试上传
  • 5、配置fastdfs-nginx-module
  • 6、安装Nginx
  • 7、重启所有服务

1、FastDFS简介

  • FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server)、存储服务器(storage server)和客户端(client)

    三个部分组成,主要解决了海量数据存储问题,特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务。

  • FastDFS是一个轻量级的开源分布式文件系统
  • 主要解决了大容量的文件存储和高并发访问的问题,文件存取时实现了负载均衡
  • FastDFS实现了软件方式的RAID,可以使用廉价的IDE硬盘进行存储
  • 支持存储服务器在线扩容,支持相同内容的文件只保存一份,节约磁盘空间

2、安装

2.1、获取安装包

mkdir -p /opt/fdfs-package
cd /opt/fdfs-packge
# 下载安装包
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -SO libfastcommon.tar.gz
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -SO fastdfs.tar.gz
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -SO fastdfs-nginx-module.tar.gz
# 解压三个压缩包
tar xzvf libfastcommon.tar.gz
tar xzvf fastdfs.tar.gz
tar xzvf fastdfs-nginx-module.tar.gz
           

2.2、安装FastDFS依赖库

cd libfastcommon-1.0.39
./make.sh
./make.sh install
           

2.3、安装 fastdfs

cd fastdfs-5.11
./make.sh
./make.sh install
           

安装好后,程序是在/usr/bin目录下:

which fdfs_trackerd
# 得到/usr/bin/fdfs_trackerd,说明安装成功
           

配置文件是在/etc/fdfs目录下:

ls /etc/fdfs
client_deploy.conf storage_ids.conf.sample  tracker.conf.sample storage.conf.sample
           

但是这些配置文件是不全的,而且都是模板,所以需要从fastdfs包中拷贝过来,并修改配置:

cp /opt/fdfs-package/fastdfs-5.11/conf/* /etc/fdfs
# 进去fastdfs-nginx-module-1.20文件夹,把mod_fastdfs.conf 也复制到/etc/fdfs
cp /opt/fdfs-package/fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs
           

2.4、修改配置

2.4.1、建立项目目录

# 建立fdfs数据目录
mkdir -p /opt/fdfs-basepath/tracker 
mkdir -p /opt/fdfs-basepath/storage
mkdir -p /opt/fdfs-basepath/client
           

2.4.2、tracker.conf

vim /etc/fdfs/tracker.conf
# the tracker server port
port=22122

# the base path to store data and log files
# tracker文件存放位置,按需修改
base_path=/opt/fdfs-basepath/tracker  

# HTTP port on this tracker server
# 若是默认端口已有服务运行记得更改
http.server_port=6666
           

2.4.3、storage.conf

vim /etc/fdfs/storage.conf
# storage所属的组
group_name=group1

# the storage server port
port=23000

# the base path to store data and log files
# storage服务存放的位置,按需修改
base_path=/opt/fdfs-basepath/storage

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/opt/fdfs-basepath/storage

# tracker服务器,虽然是同一台机器上,但是不能写127.0.0.1。这项配置可以出现一次或多次
tracker_server=IP:22122

# the port of the web server on this storage server
# 填写nginx的端口号
http.server_port=8888
           

2.4.4、client.conf

vim /etc/fdfs/client.conf
# the base path to store log files
base_path=/opt/fdfs-basepath/client
# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=IP:22122
#HTTP settings
http.tracker_server_port=6666  # 必须和tracker.conf的端口一样
           

2.4.5、mod_fastdfs.conf

vim /etc/fdfs/mod_fastdfs.conf
# the base path to store log files
base_path=/tmp

# FastDFS tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=IP:22122

# the port of the local storage server
# the default value is 23000
storage_server_port=23000

# the group name of the local storage server
group_name=group1

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf------>必须和storage.conf一样
store_path0=/opt/fdfs-basepath/storage
#store_path1=/home/yuqing/fastdfs1
           
  • 配置过程中有几点要注意:
    • 确保配置中用到的目录已经创建了
    • 确保各种配置文件之间引用的端口一直

3、启动FDFS

'注意:必须先启动tracker,再启动storage'
fdfs_trackerd /etc/fdfs/tracker.conf start
fdfs_storaged /etc/fdfs/storage.conf start
# 如上面命令无法启动,使用下面两个
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
           

ps aux | grep fdfs

查看当前是否运行

ps aux | grep fdfs
ubuntu    6710  0.0  0.0  13772  1096 pts/0    S+   19:23   0:00 grep fdfs
root     26133  0.0  0.1 146412  2304 ?        Sl   May09   0:43 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root     26199  0.0  0.1  82984  3400 ?        Sl   May09   0:25 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
           

netstat -unltp | grep fdfs

查看当前端口监听情况

netstat -unltp | grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      26133/fdfs_trackerd 
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      26199/fdfs_storaged
           

4、测试上传

fdfs_upload_file /etc/fdfs/client.conf /root/test.jpg
# 返回类似下面的结果,说明配置成功
group1/M00/00/00/CmhbQ12DF9qAcZlDAABdrSqbHGQ055.jpg
           

5、配置fastdfs-nginx-module

  • 安装依赖包
# 安装依赖包
yum install -y libxml2 libxml2-dev libxslt-dev libgd-dev libgeoip-dev libpcre3 libpcre3-dev
           
  • 修改fastdfs-nginx-module的config文件
vim /opt/fdfs-package/fastdfs-nginx-module-1.20/src/config
# 修改以下两项:
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
           

6、安装Nginx

  • 获取Nginx
# 下载nginx
cd
wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar xzvf nginx-1.10.3.tar.gz
           
  • 编译安装Nginx
cd nginx-1.10.3
./configure --add-module=/opt/fdfs-package/fastdfs-nginx-module-1.20/src/
make && make install
           
  • 修改nginx配置文件
cd /usr/local/nginx/conf/
# 备份nginx.conf
mv nginx.conf nginx.conf.bak
# 编写nginx.conf
vim nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    server {
        listen       8888;
        server_name  localhost;
        location ~/group[0-9]/ {
        ngx_fastdfs_module;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }
    }
    
}                                             
           

7、重启所有服务

fdfs_trackerd /etc/fdfs/tracker.conf restart
fdfs_storaged /etc/fdfs/storage.conf restart
nginx -s stop && nginx
           
  • 访问测试
# 上传图片
fdfs_upload_file /etc/fdfs/client.conf /home/kali.jpg
group1/M00/00/00/CmhbQ12EIwqAeiLsAAWZC4LxnZI028.jpg
# 访问
http://IP:8888/group1/M00/00/00/CmhbQ12EIwqAeiLsAAWZC4LxnZI028.jpg
           

继续阅读