天天看點

FastDFS+nginx+php的完整應用[轉儲]

FastDFS功能簡介:

FastDFS是一個開源的輕量級分布式檔案系統,它對檔案進行管理,功能包括:檔案存儲、檔案同步、檔案通路(檔案上傳、檔案下載下傳)等,解決了大容量存儲和負載均衡的問題。特别适合以檔案為載體的線上服務,如相冊網站、視訊網站等等。

首頁位址:http://code.google.com/p/fastdfs/

    #目前據說這上面已停止更新

FastDFS特點:

 FastDFS主要解決了大容量的檔案(主要是圖檔、視訊、音頻等小檔案)存儲和高并發通路的問題,并在檔案存取時實作了負載均衡。與其它類GFS系統相比,FastDFS最大的特點在于它是一個輕量級的系統,展現在以下幾個方面。

首先,FastDFS的結構比較簡單,主要由Client、Tracker

server和Storage server三部分組成。Client通過Tracker server得到Storage

server的資訊,然後直接與Storage server通信通路檔案,避免了Tracker server成為瓶頸。

 第二,FastDFS不對檔案進行分塊存儲,與支援檔案分塊存儲的DFS相比,更加簡潔高效。

 第三,FastDFS中的檔案ID是由Storage

server生成後傳回給用戶端的。檔案ID中包含了組号、檔案相對路徑和檔案名等(檔案ID中還包含檔案大小、時間戳、源Storage server

IP位址、檔案内容校驗碼、随機數等),client可以根據檔案ID直接定位到檔案所在的組(但具體通過哪個Storage server下載下傳需要詢問Tracker

server根據負載均衡原則指定)。是以FastDFS不需要存儲檔案索引資訊。而其他檔案系統則通常需要由NameServer存儲檔案索引資訊,如mogileFS采用MySQL資料庫來存儲檔案索引以及系統相關的資訊,而MySQL很容易成為系統瓶頸。

FastDFS特性:

檔案不分塊存儲,上傳的檔案和OS檔案系統中的檔案一一對應

支援相同内容的檔案隻儲存一份,節約磁盤空間

下載下傳檔案支援HTTP協定,可以使用内置Web

Server,也可以和其他Web Server配合使用

支援線上擴容

支援主從檔案

存儲伺服器上可以儲存檔案屬性(meta-data)V2.0網絡通信采用libevent,支援大并發通路,整體性能更好

FastDFS架構示意圖:

檔案上傳流程:

1. Client詢問Tracker

server應上傳到哪個Storage server;

2. Tracker

server傳回一台可用的Storage server,傳回的資料為該Storage server的IP位址和端口;

3. Client直接和該Storage

server建立連接配接,進行檔案上傳。

4. Storage

server傳回新生成的檔案ID,檔案上傳結束。

5. 連接配接關閉。

檔案下載下傳流程:

1.

client詢問tracker可以下載下傳指定檔案的storage,參數為檔案ID(組名和檔案名);

2.

tracker傳回一台可用的storage;

3.

client直接和storage通信完成檔案下載下傳。

4. 連接配接關閉。

FastDFS論壇:http://bbs.chinaunix.net/forum-240-1.html

FastDFS google

code:http://code.google.com/p/fastdfs/

關于tracker server 和 storage

server之間是如何互動的,請看我在附件中的PPT、以及PDF格式的電子書,還有一個是關于配置檔案的參數以及fastdfs各項參數是如何調優的,都有詳細的說明,下面就讓我們開始配置過程吧!

配置環境:

CentOS 5.8 x 64

tracker server:

192.168.25.204

storage server:

192.168.25.207

php 用戶端  

 :192.168.25.205

一、安裝部署

1、配置tracker server

解決依賴關系

<code>1.</code><code>[root@varnish ~]</code><code># tar xf libevent-2.0.21-stable.tar.gz</code>

