最近要用到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
做者的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
解壓命令以下:
unzip fastdfs-nginx-module-1.20.zip unzip fastdfs-5.11.zip tar -zvxf libfastcommon-1.0.39.tar.gz
進入到解壓的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-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
安裝的時候會報以下錯誤:
進入/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
tracker
和storage
服務啓動tracker
和storage
服務
[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下載。
安裝以前,最好檢查一下是否已經安裝有nginx # find -name nginx 若是系統已經安裝了nginx,那麼就先卸載 # yum remove nginx
須要新建一個用戶,禁止使用root用戶進行操做
# useradd -d ucenter # passwd ucenter # cd /ucenter # mkdir soft # chown ucenter:ucenter /ucenter/ -R
$ 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
./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分配用戶和分組
$ make $ make install
[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; # } #} }
$ 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 -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
=======================================================================================