騰訊雲centos7下搭建fastDFS+nginx

迪米特發表於2018-11-14

1.下載所需要的安裝包:

在伺服器根目錄(你也可以在其他地方下載)下載以下安裝包:
libfastcommon包下載: wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -O libfastcommon.tar.gz
fastdfs包下載:wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -O fastdfs.tar.gz
nginx包下載:wget http://nginx.org/download/nginx-1.12.0.tar.gz -O nginx.tar.gz
fastdfs-nginx模組包下載:wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -O fastdfs-nginx-module.tar.gz
### -O 表示重新命名下載包
### 除了nginx包,其他包都在這裡下載:https://github.com/happyfish100
複製程式碼

2.環境安裝

安裝gcc環境
yum -y install gcc-c++
安裝pcre-devel與openssl-devel
yum -y install pcre-devel openssl openssl-devel
這個命令是解決安裝nginx時出現的錯誤問題:./configure: error: the HTTP rewrite module requires the PCRE library.

3.安裝libfastcommon

1.解壓libfastcommon包: tar -zxvf libfastcommon.tar.gz
2.進入解壓目錄: cd libfastcommon-1.0.39
3.安裝: ./make.sh
         ./make.sh install
複製程式碼

4.安裝FastDFS

1.解壓fastDFS包: tar -zxvf fastdfs.tar.gz
2.進入解壓目錄: cd fastdfs-5.11
3.安裝:./make.sh
        ./make.sh install
如果安裝成功,在/etc/fdfs目錄下會有四個檔案:
-rw-r--r-- 1 root root  1461 Nov 13 09:54 client.conf.sample
-rw-r--r-- 1 root root  7927 Nov 13 09:54 storage.conf.sample
-rw-r--r-- 1 root root   105 Nov 13 09:54 storage_ids.conf.sample
-rw-r--r-- 1 root root  7389 Nov 13 09:54 tracker.conf.sample
進入目錄 cd /etc/fdfs,然後將這三個檔案複製並去掉.sample:
cp client.conf.sample client.conf
cp storage.conf.sample storage.conf
cp tracker.conf.sample tracker.conf
複製程式碼

5.配置tracker

1.建立儲存tracker的data和log的目錄:mkdir -p /usr/local/fast-dfs/fastdfs_tracker
2.進入目錄 cd /etc/fdfs
3.修改tracker.conf檔案:vim tracker.conf
修改如下:
    disabled=false #預設開啟
    port=22122 #預設埠號
    base_path=/usr/local/fast-dfs/fastdfs_tracker #剛剛建立的目錄
    http.server_port=6666 #預設埠是8080
修改完後,
啟動tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
檢視tracker是否有埠:netstat -ntpul |grep fdfs (如果沒有出現埠,說明tracker啟動失敗)
其它命令:
重啟tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
停止tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
新增開機啟動:
chmod +x /etc/rc.d/rc.local
echo '/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart' >> /etc/rc.d/rc.local
複製程式碼

6.配置storage

1.建立儲存storage的data和log的目錄:mkdir /usr/local/fast-dfs/fastdfs_storage
  再建立一個檔案儲存目錄:mkdir /usr/local/fast-dfs/fastdfs_storage_data
2.進入目錄:cd /etc/fdfs
3.修改storage.conf檔案:vim storage.conf
修改如下:
    disabled=false #預設false
    group_name=group1 #組名,根據實際情況修改
    port=23000 #設定storage的埠號,預設是23000,同一個組的storage埠號必須一致
    base_path=/usr/local/fast-dfs/fastdfs_storage #設定storage資料檔案和日誌目錄
    store_path_count=1 #儲存路徑個數,需要和store_path個數匹配
    store_path0=/usr/local/fast-dfs/fastdfs_storage_data #實際檔案儲存路徑
    tracker_server=伺服器公網ip地址:22122
    http.server_port=8888 #設定 http 埠號
修改儲存後建立軟引用
ln -s /usr/bin/fdfs_storaged /usr/local/bin
啟動storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
檢視storage是否有埠:netstat -ntpul |grep fdfs (如果沒有出現埠,說明storage啟動失敗)
其它命令:
重啟storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
停止storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
新增開機啟動:
echo '/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart' >> /etc/rc.d/rc.local
複製程式碼

