1Nginx+fastdfs分布式文件存储



准备,将所需的软件传到服务器上,服务器的列表如下:


fastdfs-nginx-module_v1.15.tar.gz

FastDFS_v4.06.tar.gz

libevent-2.0.21-stable.tar.gz

nginx-1.5.6.tar.gz

openssl-1.0.1c.tar.gz

pcre-8.36.tar.gz

zlib-1.2.7.tar.gz

  1. 首先切换到root用户,命令是:

su root

  1. 删除系统自带的低版本的libevent ,
    注意:这里的tracker和storage端都做相同的操作。

查看系统上是否安装了libevent

rpm -qa | grep libevent

libevent-1.4.13-4.el6.x86_64

删除已经安装过的libevent

yum remove libevent-1.4.13-4.el6.x86_64


安装libevent2.0.21,可以通过.wget的方式进行下来所需要的tar包


1>.wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

2> tar -zxvf libevent-2.0.21-stable.tar.gz

3> cd libevent-2.0.21-stable

4> ./configure --prefix=/usr/local/libevent

5> make && make install

6>
为libevent创建软链接到/lib库下,64位系统对应/lib64 
(注意下面的的只选择其一)

ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5

ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

4
安装Fastdfs


1>
下载Fastdfs,需要翻墙来。

wget http://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz

2> tar -zxvf FastDFS_v4.06.tar.gz

3> cd FastDFS

4>
由于定义/usr/local/fastdfs为fastdfs安装目录,所以需要修改make.sh

vim make.sh

#内容更改如下

TARGET_PREFIX=/usr/local 修改为 /usr/local/fastdfs

将/etc/fdfs
全部替换为 /usr/local/fastdfs/conf

vim替换命令,进入末行模式(ESC 
再按shift :)

%s/\/etc\/fdfs/\/usr\/local\/fastdfs\/conf/g
 (注意,这里”\/”表示转义’/’ ,g表示全部替换)

5>
安装

./make.sh C_INCLUDE_PATH=/usr/local/libevent/include LIBRARY_PATH=/usr/local/libevent/lib

./make.sh install

===================以上是Tracker和Storage都需要安装的部分======================

5
配置Tracker(它是单独一台电脑上的,并且在使用storage服务器的时候,此端要先启动)

注意Tracker服务器IP地址不能是127.0.0.1,Tacker不能和Storage在一台电脑上。


1>
创建tracker目录保存运行日志

mkdir -m 777 -p /home/fastdfs/tracker

2>
修改tracker.conf配置

vim /usr/local/fastdfs/conf/tracker.conf

3>
修改内容如下所示

# the tracker server port

port=22122

# the base path to store data and log files

base_path=/home/yuqing/fastdfs -> base_path=/home/fastdfs/tracker #日志目录

#开启自定义server ID取代ip形式,方便内部网络服务器更换ip#**此方式要重点理解,4.0以后新特性

use_storage_id = true #使用server ID作为storage
server标识

storage_ids_filename = storage_ids.conf #<id> <group_name> <ip_or_hostname>

id_type_in_filename = id #文件名反解析中包含server ID,以前是ip

复制代码

4>
移动storage_ids.conf文件

cp  <FastDFS源码目录>/conf/storage_ids.conf /usr/local/fastdfs/conf/

5>
编辑storage服务器ID与IP地址的对应关系

vim /usr/local/fastdfs/conf/storage_ids.conf

修改内容如下所示

#<id> <group_name> <ip_or_hostname>

100001          group1          
192.168.1.12

#100002          group2          
192.168.1.13

#100003          group2          
192.168.1.14

6>
编辑tracker服务器启动脚本

vim /etc/init.d/fdfs_trackerd

#启动脚本内容如下

#!/bin/bash

#

# fdfs_trackerd Starts fdfs_trackerd

#

#

# chkconfig: 2345 99 01

# description: FastDFS tracker server

### BEGIN INIT INFO

# Provides: $fdfs_trackerd

### END INIT INFO

# Source function library.

. /etc/init.d/functions

FastDfs=‘/usr/local/fastdfs‘

CONF="$FastDfs/conf/tracker.conf"

if [ ! -f $CONF ]; then

echo "file $CONF does not exist!"

exit 2

fi

PRG="$FastDfs/bin/fdfs_trackerd"

if [ ! -f $PRG ]; then

echo "file $PRG does not exist!"

exit 2

fi

Stop="$FastDfs/bin/stop.sh"

if [ ! -f $Stop ]; then

echo "file $Stop does not exist!"

exit 2

fi

Restart="$FastDfs/bin/restart.sh"

