软件列表

  1. 解压文件

tar -zxvf lua-5.3.1.tar.gz

  1. 进入目录,执行:

cd lua-5.3.1 make linux && make install

  1. 验证安装成功:

lua -v

  1. # 安装luaJIT
  2. 解压文件

tar -zxvf LuaJIT-2.0.4.tar.gz

  1. 进入目录,执行:

cd LuaJIT-2.0.4 make && make install

  1. # 安装tengine
  2. 安装依赖:

yum -y install pcre-devel openssl openssl-devel

  1. 解压文件:

unzip tengine-master.zip

  1. 进入目录,执行:

cd tengine-master

./configure —prefix=/usr/local/Tengine —dso-path=/usr/local/Tengine/modules —with-http_realip_module —with-http_gzip_static_module \ —with-http_stub_status_module —with-http_concat_module \ —with-http_lua_module —http-proxy-temp-path=/var/tmp/Tengine/proxy_temp \ —http-fastcgi-temp-path=/var/tmp/Tengine/fastcgi_temp \ —http-uwsgi-temp-path=/var/tmp/Tengine/uwsgi_temp \ —http-scgi-temp-path=/var/tmp/Tengine/cgi_temp \ —http-client-body-temp-path=/var/tmp/Tengine/client_body_temp \ —http-log-path=/var/log/Tengine/access.log \ —error-log-path=/var/log/Tengine/error.log

  1. 再执行:

make && make install

  1. 验证安装成功:

$ /usr/local/Tengine/sbin/nginx /usr/local/Tengine/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

  1. 提示找不到文件,配置一个软连接:

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

  1. 验证安装成功:

$ /usr/local/Tengine/sbin/nginx nginx: [emerg] mkdir() “/var/tmp/Tengine/client_body_temp” failed (2: No such file or directory)

  1. 手动创建该目录:

mkdir -p /var/tmp/Tengine/client_body_temp

  1. 然后,访问 [http://192.168.56.100/](http://192.168.56.100/) ,可以看到:

Welcome to tengine!

  1. # 安装GraphicsMagick
  2. 安装依赖:

yum install libjpeg libjpeg-devel libpng libpng-devel giflib giflib-devel freetype freetype-devel -y

  1. 解压文件

tar -zxvf GraphicsMagick-1.3.18.tar.gz

  1. 进入目录,执行:

cd GraphicsMagick-1.3.18 ./configure —prefix=/usr/local/GraphicsMagick —enable-shared

  1. 再编译安装:

make && make install

  1. 验证安装成功:

/usr/local/GraphicsMagick/bin/gm version

  1. # 配置
  2. 创建 ImageResizer.lua 文件:

mkdir /usr/local/Tengine/lua/ touch /usr/local/Tengine/lua/ImageResizer.lua

  1. ImageResizer.lua

local command = “/usr/local/GraphicsMagick/bin/gm convert “ .. ngx.var.requestfilepath \ .. “ -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);

  1. 修改 /usr/local/Tengine/conf/nginx.conf

user root; # 裁剪图片需要root权限 worker_processes 1;

events { worker_connections 1024; }

http { include mime.types; default_type application/octet-stream;

  1. sendfile on;
  2. #tcp_nopush on;
  3. keepalive_timeout 65;
  4. server {
  5. listen 80;
  6. server_name img.javachen.space;
  7. root /data/image/upload;
  8. location / {
  9. root /data/image/upload; # 站点根目录
  10. expires 1h; # 缓存时间
  11. add_header Cache-Control max-age=3600; # 缓存时间
  12. access_log /var/log/Tengine/host_access.log;
  13. }
  14. #如果 url 格式如:xxxx.gif_数字x数字.gif
  15. location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {
  16. root /data/image/upload;
  17. if (!-f $request_filename) { #如果文件不存在时才需要裁剪
  18. #此HTTP Header无实际意义,用于测试
  19. add_header X-Powered-By 'Lua GraphicsMagick';
  20. #此 HTTP Header无实际意义,用于测试
  21. add_header file-path $request_filename;
  22. #在编写外部 Lua脚本时,设置为off Nginx不会缓存
  23. lua_code_cache on; Lua,方便调试
  24. #设置原始图片路径,如:/document_root/1.gif
  25. set $request_filepath /data/image/upload$1;
  26. set $width $3; # 设置裁剪/缩放的宽度
  27. set $height $4; # 设置裁剪/缩放的高度
  28. set $ext $5; # 图片文件格式后缀
  29. #加载外部 Lua 文件
  30. content_by_lua_file /usr/local/Tengine/lua/ImageResizer.lua;
  31. }
  32. }
  33. error_page 500 502 503 504 /50x.html;
  34. location = /50x.html {
  35. root html;
  36. }
  37. }

}

  1. 重新加载配置文件:

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

  1. 创建图片上传目录:

mkdir -p /data/image/upload

  1. 配置hosts

192.168.56.100 img.javachen.space

  1. /data/image/upload 下存放一个图片 1.png,然后访问:[http://192.168.56.100/1.png](http://192.168.56.100/1.png) ,再访问缩放图片:[http://192.168.56.100/1.png_245x245.png](http://192.168.56.100/1.png_245x245.png)<br />查看 /data/image/upload 目录是否有生成新的文件:

$ ll /data/image/upload/ 1.png 1.png_145x145.png 1.png_245x245.png 1.png_45x45.png

```