centos7 安装 fastdfs

安装依赖

yum install gcc libevent libevent-devel -y

到src目录

cd /usr/local/src

安装 libfastcommon 库

libfastcommon 库是 FastDFS 文件系统运行需要的公共 C 语言函数库
下载或上传:

wget https://github.com/happyfish100/libfastcommon/archive/refs/tags/V1.0.48.tar.gz

解压
下载的文件名:

tar -zxvf V1.0.48.tar.gz

上传的文件名:

tar -zxvf libfastcommon-1.0.48.tar.gz

进入目录

cd libfastcommon-1.0.48/

执行make脚本进行编译

./make.sh

注意: make编译的时候如果报错,需解决错误后再次进行make,通常发生错误是由于Linux缺少某些依赖库导致,根据错误提示解决错误

(1) 执行make install进行安装

./make.sh install

至此 libfastcommon 库安装完毕

1.1.1 安装FastDFS

cd /usr/local/src
wget https://github.com/happyfish100/fastdfs/archive/refs/tags/V6.07.tar.gz
tar -zxvf fastdfs-6.07.tar.gz
cd fastdfs-6.07/
./make.sh
./make.sh install

至此FastDFS安装完成
所有编译出来的文件存放在/usr/bin目录下
所有配置文件存放在/etc/fdfs目录下
另外注意需要把解压后的fastdfs-5.11/conf目录下的两个文件拷贝到/etc/fdfs/ ,否则后续会有很多奇怪问题不好解决

cd /usr/local/src/fastdfs-6.07/conf
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/

修改配置文件

cd /etc/fdfs/

修改tracker.conf文件

cp tracker.conf.sample tracker.conf
vim tracker.conf

默认指向的FastDFS作者余庆的目录,因为在我们的机器上不存在,所有手动改一下

base_path=/opt/fastdfs/tracker               #配置tracker存储数据的目录

修改storage.conf文件

cp storage.conf.sample storage.conf
vim storage.conf
base_path= /opt/fastdfs/storage                      #storage存储数据目录
store_path0= /opt/fastdfs/storage/files          #真正存放文件的目录
tracker_server=192.168.235.128:22122        #注册当前存储节点的跟踪器地址

在Linux服务器上创建上面指定的目录

mkdir -p /opt/fastdfs/tracker
mkdir -p /opt/fastdfs/storage
mkdir -p /opt/fastdfs/storage/files

FastDFS启动

FastDFS服务启动需要启动两个脚本:

启动FastDFS的tracker服务

在任意目录下执行:fdfs_trackerd /etc/fdfs/tracker.conf

启动FastDFS的storage服务

在任意目录下执行:fdfs_storaged /etc/fdfs/storage.conf

查看启动进程

在这里插入图片描述
有启动的执行命令即为启动成功

查看storage是否已经注册到了tracker下

fdfs_monitor /etc/fdfs/storage.conf
在这里插入图片描述

首次启动storage后,会在配置的路径下创建存储文件的目录

在这里插入图片描述

FastDFS重启

重启tracker

fdfs_trackerd /etc/fdfs/tracker.conf restart

重启storage

fdfs_storaged /etc/fdfs/storage.conf restart

FastDFS关闭

关闭tracker执行命令

在任意目录下执行:fdfs_trackerd /etc/fdfs/tracker.conf stop

关闭storage执行命令

在任意目录下执行:fdfs_storaged /etc/fdfs/storage.conf stop

或者kill关闭fastdfs,但不建议在线上使用 kill -9 强制关闭,因为可能会导致文件信息不同步问题

FastDFS测试

     FastDFS安装完成之后,可以使用fdfs_test脚本测试文件上传
测试之前,需要修改client.conf配置文件,修改两个配置
cp client.conf.sample client.conf
base_path=/opt/fastdfs/client
tracker_server=192.168.179.128:22122

在/opt/fastdfs/目录下创建client

mkdir -p /opt/fastdfs/client
测试文件上传
fdfs_test /etc/fdfs/client.conf upload    /root/aa.txt
测试文件删除

fdfs_delete_file /etc/fdfs/client.conf group1/要删除的文件路径

注意
没有搭建集群默认只有一个组group1
后缀名包含-m的为属性文件(meta)
在Linux中并没有磁盘一说,是虚拟的

下载fastdfs-nginx

#跳转目录
cd /usr/local/src
#下载
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/refs/tags/V1.22.tar.gz
#解压(注意名字)
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
#装依赖
yum install gcc openssl openssl-devel pcre pcre-devel zlib zlib-devel –y
#跳转目录
cd nginx-1.15.9
#添加模块(注意路径)
./configure --prefix=/usr/local/nginx --with-http_ssl_module  --add-module=/usr/local/src/fastdfs-nginx-module-1.22/src
#编译
make & make install
#复制
cp ./objs/nginx /usr/local/nginx/sbin/
#查看安装情况
/usr/local/nginx/sbin/nginx -V
#将/home/soft/fastdfs-nginx-module-master/src(自己实际存放Nginx扩展模块的目录)目录下的mod_fastdfs.conf文件拷贝到 /etc/fdfs/目录下,这样才能正常启动Nginx
cp /usr/local/src/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
cd  /etc/fdfs/
修改mod_fastdfs.conf配置文件
vim mod_fastdfs.conf
base_path=/opt/fastdfs/nginx_mod
tracker_server=192.168.235.128:22122
url_have_group_name = true
store_path0=/opt/fastdfs/storage/files
在/opt/fastdfs/目录下创建nginx_mod目录
mkdir -p /opt/fastdfs/nginx_mod