if [ ! -f $Restart ]; then

echo "file $Restart does not exist!"

exit 2

fi

RETVAL=0

start() {

echo -n $"Starting FastDFS tracker server: "

$PRG $CONF &

RETVAL=$?

echo

return $RETVAL

}

stop() {

echo -n $"Stop FastDFS tracker server: "

$Stop $PRG $CONF

RETVAL=$?

return $RETVAL

}

rhstatus() {

status fdfs_trackerd

}

restart() {

$Restart $PRG $CONF &

}

case "$1" in

start)

start

;;

stop)

stop

;;

status)

rhstatus

;;

restart|reload)

restart

;;

condrestart)

restart

;;

*)

echo $"Usage: $0 {start|stop|status|restart|condrestart}"

exit 1

esac

exit $?

7>
给启动脚本增加权限

chmod 777 /etc/init.d/fdfs_trackerd

8>
启动tracker

service fdfs_trackerd restart

9>
启动成功,加入开机启动

vim /etc/rc.d/rc.local

#加入内容如下

service fdfs_trackerd start

10>
防火墙开启tracker端口22122

vim /etc/sysconfig/iptables

#加入内容如下

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT

11>
重启防火墙

service iptables restart

注意的时候,最后运行Tracker的时候,要关闭防火墙,也就是说要执行service
iptables stop

===============================================================================

6.
配置storage服务器,注意的是storage和Nginx是在另外一台电脑上的。

1>
创建Storage目录保存运行日志及其data数据

mkdir-m 777 -p /home/fastdfs/storage

2>
修改storage.conf配置

vim/usr/local/fastdfs/conf/storage.conf

#修改内容如下所示

#the name of the group this storage server belongs to

group_name=group1#设置组名

#the name of the group this storage server belongs to

#the storage server port #the storage server port

port=23000

#the base path to store data and log files #日志目录

base_path=/home/yuqing/fastdfs-> /home/fastdfs/storage

#store_path#, based 0, if store_path0 not exists, it‘s value is base_path #data数据存储目录

#the paths must be exist

store_path0=/home/fastdfs/storage

#tracker_server can ocur more than once, and tracker_server format is

#  "host:port", host can be hostnameor ip address

tracker_server=192.168.209.121:22122->192.168.1.11:22122 
这里192.168.1.11:22122表示的是tracker所在电脑的ip地址

3>
编辑启动脚本

vim/etc/init.d/fdfs_storaged

#启动脚本内容如下

#!/bin/bash

#

#fdfs_storaged Starts fdfs_storaged

#

#

#chkconfig: 2345 99 01

#description: FastDFS storage server

###BEGIN INIT INFO

#Provides: $fdfs_storaged

###END INIT INFO

#Source function library.

./etc/init.d/functions

FastDfs=‘/usr/local/fastdfs‘

CONF="$FastDfs/conf/storage.conf"

if[ ! -f $CONF ]; then

echo "file $CONF does not exist!"

exit 2

fi

PRG="$FastDfs/bin/fdfs_storaged"

if[ ! -f $PRG ]; then

echo "file $PRG does not exist!"

exit 2

fi

Stop="$FastDfs/bin/stop.sh"

if[ ! -f $Stop ]; then

echo "file $Stop does not exist!"

exit 2

fi

Restart="$FastDfs/bin/restart.sh"

if[ ! -f $Restart ]; then

echo "file $Restart does notexist!"

exit 2

fi

RETVAL=0

start(){

echo -n $"Starting FastDFS storageserver: "

$PRG $CONF &

RETVAL=$?

echo

return $RETVAL

}

stop(){

echo -n $"Stop FastDFS storage server:"

$Stop $PRG $CONF

RETVAL=$?

return $RETVAL

}

rhstatus(){

status fdfs_storaged

}

restart(){

$Restart$PRG $CONF &

}

case"$1" in

start)

start

;;

stop)

stop

;;

status)

rhstatus

;;

restart|reload)

restart

;;

condrestart)

restart

;;

*)

echo $"Usage: $0{start|stop|status|restart|condrestart}"

exit 1

esac

exit$?

4>
给启动脚本增加权限

chmod777 /etc/init.d/fdfs_storaged

5>
启动storage 
(注意的是,启动storage的时候要先启动storage)

servicefdfs_storaged restart

#正常情况,接下来会出现很多mkdirdata path,这是系统在创建数据目录

6>
启动成功,加入开机启动

vim/etc/rc.d/rc.local

#加入内容如下

servicefdfs_storaged start

7.
安装nginx(仅正在有Storage的电脑上安装)

