linux搭建FastDFS文件服務器的實現(xiàn)步驟
本文主要介紹在linux服務器如何搭建FastDFS文件服務器。大概分為9個步驟,由于內(nèi)容較為繁瑣。下面帶你入坑!
首先簡單介紹一下FastDFS是淘寶資深架構(gòu)師余慶老師主導開源的一個分布式文件系統(tǒng),用C語言編寫。適應與中小企業(yè),對文件不分塊。
主要有Tracker(管理) 和Storage (儲存)。
返回路徑= 組名 /虛擬盤符{M00/00/02} /文件名
1.軟件包
下面是安裝FastDFS
的安裝包
第一步:大家在瀏覽器地址欄輸入:https://sourceforge.net/projects/fastdfs/files,我們會看到如下圖所示界面。
第二步:下載FastDFS的Server安裝包,我們點擊上圖"FastDFS Server Source Code",會看到如下圖所示界面,我們選擇2014-12-02那個版本,因為這個版本是最穩(wěn)定的版本。
第三步:我們會看到如下圖所示界面,我們點擊"FastDFS_v5.05.tar.gz"進行下載即可。
下面是下載好的
或者是自己下載
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
檢查系統(tǒng)是否有安裝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
內(nèi)的文件拷貝到/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
目錄如果不存在,就創(chuàng)建一個。
之后,就啟動tracker
,并查看是啟動成功(出現(xiàn)如下提示,表示啟動成功):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
不存在,就創(chuàng)建該目錄
然后,就啟動storage
,并查看是否成功(出現(xiàn)如下提示,表示啟動成功):
fdfs_storaged /etc/fdfs/storage.conf start netstat -unltp | grep storage
最后,查看tracker
和storage
是不是在通信:
fdfs_monitor /etc/fdfs/storage.conf
如下提示,出現(xiàn)ACTIVE
,表示二者均正常啟動,至此就可以進行上傳文件測試了。
7.測試圖片上傳
Tracker和storage都已經(jīng)安裝完成,使用命令測試文件上傳:
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所需的其他環(huán)境,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安裝目錄下執(zhí)行如下命令:把module添加nginx中。通過設置安裝參數(shù)方式添加模塊。
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了)相對應。如果改成其它端口,則需要統(tǒng)一,同時在防火墻中打開該端口。
location 的配置,如果有多個group則配置location ~/group([0-9])/M00 ,沒有則不用配group。
在文件存儲目錄下創(chuàng)建軟連接,將其鏈接到實際存放數(shù)據(jù)的目錄,注意這個文件存儲的位置后面多個data目錄是系統(tǒng)自動生成的
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 # 設置執(zhí)行權(quán)限 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內(nèi)部綁定了libevent作為http服務器.Version 4.05之后的版本刪除了內(nèi)置的web http服務,內(nèi)置的web http服務是個累贅,不用也罷!
4.啟動storage server時,一直處于僵死狀態(tài).啟動storage server,storage將連接tracker server,如果連不上,將一直重試。直到連接成功,啟動才算真正完成!如果集群中有2臺tracker server,而
其中一臺tracker沒有啟動,可能會導致storage server一直處于僵死狀態(tài)。
到此這篇關于linux搭建FastDFS文件服務器的實現(xiàn)步驟的文章就介紹到這了,更多相關linux搭建FastDFS文件服務器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用nginx來負載均衡 本文在window與linux下配置nginx實現(xiàn)負載
使用nginx來負載均衡,本文在window與linux下配置nginx實現(xiàn)負載2010-03-03