1 安装fastDFS
1.1 安装libfastcommon
>安装fastDFS前,首先下载最新版本的libfastcommon进行安装,上传至/usr/src目录中:
https://github.com/happyfish100/libfastcommon/releases
>解压、配置和编译:
#unzip libfastcommon-1.0.35.zip #cd libfastcommon-1.0.35/ #./make.sh #./make.sh install |
PS:确认make没有错误后,执行安装,64位系统默认会复制到/usr/lib64下;
>设置环境变量、创建软链接
#export LD_LIBRARY_PATH=/usr/lib64/ #ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so |
1.2 安装fastDFS
1.2.1 下载最新版本的fastDFS,上传至/usr/src目录中:
https://github.com/happyfish100/fastdfs
>解压、配置和编译:
#unzip fastdfs-master.zip #cd fastdfs-master #./make.sh #./make.sh install |
PS:确认make没有错误后,执行安装,默认会安装到/usr/bin中,并在/etc/fdfs中添加三个配置文件;
1.2.2修改相关配置文件:
把准备好的fdfs.tar.gz包解开,里面有配置文件(client.conf、http.conf 、mime.types、mod_fastdfs.conf 、 storage.conf、storage_ids.conf、http.conf),把这些配置文件上传至/etc/fdfs/ 目录里,如下不想上传现有的配置文件,你就自己按下面的方法修改
#修改所有的配置文件中的服务器地址(上面几乎所有的配置文件都要修改下面的地址) tracker_server=172.21.177.59:22122 #这个地址是两台fastDFS的负载地址 |
1.2.3启动tracker和storage
先创建fastDFS目录 #mkdir /usr/local/fastDFS
在创建data目录 # mkdir -p /usr/local/fastDFS/storage/data
启动 #/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf # /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
查看服务,默认如果显示22122和8090,23000,80说明服务正常起来 # netstat –lnp –tcp
检查进程是否启来了 # ps -ef |grep fdfs root 7819 1 0 15:24 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf root 8046 1 0 15:36 ? 00:00:01 fdfs_storaged /etc/fdfs/storage.conf start |
PS:上面进程表示启动ok了,若有错误,可以在/home/fastdfs目录下检查日志。
1.2.4 上传图片测试
使用自带的fdfs_test来测试,使用格式如下:
#fdfs_test /etc/fdfs/client.conf upload /home/01.jpg ... group_name=group1, ip_addr=172.21.177.72, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKgdhFTV0ZmAP3AZAPk-Io7D4w8580.jpg ... example file url: http:// 172.21.177.72/group1/M00/00/00/wKgdhFTV0ZmAP3AZAPk-Io7D4w8580.jpg storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKgdhFTV0ZmAP3AZAPk-Io7D4w8580_big.jpg ... example file url: http:// 172.21.177.72/group1/M00/00/00/wKgdhFTV0ZmAP3AZAPk-Io7D4w8580_big.jpg |
PS:上张01.jpg的图片至/home目录测试,然后等安装好了nginx后,使用上面的http://172.21.177.72/group...地址在浏览器进行测试;
使用fdfs_delete_file来删除文件,格式如下:
#fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKgdhFTV11uAXgKWAPk-Io7D4w8667.jpg |
PS:可以看到,上传ok了,这里会生成两个文件,这是fastdfs的主/从文件特性,以后再介绍。example file url是不能在浏览器中直接打开的,除非配合nginx使用。删除文件需要完整的group_name和remote_filename。
2 安装LuaJIT、nginx-lua-module模块
2.1 安装LuaJIT 2.1
2.1.1 下载最新的LuaJIT,然后上传至/usr/src目录下:
# wget http://luajit.org/download/LuaJIT-2.1.0-beta2.tar.gz |
2.1.2 解压、配置
# tar zxf LuaJIT-2.1.0-beta2.tar.gz # cd LuaJIT-2.1.0-beta2 # make # make install |
2.2 安装ngx_devel_kit(NDK)模块
下载最新的ngx_devel_kit模块,然后上传至/usr/src目录下:
#cd /usr/src #wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz #tar -xzvf v0.2.19.tar.gz |
2.3 安装lua-nginx-module模块
下载最新的lua-nginx-module,然后上传至/usr/src目录下:
# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.2.tar.gz # tar -xzvf v0.10.2.tar.gz |
3 安装nginx
3.1 安装依赖包
# yum -y install pcre zlib-devel openssl-devel |
3.2 安装fastdfs-nginx-module模块
解压、配置:
#cd /usr/src # wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip # unzip master |
3.3 安装nginx
3.3.1 解压、配置:
# cd /usr/src #t ar -zxvf nginx-1.9.3.tar.gz # cd nginx-1.9.3 # ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-pcre --with-http_ssl_module --with-stream --add-module=/usr/src/ngx_devel_kit-0.2.19/ --add-module=/usr/src/lua-nginx-module-0.10.2/ --add-module=/usr/src/fastdfs-nginx-module-master/src |
错误:
./configure:error: ngx_http_lua_module requires the Lua library.
解决:
安装lua的相关依赖:yum install readline-devreadline-devel
安装lua5.1:
下载lux5.1的包 # tar -zxvf lua-5.1.4.tar.gz # cd lua-5.1.4 # make linux test # make install |
上面的错误解决后,再一次配置和编译
#./configure。 #make #make install |
如果又出现以下错误:
In file included from/usr/local/src/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:6:
/usr/local/src/fastdfs-nginx-module/src/common.c:In function ‘fdfs_http_request_handler’:
/usr/local/src/fastdfs-nginx-module/src/common.c:1245:error: ‘FDFSHTTPParams’ has no member named ‘support_multi_range’
make[1]:*** [objs/addon/src/ngx_http_fastdfs_module.o] Error 1
make[1]:Leaving directory `/usr/local/src/nginx-1.12.0'
make:*** [build] Error 2
解决:
所有安装包都要最新的,就没问题
3.3.2 创建www用户和www组
#groupadd www #useradd -g www www |
3.3.3 启动nginx
启动前先创建fastDFS 日志目录和文件 #mkdir /usr/local/fastDFS/log/ #touch /usr/local/fastDFS/log/mod_fastdfs.log
还需创建lua目录 #mkdir /usr/local/nginx/lua #然后把lua.tar.gz上传进来,并解压 #tar -zxvf lua.tar.gz
修改fastDFS的属组权限 #chown -R www:www /usr/local/fastDFS/
启动 #./nginx |
4 安装GM
4.1 安装依赖包
# yum -y install libpng-devel libpng # yum -y install libjpeg-devel libjpeg # yum -y install libtool-ltdl libtool-ltdl-devel |
4.2 安装GraphicsMagick
4.2.1 下载,上传至/usr/src目录
https://github.com/qieangel2013/nginxLuaGmFastdfs#gm-config
4.2.2 配置、编译
#tar –xvf GraphicsMagick-1.3.25.tar #cd GraphicsMagick #./configure #make #make install |
PS: 将会默认在一下目录中安装相应文件:
/usr/local/bin
/usr/local/include
/usr/local/lib
/usr/local/share
5 最终测试
比如我上传一张1111.jpg格式的图片到/home目录下:
#fdfs_test /etc/fdfs/client.conf upload /home/1111.jpg
This is FastDFS client test program v5.11
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
[2017-06-01 13:57:34] DEBUG - base_path=/usr/local/fastDFS, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
tracker_query_storage_store_list_without_group: server 1. group_name=, ip_addr=172.21.177.72, port=23000
group_name=group1, ip_addr=172.21.177.72, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/rBWxSFkvrM6AZuG_AAXj5hxfUHo730.jpg source ip address: 172.21.177.72 file timestamp=2017-06-01 13:57:34 file size=386022 file crc32=476008570 example file url: http://172.21.177.72/group1/M00/00/00/rBWxSFkvrM6AZuG_AAXj5hxfUHo730.jpg storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/rBWxSFkvrM6AZuG_AAXj5hxfUHo730_big.jpg source ip address: 172.21.177.72 file timestamp=2017-06-01 13:57:34 file size=386022 file crc32=476008570 example file url: http://172.21.177.72/group1/M00/00/00/rBWxSFkvrM6AZuG_AAXj5hxfUHo730_big.jpg |
然后通过浏览器访问http://172.21.177.72/group1/M00/00/00/rBWxSFkvrM6AZuG_AAXj5hxfUHo730_big.jpg,
最后把上面红色的big修改为下面红色的参数,看能不能压缩:
http://172.21.177.72/group1/M00/00/00/rBWxSFkvrM6AZuG_AAXj5hxfUHo730_100x100.jpg
6 fastDFS相关命令
注意:
先重启storage服务,然后再启动nginx,注意顺序,否则会报端口占用的错误
注意, 千万不要使用-9强行杀死进程 。
重启storage
# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart |
重启tracker
# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart |
关闭storage
# killall fdfs_storaged |
关闭tracker
# killall fdfs_trackered |
查看集群状态
#/usr/bin/fdfs_monitor /etc/fdfs/storage.conf |
上传
#fdfs_test /etc/fdfs/client.conf upload /home/website/platform/public/images/uploader/loading.gif |
删除
#fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/ZciEZlepkl6Abj28AAAPOSSdASU225_big.gif |
Nginx.conf
server { |
thumbnail.lua
local preName = ngx.var.a |
注意
当切图后rewrite时会有延迟 #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency. |