圖片伺服器搭建教程,伺服器搭建教程
一、安裝並配置FastDFS1、運行環境及相關軟體
CentOS 7.4 64位FastDFS_v5.08.tar.gznginx-1.8.1.tar.gzfastdfs-nginx-module_v1.16.tar.gzlibfastcommon
2、伺服器規劃
| 伺服器名稱 |
IP地址和連接埠 |
備忘 |
| fastdfs-tracker |
172.17.156.5:22122 |
Tracing Service器/調度伺服器 |
| fastdfs-storage |
172.17.156.5:23000 |
儲存伺服器 |
3、安裝FastDFS3.1 首先建立工具目錄(非必須)
[root@fastdfs-storage ~]# mkdir -p /home/oldcat/tools[root@fastdfs-storage ~]# cd /home/oldcat/tools/
3.2 下載並安裝FastDFS依賴包libfastcommon
[root@fastdfs-storage tools]# wget https://codeload.github.com/happyfish100/libfastcommon/zip/master[root@fastdfs-storage tools]# unzip master[root@fastdfs-storage tools]# cd libfastcommon-master/[root@fastdfs-storage libfastcommon-master]# lsHISTORY INSTALL libfastcommon.spec make.sh php-fastcommon README src[root@fastdfs-storage libfastcommon-master]# ./make.sh[root@fastdfs-storage libfastcommon-master]# ./make.sh install
提示:如果執行第二句話報以下錯,沒報錯直接3.3
-bash: unzip: command not found
則是命令沒有找到,其原因肯定是沒有安裝unzip。
安裝命令是:
yum install -y unzip zip
3.3 下載並安裝FastDFS
[root@fastdfs-tracker tools]# wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.08/FastDFS_v5.08.tar.gz[root@fastdfs-tracker tools]# tar xf FastDFS_v5.08.tar.gz[root@fastdfs-tracker tools]# cd FastDFS[root@fastdfs-tracker FastDFS]# ./make.sh && ./make.sh install
採用預設安裝後的檔案及目錄:
- 服務指令碼:
[root@fastdfs-tracker ~]# ll /etc/init.d/ |grep fdfs-rwxr-xr-x. 1 root root 918 4月 22 22:08 fdfs_storaged-rwxr-xr-x. 1 root root 920 4月 22 22:08 fdfs_trackerd
範例設定檔
[root@fastdfs-tracker ~]# ll /etc/fdfs/總用量 20-rw-r--r--. 1 root root 1461 4月 22 22:08 client.conf.sample-rw-r--r--. 1 root root 7927 4月 22 22:08 storage.conf.sample-rw-r--r--. 1 root root 7200 4月 22 22:08 tracker.conf.sample
命令列工具
[root@fastdfs-tracker ~]# ll /usr/bin|grep fdfs-rwxr-xr-x. 1 root root 252272 4月 22 22:08 fdfs_appender_test-rwxr-xr-x. 1 root root 252225 4月 22 22:08 fdfs_appender_test1-rwxr-xr-x. 1 root root 242449 4月 22 22:08 fdfs_append_file-rwxr-xr-x. 1 root root 242013 4月 22 22:08 fdfs_crc32-rwxr-xr-x. 1 root root 242508 4月 22 22:08 fdfs_delete_file-rwxr-xr-x. 1 root root 243627 4月 22 22:08 fdfs_download_file-rwxr-xr-x. 1 root root 243369 4月 22 22:08 fdfs_file_info-rwxr-xr-x. 1 root root 255657 4月 22 22:08 fdfs_monitor-rwxr-xr-x. 1 root root 863913 4月 22 22:08 fdfs_storaged-rwxr-xr-x. 1 root root 258712 4月 22 22:08 fdfs_test-rwxr-xr-x. 1 root root 257881 4月 22 22:08 fdfs_test1-rwxr-xr-x. 1 root root 365232 4月 22 22:08 fdfs_trackerd-rwxr-xr-x. 1 root root 243547 4月 22 22:08 fdfs_upload_appender-rwxr-xr-x. 1 root root 244453 4月 22 22:08 fdfs_upload_file
注意:雖然FastDFS區分tracker和storage伺服器,但是安裝的軟體及步驟均相同,只是不同的設定檔而已,因此以上安裝適用tracker server和storage server4、配置Tracing Service器(tracker server)4.1拷貝tracker server和client端範例設定檔並重新命名
[root@fastdfs-tracker ~]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf[root@fastdfs-storage ~]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
4.2 編輯tracker server設定檔tracker.conf,需要修改內容如下:
disabled=false(預設為false,表示是否無效)port=22122(預設為22122)base_path=/data/fastdfs/tracker
4.3 編輯client端的設定檔client.conf,需要修改內如下
base_path=/data/fastdfs/trackertracker_server=172.17.156.5:22122
IP填寫自己伺服器的私人IP,如果是虛擬機器直接用下面的
例:192.168.25.133
4.4 建立tracker server資料目錄
[root@fastdfs-tracker ~]# mkdir -p /data/fastdfs/tracker
4.5 測試啟動trackerserver,啟動成功會自動在/data/fastdfs/tracker目錄建data和logs目錄
[root@fastdfs-tracker ~]# cd /data/fastdfs/tracker/[root@fastdfs-tracker tracker]# ls[root@fastdfs-tracker tracker]# /etc/init.d/fdfs_trackerd startStarting FastDFS tracker server:[root@fastdfs-tracker tracker]# ss -lntup|grep 22122tcp LISTEN 0 128 *:22122 *:* users:(("fdfs_trackerd",3785,5)) [root@fastdfs-tracker tracker]# lsdata logs4.6 關閉tracker server
[root@fastdfs-tracker tracker]# /etc/init.d/fdfs_trackerd stopwaiting for pid [3785] exit ...pid [3785] exit.
5、配置儲存伺服器(storage server)5.1 拷貝storage server範例設定檔並重新命名
[root@fastdfs-storage ~]# cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
5.2 編輯storage server設定檔storage.conf,需要修改內容如下:
disabled=false(預設為false,表示是否無效)port=23000(預設為23000)base_path=/data/fastdfs/storagetracker_server=172.17.156.5:22122store_path0=/data/fastdfs/storagehttp.server_port=8888(預設為8888,nginx中配置的聽連接埠那之一致)
IP填寫自己伺服器的私人IP,如果是虛擬機器直接用下面的,必須與上面的tracker server一致
例:192.168.25.133
5.3 建立storage server資料目錄
[root@fastdfs-storage ~]# mkdir -p /data/fastdfs/storage
5.4 測試啟動storage server,啟動成功會自動在/data/fastdfs/tracker目錄建立data和logs目錄(啟動storage server的前提是tracker server必須事先已啟動)
[root@fastdfs-storage ~]# cd /data/fastdfs/storage/[root@fastdfs-storage storage]# ls[root@fastdfs-storage storage]# /etc/init.d/fdfs_storaged startStarting FastDFS storage server:[root@fastdfs-storage storage]# ss -lntup|grep 23000tcp LISTEN 0 128 *:23000 *:* users:(("fdfs_storaged",3786,5))[root@fastdfs-storage storage]# lsdata logs6、檔案上傳測試
執行如下上傳命令:
[root@fastdfs-tracker ~]#mkdir /home/oldcat/imgs然後通過sftp傳一張圖片到伺服器即可(改名為test.xxx);
[root@fastdfs-tracker ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/oldcat/imgs/test.jpggroup1/M00/00/00/rBIK6VcaP0aARXXvAAHrUgHEviQ394.jpg返迴文件ID即說明檔案已經上傳成功
二、儲存伺服器(storage server)安裝並配置nginx1、下載並安裝fastdfs-nginx-module模組
註:FastDFS通過Tracker伺服器,將檔案放在Storage伺服器儲存,但是同組儲存伺服器之間需要進入檔案複製,有同步延遲的問題。假設Tracker伺服器將檔案上傳到了192.168.4.125,上傳成功後檔案ID已經返回給用戶端。此時FastDFS儲存叢集機制會將這個檔案同步到同組儲存192.168.4.126,在檔案還沒有複製完成的情況下,用戶端如果用這個檔案ID在192.168.4.126上取檔案,就會出現檔案無法訪問的錯誤。而fastdfs-nginx-module可以重新導向檔案串連到原始伺服器取檔案,避免用戶端由於複寫延遲導致的檔案無法訪問錯誤。
[root@fastdfs-storage tools]# wget http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz[root@fastdfs-storage tools]# tar xf fastdfs-nginx-module_v1.16.tar.gz [root@fastdfs-storage tools]# cd fastdfs-nginx-module/src/[root@fastdfs-storage src]# vim config編輯config檔案,執行如下命令進行批量替換並儲存退出:%s+/usr/local/+/usr/+g
2、拷貝fastdfs-nginx-module模組中設定檔到/etc/fdfs目錄中並編輯
[root@fastdfs-storage ~]# cp /home/oldcat/tools/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/[root@fastdfs-storage ~]# vim /etc/fdfs/mod_fastdfs.conf修改內容如下:connect_timeout=10base_path=/tmp(預設為/tmp)tracker_server=172.17.156.5:22122(雲端服務器為私人IP)storage_server_port=23000(預設配置為23000)url_have_group_name = truestore_path0=/data/fastdfs/storagegroup_name=group1(預設配置為group1)
3、安裝nginx依賴庫
[root@fastdfs-storage nginx-1.8.1]# yum install -y pcre-devel zlib-devel
4、下載並安裝nginx
[root@fastdfs-storage tools]# wget http://nginx.org/download/nginx-1.8.1.tar.gz[root@fastdfs-storage tools]# tar xf nginx-1.8.1.tar.gz[root@fastdfs-storage tools]# cd nginx-1.8.1[root@fastdfs-storage nginx-1.8.1]# ./configure --prefix=/application/nginx/ --add-module=../fastdfs-nginx-module/src/[root@fastdfs-storage nginx-1.8.1]# make && make install
5、拷貝FastDFS中的部分設定檔到/etc/fdfs目錄中
[root@fastdfs-storage ~]# cp /home/oldcat/tools/FastDFS/conf/http.conf /etc/fdfs/[root@fastdfs-storage ~]# cp /home/oldcat/tools/FastDFS/conf/mime.types /etc/fdfs/
6、配置nginx,如下所示:
[root@fastdfs-storage ~]# vim /application/nginx/conf/nginx.conf user root; 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]/ { ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
說明:
- “user root”是解決下載操作時報404的問題
- 8888連接埠號碼與/etc/fdfs/storage.conf中的http.server_port=8888相對應
- storage對應有多個group的情況下,訪問路徑帶group名稱,例如:/group1/M00/00/00/**,對應nginx配置:
location ~/group[0-9]/ { ngx_fastdfs_module; }7、拷貝nginx服務到/etc/init.d/目錄下並啟動
[root@fastdfs-storage ~]# cp /application/nginx/sbin/nginx /etc/init.d/[root@fastdfs-storage ~]# /etc/init.d/nginx[root@fastdfs-storage ~]# ss -lntup|grep 8888tcp LISTEN 0 128 *:8888 *:* users:(("nginx",7308,6),("nginx",7309,6))8、通過瀏覽器訪問之前已經上傳的檔案
例:IP:8888/group1/M00/00/00/rBGcBVqBZ_mAGl9oAAEfvSdB99w682.jpg
瀏覽器可以成功訪問到上傳的圖片,說明使用FastDFS搭建的圖片伺服器成功完成!
注意:若是在遠程伺服器上此處IP應為公網IP,並且伺服器需要設定安全性群組開放8888連接埠