天天看點

centos7-安裝FastDFS1 FastDFS 安裝

1 FastDFS 安裝

1.1 安裝 VMware 虛拟機

安裝 CentOS7 虛拟機。

 1.2 FastDFS--tracker 安裝

分别在 192.168.101.3 和 192.168.101.4 上安裝 tracker。

注:初次安裝可隻安裝一台 tracker,快速體驗。

1.2.1 下載下傳

tracker         和        storage         使     用     相     同     的     安     裝     包     ,     下     載     地     址       :

https://github.com/happyfish100/FastDFS

 本教程下載下傳:FastDFS_v5.05.tar.gz

1.2.2 FastDFS 安裝環境

 FastDFS 是C語言開發,建議在linux上運作,本教程使用Centos7作為安裝環境。

 安裝 FastDFS 需要先将官網下載下傳的源碼進行編譯,編譯依賴gcc環境,如果

沒有gcc環境,需要安裝gcc:yum install gcc-c++

1.2.3 安裝 libevent

FastDFS 依賴 libevent 庫,需要安裝:

yum -y install libevent

1.2.4 安裝 libfastcommon

libfastcommon 是 FastDFS 官方提供的,libfastcommon 包含了 FastDFS 運作所需

要的一些基礎庫。

将 libfastcommonV1.0.7.tar.gz 拷貝至/usr/local/下

cd /usr/local tar -zxvf libfastcommonV1.0.7.tar.gz cd libfastcommon-1.0.7

./make.sh

./make.sh install

注意:

libfastcommon 安裝好後會自動将庫檔案拷貝至/usr/lib64 下,

由于 FastDFS 程式引用 usr/lib 目錄是以需要将/usr/lib64 下的庫檔案拷貝至/usr/lib 下。

要拷貝的檔案如下:

centos7-安裝FastDFS1 FastDFS 安裝

1.2.5 tracker 編譯安裝

将 FastDFS_v5.05.tar.gz 拷貝至/usr/local/下

tar -zxvf FastDFS_v5.05.tar.gz cd FastDFS

./make.sh         編譯

./make.sh install  安裝

安裝成功将安裝目錄下的 conf 下的檔案拷貝到/etc/fdfs/下。
centos7-安裝FastDFS1 FastDFS 安裝

1.2.6 配置

安裝成功後進入/etc/fdfs 目錄:
centos7-安裝FastDFS1 FastDFS 安裝

拷貝一份新的 tracker 配置檔案:

cp   tracker.conf.sample   tracker.conf   

修改 tracker.conf

vi tracker.conf

base_path=/home/yuqing/FastDFS   

改為:

base_path=/home/FastDFS

配置 http 端口:

http.server_port=80

1.2.7 啟動

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

啟動的日志顯示先停止 5619 程序(實際環境不是 5619)再啟動,如下圖:

centos7-安裝FastDFS1 FastDFS 安裝
注意:如果沒有顯示上圖要注意是否正常停止原有程序。(第一次啟動不顯示 exit...,第二次啟動才顯示)

1.3 FastDFS--storage 安裝

分别在 192.168.101.5、192.168.101.6、192.168.101.7、192.168.101.8 上安裝 storage。

注:初次安裝可隻安裝一台 storage,快速體驗。

1.3.1 安裝 libevent

同 tracker 安裝

1.3.2 安裝 libfastcommon

同 tracker 安裝。

1.3.3 storage 編譯安裝

同 tracker 編譯安裝。

1.3.4 配置

安裝成功後進入/etc/fdfs 目錄:

centos7-安裝FastDFS1 FastDFS 安裝

拷貝一份新的 storage 配置檔案:

cp  storage.conf.sample  storage.conf

修改 storage.conf

vi storage.conf

group_name=group1

 base_path=/home/yuqing/FastDFS

改為:base_path=/home/ fastdfs

store_path0=/home/yuqing/FastDFS

改為:store_path0=/home/fastdfs/fdfs_storage

#如果有多個挂載磁盤則定義多個 store_path,如下

#store_path1=.....

#store_path2=......

tracker_server=192.168.101.3:22122   #配置 tracker 伺服器:IP

#如果有多個則配置多個 tracker

tracker_server=192.168.101.4:22122

#配置 http 端口

http.server_port=80

1.3.5 啟動

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
啟動的日志顯示先停止 8931 程序(實際環境不是 8931)再啟動,如下圖:
centos7-安裝FastDFS1 FastDFS 安裝
注意:如果沒有顯示上圖要注意是否正常停止原有程序。(第一次啟動不顯示exit...,我第一次就被誤導了,第二次啟動才有)

1.4 上傳圖檔測試

1.4.1 通過 fdfs_test 程式

FastDFS 安裝成功可通過/usr/bin/fdfs_test 程式來測試上傳、下載下傳等操作。

修改/etc/fdfs/client.conf

tracker_server 根據自己部署虛拟機的情況配置 。

base_path=/home/fastdfs

tracker_server=192.168.101.3:22122

tracker_server=192.168.101.4:22122

使用格式:

/usr/bin/fdfs_test 用戶端配置檔案位址  upload  上傳檔案

比如将/home 下的圖檔上傳到 FastDFS 中:

/usr/bin/fdfs_test /etc/fdfs/client.conf    upload     /home/tomcat.png

列印如下日志:

This is FastDFS client test program v5.05

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.

[2015-05-18 02:07:10] DEBUG - base_path=/home/FastDFS, 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.101.5, port=23000     server 2. group_name=, ip_addr=192.168.101.6, port=23000

group_name=group1, ip_addr=192.168.101.5, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485.png

