FastDFS+Nginx實現文件服務器

FastDFS–tracker安裝
tracker和storage使用相同的安裝包,下載地址:http://sourceforge.net/projects/FastDFS/https://github.com/happyfish100/FastDFS(推薦)

本教程下載:FastDFS_v5.05.tar.gz
FastDFS安裝環境:
FastDFS是C語言開發,建議在linux上運行,本教程使用Centos6.4作爲安裝環境。
安裝FastDFS需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:yum install gcc-c++

安裝:libevent;
FastDFS依賴libevent庫,需要安裝:
yum -y install libevent

安裝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下。
要拷貝的文件如下:
這裏寫圖片描述

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/下。
這裏寫圖片描述

配置:
安裝成功後進入/etc/fdfs目錄:
這裏寫圖片描述
拷貝一份新的tracker配置文件:
cp tracker.conf.sample tracker.conf

修改tracker.conf
vi tracker.conf
base_path=/home/yuqing/FastDFS
改爲:
base_path=/home/FastDFS

啓動:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
啓動的日誌顯示先停止5619進程(實際環境不是5619)再啓動,如下圖:
注意:如果沒有顯示上圖要注意是否正常停止原有進程。

設置開機自動啓動:

[root@tracker FastDFS]# vim /etc/rc.d/rc.local

將運行命令行添加進文件:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

FastDFS–storage安裝:(本次測試 是在同一臺centos上操作)
同tracker安裝 libevent (省略)
同tracker安裝 libfastcommon(省略)
同tracker編譯安裝 storage(省略)
storage配置:
安裝成功後進入/etc/fdfs目錄:
這裏寫圖片描述
拷貝一份新的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/FastDFSStorage/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

啓動:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
啓動的日誌顯示先停止8931進程(實際環境不是8931)再啓動,如下圖:
這裏寫圖片描述
注意:如果沒有顯示上圖要注意是否正常停止原有進程。
設置開機自動啓動
[root@storage1 FastDFS]# vim /etc/rc.d/rc.local
將運行命令行添加進文件:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

此時我們可以測試一下,配置是否正確:
使用格式:
/usr/bin/fdfs_test 客戶端配置文件地址 upload 上傳文件
例如:
將/home下的圖片上傳到FastDFS中:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.png

http://192.168.101.3/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png就是文件的下載路徑。
對應storage服務器上的
/home/fastDFSStorage/fdfs_storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件。
由於現在還沒有和nginx整合無法使用http下載。

FastDFS 和nginx整合
在tracker上安裝nginx:
在每個tracker上安裝nginx,的主要目的是做負載均衡及實現高可用。如果只有一臺tracker服務器可以不配置nginx。

在Storage上安裝nginx:
使用FastDFS-nginx-module:
將FastDFS-nginx-module_v1.16.tar.gz傳至/usr/local/下
cd /usr/local
tar -zxvf FastDFS-nginx-module_v1.16.tar.gz
cd FastDFS-nginx-module/src
修改config文件將/usr/local/路徑改爲/usr/
將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(多個tracker配置多行)”

url_have_group_name=true #url中包含group名稱
store_path0=/home/FastDFSSrorage/fdfs_storage #指定文件存儲路徑

將libfdfsclient.so拷貝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/

創建nginx/client目錄
mkdir -p /var/temp/nginx/client

nginx安裝:
添加FastDFS-nginx-module模塊:
進入nginx源碼文件夾下:
使用命令:

./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/

此時在使用 命令nginx:
make
make install

配置nginx:
打開nginx.conf配置文件:

cd /usr/local/nginx/conf  
gedit nginx.conf 

在server節點加入下面的配置

location /group1/M00{
root /home/FastDFSStorage/fdfs_storage/data;
ngx_fastdfs_module;
}

說明:
server_name指定本機ip
location /group1/M00/:group1爲nginx 服務FastDFS的分組名稱,M00是FastDFS自動生成編號,對應store_path0=/home/FastDFSStorage/fdfs_storage,如果FastDFS定義store_path1,這裏就是M01

啓動nginx:

/usr/local/nginx/sbin/nginx  

用如下命令查看nginx是否啓動了:

netstat -tupln | grep nginx  

輸出如下(nginx默認監聽的是80端口):

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9342/nginx
查看日誌文件:

gedit /usr/local/nginx/logs/error.log   

輸出如下:

[2017-1-23 19:29:43] ERROR - file: ../storage/trunk_mgr/trunk_shared.c, line: 177,”Permission denied” can’t be accessed, error info: /home/zq/fastdfs
2015/12/23 19:29:43 [alert] 9094#0: worker process 9095 exited with fatal code 2 and cannot be respawned
2015/12/23 19:30:58 [notice] 9112#0: signal process started
ngx_http_fastdfs_process_init pid=9196
發現前面報錯了(用紅色標註的部分),意思是說/home/hadoop/fastDFS文件夾不能訪問,遇到這個問題首先想到的是權限不夠,但是當前我是以root賬號啓動的啊,應該擁有最高的權限,爲什麼說我root卻不能訪問這個文件了,這個問題確實把我搞暈了,有可能nginx啓動的後臺線程使用的不是root用戶,後來把mod_fastdfs.conf文件的base_path和store_path0改爲/home/fastDFS,發現啓動不報錯了,這太奇怪了,這個問題折騰了我好久,上網也沒找到解決的辦法。後來發現nginx.conf配置的最前面裏有配置user,只不過被註釋掉了,把user改爲root就搞定了;

最後,測試配置的nginx:
這裏寫圖片描述

PS:今天狀態 不太對,安裝了半天的服務器~ 可能寫的有點差;不喜勿噴

發佈了62 篇原創文章 · 獲贊 60 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章