FastDFS 结合 GraphicsMagick

单机安装部署(CentOS 6.7 环境)

先安装 FastDFS

  • 软件准备:
  • 安装依赖包:yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel libevent
  • 安装 libfastcommon-1.0.7.tar.gz
    • 解压:tar zxvf libfastcommon-1.0.7.tar.gz
    • 进入解压后目录:cd libfastcommon-1.0.7/
    • 编译:./make.sh
    • 安装:./make.sh install
    • 设置几个软链接:ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
    • 设置几个软链接:ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
    • 设置几个软链接:ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
    • 设置几个软链接:ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
  • 安装 tracker (跟踪器)服务 FastDFS_v5.08.tar.gz
    • 解压:tar zxvf FastDFS_v5.05.tar.gz
    • 进入解压后目录:cd FastDFS/
    • 编译:./make.sh
    • 安装:./make.sh install
  • 配置 tracker 服务
    • 复制一份配置文件:cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
    • 编辑:vim /etc/fdfs/tracker.conf,编辑内容看下面中文注释
      1. disabled=false
      2. bind_addr=
      3. port=22122
      4. connect_timeout=30
      5. network_timeout=60
      6. # 下面这个路径是保存 store data 和 log 的地方,需要我们改下,指向我们一个存在的目录
      7. # 创建目录:mkdir -p /opt/fastdfs/tracker/data-and-log
      8. base_path=/opt/fastdfs/tracker/data-and-log
      9. max_connections=256
      10. accept_threads=1
      11. work_threads=4
      12. store_lookup=2
      13. store_group=group2
      14. store_server=0
      15. store_path=0
      16. download_server=0
      17. reserved_storage_space = 10%
      18. log_level=info
      19. run_by_group=
      20. run_by_user=
      21. allow_hosts=*
      22. sync_log_buff_interval = 10
      23. check_active_interval = 120
      24. thread_stack_size = 64KB
      25. storage_ip_changed_auto_adjust = true
      26. storage_sync_file_max_delay = 86400
      27. storage_sync_file_max_time = 300
      28. use_trunk_file = false
      29. slot_min_size = 256
      30. slot_max_size = 16MB
      31. trunk_file_size = 64MB
      32. trunk_create_file_advance = false
      33. trunk_create_file_time_base = 02:00
      34. trunk_create_file_interval = 86400
      35. trunk_create_file_space_threshold = 20G
      36. trunk_init_check_occupying = false
      37. trunk_init_reload_from_binlog = false
      38. trunk_compress_binlog_min_interval = 0
      39. use_storage_id = false
      40. storage_ids_filename = storage_ids.conf
      41. id_type_in_filename = ip
      42. store_slave_file_use_link = false
      43. rotate_error_log = false
      44. error_log_rotate_time=00:00
      45. rotate_error_log_size = 0
      46. log_file_keep_days = 0
      47. use_connection_pool = false
      48. connection_pool_max_idle_time = 3600
      49. http.server_port=8080
      50. http.check_alive_interval=30
      51. http.check_alive_type=tcp
      52. http.check_alive_uri=/status.html
    • 启动 tracker 服务:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
    • 重启 tracker 服务:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
    • 查看是否有 tracker 进程:ps aux | grep tracker
  • storage (存储节点)服务部署
    • 一般 storage 服务我们会单独装一台机子,但是这里为了方便我们安装在同一台。
    • 如果 storage 单独安装的话,那上面安装的步骤都要在走一遍,只是到了编辑配置文件的时候,编辑的是 storage.conf 而已
    • 复制一份配置文件:cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
    • 编辑:vim /etc/fdfs/storage.conf,编辑内容看下面中文注释
      1. disabled=false
      2. group_name=group1
      3. bind_addr=
      4. client_bind=true
      5. port=23000
      6. connect_timeout=30
      7. network_timeout=60
      8. heart_beat_interval=30
      9. stat_report_interval=60
      10. # 下面这个路径是保存 store data 和 log 的地方,需要我们改下,指向我们一个存在的目录
      11. # 创建目录:mkdir -p /opt/fastdfs/storage/data-and-log
      12. base_path=/opt/fastdfs/storage/data-and-log
      13. max_connections=256
      14. buff_size = 256KB
      15. accept_threads=1
      16. work_threads=4
      17. disk_rw_separated = true
      18. disk_reader_threads = 1
      19. disk_writer_threads = 1
      20. sync_wait_msec=50
      21. sync_interval=0
      22. sync_start_time=00:00
      23. sync_end_time=23:59
      24. write_mark_file_freq=500
      25. store_path_count=1
      26. # 图片实际存放路径,如果有多个,这里可以有多行:
      27. # store_path0=/opt/fastdfs/storage/images-data0
      28. # store_path1=/opt/fastdfs/storage/images-data1
      29. # store_path2=/opt/fastdfs/storage/images-data2
      30. # 创建目录:mkdir -p /opt/fastdfs/storage/images-data
      31. store_path0=/opt/fastdfs/storage/images-data
      32. subdir_count_per_path=256
      33. # 指定 tracker 服务器的 IP 和端口
      34. tracker_server=192.168.1.114:22122
      35. log_level=info
      36. run_by_group=
      37. run_by_user=
      38. allow_hosts=*
      39. file_distribute_path_mode=0
      40. file_distribute_rotate_count=100
      41. fsync_after_written_bytes=0
      42. sync_log_buff_interval=10
      43. sync_binlog_buff_interval=10
      44. sync_stat_file_interval=300
      45. thread_stack_size=512KB
      46. upload_priority=10
      47. if_alias_prefix=
      48. check_file_duplicate=0
      49. file_signature_method=hash
      50. key_namespace=FastDFS
      51. keep_alive=0
      52. use_access_log = false
      53. rotate_access_log = false
      54. access_log_rotate_time=00:00
      55. rotate_error_log = false
      56. error_log_rotate_time=00:00
      57. rotate_access_log_size = 0
      58. rotate_error_log_size = 0
      59. log_file_keep_days = 0
      60. file_sync_skip_invalid_record=false
      61. use_connection_pool = false
      62. connection_pool_max_idle_time = 3600
      63. http.domain_name=
      64. http.server_port=8888
    • 启动 storage 服务:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf,首次启动会很慢,因为它在创建预设存储文件的目录
    • 重启 storage 服务:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
    • 查看是否有 storage 进程:ps aux | grep storage
  • 测试是否部署成功
    • 利用自带的 client 进行测试
    • 复制一份配置文件:cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
    • 编辑:vim /etc/fdfs/client.conf,编辑内容看下面中文注释
      1. connect_timeout=30
      2. network_timeout=60
      3. # 下面这个路径是保存 store log 的地方,需要我们改下,指向我们一个存在的目录
      4. # 创建目录:mkdir -p /opt/fastdfs/client/data-and-log
      5. base_path=/opt/fastdfs/client/data-and-log
      6. # 指定 tracker 服务器的 IP 和端口
      7. tracker_server=192.168.1.114:22122
      8. log_level=info
      9. use_connection_pool = false
      10. connection_pool_max_idle_time = 3600
      11. load_fdfs_parameters_from_tracker=false
      12. use_storage_id = false
      13. storage_ids_filename = storage_ids.conf
      14. http.tracker_server_port=80
    • 在终端中通过 shell 上传 opt 目录下的一张图片:/usr/bin/fdfs_test /etc/fdfs/client.conf upload /opt/test.jpg
    • 如下图箭头所示,生成的图片地址为:http://192.168.1.114/group1/M00/00/00/wKgBclb0aqWAbVNrAAAjn7_h9gM813_big.jpg
      • FastDFS
    • 即使我们现在知道图片的访问地址我们也访问不了,因为我们还没装 FastDFS 的 Nginx 模块

