一、簡介
FastDFS 是一個開源的高性能分佈式文件系統(DFS)。 它的主要功能包括:文件存儲,文件同步和文件訪問,以及高容量和負載平衡。主要解決了海量數據存儲問題,特別適合以中小文件(建議範圍:4KB < file_size <500MB)爲載體的在線服務。官網地址:https://github.com/happyfish100/fastdfs
FastDFS 系統有三個角色:跟蹤服務器(Tracker Server)、存儲服務器(Storage Server)和客戶端(Client)。
Tracker Server:跟蹤服務器,主要做調度工作,起到均衡的作用;負責管理所有的 storage server和 group,每個 storage 在啓動後會連接 Tracker,告知自己所屬 group 等信息,並保持週期性心跳。
Storage Server:存儲服務器,主要提供容量和備份服務;以 group 爲單位,每個 group 內可以有多臺 storage server,數據互爲備份。
Client:客戶端,上傳下載數據的服務器,也就是我們自己的項目所部署在的服務器。
二、搭建過程
軟件下載(均爲GitHub地址):
libfastcommon | fastsds的庫文件,需要先編譯安裝好
fastdfs | fastsds主文件
fastdfs-nginx-module | fastdfs的nginx模塊模塊
此次實驗均使用最新版本:libfastcommon-1.0.43.tar.gz
、fastdfs-6.06.tar.gz
、fastdfs-nginx-module-1.22.tar.gz
1、單機版
1.1、環境準備
準備一臺虛擬機,ip是:192.168.255.101
系統環境:CentOS Linux release 7.6.1810
1.2、依賴環境安裝
依賴環境安裝提前安裝,少走彎路!!!
gcc
、g++
基礎環境
# yum -y install -y gcc gcc-c++
perl
編譯環境
# yum -y install perl
vim
編輯軟件,方便修改配置文件
# yum -y install vim
wget
下載軟件
# yum -y install wget
1.3、FastDFS安裝
1.3.1、libfastcommon 安裝
libfastcommon
是從FastDFS
和FastDHT
中提取出來的公共C函數庫,FastDFS
依賴該模塊,需要首先安裝它。
解壓縮並進入解壓縮後的文件夾的根目錄:
# tar zxvf libfastcommon-1.0.43.tar.gz && cd libfastcommon-1.0.43
執行編譯安裝:
# ./make.sh && ./make.sh install
安裝完成後,返回到上一級目錄(軟件包所在的根目錄)
1.3.2、fastdfs 安裝
解壓縮並進入解壓縮後的文件夾的根目錄:
# tar zxvf fastdfs-6.06.tar.gz && cd fastdfs-6.06
執行編譯安裝:
# ./make.sh && ./make.sh install
報錯:https://github.com/happyfish100/fastdfs/issues/392,同樣問題,刪除原環境,重新來一遍,可能是由於我安裝了之前版本導致。
安裝成功後,在系統/etc
路徑下會生成一個/fdfs
目錄,該目錄下是FastDFS
相關的一些示例配置文件內容。
[root@localhost fastdfs-6.06]# ll /etc/fdfs/
總用量 32
-rw-r--r--. 1 root root 1909 6月 4 22:53 client.conf.sample
-rw-r--r--. 1 root root 10246 6月 4 22:53 storage.conf.sample
-rw-r--r--. 1 root root 620 6月 4 22:53 storage_ids.conf.sample
-rw-r--r--. 1 root root 9138 6月 4 22:53 tracker.conf.sample
將/etc/fdfs/tracker.conf.sample重命名爲/etc/fdfs/tracker.conf並編輯它:
# mv /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf && vim /etc/fdfs/tracker.conf
修改配置:
# the base path to store data and log files
base_path=/home/tyron/fastdfs/tracker
創建對應文件夾:
# mkdir -p /home/tyron/fastdfs/tracker
複製配置文件
cp /opt/fastdfs/fastdfs-6.06/conf/http.conf /etc/fdfs/http.conf
cp /opt/fastdfs/fastdfs-6.06/conf/mime.types /etc/fdfs/mime.types
將/etc/fdfs/storage.conf.sample 重命名爲/etc/fdfs/storage.conf 並編輯它:
# mv /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf && vim /etc/fdfs/storage.conf
找到`base_path`的配置行,將其修改爲:
base_path=/home/tyron/fastdfs/storage
找到`store_path0`的配置行,將其修改爲:
store_path0=/home/tyron/fastdfs/storage
找到`tracker_server`的配置行,將其修改爲`tracker`服務器的地址:
tracker_server=192.168.255.101:22122
創建對應文件夾:
# mkdir -p /home/tyron/fastdfs/storage
store_path0
如果配置的路徑不存在,則會使用base_path
配置的路徑。
service fdfs_trackerd start
service fdfs_storaged start
# 如果上面執行不成功
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
# 查看進程狀態
ps -aux | grep fdfs
顯示:storage啓動正常,tracker無法啓動
查看日誌信息:
# cat /home/tyron/fastdfs/tracker/logs/trackerd.log
/usr/bin/fdfs_trackerd: symbol lookup error: /usr/bin/fdfs_trackerd: undefined symbol: int2str
軟鏈接有問題,參考解決方式:https://blog.csdn.net/Mabanana/article/details/88076465
正常啓動頁面,至此成功一半了!!!
將 `/etc/fdfs/client.conf.sample` 重命名爲 `/etc/fdfs/client.conf` 並編輯它:
mv /etc/fdfs/client.conf.sample /etc/fdfs/client.conf && vim /etc/fdfs/client.conf
找到 `base_path`的配置行,將其修改爲:
base_path=/home/tyron/fastdfs/storage
找到 `tracker_server`的配置行,將其修改爲tracker服務器的地址:
tracker_server=192.168.255.101:22122
這裏的base_path是storage用於存儲數據文件(其路徑爲:base_path/data)以及日誌文件(其路徑爲:base_path/logs)的基路徑。
#fdfs_upload_file /etc/fdfs/client.conf 上傳的圖片
#栗子 1.jpg爲自己上傳的圖片
# fdfs_upload_file /etc/fdfs/client.conf /opt/fastdfs/1.jpg
group2/M00/00/00/wKj_ZV7cvXmARAiNAAKnQDcoJRc029.jpg
如果返回類似的文件id則說明文件上傳成功!
1.3.3、fastdfs-nginx-module 安裝
解壓縮並進入解壓縮後的文件夾的根目錄:
# tar zxvf fastdfs-nginx-module-1.22.tar.gz && cd fastdfs-nginx-module-1.22
編輯src/mod_fastdfs.conf:
# vim src/mod_fastdfs.conf
group_name改成與storage一樣的名字
# the group name of the local storage server
group_name=group2
找到 `url_have_group_name` 的配置行,將其改爲:
url_have_group_name = true
找到 `store_path0` 的配置行,將其改爲:
store_path0=/home/tyron/fastdfs/storage
複製文件 `src/mod_fastdfs.conf` 到 `/etc/fdfs/` 目錄下:
cp src/mod_fastdfs.conf /etc/fdfs/
編輯 `src/config` :
# vim src/config
找到 `CORE_INCS` 的配置行:
CORE_INCS="$CORE_INCS /usr/local/include"
去掉路徑中的 `/local` ,改爲如下並保存退出:
CORE_INCS="$CORE_INCS /usr/include/"
如果請求的url地址中包含了group_name(如“group1/M00/00/00/xxx”)則應設爲true。
如果請求的url地址中不包含group_name(如“/M00/00/00/xxx”)則應設爲false。必須配置與本機
storage.conf
中配置的store_path0
路徑相同。
1.3.4、安裝Nginx(線上)
一鍵安裝依賴
# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
創建一個文件夾
# mkdir -p /usr/local/nginx && cd /usr/local/nginx
下載tar包
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
解壓
# tar -xvf nginx-1.16.1.tar.gz
編譯nginx,並安裝
# cd /usr/local/nginx/nginx-1.16.1
# ./configure --prefix=/usr/local/nginx --add-module=/opt/fastdfs/fastdfs-nginx-module-1.22/src && make && make install
驗證是否安裝成功
# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.16.1
驗證本地可訪問
# curl http://192.168.255.101
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
出現版本信息,即表示Nginx安裝成功!
修改Nginx配置
# vim /usr/local/nginx/conf/nginx.conf
配置信息如下:
server {
listen 80;
server_name 192.168.255.101;
location /group2/M00 {
#root /home/tyron/fastdfs/storage/data/;
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
啓動Nginx
# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=43651
指定配置文件啓動nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
指定配置文件重啓nginx
/usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
如果請求的url地址中包含了group_name(如“group1/M00/00/00/xxx”)則應設爲true。
如果請求的url地址中不包含group_name(如“/M00/00/00/xxx”)則應設爲false。
1.4 Centos7關閉防火牆方法(詳解)
centos7中的防火牆改成了firewall,使用iptables無作用,開放端口的方法如下:
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含義:
--zone #作用域
--add-port=80/tcp #添加端口,格式爲:端口/通訊協議
--permanent #永久生效
systemctl restart firewalld.service #重啓防火牆
systemctl stop firewalld.service #關閉防火牆
netstat -lntp #查看監聽(Listen)的端口
netstat -lnp|grep 80 #檢查端口被哪個進程佔用
同理本機電腦(Windows)也關閉防火牆!!!
三、最後成果
最後訪問:http://192.168.255.101/group2/M00/00/00/wKj_ZV7cvXmARAiNAAKnQDcoJRc029.jpg