FastDFS安裝 --> 測試

     
     本身閒着沒事,在小黑上虛擬了 4 個 centos 64 的系統,用來安裝分佈式 fastdfs 、 nginx 負載均衡,過程一路艱辛,搞了一個通宵都沒弄好,終於在次日的傍晚終於弄好了,記錄一下過程。
     有時候看書會犯糊塗,看完了都不知道將了啥,只有動手坐坐,啥玩意都明白差很少了。
     有時候太懶了,哎,這是病,得治。

  • FastDFS安裝
        FastDFS是一個國產開源的輕量級分佈式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題.特別適合以文件爲載體的在線服務,如相冊網站、視頻網站等等.FastDFS服務端有兩個角色:跟蹤器(tracker)和存儲節點(storage).跟蹤器主要作調度工做,在訪問上起負載均衡的做用.

    什麼是FastDFS
            FastDFS是一個國產開源的輕量級分佈式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題.特別適合以文件爲載體的在線服務,如相冊網站、視頻網站等等. 

    FastDFS原理
            存儲節點採用了分組(group)的方式。存儲系統由一個或多個group組成,group與group之間的文件是相互獨立的,全部group的文件容量累加就是整個存儲系統中的文件容量。一個group能夠由一臺或多臺存儲服務器組成,一個group下的存儲服務器中的文件都是相同的,group中的多臺存儲服務器起到了冗餘備份和負載均衡的做用(一個組的存儲容量爲該組內存儲服務器容量最小的那個,不一樣組的Storage server之間不會相互通訊,同組內的Storage server之間會相互鏈接進行文件同步)。在group中增長服務器時,同步已有的文件由系統自動完成,同步完成後,系統自動將新增服務器切換到線上提供服務。當存儲空間不足或即將耗盡時,能夠動態添加group。只須要增長一臺或多臺服務器,並將它們配置爲一個新的group,這樣就擴大了存儲系統的容量。
            FastDFS只有兩個角色:Tracker server和Storage server。Tracker server做爲中心結點,其主要做用是負載均衡和調度。Tracker server在內存中記錄分組和Storage server的狀態等信息,不記錄文件索引信息,佔用的內存量不多。另外,客戶端(應用)和Storage server訪問Tracker server時,Tracker server掃描內存中的分組和Storage server信息,而後給出應答。由此能夠看出Tracker server很是輕量化,不會成爲系統瓶頸。
            FastDFS中的Storage server在其餘文件系統中一般稱做Trunk server或Data server。Storage server直接利用OS的文件系統存儲文件。FastDFS不會對文件進行分塊存儲,客戶端上傳的文件和Storage server上的文件一一對應(FastDFS中的文件標識分爲兩個部分:組名和文件名,兩者缺一不可)

    FastDFS架構:
            FastDFS服務端有三個角色:跟蹤服務器(tracker server)、存儲服務器(storage server)和客戶端(client)。
            tracker server:跟蹤服務器,主要作調度工做,起負載均衡的做用。在內存中記錄集羣中全部存儲組和存儲服務器的狀態信息,是客戶端和數據服務器交互的樞紐。相比GFS中的master更爲精簡,不記錄文件索引信息,佔用的內存量不多。

           storage server
    :存儲服務器(又稱:存儲節點或數據服務器),文件和文件屬性(meta data)都保存到存儲服務器上。Storage server直接利用OS的文件系統調用管理文件。
           client
    :客戶端,做爲業務請求的發起方,經過專有接口,使用TCP/IP協議與跟蹤器服務器或存儲節點進行數據交互。

 

FastDFS協議:
    FastDFS角色間是基於TCP/IP協議進行通訊,協議包格式爲:header + body。具體結構如圖:



上傳機制:


同步時間管理:
    當一個文件上傳成功後,客戶端立刻發起對該文件下載請求(或刪除請求)時,tracker是如何選定一個適用的存儲服務器呢?其實每一個存儲服務器都須要定時將自身的信息上報給tracker,這些信息就包括了本地同步時間(即,同步到的最新文件的時間戳)。而tracker根據各個存儲服務器的上報狀況,就可以知道剛剛上傳的文件,在該存儲組中是否已完成了同步。同步信息上報以下圖


下載機制:

精巧的FID:
    說到下載就不得不提文件索引(又稱:FID)的精巧設計了。文件索引結構以下圖,是客戶端上傳文件後存儲服務器返回給客戶端,用於之後訪問該文件的索引信息。文件索引信息包括:組名,虛擬磁盤路徑,數據兩級目錄,文件名。