<code>2.</code><code>[root@varnish ~]</code><code># cd libevent-2.0.21-stable</code>

<code>3.</code><code>[root@varnish libevent-2.0.21-stable]</code><code># ./configure --prefix=/usr/local/libevent</code>

<code>4.</code><code>[root@varnish libevent-2.0.21-stable]</code><code># make</code>

<code>5.</code><code>[root@varnish libevent-2.0.21-stable]</code><code># make install</code>

<code>6.</code><code>[root@varnish libevent-2.0.21-stable]</code><code># echo ‘/usr/local/libevent/include/‘ &gt;&gt; /etc/ld.so.conf</code>

<code>7.</code><code>[root@varnish libevent-2.0.21-stable]</code><code># echo ‘/usr/local/libevent/lib/‘ &gt;&gt; /etc/ld.so.conf</code>

<code>8.</code><code>[root@varnish libevent-2.0.21-stable]</code><code># ldconfig</code>

安裝fastdfs

注意:我們這裡使用的是最新版本的fastdfs,由于最新版的fastdfs取消了自帶的http功能,是以這裡我們就不對make.sh做修改。

<code>1.</code><code>[root@varnish ~]</code><code># tar xf FastDFS_v4.07.tar.gz</code>

<code>2.</code><code>[root@varnish ~]</code><code># cd FastDFS</code>

<code>3.</code><code>[root@varnish FastDFS]</code><code># ./make.sh C_INCLUDE_PATH=/usr/local/libevent/include/ LIBRARY_PATH=/usr/local/libevent/lib/</code>

<code>4.</code><code>[root@varnish FastDFS]</code><code># ./make.sh install</code>

<code>5.</code><code>[root@varnish FastDFS]</code><code># ls /etc/fdfs/</code>

<code>6.</code><code>client.conf  http.conf  mime.types  storage.conf  tracker.conf</code>

配置檔案解釋:

tracker.conf   //負責均衡排程伺服器配置檔案

client.conf    

 //用戶端上傳配置檔案

http.conf    

//http伺服器配置檔案

storage.conf//檔案存儲伺服器配置檔案

mime.types   //檔案類型配置檔案

修改tracker

server的配置檔案

<code>1.</code><code>[root@varnish ~]</code><code># vim /etc/fdfs/tracker.conf</code>

<code>2.</code><code>bind_addr= 改為 bind_addr=192.168.25.204</code>

<code>3.</code><code>base_path=/home/yuqing/fastdfs 改為  base_path=/data/fastdfs</code>

<code>4.</code><code>http.server_port=8080 改為 http.server_port=80</code>

建立/data/fastdfs,用于存放tracker日志、storage

server等資訊

<code>1.</code><code>[root@varnish ~]</code><code># mkdir -p /data/fastdfs</code>

<code>2.</code><code>啟動tracker server</code>

<code>3.</code><code>[root@varnish ~]</code><code># /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf</code>

2、配置storage server

上述安裝過程是一樣的這裡就不在進行示範,請大家見諒!

<code>1.</code><code>修改storage server的配置檔案:</code>

<code>2.</code><code>[root@web2 ~]</code><code># vim /etc/fdfs/storage.conf</code>

<code>3.</code><code>bind_addr= 改為 bind_addr=192.168.25.207</code>

<code>4.</code><code>base_path=/home/yuqing/fastdfs 改為 base_path=/data/fastdfs</code>

<code>5.</code><code>store_path0=/home/yuqing/fastdfs 改為 store_path0=/data/fastdfs</code>

<code>6.</code><code>tracker_server=192.168.209.121:22122 改為 tracker_server=192.168.25.204:22122</code>

<code>7.</code><code>http.server_port=8888 改為 http.server_port=80</code>

建立資料目錄,并啟動storage

server(有刷屏的感覺)

檢視tracker和storage的連結情況:

3、測試上傳檔案

我們這裡在tracker

server上配置用戶端上傳檔案

