Linux安装FastDFS

Linux安装FastDFS

参考连接:https://blog.csdn.net/weixin_43591980/article/details/110947827

安装相关软件

这里我采用的是虚拟机安装,系统是centos 7

下载依赖包

没有这些文件,nginx将不能正常编译

yum install gcc-c++ 
yum install pcre pcre-devel 
yum install zlib zlib-devel 
yum install openssl openssl--devel  
yum install libevent

上传相关的软件

软件下载链接可以在上面参考的链接下载,别忘了还有个nginx

image-20210216153308477

我这里是上传到的/usr/local/fastdfs文件夹下

image-20210216153351533

解压安装

fastdfs-5.05.tar.gz

# 解压
tar -zxvf fastdfs-5.05.tar.gz
cd fastdfs-5.05
# 安装
./make.sh
./make.sh install

同样处理libfastcommon-1.0.7.tar.gz

tar -zxvf libfastcommon-1.0.7.tar.gz
cd libfastcommon-1.0.7
./make.sh
./make.sh install

此时可以在/etc/init.d文件夹下看到相关的文件

image-20210216153743615

注:这里看到的是脚本文件,对应的二进制文件在/usr/bin下,我们常用的systemctl start xxxx 命令也是执行的/etc/init.d/文件夹下的shell文件

可以先将fastdfs-nginx-module_v1.16.tar.gz文件也进行解压,只解压

配置启动软件

创建目录

用于后面存储文件

mkdir /home/fastdfs/storage -p
mkdir /home/fastdfs/tracker -p

image-20210216175012917

复制配置文件

# 切换目录
cd /etc/fdfs
# 复制sample作为配置文件
cp storage.conf.sample storage.conf
cp tracker.conf.sample tracker.conf
# 将fdfs中的配置文件复制过来,后面要使用
cd /usr/local/fastdfs/fastdfs-5.05/conf
cp http.conf /etc/fdfs
cp mime.types /etc/fdfs
# 返回目录
cd /etc/fdfs

image-20210216175230582

修改配置文件

修改storage.conf

# 修改storage.conf
vim storage.conf

# 修改文件中以下内容
# 用于存储数据的文件
base_path=/home/fastdfs/storage
# store_path#,当store_path0不存在时将使用base_path
store_path0=/home/fastdfs/storage
# tracker 服务器地址:端口 ,不能写localhost
tracker_server=192.168.37.153:22122

修改trackerd.conf

vim trackerd.conf

# 修改以下内容
# 存储数据目录
base_path=/home/fastdfs/tracker

启动软件

# 进入usr/bin
cd /usr/bin
# 启动fdfs_trackerd
./usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
# 查看启动状态
ps -ef | grep fdfs_trackerd
# 启动fdfs_storaged
./usr/bin/fdfs_storaged /etc/fdfs/storaged.conf

查看进程信息

image-20210216154641134

image-20210216155140254

fastdfs与nginx配置

配置fastdfs-nginx-module

# 解压
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
# 进入目录
cd /fastdfs-nginx-module/src
# 修改config
vim config
# 将local删掉
# 也可以使用命令替换
:%s+/usr/local/+/usr/+g

image-20210216190525306

安装nginx

# 解压nginx
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
# 配置安装
./configure \
--prefix=/usr/local/fastdfs/nginx \    # 安装完成后将在这个目录生成nginx相关的文件
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs/fastdfs-nginx-module/src	# 指定fastdfs—nginx插件位置

# 编译安装
make && make install

修改配置信息

# 转到fastdfs-nginx-module
cd /usr/local/fastdfs/fastdfs-nginx-module/src
# 移动配置文件到/etc/fastdfs
cp mod_fastdfs.conf /etc/fastdfs
# 修改此文件
vim /etc/fastdfs/mod_fastdfs.conf


# 修改内容
# 存储日志信息的路径
base_path=/home/fastdfs/storage
# 关联的tracker地址, ip:port
tracker_server=192.168.37.153:22122


# 由于我们访问路径时xxx/group/xxxx, 因此需要为true,否则可能导致400
url_have_group_name = true
# 首选路径,没有的话就用base_path
store_path0=/home/fastdfs/storage

启动nginx

# 修改nginx配置
cd /usr/local/fastdfs/nginx/
vim conf/nginx.conf

