环境介绍
[email protected]:~# uname -a
Linux ubuntu-1.230 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
[email protected]:~# cat /etc/issue
Ubuntu 12.04.1 LTS \n \l
[email protected]:~#
PS:以下操作我只在如上系统操作,仅供参考
1.安装GraphicsMagick
GraphicsMagick 是配合im4java使用的图片处理软件,在linux/windows下均能使用。在ubuntu下安装很简单:
apt-get install graphicsmagick
yum -y install GraphicsMagick GraphicsMagick-devel (Centos安装方法)
然后运行 man gm,如果显示graphicsmagick的manual,则证明graphicsmagick安装成功。
2.安装lua_nginx
下载 LuaJIT-2.0.3.tar.gz http://luajit.org/download.html
tar -zxvf LuaJIT-2.0.3.tar.gz
cd LuaJIT-2.0.3/
make && make install
所以lib和include是直接放在/usr/local/lib和usr/local/include
3.安装nginx以及需要安装模块
查看现有版本号 nginx -v
下载nginx源码,解压
wget http://www.nginx.org/download/nginx-1.7.3.tar.gz
tar -zxvf nginx-1.7.3.tar.gz
下载ngx_devel_kit HERE 解压
https://github.com/simpl/ngx_devel_kit/tags
tar -zxvf ngx_devel_kit-0.2.19.tar.gz
下载nginx_lua_module HERE 解压
https://github.com/chaoslawful/lua-nginx-module/tags
tar -zxvf lua-nginx-module-0.9.5rc2.tar.gz
下载ngx_cache_purge-1.0.tar.gz
http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
tar -zxvf ngx_cache_purge-1.0.tar.gz
PS:
这里cache模块最好一起和lua进行编译安装避免后期不必要的麻烦
导入环境变量,编译
vi /etc/profile
export LUAJIT_LIB=/usr/local/lib #这个很有可能不一样
export LUAJIT_INC=/usr/local/include/luajit-2.0 #这个很有可能不一样
是文件环境变量生效
source /etc/profile
cd nginx-1.7.3
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_spdy_module --with-cc-opt=‘-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2‘ --with-ld-opt=‘-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-rpath,$LUAJIT_LIB‘ --with-ipv6 --add-module=/root/ngx_devel_kit-0.2.19 --add-module=/root/lua-nginx-module-0.9.5rc2
--add-module=/path/to/ngx_devel_kit #ngx_devel_kit 的源码路径
--add-module=/path/to/lua-nginx-module #nginx_lua_module 的源码路径
--add-module=/path/to/ngx_cache_purge-1.0.tar.gz
make -j2
make install
报错:
安装nginx报错没有找到pcre包
解决办法:
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载pcre模块
编译nginx的时候添加--with-pcre=pcre源码path
****************************************************************************
这种解决pcre方法网上说,但是我按照安装出错提示,直接指定源码文件 *
安装 pcre报错 *
checking windows.h usability... no *
checking windows.h presence... no *
checking for windows.h... no *
configure: error: You need a C++ compiler for C++ support. *
解决办法: *
apt-get install build-essential *
继续安装pcre 继续安装nginx *
--with-pcre=pcar-path *
编译再次出错: *
checking for OpenSSL library ... not found *
****************************************************************************
继续报错:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
解决办法:
apt-get install libssl-dev
apt-get install openssl
安装成功
4.测试nginx-lua模块安装是否成功
在nginx的配置文件server模块添加
location /hello {
default_type ‘text/plain‘;
content_by_lua ‘ngx.say("hello, lua")‘;
}
启动测试报错:
nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
我在网上寻找解决办法,找了三个办法 直接使用方法三,如果还是依旧报错在使用方法一
===========================================================================================
解决方法一
在 Nginx 编译时,需要指定 RPATH,加入下面选项即可:
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
或者
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
vi /etc/profile
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
source /etc/profile
解决方法二:
# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
解决方法三: 这才搞定
http://www.mirrtalk.net/?p=759
#ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib/libluajit-5.1.so.2
#ln -s /usr/local/lib/libluajit-5.1.so.2.0.2 /usr/lib/libluajit-5.1.so.2.0.2
再次nginx –t 检查nginx配置 OK
===========================================================================================
4.安装lua
PS:如果上面安装了luaJit这里依旧需要安装lua,原因还没深究
首先安装lua的依赖包
sudo apt-get install libreadline5
apt-get install libreadline-gplv2-dev
下载lua源码
http://download.csdn.net/detail/xinsir88/7694571
tar -zxvf lua-5.2.3.tar.gz
cd lua-5.2.3/
make linux
make install
5.配置
nginx nginx-lua GraphicsMagick ngx_cache_purge安装完毕后,我们来修改配置文件 如下:
添加三个个location
第一个location用作图片,第二个location用作html,第三个location用作承接第一个location,第四个location用作承接第三location 内容如下:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
}
############################################################################################
location /images/ {
set $image_root /usr/local/tfsimage;
if ($uri ~* "/images/([0-9a-zA-Z_]+).(([^.]+).*)") {
set $filePath "$image_root/$1.$2";
set $reqPath "/$1.$2";
set $filename "$1.$3";
}
set $file "$image_root$reqPath";
set $fileone "$image_root/$filename";
if (-f $file) {
rewrite "/images/(.+)" /innerImages$reqPath last;
}
if (!-f $file){
rewrite_by_lua ‘
//为了避免在nginx服务器上产生太多垃圾图片,在下载原图时先进行判断
function file_exists(name) //定义方法file_exists
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end ;
if file_exists(ngx.var.fileone) then print("111") else //调用方法file_exists
command = string.format("wget -P /usr/local/tfsimage/http://192.168.1.230/v1/tfs/"..ngx.var.filename) //定义下载命令
os.execute(command)//执行下载命令
end;
local originalUri;
local area;
local index = string.find(ngx.var.filePath, "([0-9]+)x([0-9]+)");
if index==nil then originalUri = ngx.var.filePath;
else
originalUri = string.sub(ngx.var.filePath, 0, index-2);
area = string.sub(ngx.var.filePath, index);
index = string.find(area, "([.])");
area = string.sub(area, 0, index-1);
end
local image_sizes = {"155x155", "400x400","104x104", "50x50", "40x40", "56x56", "172x172","800x600"};
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
if table.contains(image_sizes, area) then
local command = "gm convert " .. originalUri .. " -thumbnail " .. area .. " -background white -gravity center -extent " .. area .. " " .. ngx.var.file;
os.execute(command);
ngx.req.set_uri("/innerImages" .. ngx.var.reqPath, true);
else
ngx.req.set_uri("/innerImages" .. ngx.var.reqPath, true);
end;
‘;
}
}
location /html/ {
set $image_root /usr/local/tfshtml;
if ($uri ~* "/html/([0-9a-zA-Z]+).(.*)") {
set $filePath "$image_root/$1.$2";
set $reqPath "/$1.$2";
}
if (-f $filePath) {
rewrite "/html/(.+)" /innerhtml$reqPath last;
}
//由于html不涉及到原文件的问题我们就不做判断性下载
if (!-f $filePath) {
rewrite_by_lua ‘
command = string.format("wget -P /usr/local/tfshtml/ http://192.168.1.230/v1/tfs/"..ngx.var.reqpath);
os.execute(command);
ngx.req.set_uri("/innerhtml" .. ngx.var.reqPath, true);
‘;
}
}
location /innerImages {
alias /usr/local/tfsimage;
expires max;
tcp_nodelay off;
tcp_nopush on;
}
location /innerhtml {
alias /usr/local/tfshtml;
expires max;
tcp_nodelay off;
tcp_nopush on;
}
##############################################################################################
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
nginx+lua_nginx+GraphicsMagick缩略图+tfs获取原图+ngx_cache_purge