<code>1.</code><code>[root@varnish ~]</code><code># vim /etc/fdfs/client.conf</code>

<code>2.</code><code>base_path=/home/yuqing/fastdfs 改為 base_path=/data/fastdfs</code>

<code>3.</code><code>tracker_server=192.168.0.197:22122</code>

<code>4.</code><code>改為</code>

<code>5.</code><code>tracker_server=192.168.25.204:22122</code>

上傳個檔案,如果上傳成功,會傳回一個url,我們記住這個url,等下要用到

http://192.168.25.207/group1/M00/00/00/wKgZz1IEYnSASqs2AAAGgpr6pLY3507236_big

二、在storage

server上安裝nginx

1、安裝nginx關于fastdfs集合的擴充子產品,注意這個子產品nginx預設的編譯子產品中沒有,需手動下載下傳安裝

<code>1.</code><code>[root@web2 ~]</code><code># tar xf fastdfs-nginx-module_v1.15.tar.gz</code>

<code>2.</code><code>[root@web2 ~]</code><code># cd fastdfs-nginx-module</code>

隻需解壓開就可,這裡我們再解釋一下,好些朋友,在加載這個子產品的時候,會報一些很莫名其妙的錯,這個本人在編譯的時候也是親身體會,百思不得其解,後來經過點播才發現,版本不比對,這裡fastdfs官網給有詳細說明,請看子產品解壓目錄下的HISTORY、INSTALL問件,這裡面很詳細的安裝和版本比對說明(吃一塹一定要長一智)

安裝nginx之前需要解決很多依賴關系的,這裡不做說明,我這裡是安裝過得了,就不貼出來了,請大家按照網上的教程,解決依賴關系!

<code>01.</code><code>[root@web2 ~]</code><code># tar xf nginx-1.4.1.tar.gz</code>

<code>02.</code><code>[root@web2 ~]</code><code># cd nginx-1.4.1</code>

<code>03.</code><code>[root@web2 nginx-1.4.1]</code><code># ./configure \</code>

<code>04.</code><code>&amp;</code><code>gt</code><code>;   --prefix=/usr \</code>

<code>05.</code><code>&amp;</code><code>gt</code><code>;   --sbin-path=/usr/sbin/nginx \</code>

<code>06.</code><code>&amp;</code><code>gt</code><code>;   --conf-path=/etc/nginx/nginx.conf \</code>

<code>07.</code><code>&amp;</code><code>gt</code><code>;   --error-log-path=/var/log/nginx/error.log \</code>

<code>08.</code><code>&amp;</code><code>gt</code><code>;   --http-log-path=/var/log/nginx/access.log \</code>

<code>09.</code><code>&amp;</code><code>gt</code><code>;   --pid-path=/var/run/nginx/nginx.pid  \</code>

<code>10.</code><code>&amp;</code><code>gt</code><code>;   --lock-path=/var/lock/nginx.lock \</code>

<code>11.</code><code>&amp;</code><code>gt</code><code>;   --user=nginx \</code>

<code>12.</code><code>&amp;</code><code>gt</code><code>;   --group=nginx \</code>

<code>13.</code><code>&amp;</code><code>gt</code><code>;   --with-http_ssl_module \</code>

<code>14.</code><code>&amp;</code><code>gt</code><code>;   --with-http_flv_module \</code>

<code>15.</code><code>&amp;</code><code>gt</code><code>;   --with-http_stub_status_module \</code>

<code>16.</code><code>&amp;</code><code>gt</code><code>;   --with-http_gzip_static_module \</code>

<code>17.</code><code>&amp;</code><code>gt</code><code>;   --http-client-body-temp-path=/var/tmp/nginx/client/ \</code>

<code>18.</code><code>&amp;</code><code>gt</code><code>;   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \</code>

<code>19.</code><code>&amp;</code><code>gt</code><code>;   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \</code>

