【FastDFS】小夥伴們說在CentOS 8服務器上搭建FastDFS環境總報錯?

寫在前面

在【冰河技術】微信公衆號的【分佈式存儲】專題中,我們分別搭建了單節點FastDFS環境和高可用FastDFS集羣環境。但是,之前的環境都是基於CentOS 6.8服務器進行搭建的。很多小夥伴反饋說:自己在CentOS 8服務器上,按照我寫的文章搭建FastDFS環境時,會失敗!看到小夥伴們的這些問題,我今天就給大家分享下如何在CentOS 8服務器上搭建FastDFS環境。

什麼是FastDFS?

這裏,我就摘錄下百度百科上對於FastDFS的描述。

FastDFS是一個開源的輕量級分佈式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合以文件爲載體的在線服務,如相冊網站、視頻網站等等。

FastDFS爲互聯網量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用FastDFS很容易搭建一套高性能的文件服務器集羣提供文件上傳、下載等服務。

文件上傳下載流程

這裏,我們用兩張圖分別來說明下FastDFS文件上傳和下載的過程。這樣,小夥伴們也能一目瞭然的看到FastDFS的執行流程。

文件上傳

文件下載

瞭解了FastDFS的這些基本知識之後。接下來,我們就一起來看看如何在CentOS 8服務器上搭建FastDFS環境。

服務器版本

在正式開始搭建FastDFS環境之前,我們先確定下服務器的版本,這裏我使用的CentOS服務器的內核版本爲:release 8.1.1911,如下所示。

[root@binghe lib]# cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core) 

下載FastDFS

這裏,我們使用的FastDFS版本爲6.0.6,官方的地址爲:https://github.com/happyfish100

在FastDFS 6.0.6中,有三個子模塊,如下所示。

fastdfs v6.06
libfastcommon v1.0.43
fastdfs-nginx-module   v 1.22

我們可以在CentOS 8服務器的命令行執行如下命令來下載這些模塊。

[root@binghe source]# wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
[root@binghe source]# wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
[root@binghe source]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz

下載Nginx

Nginx的官方網址爲:http://nginx.org/

我們可以在CentOS 8服務器命令行輸入如下命令下載Nginx。

[root@binghe source]# wget http://nginx.org/download/nginx-1.17.8.tar.gz

安裝FastDFS依賴

[root@binghe dest]# yum install gcc gcc-c++
[root@binghe dest]# yum install libtool zlib zlib-devel openssl openssl-devel
[root@binghe dest]# yum -y install pcre pcre-devel libevent libevent-devel perl unzip net-tools wget

安裝libfastcommon

解壓libfastcommon的壓縮包

 [root@binghe source]# tar -zxvf V1.0.43.tar.gz

編譯並安裝

[root@binghe source]# cd libfastcommon-1.0.43/
[root@binghe libfastcommon-1.0.43]# ./make.sh && ./make.sh install

測試安裝結果

[root@binghe libfastcommon-1.0.43]# ls /usr/lib64|grep libfastcommon
libfastcommon.so
[root@binghe libfastcommon-1.0.43]# ls /usr/lib|grep libfastcommon
libfastcommon.so

編譯安裝fastdfs

解壓FastDFS

[root@binghe source]# tar -zxvf V6.06.tar.gz

安裝FastDFS

[root@binghe source]# cd fastdfs-6.06/
[root@binghe fastdfs-6.06]# ./make.sh && ./make.sh install

查看FastDFS的安裝情況

[root@binghe fastdfs-6.06]# ls /usr/bin|grep fdfs
fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_regenerate_filename
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file

修改FastDFS配置文件

[root@binghe fastdfs-6.06]# cd /etc/fdfs/
[root@binghe fdfs]# cp storage.conf.sample storage.conf
[root@binghe fdfs]# cp client.conf.sample client.conf
[root@binghe fdfs]# cp tracker.conf.sample tracker.conf

啓動FastDFS

啓動tracker服務

(1)創建tracker服務所需的目錄

