fastdfs+nginx+GraphicsMagick实现生成缩略图

功能描述:可以使用GraphicsMagick工具生成缩略图,通过http访问某个图片时,显示其对应的动态压缩图

1.安装fastdfs

1.1安装环境

yum install git gcc gcc-c++ make automake vim wget libevent -y 

1.2安装依赖

mkdir /root/fastdfs

cd /root/fastdfs

git clone https://github.com/happyfish100/libfastcommon.git --depth 1

cd libfastcommon/

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

1.3安装fastdfs

cd /root/fastdfs

wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz

tar -zxvf V5.11.tar.gz

cd fastdfs-5.11

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

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

cp /root/fastdfs/fastdfs-5.11/conf/http.conf /etc/fdfs

cp  /root/fastdfs/fastdfs-5.11/conf/mime.types /etc/fdfs

#配置环境1

vim /etc/fdfs/tracker.conf

base_path=/home/fastdfs

#配置环境2

vim /etc/fdfs/storage.conf

port=23000

base_path=/home/fastdfs # 数据和日志文件存储根目录

store_path0=/home/fastdfs # 第一个存储目录

tracker_server=39.100.67.111:22122

1.4启动

mkdir /home/fastdfs -p

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

#查看所有运行的端口

netstat -ntlp

1.5测试上传

vim /etc/fdfs/client.conf

#需要修改的内容如下

base_path=/home/fastdfs

#tracker服务器IP和端口

tracker_server=192.168.211.136:22122

#测试,返回ID表示成功 如:

/root/fastdfs/1.png /group1/M00/00/00/rBpavF79jLKAJa71AAGBvwY81YE397.png_100x100.png

#返回

group1/M00/00/00/xxx.png /usr/bin/fdfs_upload_file /etc/fdfs/client.conf

2.安装fastdfs-nginx-module

2.1下载安装

cd /root/fastdfs

wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz

#解压

tar -xvf V1.20.tar.gz

cd fastdfs-nginx-module-1.20/src

vim config

#修改第5 行 和 15 行 修改成

ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"

CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

2.2修改配置文件

cp mod_fastdfs.conf /etc/fdfs/

vim /etc/fdfs/mod_fastdfs.conf

#需要修改的内容如下

tracker_server=192.168.211.136:22122

url_have_group_name=true

store_path0=/home/fastdfs

mkdir -p /var/temp/nginx/client

3.安装nginx并配置

3.1下载nginx的lua和文件压缩依赖

yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gd-devel

#libpng-devel libjpeg-devel  png和jpeg支持

yum install -y libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel readline-devel ncurses-devel

cd /root/fastdfs

git clone https://github.com/alibaba/nginx-http-concat.git

git clone https://github.com/simpl/ngx_devel_kit.git

#如果文件下载不下来可以从自己电脑上git clone下载然后上传到这个目录下

git clone https://github.com/openresty/echo-nginx-module.git

#一定要下载这个版本的lua-nginx-module包,直接从git下载的。。。安装不到nginx上

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz

wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz

3.2安装lua(一个脚本语言,用来调用GraphicsMagick 缩放图片)并且配置

#lua的一个简易版本

tar -zxf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make

make install

export LUAJIT_LIB=/usr/local/lib

export LUAJIT_INC=/usr/local/include/luajit-2.0

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

cd ..

#lua本包

wget http://www.lua.org/ftp/lua-5.3.1.tar.gz

cd lua-5.3.1

make linux && make install

3.3下载安装GraphicsMagick (用来压缩图片的)

wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.18.tar.gz

tar -zxvf GraphicsMagick-1.3.18.tar.gz

cd GraphicsMagick-1.3.18

#这个prefix是指定安装位置的不一定是写当前目录位置

./configure --prefix=/root/fastdfs/GraphicsMagick --enable-shared

make  && make install

#看一下png和jpeg是不是yes

/root/fastdfs/GraphicsMagick -version

PNG YES

3.4下载安装配置nginx

cd /root/fastdfs

#版本太高可能会有问题

wget http://nginx.org/download/nginx-1.10.2.tar.gz

tar -zxvf nginx-1.10.2.tar.gz

cd nginx-1.10.2

#配置要添加的模块--user=nginx --group=nginx可以不写,但是为了安全还是加上比较好,遇到问题百度就可以解决

./configure  --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --add-module=/root/fastdfs/fastdfs-nginx-module-1.20/src --with-http_image_filter_module --with-pcre --add-module=/root/fastdfs/nginx-http-concat --add-module=/root/fastdfs/lua-nginx-module-0.10.9rc7 --add-module=/root/fastdfs/ngx_devel_kit --add-module=/root/fastdfs/echo-nginx-module --with-ld-opt=-Wl,-rpath,$LUAJIT_LIB

#安装

make

make install

#查看状态

/usr/local/nginx/sbin/nginx -V

vim conf/nginx.conf

配置文件nginx.conf

server {

        listen      80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root  html;

            index  index.html index.htm;

        }

                location /test {

                    default_type text/html;

                    content_by_lua '

                        ngx.say("hello world")

                        ngx.log(ngx.ERR,"err err")

                    ';

                }

        location /group1/M00 {

            alias /home/fastdfs/data;

            if ($uri ~* ^/group1/M00(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)) {

                  add_header X-Powered-By 'Lua GraphicsMagick';

                  add_header file-path $request_filename;

                  lua_code_cache on;

                  set $request_filepath /home/fastdfs/data$1;

                  set $width $3;

                  set $height $4;

                  set $ext $5;

              }

              if (!-f $request_filename) {

                content_by_lua_file conf/lua/ImageResizer.lua;

              }

          }

        }

添加用户

useradd -s /sbin/nologin -M nginx

#测试nginx是否可以启动,如果报错就按照问题百度就好

/usr/local/nginx/sbin/nginx -t

#重启

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

3.5配置lua脚本

cd /usr/loacl/nginx/conf

mkdir lua

cd lua

vim ImageResizer.lua

配置文件

if(ngx.var.height ~= '' and ngx.var.width ~= '') then

    height = tonumber(ngx.var.height);

    width = tonumber(ngx.var.width);

    if(height > 100) then

        height = 100

    end

    if(width > 100) then

        width = 100

    end

    local command = "/root/fastdfs/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. width .. "x" .. height .. " +profile \"*\" " .. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext;

    os.execute(command);

    ngx.exec(ngx.var.request_uri);

else

    ngx.exit(ngx.HTTP_NOT_FOUND);

end

测试

正常图片

http://39.100.67.111/group1/M00/00/00/rBpavF79jLKAJa71AAGBvwY81YE397.png

缩放的图片

http://39.100.67.111/group1/M00/00/00/rBpavF79jLKAJa71AAGBvwY81YE397.png_100x100.png


你可能感兴趣的:(fastdfs+nginx+GraphicsMagick实现生成缩略图)