<code>20.</code><code>&amp;</code><code>gt</code><code>;   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \</code>

<code>21.</code><code>&amp;</code><code>gt</code><code>;   --http-scgi-temp-path=/var/tmp/nginx/scgi \</code>

<code>22.</code><code>&amp;</code><code>gt</code><code>;   --with-pcre \</code>

<code>23.</code><code>&amp;</code><code>gt</code><code>;   --add-module=/root/fastdfs-nginx-module/src</code>

<code>24.</code><code>[root@web2 nginx-1.4.1]</code><code># make</code>

<code>25.</code><code>[root@web2 nginx-1.4.1]</code><code># make install</code>

<code>1.</code><code>[root@web2 ~]</code><code># cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/</code>

<code>2.</code><code>[root@web2 ~]</code><code># vim /etc/fdfs/mod_fastdfs.conf</code>

<code>3.</code><code>base_path=/tmp 改為 base_path=/data/fastdfs</code>

<code>4.</code><code>tracker_server=tracker:22122 改為 tracker_server=192.168.25.204:22122</code>

[root@web2 ~]# ln -s

/data/fastdfs/data /data/fastdfs/data/M00

修改nginx的配置檔案

<code>01.</code><code>[root@web2 ~]</code><code># vim /etc/nginx/nginx.conf</code>

<code>02.</code><code>在server段添加如下内容:</code>

<code>03.</code><code>location /group1/M00{</code>

<code>04.</code><code>alias</code>   <code>/data/fastdfs/data;</code>

<code>05.</code><code>ngx_fastdfs_module;</code>

<code>06.</code><code>}</code>

<code>07.</code><code>[root@web2 ~]</code><code># /usr/sbin/nginx -t</code>

<code>08.</code><code>ngx_http_fastdfs_set pid=28145</code>

<code>09.</code><code>nginx: the configuration </code><code>file</code> <code>/etc/nginx/nginx.conf syntax is ok</code>

<code>10.</code><code>nginx: configuration </code><code>file</code> <code>/etc/nginx/nginx.conf </code><code>test</code> <code>is successful</code>

<code>11.</code><code>[root@web2 ~]</code><code># /usr/sbin/nginx</code>

<code>12.</code><code>ngx_http_fastdfs_set pid=28148</code>

然後我們在浏覽器中輸入上邊上傳的url,通路一下:

我們可以很清楚的看到400,Bad

Request,那麼我們換種通路方式試試

<code>1.</code><code>修改nginx的配置檔案</code>

<code>2.</code><code>[root@web2 ~]</code><code># vim /etc/nginx/nginx.conf</code>

<code>3.</code><code>location /M00{</code>

<code>4.</code><code>alias</code>   <code>/data/fastdfs/data;</code>

<code>5.</code><code>ngx_fastdfs_module;</code>

<code>6.</code><code>}</code>

<code>1.</code><code>之後重新開機服務</code>

<code>2.</code><code>[root@web2 ~]</code><code># /usr/sbin/nginx -s stop</code>

<code>3.</code><code>ngx_http_fastdfs_set pid=28947</code>

<code>4.</code><code>[root@web2 ~]</code><code># /usr/sbin/nginx</code>

<code>5.</code><code>ngx_http_fastdfs_set pid=28948</code>

然後我們把url中的group1手動去掉,通路一下:

很明顯可以通路,我在配置的時候也是百思不得其解,網上也有此類問題,無意間打開了一個相關的文章,終于讓我找到了答案。

<code>1.</code><code>我們把nginx的配置檔案修改回來</code>

<code>2.</code><code>location /group1/M00{</code>

<code>3.</code><code>alias</code>   <code>/data/fastdfs/data;</code>

<code>4.</code><code>ngx_fastdfs_module;</code>

<code>5.</code><code>}</code>

修改fastdfs的子產品配置檔案

