本文所用软件包,如无特别版本要求,建议使用该版本V5.08.
1.FastDFS的安装
1.0 依赖安装与环境配置
yum install make cmake gcc gcc-c++ zip unzip
hostname 'your hostname'
1.1 下载并安装libfastcommon
github address: https://github.com/happyfish100/libfastcommon.git
gitee address: https://gitee.com/fastdfs100/libfastcommon.git
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon; git checkout -b V1.0.50
./make.sh clean && ./make.sh && ./make.sh install
1.2 安装FastDFS
cd /usr/local/src
tar -xzvf FastDFS_v5.08.tar.gz
cd FastDFS
./make.sh clean && ./make.sh && ./make.sh install
- 默认安装完检查启动脚本是否生成.
/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_trackerd
- 检查配置文件
[root@h3cgl fdfs]# ll
total 20
-rw-r--r-- 1 root root 1461 Jun 23 13:47 client.conf.sample
-rw-r--r-- 1 root root 7927 Jun 23 13:47 storage.conf.sample
-rw-r--r-- 1 root root 7200 Jun 23 13:47 tracker.conf.sample
- 命令行工具(/usr/bin目录下)
[root@h3cgl bin]# ll fdfs*
-rwxr-xr-x 1 root root 315392 Jun 23 13:47 fdfs_appender_test
-rwxr-xr-x 1 root root 315168 Jun 23 13:47 fdfs_appender_test1
-rwxr-xr-x 1 root root 302016 Jun 23 13:47 fdfs_append_file
-rwxr-xr-x 1 root root 301672 Jun 23 13:47 fdfs_crc32
-rwxr-xr-x 1 root root 302080 Jun 23 13:47 fdfs_delete_file
-rwxr-xr-x 1 root root 302816 Jun 23 13:47 fdfs_download_file
-rwxr-xr-x 1 root root 302400 Jun 23 13:47 fdfs_file_info
-rwxr-xr-x 1 root root 316216 Jun 23 13:47 fdfs_monitor
-rwxr-xr-x 1 root root 1101856 Jun 23 13:47 fdfs_storaged
-rwxr-xr-x 1 root root 325288 Jun 23 13:47 fdfs_test
-rwxr-xr-x 1 root root 320416 Jun 23 13:47 fdfs_test1
-rwxr-xr-x 1 root root 447832 Jun 23 13:47 fdfs_trackerd
-rwxr-xr-x 1 root root 303008 Jun 23 13:47 fdfs_upload_appender
-rwxr-xr-x 1 root root 304024 Jun 23 13:47 fdfs_upload_file
2. FastDFS Tracker服务器配置
2.1 复制Tracker样例配置文件,并重命名
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
2.2 修改Tracker配置文件
vim /etc/fdfs/tracker.conf
# 修改的内容如下:
disabled=false # 启用配置文件
port=22122 # tracker服务器端口(默认22122)
base_path=/fastdfs/tracker # 存储日志和数据的根目录
其它参数保留默认配置, 具体配置解释可参考官方文档说明:http://bbs.chinaunix.net/thread-1941456-1-1.html
2.3 创建日志和数据的根目录
mkdir -p /fastdfs/tracker
2.4 防火墙配置Tracker服务器端口
vi /etc/sysconfig/iptables
# 附加:若/etc/sysconfig 目录下没有iptables文件可随便写一条iptables命令配置个防火墙规则:如:
iptables -P OUTPUT ACCEPT
# 然后用命令:service iptables save 进行保存,默认就保存到 /etc/sysconfig/iptables 文件里。这时既有了这个文件。防火墙也可以启动了。接下来要写策略,也可以直接写在/etc/sysconfig/iptables 里了。
# 添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
# 重启防火墙
service iptables restart
2.5 启动Tracker服务器
/etc/init.d/fdfs_trackerd start
# 初次启动,会在/fastdfs/tracker目录下生成logs、data两个目录。
[root@h3cgl tracker]# ll
total 0
drwxr-xr-x 2 root root 60 Jun 23 14:05 data
drwxr-xr-x 2 root root 26 Jun 23 14:05 logs
# 检查FastDFS Tracker Server是否启动成功:
ps -ef | grep fdfs_trackerd
[root@h3cgl tracker]# ps -ef | grep fdfs_trackerd
root 17467 1 0 14:05 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root 17656 38280 0 14:05 pts/0 00:00:00 grep --color=auto fdfs_trackerd
3. 配置Storage服务器
3.1 复制Storage样例配置文件,并重命名
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
3.2 修改Storage配置文件
vi /etc/fdfs/storage.conf
# 修改的内容如下:
disabled=false # 启用配置文件
port=23000 # storage服务端口
base_path=/fastdfs/storage # 数据和日志文件存储根目录
store_path0=/fastdfs/storage # 第一个存储目录
tracker_server=XMZ-OSS:22122 # tracker服务器IP和端口
http.server_port=8888 # http访问文件的端口
# 其它参数保留默认配置, 具体配置解释可参考官方文档说明:http://bbs.chinaunix.net/thread-1941456-1-1.html
3.3 创建基础文件路径
mkdir -p /fastdfs/storage
3.4 防火墙配置Storage服务端口
vi /etc/sysconfig/iptables
# 添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT
# 重启防火墙
service iptables restart
3.5 启动Storage服务
/etc/init.d/fdfs_storaged start
# 初次启动,会在/fastdfs/storage目录下生成logs、data两个目录。
[root@h3cgl storage]# ll
total 12
drwxr-xr-x 259 root root 8192 Jun 23 14:28 data
drwxr-xr-x 2 root root 26 Jun 23 14:28 logs
# 检查FastDFS Tracker Server是否启动成功:
[root@h3cgl storage]# ps -ef | grep fdfs_storaged
root 27300 1 1 14:28 ? 00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
root 27707 38280 0 14:29 pts/0 00:00:00 grep --color=auto fdfs_storaged
4 测试
4.1 修改Tracker服务器客户端配置文件
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
# 修改以下配置,其它保持默认
base_path=/fastdfs/tracker
tracker_server='your hostname':22122
4.2 执行文件上传命令
# /etc/fdfs/client.conf.sample 是需要上传文件路径
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/client.conf.sample
# 返回文件ID号:group1/M00/00/00/Dk7iazDS1q2AJpmgAAAFtTzeg5c.sample
#(能返回以上文件ID,说明文件已经上传成功)
5. 配合Nginx实现文件访问
5.1 fastdfs-nginx-module 作用说明
FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但是同组存储服务器之间需要进入文件复制,有同步延迟的问题。
假设Tracker服务器将文件上传到了A,上传成功后文件 ID 已经返回给客户端。此时FastDFS存储集群机制会将这个文件同步到同组存储B,在文件还 没有复制完成的情况下,客户端如果用这个文件ID在B上取文件,就会出现文件无法访问的错误。
而fastdfs-nginx-module 可以重定向文件连接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误。(解压后的fastdfs-nginx-module在nginx安装时使用)
5.2 fastdfs-nginx-module安装
tar -xzvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src
vi config
配置文件修改如下:
ngx_addon_name=ngx_http_fastdfs_module
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/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
5.3 nginx编译安装
安装所需依赖:
yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
安装nginx:
cd /usr/local/src/
tar -zxvf nginx-1.10.0.tar.gz
cd nginx-1.10.0
./configure --add-module=/usr/local/src/fastdfs-nginx-module/src
make && make install
比对 nginx下Makefile
default: build
clean:
rm -rf Makefile objs
build:
$(MAKE) -f objs/Makefile
install:
$(MAKE) -f objs/Makefile install
modules:
$(MAKE) -f objs/Makefile modules
upgrade:
/usr/local/nginx/sbin/nginx -t
kill -USR2 'cat /usr/local/nginx/logs/nginx.pid'
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT 'cat /usr/local/nginx/logs/nginx.pid.oldbin'
5.4 fastdfs-nginx-module配置修改
默认安装完nginx的路径为/usr/local/nginx
复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录,并修改
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
修改以下配置
connect_timeout=30
base_path=/tmp
tracker_server='your hostname':22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path0=/fastdfs/storage
5.5 复制FastDFS的部分配置文件到/etc/fdfs 目录
cd /usr/local/src/FastDFS/conf
cp http.conf mime.types /etc/fdfs/
5.6 文件存储目录下创建软连接,将其链接到实际存放数据的目录
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
5.7 nginx配置修改
user root;
worker_processes 8;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8888;
server_name localhost;
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 800;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
5.8 nginx启动
# 重启防火墙
# service iptables restart
# 启动 Nginx
/usr/local/nginx/sbin/nginx
# (重启 Nginx 的命令为:/usr/local/nginx/sbin/nginx -s reload)
5.9 访问测试
http://'address':8888/group1/M00/00/00/Dk7iazDS1q2AJpmgAAAFtTzeg5c.sample