ps
html

  • 組名:文件上傳後所在的存儲組名稱,在文件上傳成功後有存儲服務器返回,須要客戶端自行保存。
  • 虛擬磁盤路徑:存儲服務器配置的虛擬路徑,與磁盤選項store_path*對應。
  • 數據兩級目錄:存儲服務器在每一個虛擬磁盤路徑下建立的兩級目錄,用於存儲數據文件。
  • 文件名:與文件上傳時不一樣。是由存儲服務器根據特定信息生成,文件名包含:源存儲服務器IP地址、文件建立時間戳、文件大小、隨機數和文件拓展名等信息。

快速定位文件:
  • 知道FastDFS FID的組成後,咱們來看看FastDFS是如何經過這個精巧的FID定位到須要訪問的文件。
  • 經過組名tracker可以很快的定位到客戶端須要訪問的存儲服務器組,並將選擇合適的存儲服務器提供客戶端訪問;
  • 存儲服務器根據「文件存儲虛擬磁盤路徑」和「數據文件兩級目錄」能夠很快定位到文件所在目錄,並根據文件名找到客戶端須要訪問的文件。


   




    開始動起手吧, 下載地址
        http://sourceforge.net/projects/fastdfs/files/
        http://code.google.com/p/fastdfs/ 
  • 總體網絡配置
    Tracker  Server    192.168.55.222         /home/tracker  端口:22122
    Tracker  Server    192.168.55.226         /home/tracker  端口:22122
    Storage1 Server    192.168.55.223  group1 /home/storage  端口:23000
    Storage4 Server    192.168.55.227  group1 /home/storage  端口:23000
    Storage2 Server    192.168.55.224  group2 /home/storage  端口:23000
    Storage3 Server    192.168.55.225  group2 /home/storage  端口:23000
    #Storage2爲group2的源服務器
    注意:
    1.group2同組的Storage2和Storage3 FastDFS服務端口必須一致: port=23000。
    2.一臺服務器能夠裝多個組(group)但不能裝同組的多個Storage,日誌會報錯誤,日誌報錯緣由是"注意1"
    3.Version 4.05以前fastdfs內部綁定了libevent做爲http服務器.Version 4.05以後的版本刪除了內置的web http服務,內置的web http服務是個累贅,不用也罷!
    4.啓動storage server時,一直處於僵死狀態.啓動storage server,storage將鏈接tracker server,若是連不上,將一直重試。直到鏈接成功,啓動纔算真正完成!若是集羣中有2臺tracker server,而其中一臺tracker沒有啓動,可能會致使storage server一直處於僵死狀態
  • 系統操做環境的設置
    #軟件安裝包存儲:
      /usr/local/src
      /usr/local/fastdfs  fastdfs安裝目錄
    
    #基本目錄列表:
    #建立fastdfs用戶
      /usr/sbin/groupadd fastdfs
      /usr/sbin/useradd -g fastdfs fastdfs
    #建立存儲數據目錄
      mkdir -p /home/fastdfs/tracker;#建立tracker目錄保存運行日誌
      mkdir -p /home/fastdfs/storage;#建立Storage目錄保存運行日誌及其data數據 
    # source .bashrc
  • 爲方便查找目錄,設置變量
    ################Tracker################
    # vi .bashrc
      alias  worksrc='cd /usr/local/src;ls'
      alias  workfastdfs='cd /usr/local/fastdfs;ls'
      alias  worktracker='cd /home/fastdfs/tracker;ls'
    #track啓動 重啓 中止
      alias  sertracker='service fdfs_trackerd'
    配置生效
    # source .bashrc
  • 爲方便查找目錄,設置變量
    ################Storage################
    # vi .bashrc
      alias  worksrc='cd /usr/local/src;ls'
      alias  workfastdfs='cd /usr/local/fastdfs;ls'
      alias  workstorage='cd /home/fastdfs/storage;ls'
      alias  workfastdfs='cd /usr/local/nginx;ls'
    #storage啓動 重啓 中止
      alias  serstorage='service fdfs_storaged'
    #nginx 啓動 重啓 中止
      alias  sernginx='service nginxd'
    配置生效
    # source .bashrc