<code>1.</code><code>[root@web2 ~]</code><code># vim /etc/fdfs/mod_fastdfs.conf</code>

<code>2.</code><code>url_have_group_name = </code><code>false</code> <code>改為 </code><code>true</code>      <code>#關于啥意思,配置檔案中有解釋</code>

然後我們再來通路一下:

如上圖所示,ok了,加上組名可以通路,到這裡基本上已經配置完成了

但是我在網上搜解決上述解決方法時,看到了一篇博文,結合了php,我覺得有必要分享一下,那好繼續我們苦逼的配置吧!

三、PHP用戶端配置

因為php的用戶端安裝也會依賴fastdfs本身的一些庫檔案,是以請保證已經安裝了fastdfs

安裝過程和上邊一樣,這裡參考上邊安裝過程!

這個php用戶端所在的測試機本來我就在上邊搭建好了LNMP,是以隻需參考上述過程安裝fastdfs即可!

<code>01.</code><code>[root@web1 ~]</code><code># cd /root/FastDFS/php_client</code>

<code>02.</code><code>[root@web1 php_client]</code><code># /usr/local/php/bin/phpize</code>

<code>03.</code><code>Configuring </code><code>for</code><code>:</code>

<code>04.</code><code>PHP Api Version:         20090626</code>

<code>05.</code><code>Zend Module Api No:      20090626</code>

<code>06.</code><code>Zend Extension Api No:   220090626</code>

<code>07.</code><code>[root@web1 php_client]</code><code># ./configure --with-php-config=/usr/local/php/bin/php-config</code>

<code>08.</code><code>[root@web1 php_client]</code><code># make &amp;&amp; make install</code>

<code>09.</code><code>[root@web1 php_client]</code><code># cat fastdfs_client.ini &gt;&gt; /usr/local/php/etc/p</code>

<code>10.</code><code>pear.conf             php-fpm.conf          php-fpm.conf.default</code>

<code>11.</code><code>[root@web1 php_client]</code><code># cat fastdfs_client.ini &gt;&gt; /etc/php.ini</code>

驗證子產品是否被正常加載

<code>1.</code><code>[root@web1 php_client]</code><code># /usr/local/php/bin/php -m | grep fastdfs_client</code>

<code>2.</code><code>fastdfs_client              </code><code>#說明子產品已加載 www.it165.net</code>

<code>3.</code> 

<code>4.</code><code>重新加載php</code>

<code>5.</code><code>[root@web1 php_client]</code><code># service php-fpm reload</code>

修改為如下配置:

<code>1.</code><code>tracker_server=192.168.25.204:22122</code>

<code>2.</code><code>http.tracker_server_port=80</code>

然後我們寫個小頁面,寫個php腳本上傳

<code>1.</code><code>[root@web1 ~]</code><code># echo "php client is here." &gt; /opt/upload.txt</code>

<code>2.</code><code>[root@web1 ~]</code><code># vim test.php</code>

<code>3.</code><code>&amp;</code><code>lt</code><code>;?php</code>

<code>4.</code><code>var_dump(function_exists(‘fastdfs_storage_upload_by_filename‘));</code>

<code>5.</code><code>$ret = fastdfs_storage_upload_by_filename(‘/opt/upload.txt‘);</code>

<code>6.</code><code>var_dump($ret);</code>

<code>7.</code><code>?&amp;</code><code>gt</code><code>;</code>

執行php腳本

[root@web1 ~]#

/usr/local/php/bin/php test.php

結果如下圖所示:

我們來通路下試試;

到此為止我們關于fastdfs的完整應用就介紹到此了,希望大家都動手做一下!還有關于fastdfs

php插件的使用方法和函數調用方法,可以看/root/FastDFS/php_client/README,裡面有詳細介紹!此篇博文過程中如果有什麼不對的地方,還請朋友們們指正,大家一起進步!

http://blog.chinaunix.net/uid-27003384-id-4059897.html