天天看點

Centos7配置FastDFS

1、安裝gcc(編譯時需要)

FastDFS是C語言開發,安裝FastDFS需要先将官網下載下傳的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc

yum install -y gcc gcc-c++      
Centos7配置FastDFS

2、安裝libevent(運作時需求)

 若安裝了桌面圖形界面,就不需要安裝;FastDFS依賴libevent庫;

yum -y install libevent      

 3、 安裝libfastcommon

  libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS運作所需要的一些基礎庫。

(1)上傳 libfastcommonV1.0.7.tar.gz到Centos機器上;

(2)将 libfastcommonV1.0.7.tar.gz解壓至/usr/local/下:

tar -zxvf libfastcommonV1.0.7.tar.gz -C /usr/local/      

(3)切換目錄到:/usr/local/libfastcommon-1.0.7/ 下,接着進行編譯和安裝;

cd /usr/local/libfastcommon-1.0.7/
./make.sh           #編譯
./make.sh install   #安裝      

安裝後的截圖:

Centos7配置FastDFS

(4)libfastcommon安裝好後會在/usr/lib64 目錄下生成  libfastcommon.so 庫檔案;

  

Centos7配置FastDFS

  注意:由于FastDFS程式引用usr/lib目錄是以需要将/usr/lib64下的庫檔案拷貝至/usr/lib下。

cp libfastcommon.so /usr/lib      

 4、安裝tracker

 (1)上傳 FastDFS_v5.05.tar.gz 到 centos 上;

(2)将FastDFS_v5.05.tar.gz 解壓至/usr/local/下

tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local      

(3)切換目錄到: /usr/local/FastDFS/ 下,編譯和安裝;

cd /usr/local/FastDFS/
./make.sh && ./make.sh install  #編譯之後接着進行安裝      

安裝後的截圖:

Centos7配置FastDFS

(4)安裝成功将安裝目錄下的conf下的檔案拷貝到/etc/fdfs/下;