1.安裝libevent 和 fastdfs 前端

  • 首先安裝libevent. fastdfs在編譯源程序時fastdfs內部調用libevent的處理機制,,須要用到libevent一些依賴文件,不然編譯fastdfs會出錯
    libevent是一個事件觸發、異步事件的網絡庫,是一個輕量級的開源高性能網絡庫,以BSD許可證發佈,適用於Windows、Linux、BSD、Mac OS等多種平臺,內部使用select、epoll等系統調用。
    http://hahaya.github.io/2013/07/15/build-libevent.html 
    #確保系統已經安裝如下工具包
    gcc perl libevent-devel make pcre-devel
    #卸載系統自帶libevent,自帶版本太低,安裝fastdfs會出錯
      rpm -qa|grep libevent
    #  或者
      yum remove libevent*
    #下載安裝libevent
    #worksrc;
      wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz;
      tar -zxvf libevent-2.0.19-stable.tar.gz;
      cd libevent-2.0.19-stable;
    #make clean;
      ./configure --prefix=/usr/local/libevent
      make && make install;
    #爲libevent建立軟連接到/lib庫下,64位系統對應/lib64
      ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
      ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
    #命令彙總:
      worksrc;wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz;tar -zxvf libevent-2.0.19-stable.tar.gz;cd libevent-2.0.19-stable;make clean;./configure --prefix=/usr/local/libevent;make && make install;cd ../
  • 安裝fastdfs的步驟
    worksrc;
    # 自行下載 4.07  http://sourceforge.net/projects/fastdfs/files/ 
    # wget http://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz
      tar -zxvf FastDFS_v4.07.tar.gz
      cd FastDFS
    
    #因爲定義/usr/local/fastdfs爲fastdfs安裝目錄,因此須要修改make.sh
      vim make.sh
    #/etc/fdfs 所有替換爲 /usr/local/fastdfs/conf
      %s/\/etc\/fdfs/\/usr\/local\/fastdfs\/conf/g
    
    # TARGET_PREFIX=/usr/local 修改成 /usr/local/fastdfs
      sed -i 's:TARGET_PREFIX=.*:TARGET_PREFIX=/usr/local/fastdfs:g' make.sh
    
    # TARGET_CONF_PATH=/etc/fdfs 修改成 /usr/local/fastdfs/conf
      sed -i 's:TARGET_CONF_PATH=.*:TARGET_CONF_PATH=/usr/local/fastdfs/conf:g' make.sh
    
    #安裝
      ./make.sh C_INCLUDE_PATH=/usr/local/libevent/include LIBRARY_PATH=/usr/local/libevent/lib
      ./make.sh install
    修改 fastdfs 啓動文件
    # 修改 fdfs_trackerd 配置文件
      vim /etc/init.d/fdfs_trackerd
    #替換
      %s/usr\/local/usr\/local\/fastdfs/g
      %s/etc\/fdfs/usr\/local\/fastdfs\/conf/g
    #增長x權限
      chmod a+x /etc/init.d/fdfs_trackerd
    
    # 修改 fdfs_storaged 配置文件
      vim /etc/init.d/fdfs_storaged
    #替換
      %s/usr\/local/usr\/local\/fastdfs/g
      %s/etc\/fdfs/usr\/local\/fastdfs\/conf/g
    #增長x權限
      chmod a+x /etc/init.d/fdfs_storaged


