天天看點

FastDFS配合Nginx服務的安裝配置1、FastDFS簡介2、安裝3、啟動FDFS4、測試上傳5、配置fastdfs-nginx-module6、安裝Nginx7、重新開機所有服務

文章目錄

  • 1、FastDFS簡介
  • 2、安裝
    • 2.1、擷取安裝包
    • 2.2、安裝FastDFS依賴庫
    • 2.3、安裝 fastdfs
    • 2.4、修改配置
      • 2.4.1、建立項目目錄
      • 2.4.2、tracker.conf
      • 2.4.3、storage.conf
      • 2.4.4、client.conf
      • 2.4.5、mod_fastdfs.conf
  • 3、啟動FDFS
  • 4、測試上傳
  • 5、配置fastdfs-nginx-module
  • 6、安裝Nginx
  • 7、重新開機所有服務

1、FastDFS簡介

  • FastDFS是一個開源的輕量級分布式檔案系統,由跟蹤伺服器(tracker server)、存儲伺服器(storage server)和用戶端(client)

    三個部分組成,主要解決了海量資料存儲問題,特别适合以中小檔案(建議範圍:4KB < file_size <500MB)為載體的線上服務。

  • FastDFS是一個輕量級的開源分布式檔案系統
  • 主要解決了大容量的檔案存儲和高并發通路的問題,檔案存取時實作了負載均衡
  • FastDFS實作了軟體方式的RAID,可以使用廉價的IDE硬碟進行存儲
  • 支援存儲伺服器線上擴容,支援相同内容的檔案隻儲存一份,節約磁盤空間

2、安裝

2.1、擷取安裝包

mkdir -p /opt/fdfs-package
cd /opt/fdfs-packge
# 下載下傳安裝包
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -SO libfastcommon.tar.gz
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -SO fastdfs.tar.gz
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -SO fastdfs-nginx-module.tar.gz
# 解壓三個壓縮包
tar xzvf libfastcommon.tar.gz
tar xzvf fastdfs.tar.gz
tar xzvf fastdfs-nginx-module.tar.gz
           

2.2、安裝FastDFS依賴庫

cd libfastcommon-1.0.39
./make.sh
./make.sh install
           

2.3、安裝 fastdfs

cd fastdfs-5.11
./make.sh
./make.sh install
           

安裝好後,程式是在/usr/bin目錄下:

which fdfs_trackerd
# 得到/usr/bin/fdfs_trackerd,說明安裝成功
           

配置檔案是在/etc/fdfs目錄下:

ls /etc/fdfs
client_deploy.conf storage_ids.conf.sample  tracker.conf.sample storage.conf.sample
           

但是這些配置檔案是不全的,而且都是模闆,是以需要從fastdfs包中拷貝過來,并修改配置:

cp /opt/fdfs-package/fastdfs-5.11/conf/* /etc/fdfs
# 進去fastdfs-nginx-module-1.20檔案夾,把mod_fastdfs.conf 也複制到/etc/fdfs
cp /opt/fdfs-package/fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs
           

2.4、修改配置

2.4.1、建立項目目錄

# 建立fdfs資料目錄
mkdir -p /opt/fdfs-basepath/tracker 
mkdir -p /opt/fdfs-basepath/storage
mkdir -p /opt/fdfs-basepath/client
           

2.4.2、tracker.conf

vim /etc/fdfs/tracker.conf
# the tracker server port
port=22122

# the base path to store data and log files
# tracker檔案存放位置,按需修改
base_path=/opt/fdfs-basepath/tracker  

# HTTP port on this tracker server
# 若是預設端口已有服務運作記得更改
http.server_port=6666
           

2.4.3、storage.conf

vim /etc/fdfs/storage.conf
# storage所屬的組
group_name=group1

# the storage server port
port=23000

# the base path to store data and log files
# storage服務存放的位置,按需修改
base_path=/opt/fdfs-basepath/storage

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/opt/fdfs-basepath/storage

# tracker伺服器,雖然是同一台機器上,但是不能寫127.0.0.1。這項配置可以出現一次或多次
tracker_server=IP:22122

# the port of the web server on this storage server
# 填寫nginx的端口号
http.server_port=8888
           

2.4.4、client.conf

vim /etc/fdfs/client.conf
# the base path to store log files
base_path=/opt/fdfs-basepath/client
# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=IP:22122
#HTTP settings
http.tracker_server_port=6666  # 必須和tracker.conf的端口一樣
           

2.4.5、mod_fastdfs.conf

vim /etc/fdfs/mod_fastdfs.conf
# the base path to store log files
base_path=/tmp

# 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=IP: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

# 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------>必須和storage.conf一樣
store_path0=/opt/fdfs-basepath/storage
#store_path1=/home/yuqing/fastdfs1
           
  • 配置過程中有幾點要注意:
    • 確定配置中用到的目錄已經建立了
    • 確定各種配置檔案之間引用的端口一直

3、啟動FDFS

'注意:必須先啟動tracker,再啟動storage'
fdfs_trackerd /etc/fdfs/tracker.conf start
fdfs_storaged /etc/fdfs/storage.conf start
# 如上面指令無法啟動,使用下面兩個
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
           

ps aux | grep fdfs

檢視目前是否運作

ps aux | grep fdfs
ubuntu    6710  0.0  0.0  13772  1096 pts/0    S+   19:23   0:00 grep fdfs
root     26133  0.0  0.1 146412  2304 ?        Sl   May09   0:43 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root     26199  0.0  0.1  82984  3400 ?        Sl   May09   0:25 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
           

netstat -unltp | grep fdfs

檢視目前端口監聽情況

netstat -unltp | grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      26133/fdfs_trackerd 
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      26199/fdfs_storaged
           

4、測試上傳

fdfs_upload_file /etc/fdfs/client.conf /root/test.jpg
# 傳回類似下面的結果,說明配置成功
group1/M00/00/00/CmhbQ12DF9qAcZlDAABdrSqbHGQ055.jpg
           

5、配置fastdfs-nginx-module

  • 安裝依賴包
# 安裝依賴包
yum install -y libxml2 libxml2-dev libxslt-dev libgd-dev libgeoip-dev libpcre3 libpcre3-dev
           
  • 修改fastdfs-nginx-module的config檔案
vim /opt/fdfs-package/fastdfs-nginx-module-1.20/src/config
# 修改以下兩項:
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
           

6、安裝Nginx

  • 擷取Nginx
# 下載下傳nginx
cd
wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar xzvf nginx-1.10.3.tar.gz
           
  • 編譯安裝Nginx
cd nginx-1.10.3
./configure --add-module=/opt/fdfs-package/fastdfs-nginx-module-1.20/src/
make && make install
           
  • 修改nginx配置檔案
cd /usr/local/nginx/conf/
# 備份nginx.conf
mv nginx.conf nginx.conf.bak
# 編寫nginx.conf
vim nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    server {
        listen       8888;
        server_name  localhost;
        location ~/group[0-9]/ {
        ngx_fastdfs_module;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }
    }
    
}                                             
           

7、重新開機所有服務

fdfs_trackerd /etc/fdfs/tracker.conf restart
fdfs_storaged /etc/fdfs/storage.conf restart
nginx -s stop && nginx
           
  • 通路測試
# 上傳圖檔
fdfs_upload_file /etc/fdfs/client.conf /home/kali.jpg
group1/M00/00/00/CmhbQ12EIwqAeiLsAAWZC4LxnZI028.jpg
# 通路
http://IP:8888/group1/M00/00/00/CmhbQ12EIwqAeiLsAAWZC4LxnZI028.jpg
           

繼續閱讀