在CentOS8上安装FastDFS实现分布式文件系统并结合Nginx实现图片文件自动裁剪

在CentOS8上安装FastDFS实现分布式文件系统并结合Nginx实现图片文件自动裁剪
本文将介绍如何在CentOS8系统上安装FastDFS+Nginx环境实现分布式图片系统以及图片文件自动裁剪功能。

一、准备环境

需要的安装文件的相关tar包:

fastdfs-6.06.tar.gz
libfastcommon-1.0.43.tar.gz
fastdfs-nginx-module-1.22.tar.gz
nginx-1.23.0.tar.gz

下载地址:

fastdfs: https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
libfastcommon:https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
fastdfs-nginx-module:https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
nginx地址: http://nginx.org/download/nginx-1.23.0.tar.gz

1、使用wget命令下载压缩包,如果没有wget命令使 yum命令安装

yum install wget

2、下载fastdfs

wget -c "https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz" \
-O fastdfs-6.06.tar.gz

3、下载 libfastcommon

wget -c "https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz" \
-O libfastcommon-1.0.43.tar.gz

4、下载fastdfs-nginx-module

wget -c "https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz" \
-O fastdfs-nginx-module-1.22.tar.gz

5、下载 nginx

    wget -c http://nginx.org/download/nginx-1.23.0.tar.gz

6、安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

7、测试环境关闭防火墙

systemctl stop firewalld.service

8、开机禁用防火墙(可选)

systemctl disable firewalld.service

9、查看防火墙状态

firewall-cmd --state

二、编译安装FastDFS和Nginx环境

1 . 解压下载好的包

tar -zxvf fastdfs-6.06.tar.gz
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
tar -zxvf libfastcommon-1.0.43.tar.gz
tar -zxvf nginx-1.23.0.tar.gz

2 . 编译安装
2.1 编译安装 libfastcommon
1、进入解压后的目录

cd /root/libfastcommon-1.0.43

2、编译安装

./make.sh && ./make.sh install

3、检查

ls /usr/lib64|grep libfastcommon
ls /usr/lib|grep libfastcommon

出现libfastcommon.so即成功

2.2 编译安装 fastdfs
1、进入解压后的目录

cd ~/fastdfs-6.06

2、编译安装

./make.sh && ./make.sh install

3、检查

ls /usr/bin|grep fdfs

4、启用配置文件

cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
cp tracker.conf.sample tracker.conf
mkdir -p /fastdfs/tracker

5、修改tracker.conf文件

vi /etc/fdfs/tracker.conf
修改base_path配置信息如下:
base_path = /fastdfs/tracker

6、启动tracker服务

/etc/init.d/fdfs_trackerd start

7、检查服务是否启动

ps -ef|grep fdfs

8、修改storage.conf文件

vi /etc/fdfs/storage.conf
修改内容如下:
base_path = /fastdfs/storage
store_path0 = /fastdfs/storage
tracker_server = 192.168.1.123:22122
http.server_port = 80

9、创建目录

mkdir -p /fastdfs/storage

10、启动storage 服务

/etc/init.d/fdfs_storaged start

查看启动的相关进程

11、设置开机启动(可选)

vi /etc/rc.d/rc.local
加入配置
/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start

12、编辑client.conf文件

vi /etc/fdfs/client.conf
修改
base_path=/fastdfs/tracker
tracker_server=192.168.1.123:22122

2.3 安装 nginxfastdfs-nginx-module
1、安装pcre

yum -y install pcre pcre-devel

安装gd,HttpImageFilterModule模块需要依赖gd-devel的支持

yum -y install gd-devel

2、进入解压目录

cd /root/nginx-1.23.0

3、进入fastdfs-nginx-module-1.22src目录下

cd /root/fastdfs-nginx-module-1.22/src/

4、编辑配置文件
vi config
5、将config文件中的/usr/local替换成/usr
:%s+/usr/local+/usr
6、进入 nginx 解压目录
cd /root/nginx-1.23.0

7、添加编译http_stub_status_module等模块

./configure --prefix=/usr/local/nginx \
--with-http_gzip_static_module \
--add-module=/root/fastdfs-nginx-module-1.22/src/ \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module

8、编译安装

make && make install

9、复制并修改fastdfs-ngin-module中的配置文件

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

修改配置

connect_timeout=10
tracker_server=192.168.1.123:22122
url_have_group_name = true
store_path0=/fastdfs/storage

10、进入fastdfs源码conf目录
cd /root/fastdfs-6.06/conf/
11、将http.confmime.types两个文件拷贝到/etc/fdfs/目录下

cp http.conf mime.types /etc/fdfs/

12、创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据 的目录

ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00

13、编辑 nginx 配置

vi /usr/local/nginx/conf/nginx.conf

修改配置

server {
    listen       80;    ## 该端口为storage.conf中的http.server_port相同
    server_name  localhost;

    location ~group1/M00/(.+)_(\d+)x(\d+)\.(jpg|gif|png|jpeg){ # .表示除\n外的任意字符 +表示1-正无穷 \d表示数字
            alias /fastdfs/storage/data; #设置别名
             ngx_fastdfs_module;
             set $w $2; #正则表达式匹配的第二个参数,此处为第一个(\d+)
             set $h $3;
             if ($w != "0") {
                  rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png|jpeg)$ group1/M00$1.$4 break;
              }set $h $3;
             if ($h != "0") {
                  rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png|jpeg)$ group1/M00$1.$4 break;
              }
            image_filter resize $w $h;
            image_filter_buffer 50M; ##原图最大50M,要裁剪的图片超过2M返回415错误
   }

    location ~/group[0-9]/ {
        ngx_fastdfs_module;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
}

14、启动 nginx

/usr/local/nginx/sbin/nginx

三、上传并测试裁剪图片文件

1、下载测试图片到本地

wget -c "https://cn.bing.com/th?id=OHR.KissingPuffins_ZH-CN2578608262_1920x1080.jpg&rf=LaDigue_1920x1080.jpg" -O birds.jpg

2、上传测试图片

/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/birds.jpg

如果成功则会返回文件名地址:

group1/M00/00/00/wKgfjmLGdKuADrt6AAVDLwY9Icw011.jpg

或者

 fdfs_test /etc/fdfs/client.conf upload ~/bird.jpg
 输出结果
 group_name=group1, remote_filename=M00/00/00/wKgfjmLGiQOADD37AAVDLwY9Icw063_big.jpg
source ip address: 192.168.1.123
file timestamp=2022-07-07 03:19:31
file size=344879
file crc32=104669644
example file url: http://192.168.1.123/group1/M00/00/00/wKgfjmLGiQOADD37AAVDLwY9Icw063_big.jpg

更多命令可以使用如下方式查看:

ll /usr/bin/fdfs*
输出:
-rwxr-xr-x. 1 root root  ç /usr/bin/fdfs_appender_test
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_appender_test1
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_append_file
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_crc32
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_delete_file
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_download_file
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_file_info
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_monitor
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_regenerate_filename
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_storaged
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_test
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_test1
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_trackerd
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_upload_appender
-rwxr-xr-x. 1 root root  ... /usr/bin/fdfs_upload_file

3、使用浏览器测试上传的图片是否可以访问
原图测试:

http://192.168.1.123/group1/M00/00/00/wKgfjmLGdKuADrt6AAVDLwY9Icw011.jpg

裁剪测试:

http://192.168.1.123/group1/M00/00/00/wKgfjmLGdKuADrt6AAVDLwY9Icw011_600x300.jpg

the end

热门文章