7.校驗整合,確定storage是否註冊到了tracker中去

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
如下圖表示成功註冊了

騰訊雲centos7下搭建fastDFS+nginx

8.配置客戶端client

1.進入目錄:cd /etc/fdfs
2.修改client.conf: vim client.conf
修改如下:
    base_path=/usr/local/fast-dfs/fastdfs_tracker #tracker伺服器檔案路徑
    tracker_server=伺服器公網ip地址:22122 #tracker伺服器IP地址和埠號
    http.tracker_server_port=6666 # tracker 伺服器的 http埠號,必須和tracker的設定對應起來!
複製程式碼

9.FastDFS的nginx模組安裝

1.進入根目錄(下載包存放的目錄):cd ~
2.解壓fastdfs的Nginx模組包:tar -zvxf fastdfs-nginx-module.tar.gz
3.重新命名模組目錄:mv fastdfs-nginx-module-1.12.0 fastdfs-nginx-module
4.修改模組配置檔案:vim /root/fastdfs-nginx-module/src/config 
修改如下:
    ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
    CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
儲存後,建立軟連結:ln -s /usr/include/fast* /usr/local/include/
5.1解壓nginx包: tar -zxvf nginx.tar.gz
5.2進入nginx解壓目錄並新增fastdfs的nginx模組:
cd nginx-1.12.0
./configure --prefix=/usr/local/storage-nginx --add-module=/root/fastdfs-nginx-module/src
如果沒出錯,就依次執行:
make
make install
複製程式碼

10.配置storage的nginx

上面的nginx安裝完後,作為storage的nginx,進行配置
1.修改nginx的配置檔案:vim /root/nginx-1.12.0/conf/nginx.conf
修改監聽埠 listen 9999, 新增location(由於nginx編碼嚴格,不要複製,手動輸入,否則會出現nginx: [emerg] unknown directive " " in /usr/local/nginx-1.12.0-storage/conf/nginx.conf:49):
server {
        listen       9999;
        server_name  localhost;


        location / {
            root   html;
            index  index.html index.htm;
        }


        location ~/group1/M00 {
            root /usr/local/fast-dfs/fastdfs_storage_data/data;
            ngx_fastdfs_module;
        }


}
然後進入FastDFS安裝時的解壓過的目錄,將http.conf和mime.types拷貝到/etc/fdfs目錄下:
cd  /root/fastdfs-5.11/conf
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/
還需要把fastdfs-nginx-module安裝目錄中src目錄下的mod_fastdfs.conf也拷貝到/etc/fdfs目錄下:
cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
再對剛複製的mod_fastdfs配置:
vim /etc/fdfs/mod_fastdfs.conf
如下:
 base_path=/usr/local/fast-dfs/fastdfs_storage  #儲存日誌目錄
 tracker_server=伺服器公網ip地址:22122 #tracker伺服器的IP地址以及埠號
 storage_server_port=23000 #storage伺服器的埠號
 url_have_group_name = true #檔案 url 中是否有 group 名
 store_path0=/usr/local/fast-dfs/fastdfs_storage_data   #儲存路徑
 group_count = 3 #設定組的個數,事實上這次只使用了group1
 在檔案的最後,設定group
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data

[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data

[group3]
group_name=group3
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data
儲存後,建立M00至storage儲存目錄的符號連線:
ln  -s  /usr/local/fast-dfs/fastdfs_storage_data/data/ /usr/local/fast-dfs/fastdfs_storage_data/data/M00
啟動nginx:
/usr/local/storage-nginx/sbin/nginx -c /root/nginx-1.12.0/conf/nginx.conf
檢視埠是否啟動:
netstat -ntpul |grep nginx #出現9999埠的nignx説明啟動成功
複製程式碼

11.配置tracker的nginx

1.再解壓一個nginx,然後進入解壓目錄新增fastdfs-nginx模組:
./configure --prefix=/usr/local/tracker-nginx --add-module=/root/fastdfs-nginx-module/src
沒出現錯誤,就依次執行:
make
make install
然後修改nginx解壓目錄下的/conf/nginx.conf:
 upstream fdfs_group1 {
        server 127.0.0.1:9999;
    }
    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location /group1/M00 {
            proxy_pass http://fdfs_group1;
        }
        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}


        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
