如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放
参考网站: nginx-lua-fastdfs-GraphicsMagick https://github.com/hpxl/nginx-lua-fastdfs-GraphicsMagick 如何安装nginx_lua_module模块 - 轩脉刃 - 博客园 http://www.cnblogs.com/yjf512/archive/2012/03/27/2419577.html 如何升级Nginx到最新稳定版 - Terry Sun - 博客园 http://www.cnblogs.com/terrysun/archive/2012/11/22/2782472.html GraphicsMagick / ImageMagick缺少lib报错no decode delegate for this image format - Arliang - 博客园 http://www.cnblogs.com/arliang/archive/2013/06/24/fix_error_no_decode_delegate_for_this_image_format.html GraphicsMagick在Linux环境的安装 - - ITeye技术网站 http://zhenghuali168.iteye.com/blog/1441926
Tengine + Lua + GraphicsMagick 实现图片自动裁剪/缩放 - 腾勇的个人页面 - 开源中国社区 http://my.oschina.net/eduosi/blog/169606?fromerr=WtR4O29O
第一章 安装Nginx+Lua开发环境 - 开涛的博客 - ITeye技术网站 http://jinnianshilongnian.iteye.com/blog/2186270
安装Nginx+Lua+OpenResty开发环境配置全过程实例 - 流风,飘然的风 - 博客园 http://www.cnblogs.com/zdz8207/p/Nginx-Lua-OpenResty.html
=========================================
fastdfs开源的分布式文件系统,此脚本利用nginx lua模块,动态生成图片缩略图,fastdfs只存一份原图。lua通过socket获取fastdfs的原图,并存放到本地,根据不同规则url,例如:_60x60.jpg、_80x80.jpg,类似淘宝图片url规则。利用gm命令生成本地缩略图,第二次访问直接返回本地图片。定时任务凌晨清除7天内未访问的图片,节省空间。
图片访问举例
- http://192.168.1.113/group1/M00/00/00/wKgBcVN0wDiAILQXAAdtg6qArdU189.jpg
- http://192.168.1.113/group1/M00/00/00/wKgBcVN0wDiAILQXAAdtg6qArdU189.jpg_80x80.jpg
- http://gi1.md.alicdn.com/imgextra/i1/401612253/T2ASPfXE4XXXXXXXXX_!!401612253.jpg_60x60.jpg
- http://gi1.md.alicdn.com/imgextra/i1/401612253/T2ASPfXE4XXXXXXXXX_!!401612253.jpg_80x80.jpg
参考网址
- https://github.com/openresty/lua-nginx-module
- https://github.com/azurewang/Nginx_Lua-FastDFS
- https://github.com/azurewang/lua-resty-fastdfs
- http://rhomobi.com/topics/23
- http://bbs.chinaunix.net/thread-4133106-1-1.html
=========================================
如何安装nginx_lua_module模块
1.检查已经装的nginx路径和版本: root@ip82:/home/user# find /|grep nginx.conf /etc/nginx/conf/nginx.conf root@ip82:/etc/nginx/conf# /etc/nginx/sbin/nginx -v nginx version: nginx/1.1.19 注:如果是nginx1.6以下是版本先看下面升级nginx的方法先升级,否则安装不了
2.下载luajit 2.0.3并安装 root@ip82:/home/user# wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz root@ip82:/home/user# tar -zxvf LuaJIT-2.0.3.tar.gz root@ip82:/home/user# cd LuaJIT-2.0.3 root@ip82:/home/user/LuaJIT-2.0.3# make && make install
3.下载ngx_devel_kit http://github.com/simpl/ngx_devel_kit/tags 解压 root@ip82:/home/user# wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz root@ip82:/home/user# tar -zxvf v0.2.19.tar.gz
4.下载nginx_lua_module http://github.com/chaoslawful/lua-nginx-module/tags 解压 root@ip82:/home/user# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.0.tar.gz root@ip82:/home/user# tar -zxvf v0.10.0.tar.gz root@ip82:/home/user# ls fdfs-wnw fdfs_wnw.tgz install_log LuaJIT-2.0.3 LuaJIT-2.0.3.tar.gz lua-nginx-module-0.10.0 ngx_devel_kit-0.2.19 v0.10.0.tar.gz v0.2.19.tar.gz
5.导入环境变量,编译用,LuaJIT默认安装在(/usr/local/lib) root@ip82:/home/user# ls /usr/local/lib libfastcommon.so libfastcommon.so.1 libfdfsclient.so libfdfsclient.so.1 libluajit-5.1.a libluajit-5.1.so libluajit-5.1.so.2 libluajit-5.1.so.2.0.3 lua pkgconfig python2.7 root@ip82:/home/user# ls /usr/local/include/ fastcommon fastdfs luajit-2.0 root@ip82:/home/user# export LUAJIT_LIB=/usr/local/lib root@ip82:/home/user# export LUAJIT_INC=/usr/local/include/luajit-2.0 上面方法的PATH 在终端关闭 后就会消失。 vim /etc/profile 在文档最后,添加: export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0 保存,退出,然后运行: #source /etc/profile 不报错则成功。
6.进入nginx源码文件夹,增加编译参数 root@ip82:/home/user# /etc/nginx/sbin/nginx -v nginx version: nginx/1.1.19 root@ip82:/home/user# find /|grep nginx-1.1.19 root@ip82:/home/user# cd /home/user/fdfs-wnw/nginx-1.1.19/ root@ip82:/home/user/fdfs-wnw/nginx-1.1.19# ./configure --prefix=/etc/nginx --add-module=/home/user/ngx_devel_kit-0.2.19 --add-module=/home/user/lua-nginx-module-0.10.0
参数说明: ./configure --prefix=/etc/nginx \ #nginx的安装路径 --add-module=/home/user/ngx_devel_kit-0.2.19 \ #ngx_devel_kit 的源码路径 --add-module=/home/user/lua-nginx-module-0.10.0 #nginx_lua_module 的源码路径
7.执行安装 root@ip82:/home/user/fdfs-wnw/nginx-1.1.19# make -j2 root@ip82:/home/user/fdfs-wnw/nginx-1.1.19# make install 发现nginx版本太低安装不了,需要先升级 /home/user/lua-nginx-module-0.10.0/src/ngx_http_lua_common.h:26:2: error: #error at least nginx 1.6.0 is required but found an older version
版本太低,先进行下面步骤升级nginx
8.测试是否成功 root@ip82:/home/user/nginx-1.9.9# find /|grep nginx.conf /etc/nginx/conf/nginx.conf 加入 location /lua { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; } root@ip82:/usr/local/lib# /etc/nginx/sbin/nginx -t
root@ip82:/etc/nginx/conf# service nginx restart
访问http://10.0.2.82/lua 会出现“hello,lua” 安装成功! ========================================= 安装GraphicsMagick依赖: 1.下载:GraphicsMagick:http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/ 注意:这个下载很慢,需要对比下载的文件大小8.58M,有可能下载不完整导致解压失败 2.上传解压:tar -zxvf GraphicsMagick-1.3.23.tar.gz 到GraphicsMagick目录下编译: cd /usr/local/share/GraphicsMagick-1.3.23 root@ip82:/usr/local/share/GraphicsMagick-1.3.23# ./configure --prefix=/usr/local/GraphicsMagick --enable-shared root@ip82:/usr/local/share/GraphicsMagick-1.3.23# make root@ip82:/usr/local/share/GraphicsMagick-1.3.23# make install 执行 root@ip82:/usr/local/share/GraphicsMagick-1.3.23# /usr/local/GraphicsMagick/bin/gm version 如果得到 Feature Support: Native Thread Safe yes Large Files (> 32 bit) yes Large Memory (> 32 bit) yes BZIP no DPS no FlashPix no FreeType no Ghostscript (Library) no JBIG no JPEG-2000 no JPEG no Little CMS no Loadable Modules no OpenMP yes (200805) PNG no TIFF no TRIO no UMEM no WebP no WMF no X11 no XML no ZLIB yes
------------------------------- JPEG yes PNG yes 则,说明安装成功!否则不成功
JPEG v1 --with-jpeg=yes no (需安装 jpeg delegate library) PNG --with-png=yes no(需安装 png delegate library)
在执行完上述命令后会有一段输出,可以查看GraphicsMagick支持的图片格式, 在Configured value下为yes的表示为支持,PNG、JPEG v1和ZLIB必须为yes,若不为yes将按照前提中所写的进行操作,然后再重复执行上述命令,一直到全部支持为止,否则将无法正常进行截图操作
脚本编写 // 调用 GraphicsMagick 进行图片转换,相关信息查看 GraphicsMagick 帮助文档 local command = "/usr/local/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.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); // 输出裁剪后的图片 // ngx.var.request_filepath、ngx.var.width、ngx.var.height 即 Nginx location 中设置的变量
========================================= GraphicsMagick / ImageMagick缺少lib报错no decode delegate for this image format
#下载包 wget http://www.imagemagick.org/download/delegates/zlib-1.2.8.tar.gz wget http://www.imagemagick.org/download/delegates/libpng-1.6.21.tar.gz 注:如果出现404说明已经升级了,到wget http://www.imagemagick.org/download/delegates/目录下选择最新版本下载,下面也同理 wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/delegates/jpegsrc.v9a.tar.gz tar zvxf zlib-1.2.8.tar.gz tar zvxf libpng-1.6.21.tar.gz tar zvxf jpegsrc.v9a.tar.gz
#安装 cd zlib-1.2.8 ./configure make make install
cd jpeg-9a ./configure make make install
cd libpng-1.6.21 ./configure make make install ---------------------------------- root@ip82:/usr/local/share/GraphicsMagick-1.3.23# /usr/local/GraphicsMagick/bin/gm version /usr/local/GraphicsMagick/bin/gm: error while loading shared libraries: libjpeg.so.9: cannot open shared object file: No such file or directory
find /|grep libjpeg.so
root@ip82:/usr/local/lib# cp libjpeg.so.9 /usr/lib/x86_64-linux-gnu/ root@ip82:/usr/local/lib# cp libjpeg.so.9.1.0 /usr/lib/x86_64-linux-gnu/ root@ip82:/usr/local/lib# cp libjpeg.so /usr/lib/x86_64-linux-gnu/ 运行sudo ldconfig进行自动清理。 root@ip82:/usr/local/lib# sudo ldconfig root@ip82:/usr/local/lib# /usr/local/GraphicsMagick/bin/gm version Feature Support: Native Thread Safe yes Large Files (> 32 bit) yes Large Memory (> 32 bit) yes BZIP no DPS no FlashPix no FreeType no Ghostscript (Library) no JBIG no JPEG-2000 no JPEG yes Little CMS no Loadable Modules no OpenMP yes (200805) PNG yes TIFF no TRIO no UMEM no WebP no WMF no X11 no XML no ZLIB yes
Host type: x86_64-unknown-linux-gnu
=========================================================== http://10.0.2.82/group1/M00/00/00/CgACUlalwCWAbKFfAAQA3nlGY5k195.jpg http://10.0.2.82/group1/M00/00/00/CgACUlalwCWAbKFfAAQA3nlGY5k195.jpg_40x40.jpg
http://10.0.2.82/group1/M00/00/00/CgACUlbBe2aAAWgRAAJF3TQI0W8948.jpg http://10.0.2.82/group1/M00/00/00/CgACUlbBe2aAAWgRAAJF3TQI0W8948.jpg_80x80.jpg
给相关目录授权: root@ip82:/etc/nginx/conf/lua# chown -R user:users /usr/local/GraphicsMagick root@ip82:/etc/nginx/conf/lua# chmod 777 /usr/local/GraphicsMagick
测试gm命令是否正常: gm convert -resize 100x80^ -gravity Center -crop 100x80+0+0 Hydrangeas.jpg thumb.jpg 其中Hydrangeas.jpg为执行命令目录下的目标源文件 thumb.jpg为生成的图片,生成后下载到本地查看算法正常。 /usr/local/GraphicsMagick/bin/gm convert -resize 80x80^ -gravity Center -crop 80x80+0+0 Hydrangeas.jpg thumb2.jpg
graphicsmagick获得图片的信息: root@ip82:/etc/nginx/conf/lua# gm identify Hydrangeas.jpg Hydrangeas.jpg JPEG 1024x768+0+0 DirectClass 8-bit 581.3K 0.000u 0:01
------------------------------------- 出现404错误,查看nginx错误日志: root@ip82:/etc/nginx/logs# ls access.log error.log nginx.pid root@ip82:/etc/nginx/logs# tail -f error.log /usr/local/GraphicsMagick/bin/gm convert: Unable to open file (/data/fdfs/data/00/00/CgACUlbBe2aAAWgRAAJF3TQI0W8948.jpg_80x80.jpg) [Permission denied]. 发现没有对应权限,进行授权: root@ip82:/data/fdfs# chmod 777 -R data 再次访问就可以了 http://10.0.2.82/group1/M00/00/00/CgACUlalwCWAbKFfAAQA3nlGY5k195.jpg_40x40.jpg http://10.0.2.82/group1/M00/00/00/CgACUlbBe2aAAWgRAAJF3TQI0W8948.jpg_60x60.jpg
========================================= 升级nginx: 1.下载新版: root@ip82:/home/user# wget http://nginx.org/download/nginx-1.9.9.tar.gz root@ip82:/home/user# tar zxvf nginx-1.9.9.tar.gz
2.查看nginx原来的配置 /etc/nginx/sbin/nginx -V nginx version: nginx/1.1.19 built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) configure arguments: --prefix=/etc/nginx --with-http_gzip_static_module --add-module=../fastdfs-nginx-module/src/
3.进入到新版本目录,执行configure命令,后面跟上原来nginx的配置,有新的也可以一并加上 root@ip82:/home/user# cd /home/user/nginx-1.9.9 root@ip82:/home/user/nginx-1.9.9# ./configure --prefix=/etc/nginx --with-http_gzip_static_module --add-module=/home/user/fdfs-wnw/fastdfs-nginx-module/src/ --add-module=/home/user/ngx_devel_kit-0.2.19 --add-module=/home/user/lua-nginx-module-0.10.0
4.执行make令命编译nginx, 编译好以后objs目录下多出一个nginx文件,这个就是已编辑好的nginx程序 root@ip82:/home/user/nginx-1.9.9# make
5.更改旧的nginx程序的名字,并复制新的程序过去,旧nginx程序放在/etc/nginx/sbin/目录中 root@ip82:/home/user/nginx-1.9.9# /etc/nginx/sbin/nginx -s stop
root@ip82:/home/user/nginx-1.9.9# mv /etc/nginx/sbin/nginx /etc/nginx/sbin/nginx-1.1.19 root@ip82:/home/user/nginx-1.9.9# cp objs/nginx /etc/nginx/sbin/nginx root@ip82:/home/user/nginx-1.9.9# /etc/nginx/sbin/nginx -t /etc/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 说明有错误, 这是因为系统默认会到/usr/lib或/usr/lib64找libluajit-5.1.so.2,解决办法: root@ip82:/home/user/nginx-1.9.9# find /|grep libluajit-5.1.so root@ip82:/home/user# find /|grep libluajit-5.1.so /usr/local/lib/libluajit-5.1.so.2.0.3 /usr/local/lib/libluajit-5.1.so /usr/local/lib/libluajit-5.1.so.2 root@ip82:/home/user# cd /usr/local/lib/ root@ip82:/usr/local/lib# ls libluajit-5.1.a libluajit-5.1.so libluajit-5.1.so.2 libluajit-5.1.so.2.0.3 root@ip82:/usr/local/lib# cp libluajit-5.1.a /usr/lib/libluajit-5.1.a root@ip82:/usr/local/lib# cp libluajit-5.1.so /usr/lib/libluajit-5.1.so root@ip82:/usr/local/lib# cp libluajit-5.1.so.2 /usr/lib/libluajit-5.1.so.2 root@ip82:/usr/local/lib# cp libluajit-5.1.so.2.0.3 /usr/lib/libluajit-5.1.so.2.0.3 root@ip82:/usr/local/lib# /etc/nginx/sbin/nginx -t ngx_http_fastdfs_set pid=27738 nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok nginx: configuration file /etc/nginx/conf/nginx.conf test is successful
10.在新nginx源码目录下执行下面的命令来升级nginx root@ip82:/usr/local/lib# cd /home/user/nginx-1.9.9 root@ip82:/home/user/nginx-1.9.9# make upgrade
11.执行nginx -v命令,输出新版本则升级完毕: root@ip82:/home/user/nginx-1.9.9# /etc/nginx/sbin/nginx -v nginx version: nginx/1.9.9
12.重启nginx: root@ip82:/home/user/nginx-1.9.9# service nginx restart * Stopping Nginx server ...done. * Starting Nginx server ngx_http_fastdfs_set pid=27785 ...done. root@ip82:/home/user/nginx-1.9.9#
注意:升级后无法使用原来的-s reload 方法重启nginx了 root@ip82:/etc/nginx/conf# /etc/nginx/sbin/nginx –s reload nginx: invalid option: "–s"
|
请发表评论