[root@binghe fdfs]# mkdir /data/fastdfs
[root@binghe fdfs]# mkdir /data/fastdfs/tracker
[root@binghe fdfs]# chmod 777 /data/fastdfs/tracker

(2)配置tracker服務

修改 tracker.conf 文件。

[root@binghe fdfs]# vi /etc/fdfs/tracker.conf

只修改base_path一項的值爲我們在上面所創建的目錄即可。

  base_path = /data/fastdfs/tracker

(3)啓動 tracker 服務

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

(4)檢查tracker服務啓動是否成功

[root@binghe fdfs]# ps auxfww | grep fdfs
root      15067  0.0  0.0  12320   964 pts/0    S+   01:14   0:00  |   |           \_ grep --color=auto fdfs
root      15026  0.0  0.1  90160  5940 ?        Sl   01:13   0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

能看到 fdfs_trackerd,表示tracker服務啓動成功

(5)檢查tracker服務是否已綁定端口 22122

[root@binghe dest]# netstat -anp | grep 22122
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      15026/fdfs_trackerd 

說明: 22122端口是在/etc/fdfs/tracker.conf中定義的。如下所示:

# the tracker server port
port = 22122

啓動storage服務

(1)創建storage服務所需的目錄

[root@binghe fdfs]# mkdir /data/fastdfs/storage
[root@binghe fdfs]# chmod 777 /data/fastdfs/storage/

(2)配置storage服務

編輯storage的配置文件:

[root@binghe fdfs]# vi /etc/fdfs/storage.conf

各配置項包括:

配置base_path爲上面所創建的storage目錄,其中,store_path 爲存儲所在的目錄,可以設置多個,注意從0開始。

base_path = /data/fastdfs/storage
store_path0 = /data/fastdfs/storage

配置tracker_server的ip和端口。

tracker_server = 192.168.175.100:22122 

指定http服務的端口

http.server_port = 80

(3)啓動storage服務

[root@binghe fdfs]# /etc/init.d/fdfs_storaged start
正在啓動 fdfs_storaged (via systemctl):                   [  確定  ]

(4)檢查storage服務啓動是否成功?

[root@binghe fdfs]# ps auxfww | grep fdfs
root      15630  0.0  0.0  12320   972 pts/0    S+   15:46   0:00  |   |           \_ grep --color=auto fdfs
root      15026  0.0  0.1 155696  6964 ?        Sl   15:13   0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root      15573  2.7  1.7 150736 66292 ?        Sl   15:45   0:02 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf

說明:看到fdfs_storaged,表示storage服務啓動成功

(5)檢查storage服務是否已綁定到端口:23000?

[root@binghe fdfs]# netstat -anp | grep 23000
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      15573/fdfs_storaged 

說明:23000 端口是在配置文件 /etc/fdfs/storage.conf中定義的,如下所示。

# the storage server port
port = 23000

配置客戶端文件

(1)配置客戶端要使用的client.conf

[root@binghe fdfs]# vi /etc/fdfs/client.conf

以下兩項配置用到的tracker目錄和服務器地址端口

base_path = /data/fastdfs/tracker
tracker_server = 192.168.175.100:22122

(2)從客戶端的配置可以看到:客戶端只需要瞭解tracker_server的信息,Tracker server作用也正是負載均衡和調度

(3)Storage server作用是文件存儲,客戶端上傳的文件最終存儲在 Storage 服務器上。

安裝nginx及fastdfs-nginx-module

解壓nginx:

[root@binghe source]# tar -zxvf nginx-1.17.8.tar.gz

解壓fastdfs-nginx-module

[root@binghe source]# tar -zxvf V1.22.tar.gz

修改config文件

修改config文件,把/usr/local 替換成 /usr

[root@binghe source]# cd fastdfs-nginx-module-1.22/
[root@binghe fastdfs-nginx-module-1.22]# cd src
[root@binghe src]# vi config

配置Nginx

