搭建Nginx+FastDFS服务器以及实现Nginx+Lua+GraphicsMagick动态压缩图片

在Linux系统安装FastDFS服务器(ip:192.168.127.128)

1、编译环境

[root@localhost ~]# yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y

2、安装libfatscommon

FastDFS分离出的一些公用函数包

#切换到安装目录准备下载安装包
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# git clone https://github.com/happyfish100/libfastcommon.git --depth 1
[root@localhost src]# cd libfastcommon/
[root@localhost src]# ./make.sh && ./make.sh install #编译安装
​
#若出现以下错误执行下面命令即可,Peer reports incompatible or #unsupported protocol version.
[root@localhost src]# yum update -y nss curl libcurl

3、安装FastDFS

[root@localhost libfastcommon]# cd ../ #返回安装目录
[root@localhost src]# git clone https://github.com/happyfish100/fastdfs.git --depth 1
[root@localhost src]# cd fastdfs/
[root@localhost fastdfs]# ./make.sh && ./make.sh install #编译安装

4、FastDFS配置

[root@localhost fastdfs]# cd ../ #返回安装目录
#拷贝相关配置文件到/etc/fdfs目录下
[root@localhost src]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
[root@localhost src]# cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
[root@localhost src]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
[root@localhost src]# cp /usr/local/src/fastdfs/conf/http.conf /etc/fdfs/ #供nginx访问使用
[root@localhost src]# cp /usr/local/src/fastdfs/conf/mime.types /etc/fdfs/ #供nginx访问使用
[root@localhost src]# mkdir /home/fastdfs #创建数据存储目录,配置文件中要指定的目录
#tracker配置
[root@localhost src]# vim /etc/fdfs/tracker.conf
#单机部署,需要修改的内容如下
port=22122  # tracker服务器端口(默认22122,一般不修改)
base_path=/home/fastdfs  # 存储日志和数据的根目录
​
#分布式部署,需要修改的内容如下
port=22122  # tracker服务器端口(默认22122,一般不修改)
base_path=/home/fastdfs  # 存储日志和数据的根目录
#storage配置
[root@localhost src]# vim /etc/fdfs/storage.conf
#单机部署,需要修改的内容如下
port=23000  # storage服务端口(默认23000,一般不修改)
base_path=/home/fastdfs  # 数据和日志文件存储根目录
store_path0=/home/fastdfs  # 第一个存储目录
tracker_server=192.168.127.128:22122  # tracker服务器IP和端口,虚拟机系统的ip
http.server_port=8888  # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
​
#分布式部署,需要修改的内容如下
port=23000  # storage服务端口(默认23000,一般不修改)
base_path=/home/fastdfs  # 数据和日志文件存储根目录
store_path0=/home/fastdfs  # 第一个存储目录
tracker_server=192.168.127.128:22122  # 服务器1
tracker_server=192.168.127.129:22122  # 服务器2
tracker_server=192.168.127.130:22122  # 服务器3
http.server_port=8888  # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
#client配置
[root@localhost src]# vim /etc/fdfs/client.conf
#单机部署,需要修改的内容如下
base_path=/home/fastdfs
tracker_server=192.168.127.128:22122    #tracker服务器IP和端口
​
#分布式部署,需要修改的内容如下
base_path=/home/fastdfs
tracker_server=192.168.127.128:22122  # 服务器1
tracker_server=192.168.127.129:22122  # 服务器2
tracker_server=192.168.127.130:22122  # 服务器3

5、安装fastdfs-nginx-module

FastDFS和nginx的关联模块

[root@localhost src]# git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
#将配置文件拷贝到/etc/fdfs目录下
[root@localhost src]# cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs

6、fastdfs-nginx-module配置

[root@localhost src]# vim /etc/fdfs/mod_fastdfs.conf
#单机部署,需要修改的内容如下
tracker_server=192.168.127.128:22122  #tracker服务器IP和端口
url_have_group_name=true
store_path0=/home/fastdfs
​
#分布式部署,需要修改的内容如下
tracker_server=192.168.127.128:22122  # 服务器1
tracker_server=192.168.127.129:22122  # 服务器2
tracker_server=192.168.127.130:22122  # 服务器3
url_have_group_name=true
store_path0=/home/fastdfs

7、安装nginx

[root@localhost src]# wget http://nginx.org/download/nginx-1.15.4.tar.gz
[root@localhost src]# tar -zxvf nginx-1.15.4.tar.gz

8、nginx配置

