FastDFS,一个开源的分布式文件系统,提供负载均衡,冗余备份等功能,可以搭建大容量,高可用的文件服务系统,很适合用于开发电商网站或者图片社交软件,这里主要分享的是详细的成功搭建过程。(本过程基于 CentOS 6环境)

下载FDFS以及依赖

yum groupinstall "Development Tools" 
#下载FastDFS
wget https://codeload.github.com/happyfish100/fastdfs/zip/master
#下载libfastcommon-master 1.27
wget https://github.com/happyfish100/libfastcommon/archive/master.zip
#下载fastdfs-nginx-module
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip
#下载Lua(ngx_openresty)
wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz

解压

unzip master
unzip master.zip
unzip master.zip.1
tar -zxvf ngx_openresty-1.7.10.1.tar.gz

安装 libfastcommon-master

cd libfastcomon-master
./make.sh
./make.sh install

安装FDFS

cd fastdfs-master
./make.sh
./make.sh install

创建数据目录

mkdir -p /data/fdfs     #存放data和log
mkdir -p /data/nginx     #存放 error.log   mod_nginx.log
mkdir -p /data/nginx/logs

修改配置文件

cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
 
#配置tracker
vi /etc/fdfs/tracker.conf
#改动
base_path=/data/fdfs
run_by_group=root
run_by_user=root
 
#配置storage
vi /etc/fdfs/storage/conf
#改动
group_name=group1
run_by_group=root
run_by_user_root
base_path=/data/fdfs
tracker_server= x.x.x.x:22122
store_path0=/data/fdfs

启动及测试

#启动tracker
 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start 
#启动storage
 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start  
#创建一个链接
 ln -s /data/fdfs/data /data/fdfs/data/M00  
#准备一张图片放到/usr/下
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /usr/hello.png

上传成功,先记录下文件路径(group1/M00/00/00/wKgAA1nLdOiAFZ-cAAAAAAAAAAA485.png)以备之后测试访问。

安装Nginx

openresty像是加强版nginx,可以集成很多Lua库/第三方模块,这里用openresty + fastDFS module实现文件访问。

#安装nginx_openresty之前,安装编译环境
yum install pcre-devel openssl openssl-devel
 
#安装nginx_openresty
cd /opt/ngx_openresty-1.7.10.1
./configure --with-luajit --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=../fastdfs-nginx-module-master/src
gmake
gmake install
 
#安装完之后把openresty的mod_fastdfs.conf复制到/etc/fdfs
cd /opt/fastdfs-nginx-module-master/src/
cp mod_fastdfs.conf /etc/fdfs
 
#拷贝http.conf和mime.types到/etc/fdfs
cd /opt/fastdfs-master/conf
cp http.conf mime.types /etc/fdfs/

配置Mod_fastdfs

vi mod_fastdfs.conf
#改动
base_path=/data/nginx
tracker_server=x.x.x.x:22122
group_name=group1
store_path0=/data/fdfs
url_have_group_name=true
log_filename=/data/nginx/logs/mod_nginx.log
group_count=1
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/fdfs

配置Nginx

vi /usr/local/openresty/nginx/conf/nginx.conf
#完整配置如下
#user  nobody;
user root;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log /data/nginx/error.log;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
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  /data/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        charset utf8;
        #access_log  logs/host.access.log  main;
        location /group1/M00 {
            #root   html;
            #index  index.html index.htm;
        alias /data/fdfs/data;
        ngx_fastdfs_module;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

最后,尝试访问之前长传的图片hello.png

http://116.196.*.13/group1/M00/00/00/wKgAA1nLdOiAFZ-cAAAAAAAAAAA485.png

找到文件,图片服务器可用。