2.安裝Tracker Server - 192.168.55.222
node

  • 引用上例中安裝libevent 和 fastdfs步驟
  • 配置及啓動Tracker Server,端口:22122
    #修改tracker.conf配置
      vim /usr/local/fastdfs/conf/tracker.conf
    # the tracker server port
      port=22122
    # the base path to store data and log files
      base_path=/home/yuqing/fastdfs -> base_path=/home/fastdfs/tracker #日誌目錄
    # sed -i 's:base_path=.*:base_path=/home/fastdfs/tracker:g' tracker.conf
      reserved_storage_space = 4GB -> reserved_storage_space = 1GB
    # sed -i 's:reserved_storage_space=.*:reserved_storage_space = 1GB:g' tracker.conf
    #unix group name to run this program,
    #not set (empty) means run by the group of current user
      run_by_group= -> fastdfs
    #unix username to run this program,
    #not set (empty) means run by current user
      run_by_user= -> fastdfs
    #開啓自定義server ID取代ip形式,方便內部網絡服務器更換ip#**此方式要重點理解,4.0之後新特性
      use_storage_id = true #使用server ID做爲storage server標識
      storage_ids_filename = storage_ids.conf #<id> <group_name> <ip_or_hostname>
      id_type_in_filename = id #文件名反解析中包含server ID,之前是ip
    複製storage_ids.conf文件
    cp -r /usr/local/src/FastDFS/conf/storage_ids.conf /usr/local/fastdfs/conf/
    #編輯storage服務器ID與IP地址的對應關係
      vim /usr/local/fastdfs/conf/storage_ids.conf
    #<id> <group_name> <ip_or_hostname>
    # 100001          group1           192.168.55.223
  • 設置 Tracker Server 用戶及組
    chown -R fastdfs:fastdfs /home/fastdfs
    增長x權限
    chmod a+x /etc/init.d/fdfs_trackerd
    啓動 Tracker
    service fdfs_trackerd start
    #啓動過程當中出現的錯誤
      ./fdfs_trackerd: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
    #解決辦法
      ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
    #查看日誌
      vim /home/fastdfs/tracker/logs/trackerd.log
    #啓動後成功的日誌
    #[2013-10-03 20:50:08] INFO - FastDFS v4.07, base_path=/home/fastdfs/tracker, run_by_group=fastdfs, run_by_user=fastdfs, connect_timeout=30s, network_timeout=60s, port=22122, bind_addr=, max_connections=256, accept_threads=1, work_threads=4, store#_lookup=2, store_group=, store_server=0, store_path=0, reserved_storage_space=10.00%, download_server=0, allow_ip_count=-1, sync_log_buff_interval=10s, check_active_interval=120s, thread_stack_size=64 KB, storage_ip_changed_auto_adjust=1, storage_sy#nc_file_max_delay=86400s, storage_sync_file_max_time=300s, use_trunk_file=0, slot_min_size=256, slot_max_size=16 MB, trunk_file_size=64 MB, trunk_create_file_advance=0, trunk_create_file_time_base=02:00, trunk_create_file_interval=86400, trunk_create_file_sp#ace_threshold=20 GB, trunk_init_check_occupying=0, trunk_init_reload_from_binlog=0, use_storage_id=1, id_type_in_filename=id, storage_id_count=1, rotate_error_log=0, error_log_rotate_time=00:00, rotate_error_log_size=0, store_slave_file_use_link=0, use_con#nection_pool=0, g_connection_pool_max_idle_time=3600s
    設置開機啓動 Tracker
    vim /etc/rc.d/rc.local
      service fdfs_trackerd start
    附目錄說明
    #  tracker server目錄及文件結構:
    #  ${base_path}
    #    |__data
    #    |     |__storage_groups.dat:存儲分組信息
    #    |     |__storage_servers.dat:存儲服務器列表
    #    |__logs
    #          |__trackerd.log:tracker server日誌文件
    安裝 nginx 作 tracker server 負載均衡 
    upstream webservergroup1 {
            server 192.168.55.223;
        }
        upstream webservergroup2 {
            server 192.168.55.224;
            server 192.168.55.225;
        }
        server {
            listen       80;
            server_name  localhost;
            #charset koi8-r;
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            location /group1/M00 {
                proxy_pass http://webservergroup1;
            }
    
            location /group2/M00 {
                proxy_pass http://webservergroup2;
            } 
        }

      
