FastDFS分布式文件存储,具体介绍不在阐述,有很多相关的资料介绍。这里描述一下FastDFS存储应用的搭建过程:
一、环境:
1、操作系统 :centos6.5
2、FastDFS版本: 5.0.8
3、Nginx版本: 1.8.1
二、架构规划:
说明:
使用最小架构,构建一组存储系统
1、使用一个tracker 追踪节点,同时部署Nginx ,做 Storage 上Nginx的反向代理;
2、使用两个Storage存储节点,同时部署Nginx,支持http在线预览文件、http下载。
三、具体部署操作:
1、下载并安装FastDFS依赖包libfastcommon
wget https://codeload.github.com/happyfish100/libfastcommon/zip/masterunzip master cd libfastcommon-master/./make.sh./make.sh install
2、下载并安装FastDFS
wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.08/FastDFS_v5.08.tar.gztar xf FastDFS_v5.08.tar.gzcd FastDFS./make.sh && ./make.sh install
3、采用默认方式安装后的文件及目录:
ll /etc/init.d/ |grep fdfsll /etc/fdfs/ll /usr/bin|grep fdfs
以上三个步骤在 三台机器上同样的操作。
4、配置跟踪服务器(tracker server)
1)拷贝tracker server和client端样例配置文件并重命名
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.confcp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
2)编辑tracker server配置文件tracker.conf,这里需要注意的是你的服务器磁盘挂载的目录空间
可以使用:df -h 命令查看,配置base_path路径应该是设置到大存储空间的路径里,这个是FastDFS文件存储的实际路径。
disabled=false(默认为false,表示是否无效)port=22122(默认为22122)base_path=/opt/fastdfs/tracker
3)编辑client端的配置文件client.conf
base_path=/data/fastdfs/trackertracker_server=192.168.1.72:22122
4)创建tracker server数据目录,就是上面配置的base_path路径
mkdir -p /opt/fastdfs/tracker
5)测试启动tracker server,查看22122端口是否监听
/etc/init.d/fdfs_trackerd startss -lntup|grep 22122
5、配置存储服务器(storage server)
1)拷贝storage server样例配置文件并重命名
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
2)编辑storage server配置文件storage.conf
disabled=false(默认为false,表示是否无效) port=23000(默认为23000) base_path=/opt/fastdfs/storage tracker_server=192.168.1.72:22122 store_path0=/opt/fastdfs/storage http.server_port=8888(默认为8888,nginx中配置的监听端口)
3)创建storage server数据目录
mkdir -p /data/fastdfs/storage
4)测试启动storage server,查看23000端口是否已经监听(启动storage server的前提是tracker server必须事先已启动)
/etc/init.d/fdfs_storaged startss -lntup|grep 23000
5)安装并配置nginx
wget http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gztar xf fastdfs-nginx-module_v1.16.tar.gzcd fastdfs-nginx-module/src/vim config编辑config文件,执行如下命令进行批量替换并保存退出:%s+/usr/local/+/usr/+g
6)拷贝fastdfs-nginx-module模块中配置文件到/etc/fdfs目录中并编辑
cp /home/oldcat/tools/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/vi /etc/fdfs/mod_fastdfs.conf修改内容如下:connect_timeout=10base_path=/tmp(默认为/tmp ,请使用这个默认路径,修改路径的话,有可能http预览文件失败)tracker_server=192.168.1.72:22122storage_server_port=23000(默认配置为23000)url_have_group_name = truestore_path0=/opt/fastdfs/storagegroup_name=group1(默认配置为group1)
7)安装nginx依赖库,并且安装Nginx
yum install -y pcre-devel zlib-develwget http://nginx.org/download/nginx-1.8.1.tar.gztar xf nginx-1.8.1.tar.gz cd nginx-1.8.1./configure --prefix=/application/nginx/ --add-module=../fastdfs-nginx-module/src/make && make install
如果过程提示 gcc 等依赖安装,就添加安装gcc
yum install gcc-c++
8)拷贝FastDFS中的部分配置文件到/etc/fdfs目录中
cp /home/oldcat/tools/FastDFS/conf/http.conf /etc/fdfs/cp /home/oldcat/tools/FastDFS/conf/mime.types /etc/fdfs/
9)配置nginx
vi /application/nginx/conf/nginx.conf配置以下内容:user root; 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; } } }
10)启动Nginx,并查看8888端口是否已经监听:
cd /application/nginx/sbin/./nginxss -lntup|grep 8888
在另外一台Storage 服务器上同样的安装和配置
6、配置Tracker服务器上Nginx,当然也可以把Nginx单独安装在另外一台服务器上:
1)安装gcc编译器
yum install gcc-c++
2)安装openssl,可以支持https
yum install openssl openssl-devel -y
3)下载Nginx
wget http://nginx.org/download/nginx-1.8.1.tar.gz
4)解压安装
tar xf nginx-1.8.1.tar.gzcd nginx-1.8.1./configure --prefix=/application/nginx/ --with-http_ssl_module make && make install
5)配置nginx.conf 配置文件
cd /application/nginx/conf/vi nginx.conf修改以下配置: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; upstream fdfs_group1 { server 192.168.1.79:8888 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.73:8888 weight=1 max_fails=2 fail_timeout=30s; } server { listen 8888; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location ~/group[0-9]/ { proxy_pass http://fdfs_group1; }
主要是增加 upstream fdfs_group1 配置,
配置Storage服务器上的Nginx 的反向代理
location ~/group[0-9]/ {
proxy_pass http://fdfs_group1;
}7、防火墙端口配置:
请参考这篇文章里的打开防火墙端口配置 :
8、启动各个服务:
启动tracker/etc/init.d/fdfs_trackerd start启动两个storage/etc/init.d/fdfs_storaged start
9、测试文件上传:
首先传一张图片到tracker 服务上的 home目录下,然后执行命令上传到FastDFS文件存储
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/889.jpg存储会返回类似这样的文件路径:group1/M00/00/00/wKgBT1sfnFeATLAXAAA1zm8yF5M375.jpg
然后打开浏览器,输入 http://192.168.1.72:8888/group1/M00/00/00/wKgBT1sfnFeATLAXAAA1zm8yF5M375.jpg
正常情况下浏览器可以直接加载这张上传后的图片。
10、配置各个服务开机自启动项:
1)tracker服务器
vi /etc/rc.d/rc.local 然后添加:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart/application/nginx/sbin/nginx
2)storage服务器
vi /etc/rc.d/rc.local 然后添加:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart/application/nginx/sbin/nginx
以上整个文件存储搭建完成。