source ip address: 192.168.101.5 file timestamp=2015-05-18 02:07:11 file size=5103

file crc32=3979441827

example                                                                               file                                                                            url:

http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485.

png storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

source ip address: 192.168.101.5 file timestamp=2015-05-18 02:07:11 file size=5103 file crc32=3979441827

example                                                                               file                                                                            url:

http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_ big.png

http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png 就是檔案的下載下傳路徑。

對應 storage 伺服器上的

/home/fastdfs/fdfs_storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_b ig.png 檔案。

由于現在還沒有和 nginx 整合無法使用 http 下載下傳。

1.5 FastDFS 和 nginx 整合

1.5.1  nginx 代理 (代理到storage伺服器上的nginx)

單獨安裝 nginx 代理服務,它的作用是代理通路 storage 上的檔案,實作負載均衡。 nginx 的安裝細節參考 nginx 文檔,這裡使用單機 nginx,也可以使用兩台 nginx 組成高可用或者采用 lvs+nginx 通路 Storage 上的 nginx。
建立 nginx-fdfs.conf 配置檔案:

events {

    worker_connections  1024;

}

http {  

            #storage群group1組

upstream storage_server_group1{

            server192.168.101.5:80 weight=10;

            server 192.168.101.6:80 weight=10;

}

#storage群group2組

upstream storage_server_group2{

           server 192.168.101.7:80 weight=10;

           server 192.168.101.8:80 weight=10;

}

server {

      listen 80;

      server_name ccc.test.com;

     location /group1{

               proxy_redirect off;

               proxy_set_header Host $host;

               proxy_set_header X-Real-IP $remote_addr;

               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

               proxy_pass http://storage_server_group1;

      }

     location /group2{

             proxy_redirect off;

             proxy_set_header Host $host;

             proxy_set_header X-Real-IP $remote_addr;

             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

             proxy_pass http://storage_server_group2;

     }

   }

}

1.5.2在Storage上安裝nginx

在storage server上安裝nginx的目的是對外通過http通路storage server上的檔案。使用nginx的子產品FastDFS-nginx-module的作用是通過http方式通路storage中

的檔案,當storage本機沒有要找的檔案時向源storage主機代理請求檔案。

1.5.2.1FastDFS-nginx-module

将FastDFS-nginx-module_v1.16.tar.gz傳至fastDFS的storage伺服器的/usr/local/下,執行如下指令:

cd  /usr/local

tar-zxvfFastDFS-nginx-module_v1.16.tar.gz

cd  FastDFS-nginx-module/src

修改config檔案将/usr/local/路徑改為/usr/

centos7-安裝FastDFS1 FastDFS 安裝

将FastDFS-nginx-module/src下的mod_FastDFS.conf拷貝至/etc/fdfs/下

cp  mod_FastDFS.conf  /etc/fdfs/

并修改mod_FastDFS.conf的内容:

vi /etc/fdfs/mod_FastDFS.conf

base_path=/home/FastDFS

tracker_server=192.168.101.3:22122

tracker_server=192.168.101.4:22122

url_have_group_name=true  #url中包含group名稱

store_path0=/home/fastdfs/fdfs_storage    #指定檔案存儲路徑

#如果有多個

将libfdfsclient.so拷貝至/usr/lib下

cp /usr/lib64/libfdfsclient.so /usr/lib/

建立nginx/client目錄

mkdir-p /var/temp/nginx/client

1.5.2.2nginx安裝

将nginx-1.8.0.tar.gz拷貝到/usr/local下

解壓nginx-1.8.0.tar.gz

進入nginx-1.8.0目錄,執行如下配置指令:

下邊紅色部分表示添加FastDFS-nginx-module子產品

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

--http-scgi-temp-path=/var/temp/nginx/scgi \

--add-module=/usr/local/FastDFS-nginx-module/src

make

make install

1.5.2.3 nginx 配置檔案

storage伺服器上的nginx配置,不是圖檔伺服器nginx的配置

建立一個 nginx 配置檔案 nginx-fdfs.conf

添加虛拟主機:

events {

    worker_connections  1024;

}

http {  

 server {        

            listen       80;        

            server_name  192.168.101.65;

           location /group1/M00/{                

                    root /home/FastDFS/fdfs_storage/data;                

                   ngx_FastDFS_module;    #如果說 ngx_FastDFS_module 這個報錯,改為:ngx_fastdfs_module

          }

}

}

說明:

server_name 指定本機 ip

location /group1/M00/:

group1 為 nginx 服務 FastDFS 的分組名稱,

M00 是 FastDFS 自動生成編号,

對應 store_path0=/home/FastDFS/fdfs_storage,如果 FastDFS 定義 store_path1,這裡就是 M01

1、啟動tracker:/usr/bin/fdfs_trackerd      /etc/fdfs/tracker.conf   restart

2、啟動storage:/usr/bin/fdfs_storaged    /etc/fdfs/storage.conf   restart

3、啟動storage上的nginx:/usr/local/nginx/sbin/nginx    ‐c       /usr/local/nginx/conf/nginx‐fdfs.conf

4、啟動圖檔虛拟主機nginx

1.5.3測試

通過java用戶端上傳檔案,使用浏覽器http通路檔案,這裡通路上傳圖檔測試的檔案:

通路storage:

http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

ip位址改為192.168.101.6也可以通路到檔案,因為同一個分組的storage檔案互相同步。

 通路tracker:

http://192.168.101.3/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

ip位址改為192.168.101.4也可以通路到檔案。

使用域名通路(推薦):

nginx對外由vip提供服務,使用域名通路如下:

比如vip對應的域名為img.test.com:

http://img.test.com/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

繼續閱讀