FastDFS实现动态缩略
一、安装前准备
配置环境为CentOS Linux release 7.6.1810,内核为Linux version 3.10.0-957.1.3.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) )
group1的IP为192.168.10.138
group2的IP为192.168.10.139
安装包libfastcommon-1.0.35.tar.gz、
FastDFS_v5.08.tar.gz、
fastdfs-nginx-module_v1.16.tar.gz、
nginx-1.11.5.tar.gz
先讲4个安装包存放到/usr/local/src/目录下。
二、yum依赖关系
yum -y install pcre* pcre-* gcc-c++ gcc* zlib zlib-devel openssl openssl-devel gc-devel gd gd-devel git
三、安装lib套件
cd /usr/local/src
tar -zxvf libfastcommon-1.0.35.tar.gz
cd libfastcommon-1.0.35
./make.sh && ./make.sh install
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
四、安装fasatdfs
cd /usr/local/src
tar -zxvf FastDFS_v5.08.tar.gz
cd FastDFS
./make.sh && ./make.sh install
cp -r conf/* /etc/fdfs/
更改配置文件仅1(group1 192.168.10.138)
vim /etc/fdfs/tracker.conf
bind_addr=192.168.10.138 #ip地址是用作tracker的ip
port=22122 #端口默认就好
base_path=/data/server/fastdfs_tracker
store_lookup=0
store_group=group1
store_server=0
use_storage_id=true
id_type_in_filename = id
vim /etc/fdfs/storage.conf
group_name=group1
bind_addr=192.168.10.138 #ip地址是自己的ip
base_path=/data/server/fastdfs_storage
store_path_count=1
store_path0=/data/server/fastdfs_storage
tracker_server=192.168.10.138:22122 #ip地址是tracker的ip
http.server_port=80
vim /etc/fdfs/storage_ids.conf (把#去掉)
100001 group1 192.168.10.138
100002 group2 192.168.10.139
vim /etc/fdfs/client.conf
base_path=/data/server/fastdfs_client
tracker_server=192.168.10.138:22122 #ip是自己的
use_storage_id = true
http.tracker_server_port=80
创建相应目录
mkdir -p /data/server/fastdfs_tracker
mkdir -p /data/server/fastdfs_storage
mkdir -p /data/server/fastdfs_client
配置group2192.168.10.139仅2的配置文件
vim /etc/fdfs/storage.conf
group_name=group2
bind_addr=192.168.10.139 #ip是自己的
base_path=/data/server/fastdfs_storage
store_path0=/data/server/fastdfs_storage
tracker_server=192.168.10.138:22122 ip是group1的
创建相应目录
mkdir -p /data/server/fastdfs_storage
先启动group1的tracker和storage
fdfs_trackerd /etc/fdfs/tracker.conf
fdfs_storaged /etc/fdfs/storage.conf
再启动group2的storage
fdfs_storaged /etc/fdfs/storage.conf
在group1中上传图片进行测试
fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/anti-steal.jpg
五、安装nginx group1+2都做
需要先安装fastdfs-nginx-module
cd /usr/local/src
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module
vim src/config
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
编译安装nginx
cd /usr/local/src/
tar -zxvf nginx-1.11.5.tar.gz
cd nginx-1.11.5
./configure --prefix=/data/server/nginx \
--add-module=/usr/local/src/fastdfs-nginx-module/src \
--with-http_gunzip_module \
--with-pcre \
--with-http_image_filter_module \
--with-http_stub_status_module
make && make install
成了
以上过程group1和group2主机都需要执行
六、编辑nginx配置文件
group1中
vim /data/server/nginx/conf/nginx.conf
在server函数中添加
location ~* /group1/M00/(\w+)/(\w+)/(.+)_(\d+)x(\d+)\.(png|jpg|gif) {
root "/data/server/nginx/html/";
ngx_fastdfs_module;
set $iw "$4";
set $ih "$5";
image_filter resize $iw $ih;
image_filter_buffer 2M;
try_files /group1/M00/$1/$2/$3.$6 /404.html;
}
location /group1/M00 {
root "/date/server/nginx/html/";
ngx_fastdfs_module;
}
创建相应目录和软链接
mkdir /data/server/nginx/html/group1
ln -s /data/server/fastdfs_storage/data /data/server/nginx/html/group1/M00
修改mod_fastdfs.conf
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
use_storage_id = true
tracker_server=192.168.10.138:22122 ip是group1的
group_name=group1
url_have_group_name = true
store_path0=/data/server/fastdfs_storage
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/server/storage
group2中
vim /data/server/nginx/conf/nginx.conf
location ~* /group2/M00/(\w+)/(\w+)/(.+)_(\d+)x(\d+)\.(png|jpg|gif) {
root "/data/server/nginx/html/";
ngx_fastdfs_module;
set $iw "$4";
set $ih "$5";
image_filter resize $iw $ih;
image_filter_buffer 2M;
try_files /group2/M00/$1/$2/$3.$6 /404.html;
}
location /group2/M00 {
root "/date/server/nginx/html/";
ngx_fastdfs_module;
}
创建相应目录和软链接
mkdir /data/server/nginx/html/group2
ln -s /data/server/fastdfs_storage/data /data/server/nginx/html/group2/M00
修改mod_fastdfs.conf
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
use_storage_id = true
tracker_server=192.168.10.138:22122 #ip是group1的
group_name=group2
url_have_group_name = true
store_path0=/data/server/fastdfs_storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/data/server/storage
七、启动nginx服务group1+2
先测试nginx的配置文档
/data/server/nginx/sbin/nginx -t
配置文件没有问题时启动nginx服务
/data/server/nginx/sbin/nginx
(tips:如果启动失败有可能是80端口被占,kill -9 号就可以启动了)
测试
在group1输入命令
fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/anti-steal.jpg
(tips:输完上面命令后会出现一串文字)
在网页输入ip/文字
上传动图:桌面图片1.gif→上传至虚拟机桌面→用mv命令到/etc/fdfs下。
用命令
fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/1.gif
重启电脑时需改错。
同理用同样的方法ip加文字打开网页输入
done
放大缩小在.jpg前加_100x200 数字可以随便改。