來源:https://www.cnblogs.com/zeussbook/p/10757699.html
https://www.cnblogs.com/qiaolizhi/p/12461901.html
安裝注意: storage的有個IP地址,填寫自己服務器的IP即可
另外主要安裝教程都在這里了,另外另外一篇的博客是輔助作用
本文主要介紹在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=10Tracker 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 </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$remote_addr - $remote_user [$time_local] "$request" </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> # </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$status $body_bytes_sent "$http_referer" </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> # </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">"$http_user_agent" "$http_x_forwarded_for"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">; #access_log logs</span>/<span style="color: rgba(0, 0, 0, 1)">access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">; keepalive_timeout </span><span style="color: rgba(128, 0, 128, 1)">65</span><span style="color: rgba(0, 0, 0, 1)">; #gzip on; server { listen </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">; server_name </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">2.231</span><span style="color: rgba(0, 0, 0, 1)">; location </span>~/group([<span style="color: rgba(128, 0, 128, 1)">0</span>-<span style="color: rgba(128, 0, 128, 1)">9</span>])/<span style="color: rgba(0, 0, 0, 1)">M00 { ngx_fastdfs_module; } #charset koi8</span>-<span style="color: rgba(0, 0, 0, 1)">r; #access_log logs</span>/<span style="color: rgba(0, 0, 0, 1)">host.access.log main; location </span>/<span style="color: rgba(0, 0, 0, 1)"> { root html; index index.html index.htm; } #error_page </span><span style="color: rgba(128, 0, 128, 1)">404</span> /<span style="color: rgba(128, 0, 128, 1)">404</span><span style="color: rgba(0, 0, 0, 1)">.html; # redirect server error pages to the </span><span style="color: rgba(0, 0, 255, 1)">static</span> page /<span style="color: rgba(0, 0, 0, 1)">50x.html # error_page </span><span style="color: rgba(128, 0, 128, 1)">500</span> <span style="color: rgba(128, 0, 128, 1)">502</span> <span style="color: rgba(128, 0, 128, 1)">503</span> <span style="color: rgba(128, 0, 128, 1)">504</span> /<span style="color: rgba(0, 0, 0, 1)">50x.html; location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html { root html; } # proxy the PHP scripts to Apache listening on </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)"> # #location </span>~<span style="color: rgba(0, 0, 0, 1)"> \.php$ { # proxy_pass http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">127.0.0.1;</span>
#}
# pass the PHP scripts to FastCGI server listening on </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)"> # #location </span>~<span style="color: rgba(0, 0, 0, 1)"> \.php$ { # root html; # fastcgi_pass </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME </span>/<span style="color: rgba(0, 0, 0, 1)">scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, </span><span style="color: rgba(0, 0, 255, 1)">if</span> Apache<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s document root</span> # concurs with nginx<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s one</span>
#
#location ~ /.ht {
# deny all;
#}
}# another </span><span style="color: rgba(0, 0, 255, 1)">virtual</span> host <span style="color: rgba(0, 0, 255, 1)">using</span> mix of IP-, name-, and port-<span style="color: rgba(0, 0, 0, 1)">based configuration # #server { # listen </span><span style="color: rgba(128, 0, 128, 1)">8000</span><span style="color: rgba(0, 0, 0, 1)">; # listen somename:</span><span style="color: rgba(128, 0, 128, 1)">8080</span><span style="color: rgba(0, 0, 0, 1)">; # server_name somename alias another.alias; # location </span>/<span style="color: rgba(0, 0, 0, 1)"> { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen </span><span style="color: rgba(128, 0, 128, 1)">443</span><span style="color: rgba(0, 0, 0, 1)"> 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:</span>!aNULL:!<span style="color: rgba(0, 0, 0, 1)">MD5; # ssl_prefer_server_ciphers on; # location </span>/<span style="color: rgba(0, 0, 0, 1)"> { # 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一直處於僵死狀態。