# 在server中修改如下:
server {
    listen       80;
    server_name  localhost;
	# 监听域名中带有group的,交给FastDFS模块处理,这里参考的别人的,也可以单独在外面配置一个server来处理fastdfs的请求
    location ~/group([0-9])/ {
        ngx_fastdfs_module;
    }
    location / {
        root   html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}


# 启动
./sbin/nginx
# 查看nginx进程
[root@localhost fdfs]# ps -ef | grep nginx
root       6305      1  0 15:23 ?        00:00:00 nginx: master process ./nginx
nobody    10064   6305  0 16:33 ?        00:00:00 nginx: worker process
root      21492   9679  0 20:27 pts/4    00:00:00 grep --color=auto nginx

# 此时可以访问下,测试下80端口是否可用,如果无响应,看看是不是防火墙没有关闭

springboot测试

参考开头的文章

新建工程:

引入依赖:

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.27.2</version>
</dependency>

添加配置:

# fastDFS 相关配置
fdfs:
  so-timeout: 1501 # 读取超时时间
  connect-timeout: 601 # 连接超时时间
  thumb-image: # 缩略图配置
    width: 60
    height: 60
  tracker-list: # tracker地址:你的虚拟机服务器地址+端口(默认是22122)
    - 192.168.37.153:22122

单元测试:

/**
 * @Auther: csp1999
 * @Date: 2020/12/10/11:33
 * @Description: 测试FastDFS 文件上传
 */
// ChatappSpringbootApplication这个是我的工程下的springboot启动类
@SpringBootTest(classes = ChatappSpringbootApplication.class)
public class FastDFSTest {

    // fastDFS 存储客户端
    @Autowired
    private FastFileStorageClient storageClient;

    // 用于获取 fastDFS 图片缩略图
    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @Test
    public void testUpload() throws FileNotFoundException {
        // 获取本机要上传的文件
        File file = new File("C:\\Users\\ye\\Desktop\\spring.jpg");

        /**
         * 上传并保存图片
         *
         * 参数:
         * 1-上传的文件流
         * 2-文件的大小
         * 3-文件的后缀
         * 4-可以不管他
         */
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "jpg", null);

        // 带分组的路径
        System.out.println(storePath.getFullPath());

        // 不带分组的路径
        System.out.println(storePath.getPath());
    }

    @Test
    public void testUploadAndCreateThumb() throws FileNotFoundException {
        // 获取本机要上传的文件
        File file = new File("C:\\Users\\ye\\Desktop\\spring.jpg");

        /**
         * 上传并保存图片并且生成缩略图
         *
         * 参数:
         * 1-上传的文件流
         * 2-文件的大小
         * 3-文件的后缀
         * 4-可以不管他
         */
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                new FileInputStream(file), file.length(), "png", null);

        // 带分组的路径
        System.out.println(storePath.getFullPath());

        // 不带分组的路径
        System.out.println(storePath.getPath());

        // 获取缩略图路径
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
        System.out.println(path);
    }
}

执行测试:

image-20210216205347772

正常的话用ip拼接第一个连接就可以访问了

image-20210216205434106

踩坑解决

nginx启动后,没有work进程

很意外,这种情况我也是第一次见,只有master process, 用浏览器访问了下,发现无响应,怀疑是防火墙的问题,把防火墙关了依然不行,在测试使用curl命令访问也不行,说明肯定是worker 进程没有成功启动的问题,因此想到查看日志信息,看看有什么错误信息

正常展示:
image-20210216203508867

先将nginx中配置文件日志打开
image-20210216203646412

重新启动nginx

./nginx -s reload

使用tail -f logs/error.log 实时显示日志状态

image-20210216204305085

通过日志信息可以看到,由于之前的步骤忘了复制mod_fastdfs.conf、http.conf到/etc/fdfs中,按照提示将相应的文件移动进去,重新启动即可解决。

可能依然没有work process

image-20210216204820766

继续复制mime.types到/etc/fdfs中,重新启动即可解决。

浏览器访问400错误

400 Bad Request

修改配置文件

vim /etc/fdfs/mod_fastdfs.conf

将url_have_group_name=false 改为 url_have_group_name=true

image-20210216161750857

重新启动fdfs_tackerd, fdfs_storage

浏览器访问404错误

image-20210216161558444

404明显是找不到资源文件导致的错误,但是在通过xftp浏览文件时发现上面目录是有文件的,说明上传路径跟访问的路径没有对应

再次查看nginx的日志信息:

image-20210216205604059

果然目录映射到其他地方去了

修改mod_fastdfs.conf 文件

将base_path、store_path0 修改成/home/fastdfs/storage即可

image-20210216210036792

image-20210216210417961

目前就遇到这些问题

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值