Nginx配置,添加fastdfs-nginx-module和http_stub_status_module 模塊

[root@binghe fdfs]# cd /usr/local/source/nginx-1.17.8/
[root@binghe nginx-1.17.8]# ./configure --prefix=/usr/local/soft/nginx --with-http_stub_status_module --add-module=/usr/local/source/fastdfs-nginx-module-1.22/src/

編譯安裝Nginx

[root@binghe nginx-1.17.8]# make && make install

檢查安裝是否成功?

[root@binghe nginx-1.17.8]# ls /usr/local/soft/ | grep nginx
nginx

驗證Nginx配置

[root@binghe fdfs]# /usr/local/soft/nginx/sbin/nginx -V
nginx version: nginx/1.17.8
built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) 
configure arguments: --prefix=/usr/local/soft/nginx --with-http_stub_status_module --add-module=/usr/local/source/fastdfs-nginx-module-1.22/src/

配置fastdfs-nginx-module

複製配置文件

[root@binghe nginx-1.17.8]# cp /usr/local/source/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/

編輯配置文件

[root@binghe nginx-1.17.8]# vi /etc/fdfs/mod_fastdfs.conf

配置以下幾項

connect_timeout=10
tracker_server=192.168.175.100:22122
url_have_group_name = true
store_path0=/data/fastdfs/storage

複製Web配置文件

複製另兩個web訪問用到配置文件到fdfs配置目錄下:

[root@binghe nginx-1.17.8]# cd /usr/local/source/fastdfs-6.06/conf/
[root@binghe conf]# cp http.conf /etc/fdfs/
[root@binghe conf]# cp mime.types /etc/fdfs/

配置nginx

編輯nginx的配置文件:

[root@binghe conf]# vi /usr/local/soft/nginx/conf/nginx.conf

在server listen 80 的這個server配置下面,

增加一個location

location ~/group([0-9]) {
	root  /data/fastdfs/storage/data;
	ngx_fastdfs_module;
}

啓動nginx

啓動Nginx

[root@binghe storage]# /usr/local/soft/nginx/sbin/nginx 

檢查nginx是否已成功啓動

[root@binghe storage]# ps auxfww | grep nginx
root      24590  0.0  0.0  12320   980 pts/0    S+   16:44   0:00  |   |           \_ grep --color=auto nginx
root      24568  0.0  0.0  41044   428 ?        Ss   16:44   0:00  \_ nginx: master process /usr/local/soft/nginx/sbin/nginx
nobody    24569  0.0  0.1  74516  4940 ?        S    16:44   0:00      \_ nginx: worker process 

測試圖片上傳

(1)命令行上傳圖片

[root@binghe storage]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/binghe/image/test.jpg
group1/M00/00/00/Ch8FQl9txnyAfrePAADhyYH1AP4653.jpg

注意fdfs所返回的地址,我們需要使用這個地址進行訪問

(2)打開瀏覽器:訪問這個地址

http://192.168.175.100/group1/M00/00/00/Ch8FQl9txnyAfrePAADhyYH1AP4653.jpg

可以看到圖片

其中,192.168.175.100是拼接的本地服務器的ip

我門可以通過命令行來查看圖片在服務器上的真實存儲路徑。

[root@binghe data]# pwd
/data/fastdfs/storage/data
[root@binghe data]# ls 00/00
Ch8FQl9txnyAfrePAADhyYH1AP4653.jpg

可見/group1/M00這兩個目錄是由fdfs所管理生成的目錄,它們分別代表fdfs生成的組名和磁盤

FastDFS其他命令

查看fdfs的版本

[root@binghe data]# fdfs_monitor /etc/fdfs/client.conf | grep version
[2020-09-24 01:58:01] DEBUG - base_path=/data/fastdfs/tracker, connect_timeout=5, 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

version = 6.06

查看fdfs的運行狀態

[root@binghe data]# fdfs_monitor /etc/fdfs/client.conf

好了,今天我們就到這兒吧,我是冰河,我們下期見!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章