1>
创建nginx日志目录

mkdir-m 777 -p /home/www/logs

2>
安装nginx必需的库:zlib-developenssl-devel pcre

yum -y install zlib-devel openssl-devel

3>
手动安装pcre

1> wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz

2> tar -zxvf pcre-8.36.tar.gz

3> cd pcre-8.36

4> ./configure

5> make && make install

6> ln -s /usr/local/lib/libpcre.so.1 /lib

4>
安装nginx

1> wget http://nginx.org/download/nginx-1.5.6.tar.gz

2> tar -zxvf nginx-1.5.6.tar.gz

3> cd nginx-1.5.6

4> ./configure --prefix=/usr/local/nginx --with-http_stub_status_module--with-http_ssl_module  (注意:这里的--with-http_ssl_module可以不加)

5> make && make install

5>
检查nginx配置是否正确

/usr/local/nginx/sbin/nginx -t

6>
查看nginx编译选项

/usr/local/nginx/sbin/nginx -V

7>
编辑启动脚本

vim /etc/init.d/nginxd

#启动脚本内容如下

#!/bin/sh

#

#nginx - this script starts and stops the nginx daemin

#

#description:  Nginx is an HTTP(S) server,HTTP(S) reverse \

#               proxy and IMAP/POP3 proxy server

#processname: nginx

#config:     /usr/local/nginx/conf/nginx.conf

#pidfile:     /usr/local/nginx/nginx.pid

#Source function library.

./etc/rc.d/init.d/functions

#Source networking configuration.

./etc/sysconfig/network

#Check that networking is up.

["$NETWORKING" = "no" ] && exit 0

#必填

nginx="/usr/local/nginx/sbin/nginx"

prog=$(basename$nginx)

#必填

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[-f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start(){

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

echo -n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

#service php-fpm start

[ $retval -eq 0 ] && touch$lockfile

return $retval

}

stop(){

echo -n $"Stopping $prog: "

$nginx -s stop

echo_success

retval=$?

echo

#service php-fpm stop

[ $retval -eq 0 ] && rm -f$lockfile

return $retval

}

restart(){

stop

start

}

reload(){

configtest || return $?

echo -n $"Reloading $prog: "

$nginx -s reload

RETVAL=$?

echo

}

force_reload(){

restart

}

configtest(){

$nginx -t -c $NGINX_CONF_FILE

}

version(){

$nginx -V

}

rh_status(){

status $prog

}

rh_status_q(){

rh_status >/dev/null 2>&1

}

case"$1" in

start)

rh_status_q&& exit 0

$1

;;

stop)

rh_status_q|| exit 0

$1

;;

restart|configtest|version)

$1

;;

reload)

rh_status_q|| exit 7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q|| exit 0

;;

*)

echo$"Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest|version}"

exit2

esac

8>
给启动脚本增加权限

chmod777 /etc/init.d/nginxd

9>
启动nginx

servicenginxd restart

如果启动失败提示找不到库,ldconfig更新库路径

10>
启动成功,加入开机启动

vim/etc/rc.d/rc.local

#加入内容如下

servicenginxd start

8.
安装nginx-module模块(仅在有Storage的电脑上)

1>
下载fastdfs-nginx-module,google被禁了,需要翻墙

wgethttp://fastdfs.googlecode.com/files/fastdfs-nginx-module_v1.15.tar.gz

2> tar -zxvffastdfs-nginx-module_v1.15.tar.gz;

3>
修改插件配置文件,主要是跟安装fastdfs目录相关

vim<fastdfs-nginx-module源码目录>/src/config

#修改内容如下

ngx_addon_name=ngx_http_fastdfs_module

HTTP_MODULES="$HTTP_MODULESngx_http_fastdfs_module"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS$ngx_addon_dir/ngx_http_fastdfs_module.c"

CORE_INCS="$CORE_INCS/usr/local/fastdfs/include/fastdfs /usr/local/fastdfs/include/fastcommon/"

CORE_LIBS="$CORE_LIBS-L/usr/local/fastdfs/lib -lfastcommon -lfdfsclient"

CFLAGS="$CFLAGS-D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE=‘256*1024‘-DFDFS_MOD_CONF_FILENAME=‘\"/usr/local/fastdfs/conf/mod_fastdfs.conf\"‘"

4>
复制mod_fastdfs.conf到/usr/local/fastdfs/conf/目录下

cp<fastdfs-nginx-module源码目录>/src/mod_fastdfs.conf /usr/local/fastdfs/conf/

5>
将/usr/local/fastdfs/lib加入系统文件/etc/ld.so.conf中(编译时使用的动态链接库)