儲存後,啟動tracker的nginx:
/usr/local/tracker-nginx/sbin/nginx -c /root/tracker-nginx/nginx-1.12.0/conf/nginx.conf
檢視埠是否有80和9999:
netstat -ntpul |grep nginx
至此FastDFS服務部署完成。
下面配置下防火牆:
1.開啟所有配置的埠:
firewall-cmd --zone=public --add-port=80/tcp --permanent #開啟80埠號
firewall-cmd --zone=public --add-port=23000/tcp --permanent #開啟23000埠號
firewall-cmd --zone=public --add-port=22122/tcp --permanent #開啟22122埠號
firewall-cmd --zone=public --add-port=9999/tcp --permanent #開啟9999埠號
firewall-cmd --zone=public --add-port=6666/tcp --permanent #開啟6666埠號
firewall-cmd --zone=public --add-port=8888/tcp --permanent #開啟8888埠號
2.重啟防火牆

CentOS7 防火牆相關命令:
firewall-cmd --zone=public --list-ports #檢視開啟的埠
firewall-cmd state   #檢視防火牆是否開啟
systemctl enable firewalld.service    #開啟防火牆
systemctl stop firewalld.service     #關閉防火牆(開機會仍會啟動)
systemctl disable firewalld.service  #禁用防火牆(開機後不再啟動)
複製程式碼

12.測試

在idea上測試,操作如下:
1.拉取fastdfs的java客戶端包:File-->New-->project from version control-->github
2.填上Url:https://github.com/happyfish100/fastdfs-client-java.git ,專案所在目錄、名稱,點選clone
3.等專案初次化完後,設定下你的maven的安裝目錄:settings-->搜尋maven-->改變maven home directory的目錄
4.開啟右邊Maven Project視窗,進行安裝fastdfs的java客戶端:lifecycle-->install
5.至此你的fastdfs的maven依賴安裝完成,在你的測試專案的pom.xml檔案加入依賴就可以測試了
<dependency>
      <groupId>org.csource</groupId>
      <artifactId>fastdfs-client-java</artifactId>
      <version>1.27-SNAPSHOT</version>
</dependency>

例子:
在resources下建立fdfs_client.conf配置檔案,新增內容:tracker_server=伺服器公網ip地址:22122
建立一個demo.java:
public class Demo {
    public static void main(String[] args) throws Exception{
        //載入配置檔案,配置檔案中的內容就是 tracker 服務的地址
        ClientGlobal.init("D:/myWork/fasDFS-demo/src/main/resources/fdfs_client.conf");
        //建立一個 TrackerClient 物件。直接 new 一個
        TrackerClient trackerClient=new TrackerClient();
        //使用 TrackerClient 物件建立連線,獲得一個 TrackerServer 物件。
        TrackerServer trackerServer=trackerClient.getConnection();
        //建立一個 StorageServer 的引用,值為 null
        StorageServer storageServer=null;
        //建立一個 StorageClient 物件,需要兩個引數 TrackerServer 物件、StorageServer 的引用
        StorageClient storageClient=new StorageClient(trackerServer,storageServer);
        //使用 StorageClient 物件上傳圖片。
        //副檔名不帶“.”
        String[] strings=storageClient.upload_file("C:/Users/Public/Pictures/Sample Pictures/test.jpg","jpg",null);
        //遍歷陣列列印包含組名和圖片的路徑。
        for (String string:strings){
            System.out.println(string);
        }
    }
}
最後在瀏覽器上訪問你上傳的圖片:
http://伺服器公網ip地址/group1/M00/00/00/rBsABFvqfIyACR3WAAl5WIkOlj8480.jpg
複製程式碼

參考文章:

blog.csdn.net/weixin_4037… cloud.tencent.com/developer/a…

相關文章