Categories
Server

试用 zimg服务器

同事推荐这个库做图片服务器,一看作者是88年的,好小,在百度工作过,稍微看了一下,照着 zimg服务器搭建手记 安装,还是遇到一些麻烦,我使用的系统是 CentOS release 6.2,64bit。

安装依赖包 libmemcached,ImageMagick,openssl, libevent:
yum install gcc-c++ gcc automake git libmemcached-devel ImageMagick-devel openssl-devel libevent-devel

安装 cmake

如果使用 yum install cmake 安装,版本太低,编译libevhtp-1.2.6 会报错:
# cmake -DCMAKE_PREFIX_PATH=/usr -DCMAKE_INSTALL_PREFIX=/usr/
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 2.8 or higher is required. You are running version 2.6.4

需要手动下载安装

wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
tar xvf cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./bootstrap --prefix=/usr && make && make install

wget https://github.com/ellzey/libevhtp/archive/1.2.6.tar.gz -O libevhtp-1.2.6.tar.gz
tar xvf libevhtp-1.2.6.tar.gz
cd libevhtp-1.2.6
cmake -DCMAKE_PREFIX_PATH=/usr -DCMAKE_INSTALL_PREFIX=/usr -DLIBEVENT_PTHREADS_LIBRARY=/usr/lib -DLIBEVENT_OPENSSL_LIBRARY=/usr/lib64/openssl/
make && make install

wget https://github.com/buaazp/zimg/archive/master.zip
unzip zimg-master.zip
cd zimg-master
make

报错:

cc -Wall -c -o zimg.o zimg.c
zimg.c:27:29: error: wand/MagickWand.h: No such file or directory

这个 Makefile 写的确实不是很专业,需要手动修改才可以编译通过,至少起码应该使用 automake或cmake。手动添加 MagickWand 头文件路径。然后链接的时候又报错:

zhttpd.o: In function `guess_type’:
zhttpd.c:(.text+0x28): undefined reference to `evutil_ascii_strcasecmp’
zhttpd.o: In function `guess_content_type’:
zhttpd.c:(.text+0xb8): undefined reference to `evutil_ascii_strcasecmp’

需要指定 -L/usr/lib 才行,下面是给Makefile打的补丁:

[root@dmzsrv01 zimg]# git diff Makefile
diff --git a/Makefile b/Makefile
index 315a8c4..05295b1 100644
--- a/Makefile
+++ b/Makefile
@@ -4,10 +4,10 @@ MAC = Darwin
ifeq ($(OS),Darwin)
LIBS = -levent -levent_openssl -levent_pthreads -lssl -lcrypto -levhtp -lMagickWand-6.Q16 -lmemcached
else
- LIBS = -levent -levent_openssl -levent_pthreads -lssl -lcrypto -levhtp -lMagickWand -lmemcached
+ LIBS = -L /usr/lib -levent -levent_openssl -levent_pthreads -lssl -lcrypto -levhtp -lMagickWand -lmemcached
endif
OBJS = zhttpd.o zspinlock.o zlog.o zmd5.o zutil.o zcache.o zimg.o main.o
-CFLAGS = -Wall
+CFLAGS = -Wall -g -I /usr/include/ImageMagick -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16
all: ${OBJS}
gcc ${CFLAGS} -o zimg ${OBJS} ${LIBS}

使用还是比较简单的,help 就可以了。需要注意的是,这个 zimg 不能运行两次,会 core dump,程序本身没处理好。最后,fork 这个项目,花了一会时间写 CMakeLists.txt 使用cmake编译,已经 pull request给作者。现在安装更方便了,在 Ubuntu 13.10 Server 测试通过:

sudo apt-get  install cmake libssl-dev libevent-dev libmagickwand-dev libmemcached-dev

wget https://github.com/ellzey/libevhtp/archive/1.2.6.tar.gz
tar xvf libevhtp-1.2.6.tar.gz
cd libevhtp-1.2.6/
cmake .
make
sudo make install

cd ..
git clone https://github.com/lytsing/zimg
cd zimg/
cmake .

跑了 test目录下的测试脚本,发现性能还是挺不错的,目前这个版本还不支持访问认证以及https,支持https需要手动写,参考 libevhtp 提供的example添加不是难事。至于生产与访问文件的 url名称 是文件的 md5 值,可以在这基础上稍微做个变化,显得更专业一些。文件存储与MooseFS 结合倒是不错的选择。

If you enjoyed this post, make sure you subscribe to my RSS feed!

8 replies on “试用 zimg服务器”

Hi,我是zimg的作者,看到你这个笔记写得太好了,commit已经看到,非常感谢编写cmakelist,不过我还不怎么会用,等学会了测试成功就merge进去~
丢人了 ^_^
不知道你微博ID是多少,以后有啥要求可以交流方便点~

建议试试 又拍云

减少了开发量,支持一些特色功能,而且有CDN,访问速度还是不错的。

另外,根据我自己的实践,imagemagick 处理图片的性能不太好。

不太了解你的实际场景,还不是很明白具体为什么图片这类的静态资源放在别人的server上会不安全。自己弄也可以,就是麻烦了点,作图如果定制多的话,也挺麻烦的。还有CDN,也自建吗?

自己弄而且对图片处理性能要求高的话,推荐用graphicsmagick, 快很多。

按freeman983和楼主的的笔记分别在CentOS和Ubuntu上都试过了,都是无法成功!现在比较忙,后面再仔细看下具体是什么原因导致的,到时看能不能给大牛们反映下

Leave a Reply to buaazp Cancel reply

Your email address will not be published. Required fields are marked *