vim/etc/ld.so.conf

#修改内容如下

/usr/local/fastdfs/lib

6>
更新库文件缓存ld.so.cache

ldconfig

7>
编译fastdfs-nginx-module模块

1> cd nginx-1.5.6/

2> ./configure
此处加上nginx之前的编译参数(使用 /usr/local/nginx/sbin/nginx -V
命令查看)--add-module=<fastdfs-nginx-module源码目录>/src

3> make && make install

8>
修改mod_fastdfs.conf配置

vim/usr/local/fastdfs/conf/mod_fastdfs.conf

#修改内容如下

#if load FastDFS parameters from tracker server

#since V1.12

#default value is false

load_fdfs_parameters_from_tracker=true

#FastDFS tracker_server can ocur more than once, and tracker_server format is

#  "host:port", host can be hostnameor ip address

#valid only when load_fdfs_parameters_from_tracker is true

tracker_server=192.168.25.11:22122

#the port of the local storage server

#the default value is 23000

storage_server_port=23000

#the group name of the local storage server

group_name=group1#当前storage机器组名

#if the url / uri including the group name

#set to false when uri like /M00/00/00/xxx

#set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx

#default value is false

url_have_group_name= true

#path(disk or mount point) count, default value is 1

#must same as storage.conf

store_path_count=1

#store_path#, based 0, if store_path0 not exists, it‘s value is base_path

#the paths must be exist

#must same as storage.conf

store_path0=/home/fastdfs/storage

#set the log filename, such as /usr/local/apache2/logs/mod_fastdfs.log

#empty for output to stderr (apache and nginx error_log file)

log_filename=/home/www/logs/mod_fastdfs.log

9.
修改nginx.conf配置

vim /usr/local/nginx/conf/nginx.conf

#修改内容如下(下面的可以替换原来文件中的所有内容)

worker_processes  2;

error_log  /home/www/logs/error.log 
notice;

pid        /home/www/logs/nginx.pid;

worker_rlimit_nofile5120;

events{

use epoll;

worker_connections  5120;

}

http{

include       mime.types;

default_type  application/octet-stream;

sendfile        on;

tcp_nopush     on;

keepalive_timeout  60;

tcp_nodelay on;

server {

listen       80;

server_name  localhost;

location/group1/M00 {

alias /home/fastdfs/storage/data;

ngx_fastdfs_module;

}

}

}

10.
测试(注意:如果不成功,还要修改client.config中的内容)

1.创建测试文件

echo "hello nginx fastdfs" >test.html

2.查看fastdfs自带测试命令使用方法

/usr/local/fastdfs/bin/fdfs_test -?

3.上传测试文件,通过tracker转存到storage服务器里

/usr/local/fastdfs/bin/fdfs_test/usr/local/fastdfs/conf/client.conf upload test.html

得到以下信息

tracker_query_storage_store_list_without_group:

server1. group_name=, ip_addr=172.16.63.13, port=23000

group_name=group1, ip_addr=172.16.63.13,port=23000

storage_upload_by_filename

group_name=group1,remote_filename=M00/00/00/oYYBAFSSxiuABDlLAAAABncc3SA45.html

source ip address: 172.16.63.13

file timestamp=2014-12-18 20:18:51

file size=6

file crc32=1998380320

file url:http://172.16.63.12:8080/group1/M00/00/00/oYYBAFSSxiuABDlLAAAABncc3SA45.html

storage_upload_slave_by_filename

group_name=group1, remote_filename=M00/00/00/oYYBAFSSxiuABDlLAAAABncc3SA45_big.html

source ip address: 172.16.63.13

file timestamp=2014-12-18 20:18:51

file size=6

file crc32=1998380320

file url:http://172.16.63.12:8080/group1/M00/00/00/oYYBAFSSxiuABDlLAAAABncc3SA45_big.html

4.打开浏览器,查看上传文件

http://172.16.63.13/group1/M00/00/00/oYYBAFSSwuKAO89DAAAABncc3SA04.html

看到"hello nginx fastdfs"则成功

备注:

1>
由于没有设置好防火墙,同组group到storage之间备份数据失败,关闭防火墙:service
iptables stop

2>
至少有一个storage到主机安装nginx

3> mod_fastdfs.conf此配置文件安装路径

4> tracker服务器ip不能是127.0.0.1

时间: 2024-07-29 15:29:35

1Nginx+fastdfs分布式文件存储的相关文章