[root@localhost src]# cd nginx-1.15.4/
#添加fastdfs-nginx-module模块
[root@localhost nginx-1.15.4]# ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/
[root@localhost nginx-1.15.4]# make && make install
​
[root@localhost nginx-1.15.4]# vim /usr/local/nginx/conf/nginx.conf
#单机部署,添加如下配置
server {
    listen       8888; #该端口为storage.conf中的http.server_port相同
    server_name  localhost;
    
    location ~/group[0-9]/ {
        ngx_fastdfs_module;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
}
​
#分布式部署,同上

12、关闭防火墙

[root@localhost nginx-1.15.4]# systemctl stop iptables;
[root@localhost nginx-1.15.4]# systemctl stop firewalld;
[root@localhost nginx-1.15.4]# systemctl disable firewalld.service; #关闭防火墙开机自启

13、启停命令介绍

/etc/init.d/fdfs_trackerd start #启动tracker服务
/etc/init.d/fdfs_trackerd restart #重启动tracker服务
/etc/init.d/fdfs_trackerd stop #停止tracker服务
chkconfig fdfs_trackerd on #自启动tracker服务
​
/etc/init.d/fdfs_storaged start #启动storage服务
/etc/init.d/fdfs_storaged restart #重动storage服务
/etc/init.d/fdfs_storaged stop #停止动storage服务
chkconfig fdfs_storaged on #自启动storage服务
​
/usr/local/nginx/sbin/nginx #启动nginx
/usr/local/nginx/sbin/nginx -s reload #重启nginx
/usr/local/nginx/sbin/nginx -s stop #停止nginx
#可编写启动脚本
[root@localhost nginx-1.15.4]# cd /usr/local/src/
[root@localhost src]# vi startup.sh
#添加如下内容
/etc/init.d/fdfs_trackerd start #启动tracker服务
/etc/init.d/fdfs_storaged start #启动storage服务
/usr/local/nginx/sbin/nginx #启动nginx
[root@localhost src]# chmod +x startup.sh
​
[root@localhost src]# vi shutdown.sh
/etc/init.d/fdfs_trackerd stop #停止tracker服务
/etc/init.d/fdfs_storaged stop #停止动storage服务
/usr/local/nginx/sbin/nginx -s stop #停止nginx
[root@localhost src]# chmod +x shutdown.sh

14、启动服务后可进行集群检测

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
# 会显示会有几台服务器 有3台就会 显示 Storage 1 - Storage 3的详细信息

使用FastDFS自带的工具将文件上传到FastDFS,并可以通过http 访问到对应的图片

1、测试上传

#先将测试图片放到linux系统随便一个目录下,例如:/home/fanxuebo/Pictures
#使用命令上传到FastDFS
[root@localhost src]# fdfs_upload_file /etc/fdfs/client.conf /home/fanxuebo/Pictures/kyrie.jpg
group1/M00/00/00/wKh_gF7-sJaAGJe_AAF8KZax7Dw403.jpg

2、通过http访问

#在浏览器地址栏输入下面地址
http://192.168.127.128:8888/group1/M00/00/00/wKh_gF7-sJaAGJe_AAF8KZax7Dw403.jpg

 

使用GraphicsMagick工具生成缩略图,通过http访问某个图片时,显示其对应的动态压缩图(Nginx+Lua+GraphicsMagick)

1、下载LuaJIT和Lua和GraphicsMagick等相关软件

一定要注意软件的版本,最好保持一致,不然很多坑的

[root@localhost src]# cd /usr/local/src
#先下载,分别进行编译安装使用
[root@localhost src]# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
[root@localhost src]# wget http://www.lua.org/ftp/lua-5.3.3.tar.gz
[root@localhost src]# wget https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.35/GraphicsMagick-1.3.35.tar.gz
#下载编译安装nginx使用的一些模块
[root@localhost src]# wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
[root@localhost src]# tar -xvf zlib-1.2.11.tar.gz
[root@localhost src]# git clone https://github.com/simpl/ngx_devel_kit.git
[root@localhost src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
[root@localhost src]# tar -xvf v0.10.9rc7.tar.gz

2、编译安装-LuaJIT

LuaJIT是Lua语言写的脚本的即时编译器(Just-In-Time Compilerfor)

[root@localhost src]# tar -zxf LuaJIT-2.0.4.tar.gz
[root@localhost src]# cd LuaJIT-2.0.4/
[root@localhost LuaJIT-2.0.4]# make && make install
[root@localhost LuaJIT-2.0.4]# export LUAJIT_LIB=/usr/local/lib
[root@localhost LuaJIT-2.0.4]# export LUAJIT_INC=/usr/local/include/luajit-2.0
[root@localhost LuaJIT-2.0.4]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

3、编译安装-Lua

一个小巧的脚本语言

[root@localhost LuaJIT-2.0.4]# cd ..
[root@localhost src]# tar -xvf lua-5.3.3.tar.gz
[root@localhost src]# cd lua-5.3.3/
[root@localhost lua-5.3.3]# make linux && make install
​
#编译时遇到错误lua.c:80:31: fatal error: readline/readline.h: No such file or directory
#说明缺少libreadline-dev依赖包,执行下面命令即可
[root@localhost lua-5.3.3]# yum install readline-devel

4、编译安装-GraphicsMagick

图像处理领域的瑞士军刀

[root@localhost lua-5.3.3]# cd ..
#安装graphicsmagick支持的图片格式
[root@localhost src]# yum install libjpeg libjpeg-devel libpng libpng-devel giflib giflib-devel freetype freetype-devel
[root@localhost src]# tar -xvf GraphicsMagick-1.3.35.tar.gz
[root@localhost src]# cd GraphicsMagick-1.3.35/
[root@localhost GraphicsMagick-1.3.35]# ./configure --prefix=/usr/local/GraphicsMagick --enable-shared
[root@localhost GraphicsMagick-1.3.35]# make && make install

5、编译安装-nginx

[root@localhost GraphicsMagick-1.3.35]# cd ..
#先查看nginx编译安装时安装了哪些模块
[root@localhost src]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --add-module=/usr/local/src/fastdfs-nginx-module/src/
​
#在nginx的源码目录下,通过–add-module=xxx的方式,追加以下模块
#--add-module=/usr/local/src/lua-nginx-module
#--add-module=/usr/local/src/ngx_devel_kit
[root@localhost sbin]# cd /usr/local/src/nginx-1.15.4/
[root@localhost nginx-1.15.4]# ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/ \
--prefix=/usr/local/nginx \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--add-module=/usr/local/src/lua-nginx-module-0.10.9rc7 \
--add-module=/usr/local/src/ngx_devel_kit
[root@localhost nginx-1.15.4]# make && make install

6、nginx配置

[root@localhost nginx-1.15.4]# vim /usr/local/nginx/conf/nginx.conf
#添加如下配置
server {
    listen       8888;
    server_name  localhost;
​
    #安装并启动nginx后,使用lua1来验证nginx是否启动成功并支持lua脚本
    #浏览器输入http://192.168.127.128:8888/lua1,可以输出“Hello, Lua!”即启动成功
    location /lua1 {
                default_type 'text/plain';
                content_by_lua 'ngx.say("Hello, Lua!")';
    }
    
    #访问原图片的匹配,本身有这个location注意别重复了
    location / {
            root   /resource/image;
            index  index.html index.htm;
    }
    
    #正则表达式用于拦截图片请求(图片请求都是以固定格式请求的,所以用正则匹配)
    location ~* ^((.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png))$ {
        root /usr/local/nginx/html;
        if (!-f $request_filename) { # 如果文件不存在时才需要裁剪
            add_header X-Powered-By 'Lua GraphicsMagick'; # 此 HTTP Header 无实际意义,用于测试
            add_header file-path $request_filename; # 此 HTTP Header 无实际意义,用于测试
            set $request_filepath /resource/image$2; # 设置原始图片路径
            set $width $4;  # 设置裁剪/缩放的宽度
            set $height $5; # 设置裁剪/缩放的高度
            set $ext $6;    # 图片文件格式后缀
            content_by_lua_file /usr/local/nginx/lua/ImageResizer.lua; # 加载外部 Lua 文件
        }
    }
}

7、创建图片目录和Lua配置

[root@localhost nginx-1.15.4]# mkdir /resource/image -p
#给目录授权,这步骤很重要!!!
[root@localhost nginx-1.15.4]# chmod 777 -R /resource/image/
[root@localhost nginx-1.15.4]# mkdir /usr/local/nginx/lua -p
[root@localhost nginx-1.15.4]# vi /usr/local/nginx/lua/ImageResizer.lua
#添加如下配置
local command = "/usr/local/GraphicsMagick/bin/gm convert   -auto-orient -strip " .. ngx.var.request_filepath .. " -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);

8、测试效果

#启动nginx
[root@localhost nginx-1.15.4]# /usr/local/nginx/sbin/nginx
在上述图片目录/resource/image中上传一张图片 kyrie.jpg
​
浏览器访问http://192.168.127.128:8888/kyrie.jpg是原图
浏览器访问http://192.168.127.128:8888/kyrie.jpg_100x100.jpg是输入尺寸的图
$2对应得是/kyrie.jpg
$3对应得是jpg
$4对应得是100
$5对应得是100
$6对应得是jpg

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章