記一次在公司伺服器上安裝fastdfs的歷程
1.確保已安裝了gcc,unzip(基本工具)
2.安裝libfastcommon-master
步驟:
unzip libfastcommon-master.zip
cd libfastcommon-master
./make.sh
./make.sh install
3.安裝fastdfs-master
步驟:
unzip fastdfs-master-V5.05.zip
cd fastdfs-master
./make.sh
./make.sh install
4.配置並啟動FastDFS
4.1配置tracker伺服器
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vi /etc/fdfs/tracker.conf
# 修改的內容如下:
disabled=false # 啟用配置檔案
port=22122 # tracker伺服器埠(預設22122)
base_path=/fastdfs/tracker # 儲存日誌和資料的根目錄
新建目錄: /fastdfs/tracker
關閉防火牆: chkconfig iptables off
啟動tracker伺服器: /etc/init.d/fdfs_trackerd start
停止tracker伺服器: /etc/init.d/fdfs_trackerd stop
檢查FastDFS Tracker Server是否啟動成功:
ps -ef | grep fdfs_trackerd
設定tracker服務開機啟動:
chkconfig fdfs_trackerd on
備註:由於系統是ubuntu,無法使用chkconfig配置開機啟動,centos無此問題,尚未解決!
4.2配置storage伺服器:
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vi /etc/fdfs/storage.conf
# 修改的內容如下:
disabled=false # 啟用配置檔案
port=23000 # storage服務埠
base_path=/fastdfs/storage # 資料和日誌檔案儲存根目錄
store_path0=/fastdfs/storage # 第一個儲存目錄
tracker_server=192.168.56.101:22122 # tracker伺服器IP和埠
http.server_port=8888 # http訪問檔案的埠
新建目錄: /fastdfs/storage
關閉防火牆: chkconfig iptables off
啟動storage伺服器:/etc/init.d/fdfs_storaged start
停止storage伺服器:/etc/init.d/fdfs_storaged stop
檢查FastDFS Tracker Server是否啟動成功:
ps -ef | grep fdfs_storaged
設定storage服務開機啟動:
chkconfig fdfs_storaged on
備註:同上
5.測試FastDFS
5.1配置Tracker客戶端
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vi /etc/fdfs/client.conf
# 修改以下配置,其它保持預設
base_path=/fastdfs/tracker
tracker_server=192.168.56.101:22122
5.2執行客戶端上傳命令
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/mao/cs.jpg
group1/M00/00/00/wKg4ZVh8U-SANp6OAAHhOIDwENQ234.jpg
能返回以上檔案ID,說明檔案已經上傳成功
6使用nginx訪問FastDFS
6.1在所有storage節點安裝fastdfs-nginx-module
unzip fastdfs-nginx-module-master.zip
6.2安裝Nginx
centos下直接執行yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
但由於ubuntu無法這樣操作,所以我是最後一直報缺少pcre庫,先是通過sudo apt search pcre查詢庫,再通過sudo apt-get install libpcre3 libpcre3-dev安裝依賴庫解決了,當然,安裝過程中一直有沒許可權建立檔案/資料夾就不提了
注意:在執行./configure配置nginx引數的時候,需要將fastdfs-nginx-moudle原始碼作為模組編譯進去:
進入nginx-1.10.0.tar.gz解壓後的目錄
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/模組存放位置/fastdfs-nginx-module-master/src
編譯並安裝:
make && make install
啟動、停止、重啟
# 1.啟動nginx
nginx (通過絕對路徑啟動的nginx,因為系統中有安裝其他nginx,但是我不想改哪個nginx,所以重新安裝nginx,不同的路徑,還好最後沒影響)
# 可通過ps -ef | grep nginx檢視nginx是否已啟動成功
# 2.停止nginx
nginx -s stop
nginx亦未配置開機自啟
7配置nginx與FastDFS關聯配置檔案
複製 fastdfs-nginx-module 原始碼中的配置檔案到/etc/fdfs 目錄, 並修改
cp /usr/local/mao/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
修改以下配置:
connect_timeout=10 # 客戶端訪問檔案連線超時時長(單位:秒)
base_path=/tmp # 臨時目錄
tracker_server=192.168.56.101:22122 # tracker服務IP和埠
storage_server_port=23000 # storage服務埠
group_name=group1 # 組名
url_have_group_name=true # 訪問連結字首加上組名
store_path0=/fastdfs/storage # 檔案儲存路徑
複製 FastDFS 的部分配置檔案到/etc/fdfs 目錄
cd /usr/local/mao/fastdfs-master/conf/
cp http.conf mime.types /etc/fdfs/
在/fastdfs/storage檔案儲存目錄下建立軟連線,將其連結到實際存放資料的目錄
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
配置nginx訪問storage檔案
vi /opt/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8888;
server_name localhost;
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
8啟動測試
nginx -s reload
啟動nginx後,如果打印出ngx_http_fastdfs_set pid=xxx,表示fastdfs-nginx-module模組載入成功了,xxx表示模組的程序ID。
如果報:
nginx: [error] open() "/opt/nginx/logs/nginx.pid" failed (2: No such file or directory)
則執行:nginx -c /opt/nginx/conf/nginx.conf
成功後訪問ip:8888/ 頁面出現welcome to nginx! 就可以通過ip:8888/00/00/xxxxxxxxxxxxxxxxxxxxx.jpg訪問圖片資源了
總結:如果是centos配置並且伺服器本來沒有nginx的話,那直接安裝上面一步步來就很輕鬆了,如果是ubuntu並且伺服器本來就有nginx的話,那可能需要確認的就多了,比如原來nginx啟動和安裝的位置,要保證不衝突,其次各個依賴庫的安裝,最後絕對路徑下啟動以及開機自啟的問題