1. 程式人生 > >FastDFS+Nginx搭建圖片伺服器(親測OK)

FastDFS+Nginx搭建圖片伺服器(親測OK)

前言

操作環境:CentOS7 X64,以下操作都是單機環境。
我把所有的安裝包下載到/softpackage/下,解壓到當前目錄。

先做一件事,修改hosts,將檔案伺服器的ip與域名對映(單機TrackerServer環境),因為後面很多配置裡面都需要去配置伺服器地址,ip變了,就只需要修改hosts即可。

# vim /etc/hosts
增加如下一行,這是我的IP
192.168.63.138 file.yunxin.com
如果要本機訪問虛擬機器,在C:\Windows\System32\drivers\etc\hosts中同樣增加一行

安裝FastDFS環境

1、安裝libevent

# yum -y install libevent

2、下載安裝 libfastcommon
① 進入到/softpackage/目錄

# cd /softpackage

② 下載libfastcommon

# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

③ 解壓

# tar -zxvf V1.0.7.tar.gz
# cd libfastcommon-1.0.7

④ 編譯、安裝

# ./make.sh
# ./make.sh install

⑤ 拷貝 libfastcommon.so到 /usr/lib/下

# cd /usr/lib64/
# cp libfastcommon.so /usr/lib

3、下載安裝FastDFS
① 進入到/softpackage/目錄

# cd /softpackage

② 下載FastDFS

# wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz

③ 解壓

# tar -zxvf V5.05.tar.gz
# cd fastdfs-5.05

④ 編譯、安裝

# ./make.sh
# ./make.sh install

⑤ 拷貝 /softpackage/fastdfs-5.05/conf下所有檔案到 /etc/fdfs/下

# cd /softpackage/fastdfs-5.05/conf
# cp * /etc/fdfs/

4、配置Tracker服務
① 進入 /etc/fdfs下,編輯 tracker.conf

# cd /etc/fdfs/
# vim tracker.conf

② 修改以下部分(指定自己的路徑),其餘預設

base_path=/fastdfs/tracker

③ 建立上一步指定的路徑,即 base_path對應的路徑

# cd /
# mkdir /fastdfs/tracker -p

④ 將client和storage也一併創建出來

# cd /fastdfs/
# mkdir client
# mkdir storage

⑤ 啟動Tracker服務

# cd /usr/bin
# fdfs_trackerd /etc/fdfs/tracker.conf

檢視 FastDFS Tracker 是否已成功啟動 ,22122埠正在被監聽,則算是Tracker服務安裝成功。

# netstat -unltp|grep fdfs

⑥ 設定Tracker開機啟動

# chkconfig fdfs_trackerd on

5、配置 FastDFS 儲存 (Storage)
① 進入 /etc/fdfs下,編輯 storage.conf

# cd /etc/fdfs/
# vim storage.conf

② 修改以下部分(指定自己的路徑),其餘預設

# group_name表示組織名,訪問圖片時需要加上這個才能訪問,組織名自己隨意定義
group_name=taofut
base_path=/fastdfs/storage
store_path0=/fastdfs/storage
tracker_server=file.yunxin.com:22122(最開始hosts配置的域名,也就是本機的ip地址)

③ 啟動Storage服務

# cd /usr/bin
# fdfs_storaged /etc/fdfs/storage.conf

檢視Storage是否成功啟動,23000埠正在被監聽,就算Storage啟動成功。

# netstat -unltp|grep fdfs

④ 設定Storage開機啟動

# chkconfig fdfs_storaged on

6、檔案上傳測試
① 修改客戶端配置檔案

# cd /etc/fdfs
# vim client.conf

② 修改以下部分,其餘預設

base_path=/fastdfs/client
tracker_server=file.yunxin.com:22122

③ 上傳測試,我事先在/home下放了一張圖片 hello.png

# cd /home
# /usr/bin/fdfs_test /etc/fdfs/client.conf upload hello.png

上傳成功後返回的結果:

This is FastDFS client test program v5.05

