CentOS7 搭建 FastDFS 环境
1、准备工作:
(1)集群规划(单 tracker,双 storage):
虚拟机 | IP | 说明 |
tracker | 192.168.229.142 | tracker 服务器 |
storage01 | 192.168.229.143 | storage01服务器【group1】 |
storage02 | 192.168.229.144 | storage02服务器【group2】 |
(2)软件清单:
软件名称 | 版本 |
fastdfs | fastdfs-5.11.zip |
fastdfs-nginx-module | fastdfs-nginx-module-master.zip |
libfastcommon | libfastcommon-1.0.36.zip |
nginx | nginx-1.10.3.tar.gz |
安装所需软件:https://pan.baidu.com/s/1htBiHoS#list/path=%2F
2、安装依赖模块(三台机器同时进行):
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
3、安装 libfastcommon 模块(三台机器同时进行):
(1)编译 libfastcommon 软件并安装:
unzip libfastcommon-1.0.36.zip ./make.sh && ./make.sh install
(2)为 libcommon 创建软链接到 /usr/local/lib 目录下::
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
4、编译安装 FastDFS(三台机器同时进行):
unzip fastdfs-5.11.zip ./make.sh && ./make.sh install
安装成功后,FastDFS 会安装在 /etc/fdfs 目录下。
5、配置 tracker:
(1)在 /etc/fdfs/ 目录将 tracker.conf.samp、client.conf.samp 示例文件分别复制一份:
cp tracker.conf.sample tracker.conf cp client.conf.sample client.conf
(2)在 home 目录下创建 fastdfs_tracker 目录,该目录用于保存 tracker 的 data 和 log:
mkdir -p /home/fastdfs_tracker
(3)修改 tracker.conf :
# vim /etc/fdfs/tracker.conf port=22122 #默认端口号 base_path=/home/fastdfs_tracker store_lookup=0 #采用轮询策略进行存储,0:轮询 1:始终定向到某个group 2:负载进行存储文件
(4)启动tracker,并加入开机启动项:
service fdfs_trackerd start
echo "service fdfs_trackerd start" |tee -a /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
6、配置 storage:
(1)在 /etc/fdfs/ 目录将 storage.conf.samp 示例文件复制一份:
cp storage.conf.sample storage.conf
(2)在 home 目录下创建 fastdfs_storage 目录,该目录用于保存 storage 的 data 和 log:
mkdir -p /home/fastdfs_storage
(3)修改 storage.conf:
# storage01 配置 # vim /etc/fdfs/storage.conf group_name=group1 base_path=/home/fastdfs_storage store_path0=/home/fastdfs_storage tracker_server=192.168.229.142:22122 http.server_port=8888
# storage02 配置 # vim /etc/fdfs/storage.conf group_name=group2 base_path=/home/fastdfs_storage store_path0=/home/fastdfs_storage tracker_server=192.168.229.142:22122 http.server_port=8888
(4)设置 storage 开机自启动:
echo "service fdfs_storaged start" |tee -a /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
(5)安装 fastdfs-nginx-module、nginx 模块:
解压 nginx 和 fastdfs-nginx-module:
tar -zxvf nginx-1.10.3.tar.gz unzip fastdfs-nginx-module-master.zip
进入 nginx 解压目录进行编译安装:
./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs/fastdfs-nginx-module-master/src #解压后fastdfs-nginx-module所在的位置 make && make install
复制 fastdfs-nginx-module 源码中的配置文件到 /etc/fdfs 目录, 并修改:
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ vim /etc/fdfs/mod_fastdfs.conf
# storage01 配置: # vim /etc/fdfs/mod_fastdfs.conf connect_timeout=10 base_path=/home/fastdfs_storage url_have_group_name=true store_path0=/home/fastdfs_storage tracker_server=192.168.229.142:22122 group_name=group1
# storage02 配置: # vim /etc/fdfs/mod_fastdfs.conf connect_timeout=10 base_path=/home/fastdfs_storage url_have_group_name=true store_path0=/home/fastdfs_storage tracker_server=192.168.229.142:22122 group_name=group2
复制 FastDFS 安装目录的部分配置文件到 /etc/fdfs 目录:
[root@Centos2 conf]# pwd /usr/local/fastdfs/fastdfs-5.11/conf [root@Centos2 conf]# cp http.conf mime.types /etc/fdfs/
创建 M00 至 storage 存储目录的符号连接:
ln -s /home/fastdfs_storage/data/ /home/fastdfs_storage/data/M00
配置 nginx, 简洁版 nginx 配置样例:
# vim /usr/local/nginx/conf/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])/M00 { ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
重新启动各节点的 nginx 服务:
/usr/local/nginx/sbin/nginx -s reload
7、文件上传测试:
(1)修改 Tracker 服务器中的客户端配置文件:
# vim /etc/fdfs/client.conf base_path=/home/fastdfs_tracker tracker_server=192.168.229.142:22122
(2)执行如下文件上传命令:
[root@Centos fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg group1/M00/00/00/wKjlj15nvoSAdU2fAAI8KP0mavw101.jpg [root@Centos fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg group2/M00/00/00/wKjlkF5nvoiAXHHiAAI8KP0mavw561.jpg
进行2次上传后,发现文件被均衡分到2个group。
8、tracker 安装 nginx:
(1)解压并编译安装 Nginx:
tar -zxvf nginx-1.10.3.tar.gz ./configure --prefix=/usr/local/nginx make && make install
(2)配置 Nginx,简洁版 nginx 配置样例:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #group1 upstream fdfs_group1 { server 192.168.229.143:8888; } #group2 upstream fdfs_group2 { server 192.168.229.144:8888; } server { listen 8000; server_name localhost; location /group1/M00 { proxy_pass http://fdfs_group1; } location /group2/M00 { proxy_pass http://fdfs_group2; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
9、重启 tracker 的Nginx,并进行访问测试:
/usr/local/nginx/sbin/nginx -s reload
通过 Tracker 中的 Nginx 来进行访问: