CentOS 安装FastDFS,配置Tracker和Storage服务

2,805 阅读7分钟

FastDFS上传过程

FastDFS官网地址:https://github.com/happyfish100/fastdfs/wiki

需要使用到的软件安装包下载地址:

使用centos 6.x 、 7.x系统都行

使用FastDFS需要准备两台服务器

功能分别为 Tracker服务 和 Storage服务

需要在两台服务器上都安装FastDFS,然后分别配置tracker和storage


安装步骤

1. 先安装 gcc c++

yum install -y gcc gcc-c++

2. 安装 libevent

yum install -y libevent

3. 安装 libfastcommon

3.1. 先将 libfastcommon 压缩包进行解压

tar -zxvf libfastcommon-1.0.48.tar.gz

3.2. 进入到解压后的文件夹进行安装

cd libfastcommon-1.0.48/
先编译:
./make.sh
再进行安装:
./make.sh install

4. 安装 fastdfs

4.1. 解压 fastdfs

tar -zxvf fastdfs-6.07.tar.gz

4.2. 进入到解压后的文件夹进行安装

cd fastdfs-6.07/
先编译:
./make.sh
再进行安装:
./make.sh install

4.3. 进入到配置文件目录,拷贝配置文件

cd conf/
将该目录下所有文件拷贝 /etc/fdfs 目录下
cp * /etc/fdfs/

fastdfs的安装目录:

/usr/bin

/etc/fdfs

/usr/lib64

/usr/lib


5. 分别配置 Tracker服务 和 Storage服务

在两台服务器上都安装好后,需要配置对应的配置文件

在 /etc/fdfs/ 目录下,有storage.conf和tracker.conf两个配置文件,

[root@localhost fdfs]# pwd
/etc/fdfs
[root@localhost fdfs]# ll
总用量 124
-rw-r--r--. 1 root root 23981 2月  18 10:22 anti-steal.jpg
-rw-r--r--. 1 root root  1909 2月  18 10:22 client.conf
-rw-r--r--. 1 root root  1909 2月  18 10:21 client.conf.sample
-rw-r--r--. 1 root root   965 2月  18 10:22 http.conf
-rw-r--r--. 1 root root 31172 2月  18 10:22 mime.types
-rw-r--r--. 1 root root 10246 2月  18 10:22 storage.conf
-rw-r--r--. 1 root root 10246 2月  18 10:21 storage.conf.sample
-rw-r--r--. 1 root root   620 2月  18 10:22 storage_ids.conf
-rw-r--r--. 1 root root   620 2月  18 10:21 storage_ids.conf.sample
-rw-r--r--. 1 root root  9189 2月  18 10:29 tracker.conf
-rw-r--r--. 1 root root  9138 2月  18 10:21 tracker.conf.sample

如果你当前这台机器是做为 Storage 服务使用,则修改 storage.conf 文件;

如果你当前这台机器是做为 Tracker 服务使用,则修改 tracker.conf 文件。

我们先修改 tracker.conf 文件,目前只修改一个地方 base_path,存储数据文件和日志文件的路径

base_path = /usr/local/fastdfs/tracker

修改成你自己定义的路径即可。

启动 Tracker 服务:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

查看是否启动成功

ps -ef | grep tracker

然后换到另外一台服务器,修改 storage.conf 文件

# 修改组名
group_name = group1

# 存储数据文件和日志文件的路径
base_path = /usr/local/fastdfs/storage
store_path0 = /usr/local/fastdfs/storage

# 在下方会有两个 tracker_server ,先注释掉一个,因为我们现在只有一个tracker服务
# 修改其中一个的ip为 tracker服务器 的ip,tracker默认端口为22122
tracker_server = 192.168.10.235:22122

启动 Storage 服务:

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

6. 测试上传

Storage 服务器上,/etc/fdfs/ 目录下,修改 client.conf 文件

base_path = /usr/local/fastdfs/client
tracker_server = 192.168.10.235:22122

事先先准备一张图片,进入到 /usr/bin/ 目录下,里面有一个 fdfs_test 文件,是用来测试的

[root@localhost fdfs]# cd /usr/bin/
[root@localhost bin]# ls fdfs*
fdfs_appender_test   fdfs_append_file  fdfs_delete_file    fdfs_file_info  fdfs_regenerate_filename  fdfs_test   fdfs_trackerd         fdfs_upload_file
fdfs_appender_test1  fdfs_crc32        fdfs_download_file  fdfs_monitor    fdfs_storaged             fdfs_test1  fdfs_upload_appender
[root@localhost bin]# 

使用如下命令进行上传测试

./fdfs_test /etc/fdfs/client.conf upload /home/test.png

remote_filename,文件存储路径

example file url,文件访问路径,目前还没有配置,所以访问不了

查看一下上传的文件

[root@localhost storage]# cd /usr/local/fastdfs/storage/data/00/00/
[root@localhost 00]# ll
总用量 1000
-rw-r--r--. 1 root root 504372 2月  18 11:11 wKgK6mAt2vqAdn7SAAeyNMzENJ4999_big.png
-rw-r--r--. 1 root root     49 2月  18 11:11 wKgK6mAt2vqAdn7SAAeyNMzENJ4999_big.png-m
-rw-r--r--. 1 root root 504372 2月  18 11:11 wKgK6mAt2vqAdn7SAAeyNMzENJ4999.png
-rw-r--r--. 1 root root     49 2月  18 11:11 wKgK6mAt2vqAdn7SAAeyNMzENJ4999.png-m
[root@localhost 00]# 