Copyright © 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General Public
License V3, which may be found in the FastDFS source kit. Please visit
the FastDFS Home Page http://www.csource.org/ for more detail.

[2019-01-01 15:20:37] DEBUG - base_path=/fastdfs/client,
connect_timeout=30, 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

tracker_query_storage_store_list_without_group: server 1.
group_name=, ip_addr=192.168.63.138, port=23000

group_name=taofut, ip_addr=192.168.63.138, port=23000
storage_upload_by_filename group_name=taofut,
remote_filename=M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189.png source ip
address: 192.168.63.138 file timestamp=2019-01-01 15:20:37 file
size=46195 file crc32=1118939218 example file url:
http://192.168.63.138/taofut/M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189.png
storage_upload_slave_by_filename group_name=taofut,
remote_filename=M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189_big.png
source ip address: 192.168.63.138 file timestamp=2019-01-01 15:20:37
file size=46195 file crc32=1118939218 example file url:
http://192.168.63.138/taofut/M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189_big.png

檢視上傳的檔案

# cd /fastdfs/storage/data/00/00

安裝Nginx

1、安裝Nginx所需環境
① gcc 安裝

# yum install gcc-c++

② pcre-devel 安裝

# yum install -y pcre pcre-devel

③ zlib 安裝

# yum install -y zlib zlib-devel

④ OpenSSL 安裝

# yum install -y openssl openssl-devel

3、安裝Nginx
① 上傳 nginx包到 /softpackage/下

② 解壓

# cd /softpackage
# tar -zxvf nginx-1.8.0.tar.gz

③ 建立臨時目錄/var/temp/nginx

mkdir /var/temp/nginx -p

④ 新增nginx配置

# cd nginx-1.8.0/

將以下內容貼上到命令列執行

./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

⑤ 編譯、安裝

# make
# make install

⑥ 修改 nginx.conf 檔案

# cd /usr/local/nginx/conf
# vim nginx.conf

新增如下配置:

server {
  listen        88;
   server_name   file.yunxin.com;
   location /taofut/M00 {
        ngx_fastdfs_module;
   }
}

4、配置fastdfs-nginx-module
① 上傳 fastdfs-nginx-module_v1.16.tar.gz包到 /softpackage(也可以wget + 地址 直接網上下載)

② 解壓

# cd /softpackage
# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

③ 修改config檔案

# cd fastdfs-nginx-module/src
# vim config

將以下三個路徑裡的local去掉

/usr/local/include/fastdfs
/usr/local/include/fastcommon/
/usr/local/lib -lfastcommon -lfdfsclient
修改成:
/usr/include/fastdfs
/usr/include/fastcommon/
/usr/lib -lfastcommon -lfdfsclient

④ 複製並修改 mod_fastdfs.conf 檔案

# cd /softpackage/fastdfs-nginx-module/src
# cp mod_fastdfs.conf /etc/fdfs/
# cd /etc/fdfs
# vim mod_fastdfs.conf

修改以下內容,其餘預設

base_path=/fastdfs/tmp
tracker_server=file.yunxin.com:22122
group_name=taofut
url_have_group_name = true
store_path0=/fastdfs/storage

建立base_path指定的目錄

# cd /fastdfs
# mkdir tmp

⑤ 新增模組

  • 先進入解壓包目錄

# cd /softpackage/nginx-1.8.0

  • 新增模組

# ./configure --add-module=/softpackage/fastdfs-nginx-module/src

⑥ 重新編譯、安裝

# make
# make install

⑦ 測試Nginx

  • 進入到nginx安裝目錄

# cd /usr/local/nginx/sbin
# ./nginx -t

  • 測試成功返回結果

ngx_http_fastdfs_set pid=10804 ngx_http_fastdfs_set pid=10804 nginx:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is
successful

⑧ 啟動Nginx服務

# ./nginx

⑨ 重啟Tracker、Storage、Nginx服務

# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# cd /usr/local/nginx/sbin/
# ./nginx -s reload

⑩ 訪問我之前上傳的檔案
輸入訪問地址:http://file.yunxin.com:88/taofut/M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189.png
訪問成功!