linux 安裝fastdfs 單機版(親測有效)
本文主要介紹在linux伺服器如何搭建FastDFS檔案伺服器。大概分為9個步驟,由於內容較為繁瑣。下面帶你入坑!
首先簡單介紹一下FastDFS是淘寶資深架構師餘慶老師主導開源的一個分散式檔案系統,用C語言編寫。適應與中小企業,對檔案不分塊。
主要有Tracker(管理) 和Storage (儲存)。
返回路徑= 組名 /虛擬碟符{M00/00/02} /檔名
1.軟體包
下面是安裝FastDFS
的安裝包,也可以下載到:FastDFS,密碼為:ideg
或者是自己下載
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz wget http://nginx.org/download/nginx-1.12.1.tar.gz
2.安裝gcc
檢查系統是否有安裝GCC
gcc –version
安裝GCC
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
就把gcc當成c語言編譯器, g++當成c++語言編譯器用就是了.(知乎)
3.安裝libfastcommon
進入libfastcommon壓縮包目錄解壓,編譯,安裝
tar -zxvf libfastcommonV1.0.7.tar.gz
進入解壓的libfastcommon-1.0.7目錄,編譯
./make.sh
安裝
./make.sh install
最後,libfastcommon.so檔案到usr/lib下 ,請按照自己的實際檔案路徑修改被複制路徑
cp /data/fastDFS/software/libfastcommon-1.0.7/src/libfastcommon.so /usr/lib
4.安裝FastDFS
解壓FastDFS_v5.05.tar.gz
,然後編譯安裝FastDFS
tar -zxvf FastDFS_v5.05.tar.gz
./make.sh
./make.sh install
安裝成功後將目錄conf
內的檔案拷貝到/etc/fdfs
目錄下:
cp /data/fastDFS/software/FastDFS/conf/* /etc/fdfs/
5.安裝tracker
進入/etc/fdfs
目錄,修改tracker.conf
檔案。如果不存在,就拷貝tracker.conf.sample
檔案為tracker.conf
,然後再修改:
base_path=/home/yuqing/fastdfs >>> base_path=/home/logs/fastdfs (註解:日誌檔案) http.server_port=8080 >>> http.server_port=80 (註解:埠,80是方便預設) store_group=group1 (註解:組名)
其中,/home/
logs
/fastdfs
目錄如果不存在,就建立一個。
之後,就啟動tracker
,並檢視是啟動成功(出現如下提示,表示啟動成功):restart重啟
fdfs_trackerd /etc/fdfs/tracker.conf start
netstat -unltp | grep tracker
6.安裝storage
由於storage和tracker執行的都是fastDFS程式,每一臺伺服器都部署一臺fasfDFS,一臺伺服器是tracker,一臺是storage
修改storage.conf
檔案。如果不存在,就拷貝storage.conf.sample
檔案為storage.conf
,然後再修改:
base_path=/home/yuqing/fastdfs >>> base_path=/home/logs/storage (日誌路徑) store_path0=/home/yuqing/fastdfs >>> store_path0=/home/data/storage (實際儲存檔案路徑,可以配置多個) tracker_server=192.168.209.121:22122 >>> tracker_server=192.168.2.231:22122 (連線tracker伺服器地址) group_name=group1 (必須和tracker的組名相同)
http.server_port=80 (這個埠也要改)
其中,如果/home/logs/storage和
/home/data/storage
不存在,就建立該目錄
然後,就啟動storage
,並檢視是否成功(出現如下提示,表示啟動成功):
fdfs_storaged /etc/fdfs/storage.conf start
netstat -unltp | grep storage
最後,檢視tracker
和storage
是不是在通訊:
fdfs_monitor /etc/fdfs/storage.conf
如下提示,出現ACTIVE
,表示二者均正常啟動,至此就可以進行上傳檔案測試了。
7.測試圖片上傳
Tracker和storage都已經安裝完成,使用命令測試檔案上傳:
FastDFS提供一個檔案上傳命令:usr/bin/fdfs_test 測試檔案上傳。測試上傳需要連線tracker伺服器,連線storage伺服器。因此需要指定一個配置檔案:client.conf配置檔案,通過Client.conf連線tracker伺服器。
修改/etc/fdfs下client.conf
base_path=/home/logs/client (日誌目錄) tracker_server=192.168.2.231:22122 (tracker埠)
測試命令
/usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg
如下圖返回地址則成功
8.安裝Nginx。
安裝Nginx所需的其他環境,gcc在上面裝過了。本人安裝沒走這一步
#gcc安裝 yum install gcc-c++ #PCRE pcre-devel 安裝 yum install -y pcre pcre-devel # zlib 安裝 yum install -y zlib zlib-devel #OpenSSL 安裝 yum install -y openssl openssl-devel
解壓、編譯、安裝。
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure
make
make install
9.安裝fastdfs-nginx-module
首先解壓fastdfs-nginx-module_v1.16.tar.gz
,修改/fastdfs-nginx-module/src/config檔案。去掉所有的local(三個)
拷貝usr/lib64目錄下庫檔案libfdfsclient.so
cp /usr/lib64/libfdfsclient.so /usr/lib
在nginx安裝目錄下執行如下命令:把module新增nginx中。通過設定安裝引數方式新增模組。
cd nginx-1.12.1
./configure --add-module=../fastdfs-nginx-module/src
注意:我這裡nginx-1.12.1和fastdfs-nginx-module在同一級目錄,所以這樣寫
重新安裝編譯
make && make install
檢視Nginx的模組
/usr/local/nginx/sbin/nginx -V
複製 fastdfs-nginx-module 原始碼中的配置檔案到/etc/fdfs 目錄, 並修改
cd fastdfs-nginx-module/src
cp mod_fastdfs.conf /etc/fdfs/
進入/etc/fdfs/修改mod_fastdfs.conf如下配置,其他預設
# 連線超時時間 connect_timeout=10 # Tracker Server tracker_server=192.168.2.231:22122 # StorageServer 預設埠 storage_server_port=23000 # 如果檔案ID的uri中包含/group**,則要設定為true url_have_group_name = true # Storage 配置的store_path0路徑,必須和storage.conf中的一致 store_path0=/home/data/storage # the base path to store log files base_path=/home/logs/storage
配置iNginx ,進入/usr/local/nginx/conf目錄下修改nginx.conf
注意:無論你在哪解壓安裝的。必須進這個目錄/usr/local/nginx/conf下修改nginx.conf
vim nginx.conf
修改配置,其它的預設 在80埠下新增fastdfs-nginx模組 location ~/group([0-9])/M00 { ngx_fastdfs_module; }
注意下面#user nobody;改成 #user root;
#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.2.231; location ~/group([0-9])/M00 { ngx_fastdfs_module; } #charset koi8-r; #access_log logs/host.access.log main; 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; } # 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; # } #} }
注意: listen 80 埠值是要與 /etc/fdfs/storage.conf 中的 http.server_port=80 (前面改成80了)相對應。如果改成其它埠,則需要統一,同時在防火牆中開啟該埠。 location 的配置,如果有多個group則配置location ~/group([0-9])/M00 ,沒有則不用配group。 在檔案儲存目錄下建立軟連線,將其連結到實際存放資料的目錄,注意這個檔案儲存的位置後面多個data目錄是系統自動生成的
ln -s /home/data/storage/data /home/data/storage/data/M00
啟動nginx
#啟動nginx cd /usr/local/nginx/sbin/ ./nginx
直接啟動
/usr/local/nginx/sbin/nginx #設定開機啟動 vim /etc/rc.local /usr/local/nginx/sbin/nginx # 設定執行許可權 chmod 755 rc.local
檢視Nginx是否啟動
ps -ef | grep nginx
OK記得雲服務的安全組 和防火牆設定需要方通 80、22122、 23000 這個不會的請參考騰訊雲伺服器tomcat埠無法訪問 中的防火牆設定
在位址列中訪問成功 http://192.175.231.128/group1/M00/00/00/rBAACVzBeU2AQBKJAAOHDqS1H9o350.jpg
報錯!不存在的。有問題110,120,119
注意:
1.group2同組的Storage2和Storage3 FastDFS服務埠必須一致: port=23000。
2.一臺伺服器可以裝多個組(group)但不能裝同組的多個Storage,日誌會報錯誤,日誌報錯原因是"注意1"
3.Version 4.05之前fastdfs內部綁定了libevent作為http伺服器.Version 4.05之後的版本刪除了內建的web http服務,內建的web http服務是個累贅,不用也罷!
4.啟動storage server時,一直處於僵死狀態.啟動storage server,storage將連線tracker server,如果連不上,將一直重試。直到連線成功,啟動才算真正完成!如果叢集中有2臺tracker server,而
其中一臺tracker沒有啟動,可能會導致storage server一直處於僵死狀態。