7. 配置nginx,提供web服务

Storage 服务器上安装nginx,安装步骤就不赘述了,参考文章Nginx安装和运行

7.1. 解压下载的 fastdfs-nginx-module-1.22.tar.gz 文件

tar -zxvf fastdfs-nginx-module-1.22.tar.gz 

7.2. 进到解压后的文件夹内的src文件夹

[root@localhost src]# pwd
/home/software/fastdfs-nginx-module-1.22/src
[root@localhost src]# ll
总用量 84
-rw-rw-r--. 1 root root 43507 11 19 2019 common.c
-rw-rw-r--. 1 root root  3995 11 19 2019 common.h
-rw-rw-r--. 1 root root   848 11 19 2019 config
-rw-rw-r--. 1 root root  3725 11 19 2019 mod_fastdfs.conf
-rw-rw-r--. 1 root root 28668 11 19 2019 ngx_http_fastdfs_module.c

7.3. 将 mod_fastdfs.conf 配置文件拷贝到 /etc/fdfs/ 目录下

cp mod_fastdfs.conf /etc/fdfs/

7.4. 进入到 /etc/fdfs/ 目录下,修改刚刚拷贝过来的 mod_fastdfs.conf 配置文件

# 修改 base_path 目录
base_path=/usr/local/fastdfs/tmp
# 修改 store_path0 的路径和 storage.conf 配置文件中的 store_path0 路径一致
store_path0= /usr/local/fastdfs/storage
# 修改 tracker_server 地址为 tracker 服务器地址
tracker_server=192.168.10.235:22122
# 修改一下 group_name
group_name = group1
# url_have_group_name,生成的访问url是否要包含group name,可以改成true
url_have_group_name = true

7.5. 返回到 fastdfs-nginx-module-1.22/src 文件夹内,修改 config 文件

[root@localhost fdfs]# cd /home/software/fastdfs-nginx-module-1.22/src/
[root@localhost src]# ll
总用量 84
-rw-rw-r--. 1 root root 43507 11 19 2019 common.c
-rw-rw-r--. 1 root root  3995 11 19 2019 common.h
-rw-rw-r--. 1 root root   848 11 19 2019 config
-rw-rw-r--. 1 root root  3725 11 19 2019 mod_fastdfs.conf
-rw-rw-r--. 1 root root 28668 11 19 2019 ngx_http_fastdfs_module.c
ngx_module_incs="/usr/local/include" 修改为 ngx_module_incs="/usr/include";
将 CORE_INCS="$CORE_INCS /usr/local/include" 修改为 CORE_INCS="$CORE_INCS /usr/include"

7.6. 因为新增nginx模块,所以需要重新安装nginx

进入到解压后的nginx目录,

[root@localhost nginx-1.18.0]# cd /home/software/nginx-1.18.0/

执行下面命令:

./configure \--prefix=/usr/local/nginx \--pid-path=/var/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--with-http_gzip_static_module \--http-client-body-temp-path=/var/temp/nginx/client \--http-proxy-temp-path=/var/temp/nginx/proxy \--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \--http-scgi-temp-path=/var/temp/nginx/scgi \--add-module=/home/software/fastdfs-nginx-module-1.22/src

注意:复制的话,最后一行末尾不要带空格或换行,不然可能会执行错误

最好是将这段命令复制出来,修改路径后再贴到命令行上去执行。

--add-module 意思是要安装的模块,后面的地址就是解压后的 fastdfs-nginx-module-1.22/src 目录,一定要写到src

然后进行安装

[root@localhost nginx-1.18.0]# make & make install

7.7. 安装完成后,修改 nginx.conf 配置文件

[root@localhost nginx]# cd /usr/local/nginx/conf/
[root@localhost conf]# ll
总用量 68
-rw-r--r--. 1 root root 1077 2月   4 17:54 fastcgi.conf
-rw-r--r--. 1 root root 1077 2月  18 13:10 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 2月   4 17:54 fastcgi_params
-rw-r--r--. 1 root root 1007 2月  18 13:10 fastcgi_params.default
-rw-r--r--. 1 root root 2837 2月  18 13:10 koi-utf
-rw-r--r--. 1 root root 2223 2月  18 13:10 koi-win
-rw-r--r--. 1 root root 5231 2月   4 17:54 mime.types
-rw-r--r--. 1 root root 5231 2月  18 13:10 mime.types.default
-rw-r--r--. 1 root root 2672 2月  18 11:32 nginx.conf
-rw-r--r--. 1 root root 2656 2月  18 13:10 nginx.conf.default
-rw-r--r--. 1 root root  636 2月   4 17:54 scgi_params
-rw-r--r--. 1 root root  636 2月  18 13:10 scgi_params.default
-rw-r--r--. 1 root root  664 2月   4 17:54 uwsgi_params
-rw-r--r--. 1 root root  664 2月  18 13:10 uwsgi_params.default
-rw-r--r--. 1 root root 3610 2月  18 13:10 win-utf

nginx.conf 中添加如下配置

server {
	## 该端口为storage.conf中的http.server_port相同
    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;
    }
}

location 对应的是之前配置文件中设置的 group_name 名称

比如之前设置的 group_name=test,location就应该写成

location /test/M00 {
	ngx_fastdfs_module;
}

重启nginx,访问之前上传的图片

http://ip:8888/test/M00/00/00/wKgK6mAt2vqAdn7SAAeyNMzENJ4999.png

图片文件是在 /usr/local/fastdfs/storage/data/00/00/ 目录下。