安装 nginx-lua-GraphicsMagick

  • 来源:https://github.com/yanue/nginx-lua-GraphicsMagick/blob/master/nginx-install.md
  • 添加专用用户,后面有用
    • groupadd www
    • useradd -g www www -s /bin/false
  • 安装依赖包
    • yum install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
    • yum install -y libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel
    • yum install -y GraphicsMagick GraphicsMagick-devel
  • 下面的这些软件都在本文在开头的那个压缩包里面。现在我们需要解压这些压缩包
    • cd /opt/setups
    • tar -zxvf nginx-1.8.0.tar.gz
    • tar -zxvf LuaJIT-2.0.4.tar.gz
    • tar -zxvf GraphicsMagick-1.3.21.tar.gz
    • tar -zxvf zlib-1.2.8.tar.gz
  • 安装 LuaJIT
    • cd /opt/setups/LuaJIT-2.0.4
    • make
    • make install
    • export LUAJIT_LIB=/usr/local/lib
    • export LUAJIT_INC=/usr/local/include/luajit-2.0
    • ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
  • 修改一些配置文件
    • 编辑 Nginx 模块的配置文件:vim /opt/setups/fastdfs-nginx-module/src/config
    • 找到下面一行包含有 local 字眼去掉,因为这三个路径根本不是在 local 目录下的。(如果你的配置文件没有这个 local,那这一步跳过)
      1. CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
    • 改为如下:
      1. CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
    • 复制文件:cp /opt/setups/FastDFS/conf/http.conf /etc/fdfs
    • 复制文件:cp /opt/setups/FastDFS/conf/mime.types /etc/fdfs
  • 开始安装 Nginx
    • cd /opt/setups/nginx-1.8.0
    • mkdir -p /usr/local/nginx /var/log/nginx /var/temp/nginx /var/lock/nginx
    • 执行下面编译语句:
      1. ./configure --prefix=/usr/local/nginx \
      2. --user=www \
      3. --group=www \
      4. --pid-path=/var/local/nginx/nginx.pid \
      5. --lock-path=/var/lock/nginx/nginx.lock \
      6. --error-log-path=/var/log/nginx/error.log \
      7. --http-log-path=/var/log/nginx/access.log \
      8. --http-client-body-temp-path=/var/temp/nginx/client \
      9. --http-proxy-temp-path=/var/temp/nginx/proxy \
      10. --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
      11. --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
      12. --http-scgi-temp-path=/var/temp/nginx/scgi \
      13. --sbin-path=/usr/local/nginx/sbin/nginx \
      14. --with-http_ssl_module \
      15. --with-http_realip_module \
      16. --with-http_sub_module \
      17. --with-http_flv_module \
      18. --with-http_dav_module \
      19. --with-http_gzip_static_module \
      20. --with-http_stub_status_module \
      21. --with-http_addition_module \
      22. --with-http_spdy_module \
      23. --with-pcre \
      24. --with-zlib=/opt/setups/zlib-1.2.8 \
      25. --add-module=/opt/setups/nginx-http-concat \
      26. --add-module=/opt/setups/lua-nginx-module \
      27. --add-module=/opt/setups/ngx_devel_kit \
      28. --add-module=/opt/setups/fastdfs-nginx-module/src
    • make
    • make install
  • 修改一下配置
    • 复制 Nginx 模块的配置文件:cp /opt/setups/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
    • 编辑 Nginx 模块的配置文件:vim /etc/fdfs/mod_fastdfs.conf,编辑内容看下面中文注释
    • 如果在已经启动 Nginx 的情况下修改下面内容记得要重启 Nginx。
      1. connect_timeout=2
      2. network_timeout=30
      3. # 下面这个路径是保存 log 的地方,需要我们改下,指向我们一个存在的目录
      4. # 创建目录:mkdir -p /opt/fastdfs/fastdfs-nginx-module/data-and-log
      5. base_path=/opt/fastdfs/fastdfs-nginx-module/data-and-log
      6. load_fdfs_parameters_from_tracker=true
      7. storage_sync_file_max_delay = 86400
      8. use_storage_id = false
      9. storage_ids_filename = storage_ids.conf
      10. # 指定 tracker 服务器的 IP 和端口
      11. tracker_server=192.168.1.114:22122
      12. storage_server_port=23000
      13. group_name=group1
      14. # 因为我们访问图片的地址是:http://192.168.1.114/group1/M00/00/00/wKgBclb0aqWAbVNrAAAjn7_h9gM813_big.jpg
      15. # 该地址前面是带有 /group1/M00,所以我们这里要使用 true,不然访问不到(原值是 false)
      16. url_have_group_name = true
      17. store_path_count=1
      18. # 图片实际存放路径,如果有多个,这里可以有多行:
      19. # store_path0=/opt/fastdfs/storage/images-data0
      20. # store_path1=/opt/fastdfs/storage/images-data1
      21. # store_path2=/opt/fastdfs/storage/images-data2
      22. store_path0=/opt/fastdfs/storage/images-data
      23. log_level=info
      24. log_filename=
      25. response_mode=proxy
      26. if_alias_prefix=
      27. flv_support = true
      28. flv_extension = flv
      29. group_count = 0
    • 创建文件夹:mkdir -p /opt/fastdfs/thumb
    • 编辑 Nginx 配置文件
    • vim /usr/local/nginx/conf/nginx.conf
      ``` nginx

      注意这一行行,我特别加上了使用 root 用户去执行,不然有些日记目录没有权限访问

      user root;
      worker_processes 1;
  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include mime.types;
  6. default_type application/octet-stream;
  7. sendfile on;
  8. keepalive_timeout 65;
  9. server{
  10. listen 80;
  11. server_name 192.168.1.112;
  12. set $img_thumbnail_root /opt/fastdfs/thumb;
  13. set $img_file $img_thumbnail_root$uri;
  14. # like:/pic/M00/xx/xx/xx.jpg_200x100.jpg
  15. # /group1/M00
  16. location ~* ^(\/(\w+)(\/M00)(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png))$ {
  17. root $img_thumbnail_root;
  18. set $fdfs_group_root /opt/fastdfs/storage/images-data/data;
  19. # 如果缩略图不存在
  20. if (!-f $img_file) {
  21. add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue';
  22. add_header file-path $request_filename;
  23. set $request_filepath $fdfs_group_root$4;
  24. set $img_width $6;
  25. set $img_height $7;
  26. set $img_ext $5;
  27. content_by_lua_file /opt/setups/lua/cropSize.lua;
  28. }
  29. }
  30. location /group1/M00 {
  31. alias /opt/fastdfs/storage/images-data/data;
  32. ngx_fastdfs_module;
  33. }
  34. }
  35. }
  36. ```
  37. - 启动 Nginx
  38. - 停掉防火墙:`service iptables stop`
  39. - 启动:`/usr/local/nginx/sbin/nginx`,启动完成 shell 是不会有输出的
  40. - 访问:`192.168.1.114`,如果能看到:`Welcome to nginx!`,即可表示安装成功
  41. - 检查 时候有 Nginx 进程:`ps aux | grep nginx`,正常是显示 3 个结果出来
  42. - 刷新 Nginx 配置后重启:`/usr/local/nginx/sbin/nginx -s reload`
  43. - 停止 Nginx:`/usr/local/nginx/sbin/nginx -s stop`
  44. - 如果访问不了,或是出现其他信息看下错误立即:`vim /var/log/nginx/error.log`

多机安装部署(CentOS 6.7 环境)

资料