FastDFS分布式文件存储

  存储能力是提供给上层业务系统以实现文件存取服务,这个存储能力为XDFS,即可扩展的分布式文件系统,实现的原理是封装了第三方的分布式存储系统实现的. 目前封装的第三方分布式系统包括FastDFS.Hadoop的HDFS,所提供的存储接口如下: 1  文件上传 2 文件断点上传 3 文件下载 4 文件断点下载 5  文件删除 6 获取文件大小 7  获取文件Http下载地址 8 批量获取文件Http下载地址 FastDFS适合的场景是互联网大中型企业的高并发文件存储服务,比如存储图片.音频.视频

FastDFS 分布式文件存储

FastDFS 分布式文件存储 什么是FastDFS? FastDFS是一个开源的轻量级的分布式文件系统.他解决了大量数据存储和负载均衡等问题.特别适合以中小文件(4KB < FileSize < 500MB)为载体的在线服务,如视频,音频,图片网站等等.FastDFS是一款开源的轻量级分布式文件系统,他是由纯 C 实现,支持Linux,FreeBSD等UNIX系统类,不是通用的文件系统,只能通过专有的API访问,目前提供了C.Java和PHP API为互联网应用量身定做,解决大容量文件存储问

FastDFS分布式文件系统配置与部署

? ? ? FastDFS分布式文件系统配置与部署 白宁超 2017年4月14日 ? ? 注:1.1-1.4节是对FastDFS基本配置,如果读者只是安装分布式文件系统,简单关注其原理建议阅读1.2节与1.5节.如果读者仅仅就是安装为目的建议直接阅读1.5节. 本人邮箱:[email protected] 主页网址:http://www.cnblogs.com/baiboy/ ? 目录 ? 1.1 分布式文件系统介绍????1 1.2 系统架构介绍????1 1.3 FastDFS性能方案???

分布式文件存储fastdfs

FastDFS是一个开源的轻量级应用级的分布式文件存储服务,它对文件进行管理,功能包括: 文件存储.文件同步.文件访问(文件上传.文件下载)等解决了大容量存储和负载均衡的问题. 特别适合以文件为载体的在线服务,如相册网站.视频网站等等. FastDFS有三个角色:跟踪服务器(tracker server).存储服务器(storage server)和客户端(client). 1)跟踪服务器:主要做调度工作 起负载均衡的作用 2)存储服务器:存储节点存储文件 完成文件管理的所有功能:存储.同步和提

.Net平台下,分布式文件存储的实现

遇到的问题 对于Web程序,使用一台服务器的时候,客户端上传的文件一般也都是存储在这台服务器上.但在集群环境中就行不通了,如果每个服务器都存储自己接受到的文件,就乱套了,数据库中明明有这个附件的记录,却找不到这个文件.于是,文件需要进行统一集中管理,并向集群中的服务器提供统一的路径. 基于NFS的分布式文件存储实现 Network File System 简称NFS,用人话说叫共享文件夹,可以实现分布式存储文件.只需要在文件服务器上共享文件夹,并指定相应账号的权限,并给Web服务器设置可以访问共

分布式文件存储

Storage不小心看到这个类,注释写着// 分布式文件存储类....不懂,吖的,google不能用,只能用某度,挺火大的. 又找到ThinkPHP\Library\Think\Storage\Driver 下面的File.class.php,那么,Storage类只是通过判断type去加载File.class.php或Sea.class.php 暂时不懂原理,Storage就两个方法,能看懂,但不理解为什么要这样写,继续看. 分布式文件存储,布布扣,bubuko.com

分布式文件存储FastDFS(一)初识FastDFS

一.FastDFS简介 FastDFS是一款开源的.分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发.作为一个分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题,特别适合中小文件(建议范围:4KB < file_size <500MB),对以文件为载体的在线服务,如相册网站.视频网站等等具有显著的效果. 二.FastDFS架构 FastDFS由客户端,跟踪服务器和

(转) 分布式文件存储FastDFS(一)初识FastDFS

http://blog.csdn.net/xingjiarong/article/details/50559849 一.FastDFS简介 FastDFS是一款开源的.分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发.作为一个分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题,特别适合中小文件(建议范围:4KB < file_size <500MB),对以文件为载

FastDFS + Nginx实现基于CentOS7平台的分布式文件存储与访问

相比较于MogileFS,FastDFS同样有tracker和storage这样的功能分类.但是FastDFS的tracker节点的元数据信息是由各个storage节点通过tcp协议上报得到的,因此在一定程度上减轻了tracker的负载压力.storage节点以group为单位进行组织.任何一个storage server都应该属于某个group,一个group应该包含多个storage server:在同一个group内部,各storage server的数据互相冗余. 本文通过构建FastD