3.安裝Storage Server -192.168.55.223
nginx

  • 引用上例中安裝libevent 和 fastdfs步驟
  • 修改storage.conf配置
    vim /usr/local/fastdfs/conf/storage.conf
    # the name of the group this storage server belongs to
      group_name=group1
    # the name of the group this storage server belongs to
    # the storage server port #the storage server port
      port=23000
    # the base path to store data and log files #日誌目錄
      base_path=/home/yuqing/fastdfs -> /home/fastdfs/storage
    # store_path#, based 0, if store_path0 not exists, it's value is base_path #data數據存儲目錄
    # the paths must be exist
      store_path0=/home/fastdfs/storage
    # tracker_server can ocur more than once, and tracker_server format is
    #  "host:port", host can be hostname or ip address
      tracker_server=192.168.209.121:22122 ->192.168.55.222:22122
    #unix group name to run this program,
    #not set (empty) means run by the group of current user
      run_by_group= -> fastdfs   #這兒必定填寫執行的用戶名,否則會啓動報錯,測試環境填寫的 root
    #unix username to run this program,
    #not set (empty) means run by current user
      run_by_user= -> fastdfs   #這兒必定填寫執行的用戶名,否則會啓動報錯,測試環境填寫的 root
    設置 Storage Server 用戶及組
    chown -R fastdfs:fastdfs /home/fastdfs
    增長x權限
    chmod a+x /etc/init.d/fdfs_storaged
    啓動 Storage
    service fdfs_storaged start
    #接下來會出現不少mkdir data path,這是系統在建立數據目錄
    #data path: /home/fastdfs/storage/data, mkdir sub dir...
    #mkdir data path: 00 ...
    #mkdir data path: 01 ...
    #mkdir data path: 02 ...
    #mkdir data path: 03 ...
    #.......................
    #data path: /home/fastdfs/storage/data, mkdir sub dir done.
    設置開機啓動 Storage 
    vim /etc/rc.d/rc.local
      service fdfs_storaged start
    附目錄說明
    #  storage server目錄及文件結構:
    #  ${base_path}
    #    |__data
    #    |     |__storage_stat.dat:本地存儲信息
    #    |     |__sync
    #    |           |__ binlog.000
    #    |           |__ binlog.index
    #    |__logs
    #          |__storaged.log:storage server日誌文件
  • 安裝fastdfs-nginx-module模塊
    #建立相關用戶和目錄建立www用戶和組
      /usr/sbin/groupadd www
      /usr/sbin/useradd -g www www
    #建立nginx日誌目錄
      mkdir -p /home/www/logs
      chmod a+w /home/www/logs
      chown -R www:www /home/www/logs
    安裝nginx (這裏
    cd /usr/local/src
    #下載 nginx
    #wget http://nginx.org/download/nginx-1.5.4.tar.gz
      tar -zxvf nginx-1.5.4.tar.gz
      cd nginx-1.5.4/
      ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module
      make && make install
    #--with-http_stub_status_module 用來監控nginx的當前狀態
    安裝fastdfs-nginx-module插件
    #worksrc;
      cd /usr/local/src
    # wget http://fastdfs.googlecode.com/files/fastdfs-nginx-module_v1.15.tar.gz
      tar -zxvf fastdfs-nginx-module_v1.15.tar.gz
    #修改插件配置文件
      vim /usr/local/src/fastdfs-nginx-module/src/config
    ngx_addon_name=ngx_http_fastdfs_module
    HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
    CORE_INCS="$CORE_INCS /usr/local/fastdfs/include/fastdfs /usr/local/fastdfs/include/fastcommon/"
    CORE_LIBS="$CORE_LIBS -L/usr/local/fastdfs/lib -lfastcommon -lfdfsclient"
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/usr/local/fastdfs/conf/mod_fastdfs.conf\"'"
    #複製mod_fastdfs.conf到/usr/local/fastdfs/conf/目錄下
      cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /usr/local/fastdfs/conf/
    上面編譯時使用的動態連接庫
    #啓動nginx報錯
    #./nginx: error while loading shared libraries: libfastcommon.so: cannot open shared object file: No such file or directory
    #解決辦法 ---> 將/usr/local/fastdfs/lib 加入系統文件/etc/ld.so.conf中
      vim /etc/ld.so.conf
      /usr/local/fastdfs/lib
    #更新庫文件緩存ld.so.cache
      /sbin/ldconfig -v
    編譯fastdfs-nginx-module模塊
    # 從新編譯nginx  
      cd /usr/local/src/nginx-1.5.4
      ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src
      make; make install
    修改mod_fastdfs.conf配置
    vim /usr/local/fastdfs/conf/mod_fastdfs.conf
    # the base path to store log files
      base_path=/tmp
    # if load FastDFS parameters from tracker server
    # since V1.12
    # default value is false
      load_fdfs_parameters_from_tracker=true
    # FastDFS tracker_server can ocur more than once, and tracker_server format is
    #  "host:port", host can be hostname or ip address
    # valid only when load_fdfs_parameters_from_tracker is true
      tracker_server=192.168.55.222:22122
    # the port of the local storage server
    # the default value is 23000
      storage_server_port=23000
    # the group name of the local storage server
      group_name=group1
    # if the url / uri including the group name
    # set to false when uri like /M00/00/00/xxx
    # set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
    # default value is false
      url_have_group_name = true
    # path(disk or mount point) count, default value is 1
    # must same as storage.conf
      store_path_count=1
    # store_path#, based 0, if store_path0 not exists, it's value is base_path
    # the paths must be exist
    # must same as storage.conf
      store_path0=/home/fastdfs/storage
    # set the log filename, such as /usr/local/apache2/logs/mod_fastdfs.log
    # empty for output to stderr (apache and nginx error_log file)
      log_filename=/home/www/logs/mod_fastdfs.log
    nginx配置簡潔版本
    #vim /usr/local/nginx/conf/nginx.conf
    user  www www;
    worker_processes  2;
    error_log  /home/www/logs/error.log  notice;
    pid        /home/www/logs/nginx.pid;
    
    worker_rlimit_nofile 5120;
    events {
        use epoll;
        worker_connections  5120;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        tcp_nopush     on;
        keepalive_timeout  60;
        tcp_nodelay on;
    
        server {
            listen       80;
            server_name  localhost;
            location /group1/M00 {
                alias /home/fastdfs/storage/data;
                ngx_fastdfs_module;
            }
        }
    }
    
    #啓動nginx
      /usr/local/nginx/sbin/nginx


4.安裝Storage Server -192.168.55.224
git

  • 引用上例中安裝libevent 和 fastdfs步驟
  • 修改storage.conf配置 
    vim /usr/local/fastdfs/conf/storage.conf
    # the name of the group this storage server belongs to
      group_name=group2
    # the name of the group this storage server belongs to
    # the storage server port #the storage server port
      port=23000
    # the base path to store data and log files #日誌目錄
      base_path=/home/yuqing/fastdfs -> /home/fastdfs/storage
    # store_path#, based 0, if store_path0 not exists, it's value is base_path #data數據存儲目錄
    # the paths must be exist
      store_path0=/home/fastdfs/storage
    #unix group name to run this program,
    #not set (empty) means run by the group of current user
      run_by_group= -> fastdfs    #這兒必定填寫執行的用戶名,否則會啓動報錯,測試環境填寫的 root
    #unix username to run this program,
    #not set (empty) means run by current user
      run_by_user= -> fastdfs   #這兒必定填寫執行的用戶名,否則會啓動報錯,測試環境填寫的 root
    # tracker_server can ocur more than once, and tracker_server format is
    #  "host:port", host can be hostname or ip address
      tracker_server=192.168.xxx.xxxx:22122 -> tracker_server=192.168.55.222:22122
  • 引用上例中 "編輯啓動腳本" 步驟
    #啓動 Storage  
      service fdfs_storaged restart
    #接下來會出現不少mkdir data path,這是系統在建立數據目錄
    #data path: /home/fastdfs/storage/data, mkdir sub dir...
    #mkdir data path: 00 ...
    #mkdir data path: 01 ...
    #mkdir data path: 02 ...
    #mkdir data path: 03 ...
    #.......................
    #data path: /home/fastdfs/storage/data, mkdir sub dir done.
  • 安裝fastdfs-nginx-module插件
    #worksrc;
      cd /usr/local/src
    # wget http://fastdfs.googlecode.com/files/fastdfs-nginx-module_v1.15.tar.gz
      tar -zxvf fastdfs-nginx-module_v1.15.tar.gz
    #修改插件配置文件
      vim /usr/local/src/fastdfs-nginx-module/src/config
    ngx_addon_name=ngx_http_fastdfs_module
    HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
    CORE_INCS="$CORE_INCS /usr/local/fastdfs/include/fastdfs /usr/local/fastdfs/include/fastcommon/"
    CORE_LIBS="$CORE_LIBS -L/usr/local/fastdfs/lib -lfastcommon -lfdfsclient"
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/usr/local/fastdfs/conf/mod_fastdfs.conf\"'"
    #複製mod_fastdfs.conf到/usr/local/fastdfs/conf/目錄下
      cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /usr/local/fastdfs/conf/
    上面編譯時使用的動態連接庫
    #將/usr/local/fastdfs/lib 加入系統文件/etc/ld.so.conf中
      vim /etc/ld.so.conf
      /usr/local/fastdfs/lib
    #更新庫文件緩存ld.so.cache
      /sbin/ldconfig
    編譯fastdfs-nginx-module模塊
    # 從新編譯nginx  
      cd /usr/local/src/nginx-1.5.4
      ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src
      make; make install
    修改mod_fastdfs.conf配置
    vim /usr/local/fastdfs/conf/mod_fastdfs.conf
    # the base path to store log files
      base_path=/tmp
    # if load FastDFS parameters from tracker server
    # since V1.12
    # default value is false
      load_fdfs_parameters_from_tracker=true
    # FastDFS tracker_server can ocur more than once, and tracker_server format is
    #  "host:port", host can be hostname or ip address
    # valid only when load_fdfs_parameters_from_tracker is true
      tracker_server=192.168.25.11:22122
    # the port of the local storage server
    # the default value is 23000
      storage_server_port=23000
    # the group name of the local storage server
      group_name=group2
    # if the url / uri including the group name
    # set to false when uri like /M00/00/00/xxx
    # set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
    # default value is false
      url_have_group_name = true
    # path(disk or mount point) count, default value is 1
    # must same as storage.conf
      store_path_count=1
    # store_path#, based 0, if store_path0 not exists, it's value is base_path
    # the paths must be exist
    # must same as storage.conf
      store_path0=/home/fastdfs/storage
    # set the log filename, such as /usr/local/apache2/logs/mod_fastdfs.log
    # empty for output to stderr (apache and nginx error_log file)
      log_filename=/home/www/logs/mod_fastdfs.log
    nginx配置簡潔版本
    #vim /usr/local/nginx/conf/nginx.conf
    user  www www;
    worker_processes  2;
    error_log  /home/www/logs/error.log  notice;
    pid        /home/www/logs/nginx.pid;
    
    worker_rlimit_nofile 5120;
    events {
        use epoll;
        worker_connections  5120;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        tcp_nopush     on;
        keepalive_timeout  60;
        tcp_nodelay on;
    
        server {
            listen       80;
            server_name  localhost;
            location /group2/M00 {
                alias /home/fastdfs/storage/data;
                ngx_fastdfs_module;
            }
        }
    }
    
    #啓動nginx
      /usr/local/nginx/sbin/nginx

5.安裝Storage Server - 192.168.55.225
github

  • 參考 192.168.55.224 安裝,不一樣點就是192.168.55.224是group2的源服務,storage.conf配置不一樣,須要注意。

6.測試及使用fastdfs-192.168.55.222 web

  • fastdfs之配置client
    #修改 client.conf
      vim /usr/local/fastdfs/conf/client.conf
      base_path=/home/yuqing/fastdfs-> base_path=/home/fastdfs/tracker
      tracker_server=192.168.209.121:22122 -> tracker_server=192.168.55.222:22122
    #或
      sed -i 's:base_path=.*:base_path=/home/fastdfs/tracker:g' /usr/local/fastdfs/conf/client.conf
      sed -i 's:tracker_server=.*:tracker_server=192.168.55.222\:22122:g' /usr/local/fastdfs/conf/client.conf
    #upload
      cd /usr/local/fastdfs/bin
      ./fdfs_upload_file /usr/local/fastdfs/conf/client.conf /tmp/t1.jpg 
    #-——————————————————————————————————————
    #group1/M00/00/00/oYYBAFJNRnGASWekAAAXkpktHxI423.jpg
    查看文件信息
    ./fdfs_file_info /usr/local/fastdfs/conf/client.conf group1/M00/00/00/oYYBAFJNQiiAYIXzAAAXkpktHxI095.jpg
    #————————————————————————————————————————
    source storage id: 100001
    source ip address: 192.168.55.223
    file create timestamp: 2013-10-03 18:26:57
    file size: 6034
    file crc32: 2569871122 (0x992D1F12)
    測試上傳
    ./fdfs_test /usr/local/fastdfs/conf/client.conf upload /tmp/t1.jpg 
    #————————————————————————————————————————
    This is FastDFS client test program v4.07
    
    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.
    
    [2013-10-04 02:20:19] DEBUG - base_path=/home/fastdfs/tracker, 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=192.168.55.223, port=23000
    
    group_name=group1, ip_addr=192.168.55.223, port=23000
    storage_upload_by_filename
    group_name=group1, remote_filename=M00/00/00/oYYBAFJNRWeAHhGuAAAXkpktHxI578.jpg
    source ip address: 192.168.55.223
    file timestamp=2013-10-03 18:22:31
    file size=6034
    file crc32=2569871122
    example file url: http://192.168.55.223/group1/M00/00/00/oYYBAFJNRWeAHhGuAAAXkpktHxI578.jpg
    storage_upload_slave_by_filename
    group_name=group1, remote_filename=M00/00/00/oYYBAFJNRWeAHhGuAAAXkpktHxI578_big.jpg
    source ip address: 192.168.55.223
    file timestamp=2013-10-03 18:22:31
    file size=6034
    file crc32=2569871122
    example file url: http://192.168.55.223/group1/M00/00/00/oYYBAFJNRWeAHhGuAAAXkpktHxI578_big.jpg

    在瀏覽器中-->http://192.168.55.222/group1/M00/00/00/oYYBAFJNQiiAYIXzAAAXkpktHxI095.jpg
    輸入上圖中的url地址, tracker server 會自動重定向到存儲文件的 storage server,文件下載成功。至此,已經成功搭建了fastdfs。弄了兩天,終於好了 - 0-:


  • 常規命令範例:
    #監控storage
    /usr/local/fastdfs/bin/fdfs_monitor /usr/local/fastdfs/conf/storage.conf
    #若是存在多個多個組,只須要監控其中一個組,就能調出全部組的狀態
    
    #刪除組內服務器storage和查看各個組內服務器狀態
    /usr/local/fastdfs/bin/fdfs_monitor /usr/local/fastdfs/conf/client.conf delete group2 192.168.55.223
    /usr/local/fastdfs/bin/fdfs_monitor /usr/local/fastdfs/conf/client.conf
    storage server有7個狀態,以下(數值從1~7):
    # FDFS_STORAGE_STATUS:INIT      :初始化,還沒有獲得同步已有數據的源服務器
    # FDFS_STORAGE_STATUS:WAIT_SYNC :等待同步,已獲得同步已有數據的源服務器
    # FDFS_STORAGE_STATUS:SYNCING   :同步中
    # FDFS_STORAGE_STATUS:DELETED   :已刪除,該服務器從本組中摘除
    # FDFS_STORAGE_STATUS:OFFLINE   :離線
    # FDFS_STORAGE_STATUS:ONLINE    :在線,尚不能提供服務
    # FDFS_STORAGE_STATUS:ACTIVE    :在線,能夠提供服務
  • 經常使用操做命令
    #調整防火牆的規則,開放端口:22122
      iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 22122 -j ACCEPT
      /etc/init.d/iptables save
    
    #調整防火牆的規則,開放端口:23000
      iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 23000 -j ACCEPT
      /etc/init.d/iptables save
    
    #重啓防火牆:
      /etc/init.d/iptables restart
    #查看CentOS防火牆信息:
    
      /etc/init.d/iptables status  
    關閉CentOS防火牆服務:
      /etc/init.d/iptables stop 
    
    #刪除端口信息
      iptables -D INPUT 3  //刪除input的第3條規則  
      iptables -L -n
    ——————————————————————————————————————
    #啓動tracker進程:
      /etc/init.d/fdfs_trackerd start
    
    #查看trackerd狀態:
      /etc/init.d/fdfs_trackerd status
    
    #查看trackerd監聽的端口:
      netstat -plantu | grep tracker
    
    #啓動Storage進程:
      /etc/init.d/fdfs_storaged start
    
    #查看Storage進程:
      ps -ef | grep storaged | grep -v grep


可以完成基於這兩個做者的博文, wangying  、  zrwm  thk。


錯誤及解決方案:

  • mod_fastdfs.log --->  Permission denied
    #經過nginx前端服務器請求 fastdfs 圖片資源是 日誌打印錯誤:
    #查看日誌:
    /home/www/logs/mod_fastdfs.log
    [2013-10-12 14:28:50] ERROR - file: /usr/local/src/fastdfs-nginx-module/src/common.c, line: 881, stat file: /home/fastdfs/storage/data/00/00/pIYBAFJY6LmAHKsiAAAXkpktHxI541.jpg fail, errno: 13, error info: Permission denied
    
    #解決方案:
    #找到fastdfs存儲目錄,如:/home/fastdfs
    #看是否有執行權限:
    drwx------. 5 fastdfs fastdfs  4096 10月 12 09:10 fastdfs
    #增長執行權限就行
    chmod 755 fastdfs/
    #設置用戶和組
    chown -R fastdfs:fastdfs fastdfs/


安裝圖片過濾處理

  • http_image_filter_module是nginx提供的集成圖片處理模塊,支持nginx-0.7.54之後的版本,在網站訪問量不是很高磁盤有限不想生成多餘的圖片文件的前提下可,就能夠用它實時縮放圖片,旋轉圖片,驗證圖片有效性以及獲取圖片寬高以及圖片類型信息,因爲是即時計算的結果,因此網站訪問量大的話,不建議使用。
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=/usr/local/software/fastdfs-nginx-module/src/ --with-http_image_filter_module --with-pcre --with-http_gzip_static_module --with-http_ssl_module --with-openssl=/usr/bin/openssl
  • 若是未安裝回報「/configure: error: the HTTP image filter module requires the GD library.」錯誤
    yum install gd-devel
    
    apt-get install libgd2-xpm libgd2-xpm-dev
  • make&&make install後就能夠進行配置了,作最簡單的配置,先讓模塊能夠跑起來
    location ~ /simg/.*\.jpg$ {
        #proxy_pass     http://10.11.11.11;
        #rewrite "/simg/(.*\.jpg)$" /img/$1 break ;
        image_filter   resize  100 100;
        error_page     415   = /empty;
    }
  • 最後開啓nginx,這樣訪問/simg/目錄下的圖片,都會按照高度最高100而且寬度最高100按照原圖比例進行截取出來,並輸出給瀏覽器。 固然也能夠開啓重寫去讀取本機另外一個目錄下源文件;若是不在一臺機器上就能夠開啓proxy_pass,並加上重寫便可。 http_image_filter_module支持5種指令: image_filter:測試圖片文件合法性(image_filter test);3個角度旋轉圖片(image_filter rotate 90 | 180 | 270);以json格式輸出圖片寬度、高度、類型(image_filter size);最小邊縮小圖片保持圖片完整性(resize width height);以及最大邊縮放圖片後截取多餘的部分(image_filter crop [width] [height]); image_filter_jpeg_quality:設置jpeg圖片的壓縮質量比例(官方最高建議設置到95,但平時75就能夠了); image_filter_buffer:限制圖片最大讀取大小,默認爲1M; image_filter_transparency:用來禁用gif和palette-based的png圖片的透明度,以此來提升圖片質量。 image_filter_sharpen:這個指令在nginx-1.1.8和1.0.11版本後增長的,目前還不知道是幹啥用,有知道的朋友或者發現文中有什麼錯誤的朋友請留言給我吧^^
相關文章
相關標籤/搜索