1. 程式人生 > >【FastDFS】小夥伴們說在CentOS 8伺服器上搭建FastDFS環境總報錯?

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

## 寫在前面 > 在【冰河技術】微信公眾號的【分散式儲存】專題中,我們分別搭建了單節點FastDFS環境和高可用FastDFS叢集環境。但是,之前的環境都是基於CentOS 6.8伺服器進行搭建的。很多小夥伴反饋說:自己在CentOS 8伺服器上,按照我寫的文章搭建FastDFS環境時,會失敗!看到小夥伴們的這些問題,我今天就給大家分享下如何在CentOS 8伺服器上搭建FastDFS環境。 ## 什麼是FastDFS? 這裡,我就摘錄下百度百科上對於FastDFS的描述。 FastDFS是一個開源的輕量級分散式檔案系統,它對檔案進行管理,功能包括:檔案儲存、檔案同步、檔案訪問(檔案上傳、檔案下載)等,解決了大容量儲存和負載均衡的問題。特別適合以檔案為載體的線上服務,如相簿網站、視訊網站等等。 FastDFS為網際網路量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高效能等指標,使用FastDFS很容易搭建一套高效能的檔案伺服器叢集提供檔案上傳、下載等服務。 ## 檔案上傳下載流程 這裡,我們用兩張圖分別來說明下FastDFS檔案上傳和下載的過程。這樣,小夥伴們也能一目瞭然的看到FastDFS的執行流程。 ### 檔案上傳 ![](https://img-blog.csdnimg.cn/20200925203542292.png) ### 檔案下載 ![](https://img-blog.csdnimg.cn/20200925203552237.png) 瞭解了FastDFS的這些基本知識之後。接下來,我們就一起來看看如何在CentOS 8伺服器上搭建FastDFS環境。 ## 伺服器版本 在正式開始搭建FastDFS環境之前,我們先確定下伺服器的版本,這裡我使用的CentOS伺服器的核心版本為:release 8.1.1911,如下所示。 ```bash [root@binghe lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) ``` ## 下載FastDFS 這裡,我們使用的FastDFS版本為6.0.6,官方的地址為:[https://github.com/happyfish100](https://github.com/happyfish100) 在FastDFS 6.0.6中,有三個子模組,如下所示。 ```bash fastdfs v6.06 libfastcommon v1.0.43 fastdfs-nginx-module v 1.22 ``` 我們可以在CentOS 8伺服器的命令列執行如下命令來下載這些模組。 ```bash [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/](http://nginx.org/) 我們可以在CentOS 8伺服器命令列輸入如下命令下載Nginx。 ```bash [root@binghe source]# wget http://nginx.org/download/nginx-1.17.8.tar.gz ``` ## 安裝FastDFS依賴 ```bash [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的壓縮包 ```bash [root@binghe source]# tar -zxvf V1.0.43.tar.gz ``` ### 編譯並安裝 ```bash [root@binghe source]# cd libfastcommon-1.0.43/ [root@binghe libfastcommon-1.0.43]# ./make.sh && ./make.sh install ``` ### 測試安裝結果 ```bash [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 ```bash [root@binghe source]# tar -zxvf V6.06.tar.gz ``` ### 安裝FastDFS ```bash [root@binghe source]# cd fastdfs-6.06/ [root@binghe fastdfs-6.06]# ./make.sh && ./make.sh install ``` ### 檢視FastDFS的安裝情況 ```bash [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配置檔案 ```bash [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服務所需的目錄** ```bash [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 檔案。 ```bash [root@binghe fdfs]# vi /etc/fdfs/tracker.conf ``` 只修改base_path一項的值為我們在上面所建立的目錄即可。 ```bash base_path = /data/fastdfs/tracker ``` **(3)啟動 tracker 服務** ```bash [root@binghe fdfs]# /etc/init.d/fdfs_trackerd start ``` **(4)檢查tracker服務啟動是否成功** ```bash [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** ```bash [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中定義的。如下所示: ```bash # the tracker server port port = 22122 ``` ### 啟動storage服務 **(1)建立storage服務所需的目錄** ```bash [root@binghe fdfs]# mkdir /data/fastdfs/storage [root@binghe fdfs]# chmod 777 /data/fastdfs/storage/ ``` **(2)配置storage服務** 編輯storage的配置檔案: ```bash [root@binghe fdfs]# vi /etc/fdfs/storage.conf ``` 各配置項包括: 配置base_path為上面所建立的storage目錄,其中,store_path 為儲存所在的目錄,可以設定多個,注意從0開始。 ```bash base_path = /data/fastdfs/storage store_path0 = /data/fastdfs/storage ``` 配置tracker_server的ip和埠。 ```bash tracker_server = 192.168.175.100:22122 ``` 指定http服務的埠 ```bash http.server_port = 80 ``` **(3)啟動storage服務** ```bash [root@binghe fdfs]# /etc/init.d/fdfs_storaged start 正在啟動 fdfs_storaged (via systemctl): [ 確定 ] ``` **(4)檢查storage服務啟動是否成功?** ```bash [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?** ```bash [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中定義的,如下所示。 ```bash # the storage server port port = 23000 ``` ## 配置客戶端檔案 **(1)配置客戶端要使用的client.conf** ```bash [root@binghe fdfs]# vi /etc/fdfs/client.conf ``` 以下兩項配置用到的tracker目錄和伺服器地址埠 ```bash 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: ```bash [root@binghe source]# tar -zxvf nginx-1.17.8.tar.gz ``` ### 解壓fastdfs-nginx-module ```bash [root@binghe source]# tar -zxvf V1.22.tar.gz ``` ### 修改config檔案 修改config檔案,把/usr/local 替換成 /usr ```bash [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 模組 ```bash [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 ```bash [root@binghe nginx-1.17.8]# make && make install ``` ### 檢查安裝是否成功? ```bash [root@binghe nginx-1.17.8]# ls /usr/local/soft/ | grep nginx nginx ``` ### 驗證Nginx配置 ```bash [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 ### 複製配置檔案 ```bash [root@binghe nginx-1.17.8]# cp /usr/local/source/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/ ``` ### 編輯配置檔案 ```bash [root@binghe nginx-1.17.8]# vi /etc/fdfs/mod_fastdfs.conf ``` 配置以下幾項 ```bash connect_timeout=10 tracker_server=192.168.175.100:22122 url_have_group_name = true store_path0=/data/fastdfs/storage ``` ### 複製Web配置檔案 複製另兩個web訪問用到配置檔案到fdfs配置目錄下: ```bash [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的配置檔案: ```bash [root@binghe conf]# vi /usr/local/soft/nginx/conf/nginx.conf ``` 在server listen 80 的這個server配置下面, 增加一個location ```bash location ~/group([0-9]) { root /data/fastdfs/storage/data; ngx_fastdfs_module; } ``` ## 啟動nginx ### 啟動Nginx ```bash [root@binghe storage]# /usr/local/soft/nginx/sbin/nginx ``` ### 檢查nginx是否已成功啟動 ```bash [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)命令列上傳圖片** ```bash [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)開啟瀏覽器:訪問這個地址** ```bash http://192.168.175.100/group1/M00/00/00/Ch8FQl9txnyAfrePAADhyYH1AP4653.jpg ``` 可以看到圖片 ![](https://img-blog.csdnimg.cn/20200925203614573.jpg) 其中,192.168.175.100是拼接的本地伺服器的ip 我門可以通過命令列來檢視圖片在伺服器上的真實儲存路徑。 ```bash [root@binghe data]# pwd /data/fastdfs/storage/data [root@binghe data]# ls 00/00 Ch8FQl9txnyAfrePAADhyYH1AP4653.jpg ``` 可見/group1/M00這兩個目錄是由fdfs所管理生成的目錄,它們分別代表fdfs生成的組名和磁碟 ## FastDFS其他命令 ### 檢視fdfs的版本 ```bash [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的執行狀態 ```bash [root@binghe data]# fdfs_monitor /etc/fdfs/client.conf ``` **好了,今天我們就到這兒吧,我是冰河,我們下期見