一、简单说明
1.源码项目地址:
注:本次安装的不是github上面的FastDfs原作者happyfish100的项目,而是另一个作者tobato在源码基础上做了一些封装优化,即这里安装的其实是一个第三方的FastDfs
tobato项目地址:
https://github.com/tobato/FastDFS_Client.githttps://github.com/tobato/FastDFS_Client.git
本文安装包地址:
2.服务器规划:
跟踪服务器1【主机】(Tracker Server):192.168.22.226
跟踪服务器2【备机】(Tracker Server):192.168.22.227
存储服务器1(Storage Server):192.168.22.228
存储服务器2(Storage Server):192.168.22.229
所有相关软件安装包,在/opt/fastdfs_setup目录
所有相关的配置文件,在/etc/fdfs目录
二、安装基础环境
1.安装依赖
yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
yum install -y make cmake gcc gcc-c++
yum install -y make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.新建安装目录,用于存放安装过程使用到的相关安装包,下面安装也都在该目录进行
mkdir -p /opt/fastdfs_setup
3.克隆源码,编译安装
cd /opt/fastdfs_setup
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.40.tar.gz
wget https://github.com/happyfish100/fastdfs/archive/V5.12.tar.gz
tar -zxvf V1.0.40.tar.gz
cd libfastcommon
./make.sh
./make.sh install
cd ../
tar -zxvf V5.12.tar.gz
cd fastdfs-5.12
./make.sh
./make.sh install
4.创建软链接
cd /usr/local/lib #如果没有/usr/local/lib,则mkdir /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
ln -s /usr/bin/fdfs_trackerd /usr/local/bin
ln -s /usr/bin/fdfs_storaged /usr/local/bin
ln -s /usr/bin/stop.sh /usr/local/bin
ln -s /usr/bin/restart.sh /usr/local/bin
如果是fastdfs5.0.8,上述创建软连接,可能会提示已经存在,但是为了保证没有漏掉,建议还是都执行一遍
三、配置tracker服务器
1.复制样例配置文件到/etc/fdfs目录,并重命名
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
2.修改配置文件
vi /etc/fdfs/tracker.conf
##修改的内容如下:
disabled=false # 启用配置文件
port=22122 # tracker服务器端口(默认22122)
base_path=/opt/fdfs/tracker0 # 存储日志和数据的根目录
store_group=group1
其它参数保留默认配置, 具体配置解释可参考官方文档说明:FastDFS 配置文件详解(修订版1) - 分布式文件系统(FastDFS)-Chinaunix
3.创建base_path指定的目录
mkdir -p /opt/fdfs/tracker0
4.防火墙打开默认端口(22122),重启防火墙
firewall-cmd --zone=public --add-port=22122/tcp --permanent
systemctl restart firewalld.service
5.启动tracker服务器
方式一:
/etc/init.d/fdfs_trackerd start #启动tracker命令
/etc/init.d/fdfs_trackerd stop #停止tracker命令
方拾二:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start #启动tracker命令
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop #关闭tracker命令
方式三:
service fdfs_trackerd start
service fdfs_trackerd stop
四、配置storage服务器
1.复制样例配置文件到/etc/fdfs目录,并重命名
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
2.修改配置文件
vi /etc/fdfs/storage.conf
## 修改的内容如下:
disabled=false # 启用配置文件
port=23000 # storage服务端口
base_path=/opt/fdfs/storage0 # 数据和日志文件存储根目录
store_path0=/opt/fdfs/fs0 # 第一个存储目录
tracker_server=192.168.22.226:22122 # tracker服务器IP和端口
#tracker_server=192.168.22.227:22122 # tracker服务器IP2和端口 227主机tracker配置好启动服务后放开
http.server_port=8888 # http访问文件的端口,默认8888,如果配置nginx需要更改与nginx配置文件监听端口一致,后面配置nginx设置的5080
其它参数保留默认配置, 具体配置解释可参考官方文档说明:FastDFS 配置文件详解(修订版1) - 分布式文件系统(FastDFS)-Chinaunix
3.创建base_path指定的目录
mkdir -p /opt/fdfs/storage0
mkdir -p /opt/fdfs/fs0
4.防火墙打开默认端口(23000),重启防火墙
firewall-cmd --zone=public --add-port=23000/tcp --permanent
systemctl restart firewalld.service
5.启动storage服务器
方式一:
/etc/init.d/fdfs_storaged start #启动tracker命令
/etc/init.d/fdfs_storaged stop #停止tracker命令
方拾二:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start #启动tracker命令
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop #关闭tracker命令
方式三:
service fdfs_storaged start #启动storage命令
service fdfs_storaged stop #停止storage命令
五、文件上传测试
1.修改tracker服务器客户端配置文件
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vi /etc/fdfs/client.conf
# 修改以下配置,其它保持默认
base_path=/opt/fdfs/client
tracker_server=192.168.22.226:22122 #tracker服务器IP和端口
#tracker_server=192.168.22.227:22122 #tracker服务器IP2和端口,227主机配置好并启动tracker服务后添加或放开
#创建数据目录
mkdir -p /opt/fdfs/client
修改后注意重启tracker
2.测试文件上传命令
测试文件:/tmp/401.gif
#/tmp/401.gif 是需要上传文件路径
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /tmp/401.gif
返回文件ID号:group1/M00/00/00/wKgW5F2pD3WAetnoAARhaP7NdhU566.gif(能返回以上文件ID,说明文件已经上传成功)
文件存储路径:192.168.22.228主机:/opt/fdfs/fs0/data/00/00/wKgW5F2pD3WAetnoAARhaP7NdhU566.gif
到此,fastdfs已经安装成功,下面还需要一些其他安装配置
六、storage服务器安装fastdfs-nginx-module
1.fastdfs-nginx-module模块作用说明
FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储,但是同组存储服务器之间需要进入 文件复制,有同步延迟的问题。假设 Tracker 服务器将文件上传到了 ip01,上传成功后文件 ID 已经返回给客户端。此时 FastDFS 存储集群机制会将这个文件同步到同组存储 ip02,在文件还 没有复制完成的情况下,客户端如果用这个文件 ID 在 ip02 上取文件,就会出现文件无法访问的 错误。而 fastdfs-nginx-module 可以重定向文件连接到源服务器取文件,避免客户端由于复制延迟导致的 文件无法访问错误。(解压后的 fastdfs-nginx-module 在 nginx 安装时使用)
2.下载压缩包,解压
cd /opt/fastdfs_setup/
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
tar -zxvf V1.20.tar.gz
3.修改配置文件
cd fastdfs-nginx-module/src
vi config
将
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
修改为:
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
这一步好像不改也可以...可以试试
4.复制fastdfs-nginx-module中的配置文件到/etc/fdfs目录
cd /opt/fastdfs_setup/fastdfs-nginx-module/src/
cp mod_fastdfs.conf /etc/fdfs/
#修改配置文件
vi /etc/fdfs/mod_fastdfs.conf
#修改内容:比如连接超时时间、跟踪器路径配置、url的group配置、
connect_timeout=10
tracker_server=192.168.22.226:22122
url_have_group_name = true
store_path0=/opt/fdfs/fs0
七、storage服务器安装nginx,并添加fastdfs-nginx-module模块
1.安装pcre
cd /opt/fastdfs_setup
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz #安装pcre
tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35 #进入pcre-8.35目录
./configure
make && make install #编译安装
pcre-config --version #查看版本
2.安装nginx
可参考之前文章:
linux环境编译安装nginx_zhanggd57的博客-CSDN博客
3.复制FastDfs中两个配置文件,到/etc/fdfs目录(注:不复制这两个配置文件,nginx无法正常启动)
cd /opt/fastdfs_setup/fastdfs-5.12/conf
cp http.conf mime.types /etc/fdfs/ #将http.conf,mime.types两个文件拷贝到/etc/fdfs/目录下
4.创建软链接
cd /opt/fdfs/fs0 #进入/opt/fdfs/fs0目录下
ln -s /opt/fdfs/fs0/data/ /opt/fdfs/fs0/data/M00
5.nginx添加fastdfs-nginx-module模块
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=CentOS \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module\
--add-module=/opt/fastdfs_setup/fastdfs-nginx-module-1.20/src\
如果是第一次安装nginx在解压完nginx压缩包后进入nginx目录执行上述命令,之后执行安装nginx命令
make && make install #编译安装
如果之前已经安装过nginx,可以执行nginx -V命令,查看安装目录:--prefix后面的参数
然后去上面目录执行上述命令,注意,不要再执行make安装命令了,因为已经安装过了
然后重启nginx即可,重启之前通过nginx -t检查配置文件等是否正确无误。
6.修改nginx配置文件
vi /etc/nginx/nginx.conf
server {
listen 5080; ##监听端口,需要是防火墙开放的端口,同时与storage.conf最后一行配置保持一致
server_name localhost;
```
#charset koi8-r;
#access_log logs/host.access.log main;
location ~/group[0-9]/ {
ngx_fastdfs_module; ##添加模块,这三行要加
}
location / {
root /usr/share/nginx/html; ##nginx默认主页,调试用的,可以默认不用修改
index index.html index.htm;
}
```
然后,去修改storage.conf配置文件中的端口,改为上面的5080
vi /etc/fdfs/storage.conf
the port of the web server on this storage server
http.server_port=5080
重启storage服务,重启nginx服务
测试访问:
http://192.168.22.228:5080/group1/M00/00/00/wKgW5F2pD3WAetnoAARhaP7NdhU566.gif
正常访问,说明安装成功
关于配置nginx代理转发(这些不配置不会影响上面系统)
修改nginx配置文件
vi /etc/nginx/nginx.conf
http{
upstream fastdfs{
#server 192.168.22.118:80;#添加服务器到负载均衡
server 192.168.22.228:5080;
server 192.168.22.229:5080;
#server 192.168.22.113:8085 weight=11;#该服务器在负载均衡中的权重,默认为1;越高则访问的频次越大
#server 192.168.22.110:8085 weight=22;
#server 192.168.22.113:8085 down; #表示down掉这服务器,不参与负载均衡
#server 192.168.22.114:8085 backup; #备用服务器,其他服务器忙或down掉之才启动这台
}
server{
listen 5080; #监听端口
server_name FastDfs-nginx01; #监听域名/地址
location / {
root /usr/share/nginx/html; #nginx启动页面
index index.html index.htm;
}
location /fastdfsservice/file/ {
proxy_pass http://fastdfs/;
# Fix the "It appears that your reverse proxy set up is broken" error.
# 修复点击系统管理,出现的反向代理设置有误提示
#port_in_redirect on;
#proxy_redirect off;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
保存退出,重启nginx
以上配置表示,当浏览器访问
192.168.22.226:5080/fastdfsservice/file/*
192.168.22.227:5080/fastdfsservice/file/*
请求将被转发至
192.168.22.228:5080/fastdfsservice/file/*
192.168.22.229:5080/fastdfsservice/file/*