CentOS7 安裝FastDFS分佈式文件系統

CentOS7 安裝FastDFS分佈式文件系統

最近要用到fastDFS,因此本身研究了一下,在搭建FastDFS的過程當中遇到過不少的問題,爲了能幫忙到之後搭建FastDFS的同窗,少走彎路,與你們分享一下。FastDFS的做者淘寶資深架構餘慶,這個優秀的輕量及的分佈式文件系統的開源沒多久,立馬就火了。php

FastDFS是爲互聯網應用量身定作的一套分佈式文件存儲系統,很是適合用來存儲用戶圖片、視頻、文檔等文件html

版本介紹

系統:centos7.4

FastDFS:5.11

libfastcommon:1.0.39 

fastdfs-nginx-module:1.20

nginx:1.14.1

一.FastDFS搭建工具下載

做者的GitHub地址:https://github.com/happyfish100nginx

此次搭建的全部工具,均可以在上面下載到。我搭建的是目前最新版本Version 5.11 2017-05-26c++

Version 5.11對應的fastdfs-nginx-module的Version 1.20 ** **Version 5.10對應的fastdfs-nginx-module的Version 1.19git

之因此在安裝前寫了這麼一段話,是由於這個很重要,版本不對應會給接下來的安裝帶來各類問題。github

把源碼下載下來3個zip包,再去下個nginx:shell

注意: 下載的時候要下fastdfs-5.11.zip 這個是沒有錯誤的。若是下載 fastdfs-5.11.tar.gz 編譯安裝會報錯;對於fastdfs-nginx-module-1.20.zip也要下載zip結尾的,否則編譯和安裝會有問題。vim

固然能夠直接去官網去下載,可是官網上不是最新的。windows

二:安裝須要用到的應用與類庫

首先安裝所需的基礎依賴centos

yum install vim wget zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl net-tools unzip

三:添加用戶:

[root@localhost /]# groupadd fastdfs
[root@localhost /]# useradd -s /sbin/nologin -g fastdfs fastdfs

四:建立目錄

[root@localhost /]# mkdir -p /usr/local/FastDFS
[root@localhost /]# mkdir -p /data/fastdfs/storage
[root@localhost /]# chown -R fastdfs:fastdfs /data/fastdfs

五:經過工具把文件上傳到」/usr/local/FastDFS」中,並解壓

解壓命令以下:

unzip fastdfs-nginx-module-1.20.zip
unzip fastdfs-5.11.zip
tar -zvxf libfastcommon-1.0.39.tar.gz

六:安裝libfastcommon

進入到解壓的libfastcommon-1.0.39.tar.gz

[root@localhost FastDFS]# cd libfastcommon-1.0.39
[root@localhost libfastcommon-1.0.39]# ./make.sh
[root@localhost libfastcommon-1.0.39]# ./make.sh install

libfastcommon默認會被安裝到/usr/lib64/libfastcommon.so可是FastDFS的主程序卻在/usr/local/lib目錄下 這個時候咱們就要創建一個軟連接了,實際上也至關於windows上的快捷方式。

ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

七:解壓FastDFS安裝包

進入fastdfs-5.11文件夾中

[root@localhost FastDFS]# cd fastdfs-5.11
[root@localhost fastdfs-5.11]# ./make.sh
[root@localhost fastdfs-5.11]# ./make.sh install

安裝成功後copy一下配置文件

[root@localhost fastdfs-5.11]# cp conf/* /etc/fdfs/

若是下載的是fastdfs-5.11.tar.gz 安裝的時候會報以下錯誤:

八:配置FastDFS文件

進入/etc/fdfs目錄下修改配置

①修改tracker.conf

[root@localhost fastdfs-5.11]# cd /etc/fdfs/
[root@localhost fastdfs-5.11]# vim tracker.conf

主要修改內容以下:

bind_addr=0.0.0.0
base_path=/data/fastdfs/storage
http.server_port=80

②修改storage.conf

[root@localhost fastdfs-5.11]# vim storage.conf

主要修改內容以下:

bind_addr=0.0.0.0
base_path=/data/fastdfs/storage
store_path0=/data/fastdfs/storage
# ip地址是你FastDFS服務器的地址
tracker_server=192.168.0.137:22122
http.server_port=80

③修改client.conf

[root@localhost fastdfs-5.11]# vim client.conf

主要修改內容以下:

base_path=/data/fastdfs/storage
tracker_server=192.168.0.137:22122
http.tracker_server_port=80

九:受權對應的端口開放

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=22122/tcp --permanent
firewall-cmd --zone=public --add-port=23000/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all

十:啓動trackerstorage服務

啓動trackerstorage服務

[root@localhost fdfs]# service fdfs_trackerd start
Starting fdfs_trackerd (via systemctl):                    [  肯定  ]
[root@localhost fdfs]# service fdfs_storaged start
Starting fdfs_storaged (via systemctl):                    [  肯定  ]

十一:測試文件上傳

首先:須要咱們使用工具上傳一張圖片到/usr/local/IMG_0076.JPG

而後執行以下命令:

[root@localhost fdfs]# /usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/ucenter/soft/IMG_0076.JPG

因爲如今尚未和nginx整合沒法使用http下載。

十二:FastDFS 和nginx整合

安裝以前,最好檢查一下是否已經安裝有nginx
# find -name nginx  
若是系統已經安裝了nginx,那麼就先卸載
# yum remove nginx

