FastDFS系統搭建

手摸手教你搭建FasfDFS分佈式文件系統

因爲正在學習的分佈式項目中用到了FastDFS文件系統,由於各類緣由就只好手動搭建環境。搭建過程確實很複雜,我也是折騰了好長時間才解決的,看了網上的教程,但不多有直接就能搭建成功得博文教程,因此這裏本身寫了一個完成的教程,並附帶了所需的配置文件。html

須要注意的是Linux系統、版本的問題,我這裏使用的是CentOS7的版本,若是你們搭建請儘可能保證版本一致。nginx

<!--more-->c++

起步

一、下載Linux系統(以CentOS爲例)
  [CentOS7-Minimal](http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1804.iso)
  
二、安裝SecureCRT
  由於實際的服務器並不存在桌面,因此咱們安裝Minimal版本的CentOS便可,可是在CentOS的黑窗口中操做並不方便,因此下載SecureCRT操縱服務器。

<br/>git

安裝CentOS

本例中用VMware安裝的CentOS系統,在安裝時須要注意一個問題:github

如上圖所示,在安裝CentOS時不要建立新用戶,設置個ROOT密碼便可,這樣登陸進去系統後默認就是ROOT權限,避免了權限不夠的問題。(登陸用戶名密碼默認都是rootvim

一、登陸系統並配置鏈接網絡centos

輸入命令:cd /etc/sysconfig/network-scripts/ && ls,編輯列表中的第一個文件(由於文件名稱可能不相同)服務器

將最後行的no改爲yes便可網絡

修改點擊鍵盤的i鍵進入編輯模式,修改完成後按下ESC鍵,再輸入:wq!保存退出便可;而後執行命令service network restart重啓服務。分佈式

此時已經完成了CentOS的聯網,輸入命令ip addr查看服務器的IP地址(若是聯網成功,會出現192.168.xx.xx相似這樣的IP地址):

這個IP地址即便此服務器的外網IP,即咱們要用SecureCRT鏈接的服務器的IP

二、使用SecureCRT鏈接服務器

SecureCRT的使用方法不在說了,就是簡單的建立一個鏈接,輸入服務器的IP地址、帳戶、密碼便可完成鏈接。

三、關閉CentOS7的防火牆

做爲咱們測試用的本地服務器,加一個防火牆實在是沒有必要的,因此索性關閉防火牆。 輸入命令:

systemctl stop firewalld.service #中止firewall
systemctl disable firewalld.service #禁止firewall開機啓動
firewall-cmd --state #查看默認防火牆狀態(關閉後顯示notrunning,開啓後顯示running)

<br/>

環境準備

安裝所需命令

[root@localhost ~]# yum install make cmake gcc gcc-c++ 
[root@localhost ~]# yum install vim-enhanced
[root@localhost ~]# yum install zip unzip
[root@localhost ~]# yum install -y pcre pcre-devel
[root@localhost ~]# yum install -y zlib zlib-devel
[root@localhost ~]# yum install -y openssl openssl-devel
[root@localhost ~]# yum install net-tools
[root@localhost ~]# yum install git

<br/>

clone所需的安裝文件

我將所需的安裝文件都上傳到了個人 GitHub 上,你們只須要clone下載便可,這些版本我測試過是可用的。

GitHub地址: https://github.com/TyCoding/build-fastdfs

[root@localhost ~]# git clone https://github.com/TyCoding/build-fastdfs.git
[root@localhost ~]# mv build-fastdfs/* /root/ && rm -rf build-fastdfs
[root@localhost ~]# cd software

<br/>

開始

安裝libfastcommon

libfastcommon-master.zip解壓到/usr/local/fast目錄下,並編譯安裝

[root@localhost software]# unzip libfastcommon-master.zip -d /usr/local/fast/
[root@localhost software]# cd /usr/local/fast/libfastcommon-master/
[root@localhost libfastcommon-master]# ./make.sh && ./make.sh install

因爲libfastcommon安裝好後悔自動將庫文件拷貝至/usr/lib64目錄下,又由於FastDFS程序引用/usr/lib目錄,因此咱們能夠經過建立軟鏈接的方式來解決FastDFS的引用路徑問題,固然經過將/usr/lib64下的庫文件拷貝到/usr/lib下也行。

[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so  
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so 
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so  
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

<br/>

安裝FastDFS

FastDFS_v5.05.tar.gz解壓到/usr/local/fast目錄下

[root@localhost libfastcommon-master]# cd ~/software
[root@localhost software]# tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast/
[root@localhost software]# cd /usr/local/fast/FastDFS/
[root@localhost FastDFS]# ./make.sh && ./make.sh install

由於FastDFS服務器腳本設置的bin目錄爲/usr/local/bin目錄下,可是實際咱們安裝在usr/bin下面,因此咱們要修改FastDFS配置文件中的路徑,須要修改配置文件:/etc/init.d/fdfs_storaged以及/etc/init.d/fdfs_trackerd,修改方式以下: 輸入命令:

[root@localhost FastDFS]# vi /etc/init.d/fdfs_storaged 

--這裏涉及到了vim的語法,進入這個文件的編輯頁面後,直接將輸入法切換至英文狀態,輸入
:%s+/usr/local/bin+/usr/bin

--當輸入完成上面的指令後直接回車會看到:7 substitutions on 7 lines,輸入
:wq!
--保存並退出

必定要注意的是,這裏並非在文件中增長這一行代碼,如上圖,咱們是給vim輸入了一串指令,這個指令的做用是批量修改一些代碼,當回車後會顯示:7 substitutions on 7 lines,而後咱們輸入:wq!保存並退出文件的編輯

一樣的方式,修改/etc/init.d/fdfs_trackerd,輸入命令

[root@localhost FastDFS]# vi /etc/init.d/fdfs_trackerd

:%s+/usr/local/bin+/usr/bin

:wq!

<br/>

配置tracker

進入/etc/fdfs目錄並複製一份tracker.conf.sample並命名爲tracker.conf,並修改tracker.conf,將其中的base_path參數的值修改成/fastdfs/tracker,並使用makir -p /fastdfs/tracker建立兩個目錄

[root@localhost FastDFS]# cd /etc/fdfs && ll
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf
[root@localhost fdfs]# vi tracker.conf

點擊鍵盤的i鍵進入vim的編輯模式,若是編輯完成後要先點擊鍵盤的ESC鍵退出vim的編輯模式再點擊鍵盤的鍵:wq!直接會在左下角寫入

:wq!

[root@localhost fdfs]# mkdir -p /fastdfs/tracker

啓動tracker:

[root@localhost fdfs]# /etc/init.d/fdfs_trackerd start

<br/>

配置storage

進入/etc/fdfs目錄,賦值一份storage.conf.sample並命名爲storage.conf,並修改storage.conf

[root@localhost fdfs]# cp storage.conf.sample storage.conf
[root@localhost fdfs]# vi storage.conf

修改以下幾個參數:

base_path=/fastdfs/storage
store_path0=/fastdfs/storage
tracker_server=192.168.148.131:22122  #這個IP地址是你服務器的IP,輸入命令`ip addr`便可查看到服務器的IP

保存退出並建立/fastdfs/storage目錄

:wq!

[root@localhost fdfs]# mkdir /fastdfs/storage

啓動storage

/etc/init.d/fdfs_storaged start

查看tracker和storage是否啓動成功

ps -ef | grep fdfs

出現如上的顯示才證實tracker和storage都正常啓動了。 注意 這個地方要吐槽一下啦,要是你輸入ps -ef | grep fdfs出現的不是這三行,而是兩行或其餘,就證實確定有一個服務沒有啓動成功,通常就是你的配置有錯誤; 那麼你有下面兩個選擇來解決這一錯誤狀況:

一、執行/etc/init.d/fdfs_storaged stop/etc/init.d/fdfs_trackerd stop關閉服務,而後先啓動tracker服務再執行storage服務,通常就能夠解決 二、若是上面的辦法仍是不行,就從新安裝FastDFS(重新直接編譯方法便可);其次還要從新刪除tracker.confstorage.conf從新配置;最後再從新啓動服務(儘可能重啓一下服務器)

如上咱們就完成了FastDFS的配置。

<br/>

測試文件上傳

進入/etc/fdfs目錄並複製一份client.conf.sample並命名爲client.conf

[root@localhost fdfs]# cp client.conf.sample client.conf
[root@localhost fdfs]# vi client.conf

編輯下列兩項內容

base_path=/fastdfs/tracker  
tracker_server=192.168.148.131:22122  #這個IP地址是你服務器的IP地址,輸入命令`ip addr`便可查看到。不要盲目複製

下面使用fdfs_upload_file腳本進行文件上傳操做。先建立測試文件,輸入命令vi test.txt,回車後點擊i鍵進入編輯模式,輸入Hello TyCoding!,點擊ESC鍵,輸入:wq!保存並退出,經過

[root@localhost ~]# vi test.txt
Hello TyCoding!

/usr/bin/fdfs_upload_file /etc/fdfs/client.conf ~/test.txt

若是出現相似下面的返回地址證實上傳成功

<br/>

FastDFS與Nginx結合

解壓nginx-1.6.2.tar.gz/usr/local目錄下;解壓fastdfs-nginx-module_v1.16.tar.gz/usr/local目錄下,編譯和安裝

[root@localhost ~]# cd ~/software
[root@localhost software]# tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/ 
[root@localhost software]# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/

進入/usr/local/fast/fastdfs-nginx-module/src/目錄下,修改其中的config文件,把其中第四行的usr/local/include都改成/usr/include

<br/>

編譯nginx

[root@localhost src]# cd /usr/local/ && ll
[root@localhost local]# cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx --add-module=../fast/fastdfs-nginx-module/src/

正常狀況下,會顯示以下信息證實編譯成功,由於這裏須要爲nginx添加fastdfs-nginx-module模塊,和日常安裝的nginx是不一樣的,若是不顯示以下頁面就從新進行編譯

安裝,並copy /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf/etc/fdfs

[root@localhost nginx-1.6.2]# make && make install 
[root@localhost nginx-1.6.2]# cd /usr/local/fast/fastdfs-nginx-module/src/
[root@localhost src]# cp /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@localhost src]# cd /etc/fdfs && ll
[root@localhost fdfs]# vi mod_fastdfs.conf

修改以下部分

connect_timeout=10
tracker_server=192.168.148.131:22122  #IP地址仍是根據本身的服務器IP地址來定
url_have_group_name = true
store_path0=/fastdfs/storage

賦值FastDFS裏的2個文件,到/etc/fdfs目錄中,建立一個軟鏈接,在/fastdfs/storage文件儲存目錄下建立軟鏈接,將其鏈接到實際存放數據的目錄

[root@localhost fdfs]# cd /usr/local/fast/FastDFS/conf/ && ll
[root@localhost conf]# cp http.conf mime.types /etc/fdfs/ 
[root@localhost conf]# ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00

修改nginx的配置文件nginx-conf

[root@localhost conf]# cd /usr/local/nginx/conf && ll
[root@localhost conf]# vi nginx.conf

修改以下部分

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

啓動nginx

[root@localhost conf]# /usr/local/nginx/sbin/nginx

若是出現以上顯示證實nginx啓動成功,否者的話啓動失敗,要從新編譯安裝nginx,通常都是fastdfs-nginx-module模塊注入到nginx失敗。

以上咱們已經完成了所有的配置,測試各個端口的啓動狀況,輸入netstat -ntlp,能夠看到以下信息:

出現上述信息證實各個配置都正常

<br/>

經過http的方式訪問上傳文件

[root@localhost conf]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf ~/test.txt

<br/>

設置服務開機自啓動

通過上面的環境配置,咱們一共須要啓動三個服務才能實現最終的效果,那麼每次啓動服務器都手動去啓動這些服務未免顯得太過於麻煩了,因此咱們配置這些服務開機自啓動便可:

[root@localhost ~]#  vi /etc/rc.d/rc.local

向其中添加須要開機自啓動服務的腳本

/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start
/usr/local/nginx/sbin/nginx

其次,咱們還要給rc.local文件賦予權限才能實現開機自啓動

[root@localhost ~]# chmod +x /etc/rc.d/rc.local

輸入命令reboot重啓服務器,再輸入netstat -ntlp查看當前啓動的服務,能夠看到storage, tracker, nginx三個服務都自啓動了。

注意

參考文章:https://qq52o.me/2077.html
我也是根據上述文章中的介紹搭建的系統,可是並無成功,因而我改變了其中的一些步驟(命令行),參看了網上其餘的文章,最終成功搭建!

  • 教程中使用的系統版本是CentOS7

  • 若是你在配置tracker和storage完成後啓動服務,兩個服務並無都啓動,請先執行xxx stop的命令關閉服務,而後按照先啓動tracker再啓動storage的順序啓動服務;若是仍是沒有啓動成功,最好從新安裝FastDFS(直接從新執行命令便可),從新配置相關的文件,而後重啓服務器,再重啓服務,通常就解決了。

  • 若是你在安裝nginx時添加fastdfs-nginx-module模塊時出現error,請從新編譯nginx,從新配置相關的文件,通常就能解決了。

最後祝願你們都能成功的搭建FastDFS系統。

<br/>

交流

若是你們有興趣,歡迎你們加入個人Java交流羣:671017003 ,一塊兒交流學習Java技術。博主目前一直在自學JAVA中,技術有限,若是能夠,會盡力給你們提供一些幫助,或是一些學習方法,固然羣裏的大佬都會積極給新手答疑的。因此,別猶豫,快來加入咱們吧!

<br/>

聯繫

If you have some questions after you see this article, you can contact me or you can find some info by clicking these links.

相關文章
相關標籤/搜索