cp /usr/local/FastDFS/conf/* /etc/fdfs/      
Centos7配置FastDFS

 5、配置和啟動tracker

(1)切換目錄到: /etc/fdfs/ 目錄下;

(2)拷貝一份新的tracker配置檔案

cp tracker.conf.sample tracker.conf      

(3)修改tracker.conf ;   vim tracker.conf

base_path=/home/yuqing/fastdfs  改為:  base_path=/home/caychen/fastdfs
即:
base_path=/home/fastdfs

      

(4)建立 /home/caychen/fastdfs 目錄

mkdir /home/caychen/fastdfs      

 (5)啟動tracker,運作如下指令:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart      
Centos7配置FastDFS

注意:在/home/fastdfs/ 目錄下生成兩個目錄, 一個是資料data,一個是日志logs;

6、配置和啟動storage

 由于上面已經安裝過FastDFS,這裡隻需要配置storage就好了;

(1)切換目錄到: /etc/fdfs/ 目錄下;

(2)拷貝一份新的storage配置檔案

cp storage.conf.sample storage.conf      

(3)修改storage.conf ;   vim storage.conf

group_name=group1 #配置組名
base_path=/home/yuqing/fastdfs    改為:  base_path=/home/caychen/fastdfs

#store存放檔案的位置(store_path)
store_path0=/home/yuqing/fastdfs  改為: store_path0=/home/caychen/fastdfs/fdfs_storage 
#如果有多個挂載磁盤則定義多個store_path,如下
#store_path1=.....
#store_path2=......

#配置tracker伺服器:IP
tracker_server=192.168.0.107:22122
#如果有多個則配置多個tracker
#tracker_server=192.168.0.108:22122

      

 (4)建立 /home/fdfs_storage 目錄

mkdir /home/caychen/fastdfs/fdfs_storage      

(5)啟動storage, 運作指令如下:

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart      

啟動完成後進入 /home/fdfs_storage/data 目錄下,顯示目錄如下:

Centos7配置FastDFS

 7、使用FastDFS自帶工具測試

(1)切換目錄到 /etc/fdfs/ 目錄下;

(2)拷貝一份新的client配置檔案

cp client.conf.sample client.conf      

(3)修改client.conf ;   vim client.conf,修改基本路徑和tracker_server如下:

        base_path=/home/caychen/fastdfs

        tracker_server=192.168.0.107:22122

  注意:若tracker有多個,可以配置多個,如下:

  #tracker_server=......

  #tracker_server=......

(4)拷貝一張圖檔brand06.png 到Centos伺服器上的 /home/caychen 目錄下;

  

(5)進行測試,運作如下:(運作測試程式,讀取/etc/fdfs/client.conf 檔案,上傳/usr1目錄下的luzhouYue.jpg檔案)

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/caychen/brand06.png      

 結果如下,表示搭建成功;

Centos7配置FastDFS

以上圖中的檔案位址:http://192.168.0.107/group1/M00/00/00/wKgAa1wCu1iAH91WAAALOZRKOIc790_big.png  對應storage伺服器上的/home/caychen/fastdfs/fdfs_storage/data/00/00/wKgAa1wCu1iAH91WAAALOZRKOIc790_big.png檔案;

由于現在還沒有和nginx整合無法使用http下載下傳。

8、FastDFS 和nginx整合

 8.1 在tracker上安裝 nginx

 在每個tracker上安裝nginx,的主要目的是做負載均衡及實作高可用。如果隻有一台tracker可以不配置nginx。

 一個tracker對應多個storage,通過nginx對storage負載均衡;

 8.2 在storage 上安裝nginx

 (1)上傳 fastdfs-nginx-module_v1.16.tar.gz 到Centos伺服器上;

(2)解壓 fastdfs-nginx-module_v1.16.tar.gz 到 /usr/local目錄下;

tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local      

(3)切換目錄到: /usr/local/fastdfs-nginx-module/src/ 目錄下

cd /usr/local/fastdfs-nginx-module/src/      

(4)修改config檔案,将檔案中的所有 /usr/local/ 路徑改為 /usr/

Centos7配置FastDFS

修改完成後:

Centos7配置FastDFS

(5)将fastdfs-nginx-module/src下的mod_fastdfs.conf拷貝至/etc/fdfs/下

cp mod_fastdfs.conf /etc/fdfs/      

(6)并修改 /etc/fdfs/mod_fastdfs.conf 的内容;vi /etc/fdfs/mod_fastdfs.conf

 base_path=/tmp 修改為 base_path=/home/caychen/fastdfs

base_path=/home/caychen/fastdfs
tracker_server=192.168.0.107:22122 
#tracker_server=192.168.0.108:22122 #(多個tracker配置多行)
url_have_group_name=true        #url中包含group名稱
store_path0=/home/caychen/fastdfs/fdfs_storage  #指定檔案存儲路徑(上面配置的store路徑)      

(7)将libfdfsclient.so拷貝至/usr/lib下

cp /usr/lib64/libfdfsclient.so /usr/lib/      

(8)建立nginx/client目錄

mkdir -p /var/temp/nginx/client      

8.3 Nginx的安裝

(1)上傳 nginx-1.14.0.tar.gz 到Centos伺服器上;

(2)解壓 nginx-1.14.0.tar.gz 到 /usr/local目錄下;

tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/      

(3)安裝nginx的依賴庫

yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel      

(4)進入nginx解壓的目錄下:cd /usr/local/nginx-1.14.0/

(5)加入子產品指令配置

Centos7配置FastDFS
./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      
Centos7配置FastDFS

  

Centos7配置FastDFS

注意:上邊将臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立temp及nginx目錄;

(6)編譯并安裝

make && make install      

安裝成功後檢視生成的目錄,如下所示:

Centos7配置FastDFS

(7)拷貝配置檔案到 /etc/fdfs 下;

cd /usr/local/FastDFS/conf
cp http.conf mime.types /etc/fdfs/      

(8)修改nginx配置檔案

Centos7配置FastDFS
mkdir /usr/local/nginx/logs # 建立logs目錄
cd /usr/local/nginx/conf/
vim nginx.conf      

做如下的修改:

Centos7配置FastDFS

說明:

  (a.) server_name指定本機ip;

  (b.) location /group1/M00/:group1為nginx 服務FastDFS的分組名稱,M00是FastDFS自動生成編号,對應store_path0=/home/caychen/fastdfs/fdfs_storage,如果FastDFS定義store_path1,這裡就是M01

 (9)nginx的啟動

cd /usr/local/nginx/sbin/
./nginx       

 查詢nginx程序

  

Centos7配置FastDFS

(10) fastdfs 和 nginx 服務的開機自啟動:

編輯檔案 /etc/rc.d/rc.local

追加如下:

# fastdfs start
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

# nginx start
/usr/local/nginx/sbin/nginx      
Centos7配置FastDFS

在centos7中, /etc/rc.d/rc.local 檔案的權限被降低了,需要給rc.local 檔案增加可執行的權限;

chmod +x /etc/rc.d/rc.local      

9、在浏覽器中通路上傳到fastDFS的圖檔

因為Centos系統有防火牆,需要先關閉掉,才可以在浏覽器中通路;

(1)CentOS 7.0預設使用的是firewall作為防火牆;若沒有啟用iptables 作為防火牆,則使用以下方式關閉防火牆:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
firewall-cmd --state #檢視預設防火牆狀态(關閉後顯示notrunning,開啟後顯示running)      

  

Centos7配置FastDFS

(2)若已經啟用iptables作為防火牆,則使用以下方式關閉:

service iptables stop  #臨時關閉防火牆
chkconfig iptables off #永久關閉防火牆
      

 (3)在谷歌浏覽器中通路剛才上傳的圖檔:

剛才上傳的圖檔位址為:http://192.168.0.107/group1/M00/00/00/wKgAa1wCu1iAH91WAAALOZRKOIc790_big.png ,

如果正常顯示圖檔,則表示配置成功了。

繼續閱讀