Manjaro/Arch/Linux 安裝 FastDFS 並集成 Nginx 教程

前言

博主發現很多博文沒有介紹開發者信息,本人習慣加上。FastDFS開發者爲happyfish100(真名不清楚),目前在github上發佈公開源碼,地址爲:https://github.com/happyfish100。本文用到的作者的作品有:

fastdfs、libfastcommon、fastdfs-nginx-module。

博主電腦是Manjaro 18.0.4 ,內核4.19.62-1-Manjaro。最近想要配置一個分佈式的文件存儲服務,相中了FastDFS,然而在網上找到的都是用CentOS或者Ubuntu的安裝經驗,沒有Manjaro的,遂自行挖坑。

索性結局完美,經對比其他系統安裝經驗,發現Manjaro安裝更簡單些,因爲不需要再額外導入任何的第三方依賴包!

FastDFS是用c語言編寫的一款開源的分佈式文件系統。FastDFS爲互聯網量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用FastDFS很容易搭建一套高性能的文件服務器集羣提供文件上傳、下載等服務。

關於FastDFS的架構和能力介紹網上有很多,本文重點是安裝測試,架構這裏不再贅述。想要了解架構這方面概念的請先請參考:https://blog.csdn.net/kamroselee/article/details/80334621

準備

博主發現各大博文教程都是直接下載的FastDFS歷史源碼,然後做編譯安裝,這本身沒毛病,但是這個方案當時可能可行,隨着原項目開發人員的更新和迭代,如果按照固定地址下載源碼的方式安裝,某些部件可能已過時。尤其是博主自己安裝的時候,總是發現libfastcommon這個官方必要的依賴庫不可用。這個包和fastdfs版本是相對應的,在作者github中,fastdfs項目History文件上聲明瞭:correct CRC32, you must upgrade libfastcommon to V1.38 or later,這導致博主在下載其他博主的libfastcommon時編譯不通過。

博主推薦個不會隨着時間流逝導致這篇博文過時的方式,原作者提供的也僅僅是源代碼,需要編譯,沒有編譯好的。

  1. 下載FastDFS最新源碼:https://github.com/happyfish100/fastdfs.git
  2. 下載libfastcommon最新源碼:https://github.com/happyfish100/libfastcommon.git
  3. 下載fastdfs-nginx-module最新源碼:https://github.com/happyfish100/fastdfs-nginx-module.git
  4. 下載nginx最新源碼:http://nginx.org/en/download.html
  5. 把它們放在同一個目錄下,例如:/home/gaoxiang/fastdfs(實際上放哪兒都行,只要你記得路徑,這裏圖方便)
  6. 解壓全部安裝包。
  7. 切換到root賬戶。(普通賬戶編譯可以,安裝不行。尤其是最後的nginx,要配置user爲root。    -----寫給linux小白。)

安裝、配置

我安裝的時候沒有遇到系統缺少包的情況,推薦各位安裝前更新到最新版本。

在 /home/gaoxiang/fastdfs 下提前創建兩個目錄fdfs-data 和 fdfs-storage。(可以放別處,系統允許範圍內隨意)

編譯安裝 libfastcommon 

  1. 進入 libfastcommon 源碼目錄
  2. 執行 ./make.sh 進行編譯
  3. 執行 ./make.sh install 進行安裝

編譯安裝 FastDFS

  1. 進入 fastdfs 源碼目錄
  2. 執行 ./make.sh 進行編譯
  3. 執行 ./make.sh install 進行安裝
  4. 安裝完成後可執行 ls -la /usr/bin/fdfs* 查看安裝的可執行命令有哪些。

配置 FastDFS Tracker 服務

  1. 進入 /etc/fdfs 目錄,有三個後綴爲 .sample 的配置文件,拷貝三分,去掉後綴做正式配置文件。
  2. 編輯 tracker.conf 文件,修改以下內容:
  3. 更改:base_path=/home/gaoxiang/fastdfs/fdfs-data #tracker存儲data和log的跟路徑,必須提前創建好 
  4. 端口 port(默認:22122) 和 綁定地址 bind_addr 不用寫,保持默認即可,保存文件。
  5. 啓動 tracker :/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start  (命令在/usr/bin/下,一般不需要輸入絕對路徑)