修改Nginx.config:

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

添加:

location ~ /group[1-9]/M0[0-9] {   
     ngx_fastdfs_module; 
}

ngx_fastdfs_module; #这个指令不是Nginx本身提供的,是扩展模块提供的,根据这个指令找到FastDFS提供的Nginx模块配置文件,然后找到Tracker,最终找到Stroager

启动Nginx

/usr/local/nginx/sbin/nginx -s reload

重启或启动FastDFS服务进程

fdfs_trackerd /etc/fdfs/tracker.conf restart
fdfs_storaged /etc/fdfs/storage.conf restart

java中配置

<!--加入FastDFS的java客户端依赖-->
<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/fastdfs-client-java-1.27-SNAPSHOT.jar</systemPath>
</dependency>

解压GitHub下载的包
在项目根目录创建lib文件夹,放入解压后的jar包

拷贝源代码包中的fdfs_client.conf文件到resources目录下,在里面主要配置tracker地址

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 121.5.253.62:22122

创建工具类

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

@Controller
public class FastDFS {
    private static TrackerServer trackerServer = null;
    private static StorageServer storageServer = null;


    @RequestMapping("/upload")
    @ResponseBody
    public String statical(MultipartFile file) {
        return fileUpload(file);
    }


    public static StorageClient getStorageClient() throws IOException, MyException {
        //1.加载配置文件,默认去classpath下加载
        ClientGlobal.init("fdfs_client.conf");
        //2.创建TrackerClient对象
        TrackerClient trackerClient = new TrackerClient();
        //3.创建TrackerServer对象
        trackerServer = trackerClient.getConnection();
        //4.创建StorageServler对象
        storageServer = trackerClient.getStoreStorage(trackerServer);
        //5.创建StorageClient对象,这个对象完成对文件的操作
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        return storageClient;
    }


    public static void closeFastDFS() {
        if (storageServer != null) {
            try {
                storageServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (trackerServer != null) {
            try {
                trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        fileUpload();
    }

    /**
     * 上传文件的方法
     */
    public static void fileUpload() {
        try {
            //1. 获取StorageClient对象
            StorageClient storageClient = getStorageClient();
            //2.上传文件  第一个参数:本地文件路径 第二个参数:上传文件的后缀 第三个参数:文件信息
//            String[] uploadArray = storageClient.upload_file(file.getBytes(), "png", null);
            String[] uploadArray = storageClient.upload_file("D:/My Documents/Pictures/29_785_71a161d6d05edb652d007abdd7554223_f5749f54cb48aa496e43d9ccf337c513.png", "png", null);

            for (String str : uploadArray) {
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } finally {
            closeFastDFS();
        }
    }

    /**
     * 上传文件的方法
     */
    public static String fileUpload(MultipartFile file) {
        try {
            String type = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            System.out.println(type);
            //1. 获取StorageClient对象
            StorageClient storageClient = getStorageClient();
            //2.上传文件  第一个参数:本地文件路径 第二个参数:上传文件的后缀 第三个参数:文件信息
            String[] uploadArray = storageClient.upload_file(file.getBytes(), type, null);
            StringBuilder url = new StringBuilder("http://121.5.253.62");
            for (String str : uploadArray) {
                url.append("/").append(str);
            }
            return url.toString();
        } catch (IOException | MyException e) {
            e.printStackTrace();
        } finally {
            closeFastDFS();
        }
        return null;
    }

    /**
     * 下载文件的方法
     */
    public static void fileDownload() {
        try {
            //1. 获取StorageClient对象
            StorageClient storageClient = getStorageClient();
            //2.下载文件 返回0表示成功,其它均表示失败
            int num = storageClient.download_file("group1",
                    "M00/00/00/rBEAA2B-pjOAWS8DAAAADcVHeBs728.txt", "E:/bb.txt");
            System.out.println(num);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } finally {
            closeFastDFS();
        }
    }


    /**
     * 删除文件的方法
     */
    public static void fileDelete() {
        try {
            //1. 获取StorageClient对象
            StorageClient storageClient = getStorageClient();
            //2.删除文件 返回0表示成功,其它均表示失败
            int num = storageClient.delete_file("group1",
                    "M00/00/00/rBEAA2B-pjOAWS8DAAAADcVHeBs728.txt");
            System.out.println(num);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } finally {
            closeFastDFS();
        }
    }

}

文件存放地址:/opt/fastdfs/storage/files/data
阿里云开放端口:23000,80,8080,22122,包括防火墙
重启或启动FastDFS服务进程
fdfs_trackerd /etc/fdfs/tracker.conf restart
fdfs_storaged /etc/fdfs/storage.conf restart

spring boot配置:

spring:
  servlet:
    multipart:
      enabled: true
      max-file-size: 20MB
      max-request-size: 20MB

Nginx配置:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#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  logs/access.log  main;
    #上传大小限制
    client_max_body_size 5000m;
    #下载大小限制
    proxy_max_temp_file_size 5000m;
    #设置为on表示启动高效传输文件的模式
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /data/build/;
            add_header Cache-Control no-store;
            add_header Cache-Control no-cache;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        
         location ^~ /static/ {
            alias   /data/build/static/;
        }
        
        location ~ /group[1-9]/M0[0-9] {    
             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;
    #    }
    #}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值