Linux下安装配置FastDFS

前言
在项目开发过程中难免会碰到上传的图片管理的问题,一般传统的方式是使用本地的文件管理,但是一旦图片的量大起来,就会遇到扩展及管理困难等问题,并且图片的访问会占用较多的服务器带宽,如果和应用放在一起会拖慢整个网站的速度,因此在图片较多的应用上,比如商城,引入一个专业的图片管理服务是必要的。
笔者在一个项目中遇到,使用者需要在APP端上传大量图片的情况,因此引入了FastDFS。
FastDFS是一个开源的分布式文件管理系统,主要提供文件的存储、同步、访问等功能,解决大容量存储及负载均衡问题。

下面介绍在Linux下安装及配置FastDFS的操作步骤:

安装环境:
OS:CentOS release 6.6 (Final)
JDK:jdk-7u79-linux-x64.tar.gz
FastDFS:FastDFS_v5.08.tar.gz
访问代理:nginx-1.8.1.tar.gz

1、基础环境的安装
JDK
由于FastDFS是在Java平台上开发的,因此需要服务器上安装Java,具体的操作步骤,可以参照笔者的另一篇博客Linux
下配置Java和Tomcat

,此处不做重点介绍。

安装zlib
依次执行如下命令

tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8 
./configure 
make && make install

安装pcre
依次执行如下命令

tar -zxvf pcre-8.38.tar.gz 
cd pcre-8.38
./configure 
make && make install

安装libfastcommon
依次执行如下命令

unzip libfastcommon-master.zip 
cd libfastcommon-master
./make.sh
./make.sh install

需要注意的是,执行完上述安装后,库文件被安装在/usr/lib64目录下,但是FastDFS的默认查找地址为/usr/local/lib,因此需要建立文件的软连接,否则在安装时会找不到对应的文件。
执行如下命令:

ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so

2、安装FastDFS
依次执行如下命令

tar -zxvf FastDFS_v5.08.tar.gz 
cd FastDFS
./make.sh 
./make.sh install

如果第一步中的基础文件安装成功,这一次安装会很顺利的。

3、配置FastDFS
为FastDFS建立对应的文件存储目录

/fdfs/tracker
/fdfs/storage
/fdfs/client

安装好后会在/etc/fdfs/目录下找到对应的实例配置文件

-rw-r--r--. 1 root root 1461 5月  31 13:40 client.conf.sample
-rw-r--r--. 1 root root 7927 5月  31 13:40 storage.conf.sample
-rw-r--r--. 1 root root 7200 5月  31 13:40 tracker.conf.sample

依次配置对应的文件
配置Tracker服务器

cp tracker.conf.sample tracker.conf

修改配置内容
base_path=/fdfs/tracker

启动服务

fdfs_trackerd /etc/fdfs/tracker.conf

配置storage

cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

修改配置
base_path=/fdfs/storage
store_path0=/fdfs/storage
tracker_server=192.168.255.143:22122

启动服务器

fdfs_storaged /etc/fdfs/storage.conf

配置Client

cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

修改配置
base_path=/fdfs/client
tracker_server=192.168.255.143:22122

测试配置

fdfs_upload_file /etc/fdfs/client.conf install.log

如果返回

group1/M00/00/00/wKj_j1dNK22AIvsRAAAlWhKQqA4805.log

证明配置成功

4、配置FastDFS的nginx代理
解压fastdfs-nginx-module
FastDFS通过Tracker服务器,将文件放在Storage服务器存储,
但是同组之间的服务器需要复制文件,有延迟的问题.
假设Tracker服务器将文件上传到了192.168.1.80,文件ID已经返回客户端,
这时,后台会将这个文件复制到192.168.1.30,如果复制没有完成,客户端就用这个ID在192.168.1.30取文件,肯定会出现错误
这个fastdfs-nginx-module可以重定向连接到源服务器取文件,避免客户端由于复制延迟的问题,出现错误。

tar -zxvf fastdfs-nginx-module_v1.16.tar.gz 

修改配置文件
源代码:CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
改为:CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

安装nginx

tar -zxvf nginx-1.8.1.tar.gz 
cd nginx-1.8.1
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--add-module=/root/tools/fastdfs-nginx-module/src
make && make install

配置fastdfs-nginx-module

cp /root/tools/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

修改配置文件
base_path=/fdfs/client
tracker_server=192.168.255.143:22122
store_path0=/fdfs/storage

增加mime和http配置文件,如果少了这一步nginx可能会不能启动的,因为找不到对应的mime类型。

cd /root/tools/FastDFS/conf/
cp mime.types /etc/fdfs/
cp http.conf /etc/fdfs/

配置nginx

cd /usr/local/nginx/conf/

vim nginx.conf

添加如下配置
location / {
    root   html;
    index  index.html index.htm;
}

location /M00{
        root /fdfs/storage/data/M00;
        ngx_fastdfs_module;
}

配置nginx启动脚本

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# 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/logs/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"

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
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

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)
        $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}"
        exit 2
esac

配置完成以后在启动nginx服务

service nginx start

在浏览器中访问http://192.168.255.143:8888/M00/00/00/wKj_j1dNK22AIvsRAAAlWhKQqA4805.log
如果能够下载刚刚上传的文件则配置成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FastDFS是一个开源的轻量级分布式文件系统,可以用于存储大文件,支持文件上传、下载和删除等操作。下面是在Linux安装配置FastDFS的步骤: 1. 安装libfastcommon ``` $ git clone https://github.com/happyfish100/libfastcommon.git $ cd libfastcommon/ $ ./make.sh $ ./make.sh install ``` 2. 安装FastDFS ``` $ git clone https://github.com/happyfish100/fastdfs.git $ cd fastdfs/ $ ./make.sh $ ./make.sh install ``` 3. 配置Tracker服务器 在/etc/fdfs目录下创建tracker目录,并在该目录下创建tracker.conf文件,内容如下: ``` #bind_addr= # bind to all IP addresses port=22122 # base_path=/home/yuqing/fastdfs/tracker # the base storage path for storing data and logs # store_group=group1,group2 # the group name list that can be used in this tracker server # group_name=group1 # the group name of this tracker server ``` 4. 配置Storage服务器 在/etc/fdfs目录下创建storage目录,并在该目录下创建storage.conf文件,内容如下: ``` #base_path=/home/yuqing/fastdfs/storage # the base storage path for storing data and logs #store_path0=/home/yuqing/fastdfs/storage # the first storage path for storing data tracker_server=192.168.1.100:22122 #group_name=group1 # the group name of this storage server #http.server_port=8888 # the port that the HTTP server listens on ``` 其中,tracker_server为Tracker服务器的IP和端口号。 5. 启动Tracker和Storage服务器 启动Tracker服务器: ``` $ /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf ``` 启动Storage服务器: ``` $ /usr/bin/fdfs_storaged /etc/fdfs/storage.conf ``` 6. 测试上传和下载文件 上传文件: ``` $ /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg ``` 下载文件: ``` $ /usr/bin/fdfs_download_file /etc/fdfs/client.conf group1 M00/00/00/test.jpg /tmp/test.jpg ``` 其中,group1为存储文件的组名,M00/00/00/test.jpg为文件的路径和文件名。 以上就是在Linux安装配置FastDFS的全部步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值