配置 FastDFS Storage 服務

  1. 進入 /etc/fdfs 目錄,編輯 storage.conf 文件,並修改以下內容
  2. 更改:base_path=/home/gaoxiang/fastdfs/fdfs-data
  3. 更改:store_path0=/home/gaoxiang/fastdfs/fdfs-storage
  4. 更改:tracker_server=你的IP:22122 #如果tracker在其他服務器,則填寫其它服務器的IP,當然,原理上不推薦這麼幹。
  5. 端口 port(默認:23000) 和 綁定地址 bind_addr 不用寫,保持默認即可,保存文件。
  6. 啓動 storage :fdfs_storaged /etc/fdfs/storage.conf start  (命令在/usr/bin/下,一般不需要輸入絕對路徑)

檢查 storage 和 tracker 的綁定情況

  1. 執行:/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
  2. 查看系統打印,Group1 下 找到 Storage 1 : 後面 ip_addr 的最後如果是 ACTIVE ,則綁定成功

測試 FastDFS 部署是否可用

  1. 進入 /etc/fdfs 目錄,編輯 client.conf,並修改以下內容:
  2. 更改:base_path=/home/gaoxiang/fastdfs/fdfs-data
  3. 更改:tracker_server=你的IP:22122 
  4. 其它保持默認,保存文件。
  5. 執行:fdfs_test /etc/fdfs/client.conf upload test1.jpeg  (看好中間的upload參數別忘了,圖片自定義路徑)
  6. 會打印上傳結果和文件路徑,如:group1/M00/00/00/wKgBI11JMmmANd.jpeg 
  7. 路徑中group1代表組名, M00 代表磁盤,00/00 是目錄,wKgBI11JMmmANd.jpeg 是隨機生成的文件名稱。
  8. 此時在web中還無法訪問,不過在 fdfs_storage 目錄中可以看到這張圖片了。安裝nginx模塊後可訪問。

整合編譯 Nginx 和 fastdfs-nginx-module 模塊

  1. 進入 nginx 源碼路徑
  2. 配置加載 fastdfs-nginx-module 模塊 :./configure --add-module=~/fastdfs/fastdfs-nginx-module/src/(換成自己路徑)
  3. 執行 make 編譯
  4. 執行 make install 安裝

配置 Nginx 服務

  1. 拷貝 fastdfs-nginx-module/src 目錄下的 mod_fastdfs.conf 到 /etc/fdfs 下
  2. 進入 /etc/fdfs 目錄,編輯 mod_fastdfs.conf,並修改以下內容:
  3. 更改:base_path=/home/gaoxiang/fastdfs/fdfs-data
  4. 更改:tracker_server=你的IP:22122 
  5. 更改:storage_server_port=23000
  6. 更改:url_have_group_name=true
  7. 更改:store_path0=/home/gaoxiang/fastdfs/fdfs_storage
  8. 其它默認,保存文件。

配置 Nginx 增加 fastdfs 模塊

拷貝 fastdfs/conf (源碼中)下的所有配置文件到 /etc/fdfs 下

進入 /usr/local/nginx/conf 目錄下,更改nginx.conf 文件如下:

在server 80 端口下添加 

location ~/group([0-9])/M00 {
    ngx_fastdfs_module;
}

文件的最上方要添加上 user root; (這個坑使我啓動失敗多次)

完整代碼如下:


user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    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 ~/group([0-9])/M00{
            ngx_fastdfs_module;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       
    }

}

啓動 Nginx,執行:/usr/local/nginx/nginx

如出現 ngx_http_fastdfs_set pid=** 字樣,則啓動成功。

瀏覽器訪問:http://localhost/group1/M00/00/00/wKgBI11JMmmANd.jpeg 可查看剛開始上傳的圖片

至此 服務搭建完成 後續如果有需要會再寫一篇 spring cloud 集成 fastdfs 的文章。

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