新建一個用戶

須要新建一個用戶,禁止使用root用戶進行操做
# useradd  -d ucenter
# passwd ucenter
# cd /ucenter
# mkdir soft
# chown ucenter:ucenter /ucenter/ -R

在soft目錄下直接下載和安裝nginx

$ cd /home/ucenter/soft
$ mkdir nginx
$ wget http://nginx.org/download/nginx-1.14.1.tar.gz
#解壓nginx壓縮包
$ tar -vxf nginx-1.14.1.tar.gz
#解壓後會產生一個nginx-1.14.1的目錄,進入這個目錄
$ cd nginx-1.14.1

切換到root目錄,而後指定默認的安裝目錄

./configure --prefix=/home/ucenter/soft/nginx  --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module  --add-module=/usr/local/FastDFS/fastdfs-nginx-module-1.20/src/ --user=ucenter --group=ucenter

/usr/local/FastDFS/fastdfs-nginx-module-1.20/src/fastdfs-nginx-module-1.20.zip解壓的目錄

--prefix=/home/ucenter/soft/nginx是指編譯而後安裝到此目錄下

--user=ucenter --group=ucenter 給nginx分配用戶和分組

再次切換用戶到ucenter,而後進入nginx-1.14.1目錄下進行編譯安裝

$ make
$ make install

編譯完畢後進入到編譯好的nginx目錄

[root@localhost conf]# cd /home/ucenter/soft/nginx/conf
[root@localhost conf]# vim nginx.conf

修改內容以下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  192.168.0.137;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /group1/M00/ {
            #root   html;
            #index  index.html index.htm;
            root /data/fastdfs/storage/data;
            ngx_fastdfs_module;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

安裝完畢後,啓動nginx

$ cd /home/ucenter/soft/nginx
$ cd sbin
$ ./nginx

#centos中啓動,從新加載,中止命令以下

./nginx    -------開啓nginx服務
./nginx -s reload ----------從新加載nginx服務
./nginx -s stop ----------關閉nginx服務

或者

pkill -9 nginx  ---------- 殺死全部的nginx進城
要到問題須要注意:
centos7 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

若是切換到普通用戶下啓動,有可能會報這個錯誤,怎麼解決了?

執行以下命令:

$ su root
$ passwd --輸入密碼
# cd /ucenter/soft/nginx/sbin/
# ll 
# chown root nginx
# chmod u+s nginx
# ll
# su ucenter
$ ./nginx
$ ps -ef|grep nginx

如何結束掉nginx進程

#通常狀況下直接能夠直接使用nginx的自帶命令
$ ./nginx -s stop
#若是命令不起做用則可使用
$ ps -ef | grep nginx
root      13064      1  0 16:46 ?        00:00:00 nginx: master process ./nginx
ucenter   13065  13064  0 16:46 ?        00:00:00 nginx: worker process
ucenter   13069  12602  0 16:46 pts/2    00:00:00 grep --color=auto nginx
#而後分別殺死主進程和分進程
$ kill -9 13064
$ kill -9 13065
#或者能夠經過以下命令直接中止
$ pkill -9 nginx

若是防火牆設置好了,就能夠直接在瀏覽器中訪問了

訪問的地址是以下:http://192.168.0.137/group1/M00/00/00/wKgAiVxH6ZmAaDnwAAEpShblOro831_big.JPG

=======================================================================================

其中遇到的錯誤問題以下:

第一個問題:

這個問題是由於沒有安裝依賴:因此要執行

yum install vim wget zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl net-tools unzip

第二個問題:

所下載的fastdfs-5.11.tar.gz或者fastdfs-5.11.zip有問題,須要從新下載

第三個問題:

這個問題通常出如今高版本中,這個問題須要手動修改fastdfs-nginx-module-1.20/src/config文件

vim fastdfs-nginx-module-1.20/src/config

修改後文件以下:

ngx_addon_name=ngx_http_fastdfs_module

if test -n "${ngx_module_link}"; then
    ngx_module_type=HTTP
    ngx_module_name=$ngx_addon_name
    ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
    ngx_module_libs="-lfastcommon -lfdfsclient"
    ngx_module_srcs="$ngx_addon_dir/ngx_http_fastdfs_module.c"
    ngx_module_deps=
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
    . auto/module
else
    HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
    CORE_LIBS="$CORE_LIBS -lfastcommon -lfdfsclient"
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
fi

主要修改部分是

ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"

CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

而後保存後從新編譯nginx就能夠了

第四個問題:

這個問題主要是/etc/fdfs下的tracker.conf或者是storage.conf配置錯誤,須要回去仔細覈對而後再次從新啓動tracker服務或者是storage服務

第五個問題:開機啓動問題

若是要把tracker,storage加入開機啓動的話命令以下

# 設置tracker開機啓動
echo "service fdfs_trackerd start" |tee -a /etc/rc.d/rc.local
# 設置storage開機啓動
echo "service fdfs_storaged start" |tee -a /etc/rc.d/rc.local

查看一下tracker的端口監聽狀況

[root@localhost soft]# netstat -unltp|grep fdfs
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      24261/fdfs_storaged 
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      24078/fdfs_trackerd 

 

=======================================================================================

新博客地址:https://www.codepeople.cn/

相關文章
相關標籤/搜索