图片压缩
安装依赖库
yum install -y git gcc gcc-c++ make automake vim wget libevent pcre-devel
yum install -y libpng-devel libpng libjpeg-devel libjpeg
yum install -y libjpeg libjpeg-devel libpng libpng-devel giflib giflib-devel freetype freetype-devel
安装libfastcommon类库
mkdir /opt/fastdfs
cd /opt/fastdfs
git clone https://github.com/happyfish100/libfastcommon.git --depth 1
cd libfastcommon/
./make.sh && ./make.sh install
安装 fastdfs
cd /opt/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 /opt/fastdfs/fastdfs-5.11/conf/http.conf /etc/fdfs
cp /opt/fastdfs/fastdfs-5.11/conf/mime.types /etc/fdfs
vim /etc/fdfs/tracker.conf
#需要修改的内容如下
port=22122
base_path=/home/fastdfs
vim /etc/fdfs/storage.conf
#需要修改的内容如下
port=23000
base_path=/home/fastdfs
# 数据和日志文件存储根目录
store_path0=/home/fastdfs
# 第一个存储目录
tracker_server=192.168.211.136:22122
# http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
http.server_port=8888
vim /etc/fdfs/client.conf
# 数据和日志文件存储根目录
base_path=/home/fastdfs
tracker_server=172.16.85.148:22122
mkdir /home/fastdfs -p
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
查看所有运行的端口
netstat -ntlp
安装 GraphicsMagick
wget https://jaist.dl.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.36/GraphicsMagick-1.3.36.tar.gz
tar -zxvf GraphicsMagick-1.3.36.tar.gz
cd GraphicsMagick-1.3.36
./configure
make
make install
gm version
安装luaJIT
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar -xvf 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
分别下载和解压 lua_nginx_module-0.8.10.tar.gz
wget -O lua-nginx-module-0.8.10.tar.gz https://github.com/openresty/lua-nginx-module/archive/v0.8.10.tar.gz
下载 ngx_devel_kit-0.2.18.tar.gz
wget -O ngx_devel_kit-0.2.18.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
下载 fastdfs-nginx-module
wget -O fastdfs-nginx-module-1.20.tar.gz https://codeload.github.com/happyfish100/fastdfs-nginx-module/tar.gz/V1.20
tar -zxvf fastdfs-nginx-module-1.20.tar.gz
修改 fastdfs-nginx-module-1.20/src/config 文件
新内容如下:
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
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
安装 nginx
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2/
./configure --add-module=/opt/fastdfs/fastdfs-nginx-module-1.20/src --add-module=/opt/lua-nginx-module-0.8.10 --add-module=/opt/ngx_devel_kit-0.2.18
make
make install
修改nginx配置文件
server{
listen 8888;
server_name localhost;
location / {
root /home/fastdfs/data/00/00;
}
location /test {
default_type text/html;
content_by_lua '
ngx.say("hello world")
ngx.log(ngx.ERR,"err err")
';
}
location ~/group[0-9]/M00 {
alias /home/fastdfs/data;
set $image_root "/home/fastdfs/data/";
if ($uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)"){
set $image_dir "$image_root/$3/$4/";
set $image_name "$5";
set $file "$image_dir$image_name";
}
if (!-f $file) {
content_by_lua_file /usr/local/nginx/conf/lua/fastdfs.lua;
}
ngx_fastdfs_module;
}
}
配置 restyfastdfs
git clone https://github.com/hpxl/nginx-lua-fastdfs-GraphicsMagick.git
cd nginx-lua-fastdfs-GraphicsMagick/lua
cp ./* /usr/local/nginx/conf/lua/
cd /usr/local/nginx/conf/lua/
编写lua脚本
--
-- Created by IntelliJ IDEA.
-- User: zhoufy
-- Date: 2020-08-04
-- Time: 14:27
-- To change this template use File | Settings | File Templates.
--
local os = require('os')
local io = require("io")
local function writefile(filenme, info)
local wfile = io.open(filenme, "w")
assert(wfile)
wfile.write(info)
wfile.close()
end
local function is_dir(sPath)
if type(sPath) ~= "string" then return false end
local response = os.execute("cd " .. sPath)
if response == 0 then
return true
end
return false
end
local file_exists = function(name)
local f=io.open(name, "r")
if f ~= nil then io.close(f) return true
else return false
end
end
local area = nil
local originalUri = ngx.var.uri;
local originalFile = ngx.var.file;
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");
if index then
originalUri = string.sub(ngx.var.uri, 0, index - 2);
area = string.sub(ngx.var.uri, index);
index = string.find(area, "([.])")
area = string.sub(area, 0, index - 1)
local index = string.find(originalFile, "([0-9]+)x([0-9]+)")
originalFile = string.sub(originalFile, 0, index - 2)
end
if not file_exists(originalFile) then
ngx.log(ngx.ERR, originalUri)
local fileid = string.sub(originalUri, 2);
local fastdfs = require('restyfastdfs');
local fdfs = fastdfs:new()
fdfs.set_tracker("172.16.85.148", 22122)
fdfs.set_timeout(1000)
fdfs.set_tracker_keepalive(0,100)
fdfs.set_storage_keepalive(0,100)
local dsta = fdfs:do_download(fileid)
if data then
if not is_dir(ngx.var.image_dir) then
os.execute("mkdir -p " .. ngx.var.image_dir)
end
writefile(originalFile, data)
end
end
local image_sizes = { "80x80", "800x600", "40x40", "60x60" }
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 " .. originalFile .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;
ngx.log(ngx.ERR, command)
os.execute(command);
end
if file_exists(ngx.var.file) then
ngx.exec(ngx.var.uri)
else
ngx.exit(404)
end
vim /etc/ld.so.conf
#增加内容
/usr/local/lib
#刷新命令
ldconfig