第一步 搭建Linux虛擬機環境
因爲 FastDFS 必須在 Linux 系統上運行,所以這裏準備好 Linux 環境,筆者的是 CentOS7, 並且是最小化安裝,
這裏提供 VMware 虛擬機下的最小化的安裝包,初始用戶是:root, 密碼:123456,解壓直接運行,安全無毒,可以放心使用。百度雲:https://pan.baidu.com/s/1uxmDyhSueX7OIoRi6Auf0Q
第二步 配置虛擬機的靜態IP
正常情況下我們的 IP 都是動態變化的,每次變化之後我們都要相應調整,很麻煩,所以這裏固定一下 IP
步驟
- 因爲之後需要聯網,我們必須設置虛擬機的網絡適配器爲 NAT 或者 橋接模式,但不能是 僅主機 模式, 否則不能聯網
-
打開 VMware 的虛擬網絡編輯器: 菜單欄 —》編輯 —》虛擬網絡編輯器
-
查看 NAT模式 相關的參數(因爲我剛剛配FastDFS虛擬機配的是 NAT 模式,所以這裏查看 NAT模式的參數, 讀者若是 橋接模式,這裏請查看 橋接模式 的相關參數,但建議與筆者一致,因爲這兩種方式使用起來並沒什麼區別)
記錄相關參數:
子網IP:192.168.67.0
子網掩碼:255.255.255.0
網關IP:192.168.67.2
-
打開 FastDFS 虛擬機,修改 /etc/sysconfig/network-scripts/ifcfg-ens32 文件,請注意,讀者的網卡文件不一定是 ifcfg-ens32,但一定是 ifcfg-xxx 類型的文件名(ifcfg-lo除外)
[root@fastdfs:# network-scripts] vi /etc/sysconfig/network-scripts/ifcfg-ens32 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static # 將 dhcp(動態分配) 改成 static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens32 UUID=c119b162-a6a5-4f59-9176-5c8d9b1759d2 DEVICE=ens32 ONBOOT=yes # 改成 yes, 剛裝的時候可能是 no IPADDR=192.168.67.132 # 設置FastDFS的IP, 重要自己隨便起一個(192.168.67.1~192.168.67.254) GATEWAY=192.168.67.2 # 填入剛纔記錄的網關IP NETMASK=255.255.255.0 # 填入剛纔記錄的子網掩碼 DNS1=192.168.67.2 # 與 GATEWAY 一致
以上讀者視實際情況改動
建議在讀者自己的Windows和FastDFS的 host 文件中都添加一個好記的名稱,添加了之後等下配置的時候可以直接使用,省的多次寫 IP。(至於這個文件是什麼學過 計算機網絡 的童鞋應該清楚,不清出的可以百度,這裏不再贅述)
[root@fastdfs:# etc] vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.67.132 fastdfs.com # 我加的是 fastdfs.com
在Windows中無法改動 hosts 文件的讀者可以右鍵 hosts 文件,屬性,更改一下權限,修改完之後記得把權限改回來!具體方法百度。。
- 設置好 IP 之後可以重啓或 service network restart 讓改動生效
第三步 安裝 VIM
[root@fastdfs:# etc] yum install -y vim
安裝之後如果想讓 vim 編輯界面好看點可以修改 /etc/vimrc 文件,不改也沒關係的
root@fastdfs:# etc] vim /etc/vimrc
......
set nu 顯示行號
set ts=4 table鍵爲4個字符
set smartindent 智能換行
......
第四步 安裝 gcc/g++ 編譯器
因爲 FastDFS 是源碼,必須要經過編譯才能安裝,所以 gcc 編譯器必不可少
[root@fastdfs:# etc] yum install -y gcc gcc-c++
第五步 上傳安裝包到 FastDFS 服務器
這裏筆者提供好了:https://pan.baidu.com/s/17ewWvb8iYy_fgzJzn_HI6w
想自己下載的讀者可以自行百度
第六步 安裝 libfastcommon
- 解壓
[root@fastdfs:# software] tar -xvf libfastcommon-1.0.7.tar.gz
- 進入解壓後的目錄
[root@fastdfs:# software] cd libfastcommon-1.0.7
[root@fastdfs:# libfastcommon-1.0.7] ls
HISTORY INSTALL libfastcommon.spec make.sh README src
[root@fastdfs:# libfastcommon-1.0.7]
- 編譯
[root@fastdfs:# libfastcommon-1.0.7] ./make.sh
- 安裝
[root@fastdfs:# libfastcommon-1.0.7] ./make.sh install
第七步 創建軟連接
[root@fastdfs:# libfastcommon-1.0.7] ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
[root@fastdfs:# libfastcommon-1.0.7] ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
[root@fastdfs:# libfastcommon-1.0.7] ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
[root@fastdfs:# libfastcommon-1.0.7] ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
第八步 安裝FastDFs
進入安裝包文件夾,解壓FastDFS壓縮包
[root@fastdfs:# software] tar -xvf fastdfs-5.05.tar.gz
進入解壓後的目錄,編譯,安裝
[root@fastdfs:# software] cd fastdfs-5.05
[root@fastdfs:# fastdfs-5.05] ./make.sh
[root@fastdfs:# fastdfs-5.05] ./make.sh install
第九步 查看是否安裝成功
- 查看 /etc/init.d/ 文件夾是否有相關文件
[root@fastdfs:# fastdfs-5.05] cd /etc/init.d/ && ls | grep fdfs
fdfs_storaged
fdfs_trackerd
[root@fastdfs:# fastdfs-5.05]
- 查看配置文件是否存在
[root@fastdfs:# fdfs] cd /etc/fdfs/ ; ls | grep sample
client.conf.sample
storage.conf.sample
tracker.conf.sample
[root@fastdfs:# fdfs]
- 查看相關的腳本是否已經安裝
[root@fastdfs:# ~] cd /usr/bin/ && ls | grep fdfs
fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file
[root@fastdfs:# bin]
第十步 修改配置文件
因爲 FastDFS 服務腳本設置的 bin 目錄爲 /usr/local/bin/ 下,但是實際我們安
裝在了 /usr/bin/ 下面。所以我們需要修改 FastDFS 配置文件中的路徑,也就是
需要修改兩個配置文件
修改 /etc/init.d/fdfs_storaged
在命令模式下替換字符串, 底行輸入 “:%s+/usr/local/bin+/usr/bin”
[root@fastdfs:# bin] vim /etc/init.d/fdfs_storaged
提示有7處修改
同樣的,修改 vim /etc/init.d/fdfs_trackerd
[root@fastdfs:# bin] vim /etc/init.d/fdfs_trackerd
提示有7處修改
第十一步 配置跟蹤器
- 進入到 /etc/fdfs 目錄並且複製一份 tracker.conf.sample 並命名爲
tracker.conf
[root@fastdfs:# bin] cd /etc/fdfs/
[root@fastdfs:# fdfs] cp tracker.conf.sample tracker.conf
[root@fastdfs:# fdfs] vim /etc/fdfs/tracker.conf # 修改 base_path 爲 /opt/fdfs/tracker,這是一個目錄,自己可以隨意指定,目前還不存在,等下再創建
- 創建 FastDFS 的文件夾
[root@fastdfs:# opt] mkdir -p /opt/fdfs/tracker # -p 選項表示創建多級目錄
-
添加防火牆規則
修改 /etc/sysconfig/iptables 文件, 添加 -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
[root@fastdfs:# opt] vim /etc/sysconfig/iptables
-
重啓防火牆
[root@fastdfs:# opt] service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@fastdfs:# opt]
- 啓動 /etc/init.d/fdfs_trackerd start
[root@fastdfs:# opt] /etc/init.d/fdfs_trackerd start
Starting fdfs_trackerd (via systemctl): [ 確定 ]
[root@fastdfs:# opt]
- 查看 tracker 文件夾下多了兩個文件夾 data 和 logs
[root@fastdfs:# opt] ls /opt/fdfs/tracker/
data logs
[root@fastdfs:# opt]
-
設置 tracker 開機自啓動
在 rc.local 文件中添加 /etc/init.d/fdfs_trackerd start
[root@fastdfs:# opt] vim /etc/rc.d/rc.local
1 #!/bin/bash
2 # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
3 #
4 # It is highly advisable to create own systemd services or udev rules
5 # to run scripts during boot instead of using this file.
6 #
7 # In contrast to previous versions due to parallel execution during boot
8 # this script will NOT be run after all other services.
9 #
10 # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
11 # that this script will be executed during boot.
12
13 touch /var/lock/subsys/local
14 /etc/init.d/fdfs_trackerd start # 添加這個
第十二步 配置 FastDFS 存儲
- 進入 /etc/fdfs 目錄,複製一份 storage.conf.sample 文件並命名爲
storage.conf
[root@fastdfs:# opt] cd /etc/fdfs/
[root@fastdfs:# fdfs] cp storage.conf.sample storage.conf
- 修改存儲文件 storage.conf
[root@fastdfs:# fdfs] vim storage.conf
- 修改以下四項
。。。。
40 # the base path to store data and log files
41 base_path=/opt/fdfs/storage # 此目錄讀者可以酌情修改,並非固定,但建議與筆者一致
。。。
107 # store_path#, based 0, if store_path0 not exists, it's value is base_path
108 # the paths must be exist
109 store_path0=/opt/fdfs/storage
。。。。
116 # tracker_server can ocur more than once, and tracker_server format is
117 # "host:port", host can be hostname or ip address
118 tracker_server=fastdfs.com:22122 # 注意,這裏填自己的FastDFS虛擬機的IP:端口號, 比如:192.168.67.133:22122,因爲筆者先前在 /etc/hosts 文件中有過設置,所以也可以寫 fastdfs.com:22122,這樣好記,也利於修改
。。。。
280 # the port of the web server on this storage server
281 http.server_port=8888 # 8888 不是固定的,讀者可以酌情修改,但建議與筆者一致
- 創建存儲目錄
[root@fastdfs:# fdfs] mkdir -p /opt/fdfs/storage
- 修改防火牆,允許外界訪問 storage 的默認端口 23000
[root@fastdfs:# fdfs] vim /etc/sysconfig/iptables
- 重啓防火牆
[root@fastdfs:# ~] service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@fastdfs:# ~]
- 查看生成目錄,在防火牆重啓之後,啓動 srotage, /opt/fdfs/storage/ 目錄會生成 data 和 logs 兩個文件夾、
[root@fastdfs:# storage] /etc/init.d/fdfs_storaged start
[root@fastdfs:# ~] cd /opt/fdfs/storage/
[root@fastdfs:# storage] ll
總用量 12
drwxr-xr-x. 259 root root 8192 12月 19 21:36 data
drwxr-xr-x. 2 root root 26 11月 29 20:06 logs
[root@fastdfs:# storage]
第十三步 驗證 FastDFS tracker 和 storage 是否啓動成功
[root@fastdfs:# init.d] ps -ef | grep fdfs
root 1201 1 0 12:19 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/track
root 1226 1 0 12:19 ? 00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/stora
root 2217 1139 0 12:22 pts/0 00:00:00 grep --color=auto fdfs
[root@fastdfs:# init.d]
進入到 /fastdfs/storage/data/ 目錄下,可以看到兩級共 256*256 個目錄,每級都是從 00 到 FF,如下只是列出了第一級的目錄, 點進去每個目錄都還有 00 到 FF共 256 個目錄
[root@fastdfs:# init.d] cd /opt/fdfs/storage/data/
[root@fastdfs:# data] ls
00 12 24 36 48 5A 6C 7E 90 A2 B4 C6 D8 EA FC
01 13 25 37 49 5B 6D 7F 91 A3 B5 C7 D9 EB FD
02 14 26 38 4A 5C 6E 80 92 A4 B6 C8 DA EC fdfs_storaged.pid
03 15 27 39 4B 5D 6F 81 93 A5 B7 C9 DB ED FE
04 16 28 3A 4C 5E 70 82 94 A6 B8 CA DC EE FF
05 17 29 3B 4D 5F 71 83 95 A7 B9 CB DD EF M00
06 18 2A 3C 4E 60 72 84 96 A8 BA CC DE F0 storage_stat.dat
07 19 2B 3D 4F 61 73 85 97 A9 BB CD DF F1 sync
08 1A 2C 3E 50 62 74 86 98 AA BC CE E0 F2
09 1B 2D 3F 51 63 75 87 99 AB BD CF E1 F3
0A 1C 2E 40 52 64 76 88 9A AC BE D0 E2 F4
0B 1D 2F 41 53 65 77 89 9B AD BF D1 E3 F5
0C 1E 30 42 54 66 78 8A 9C AE C0 D2 E4 F6
0D 1F 31 43 55 67 79 8B 9D AF C1 D3 E5 F7
0E 20 32 44 56 68 7A 8C 9E B0 C2 D4 E6 F8
0F 21 33 45 57 69 7B 8D 9F B1 C3 D5 E7 F9
10 22 34 46 58 6A 7C 8E A0 B2 C4 D6 E8 FA
11 23 35 47 59 6B 7D 8F A1 B3 C5 D7 E9 FB
[root@fastdfs:# data]
- 設置 storage 開機自啓動,添加一行 /etc/init.d/fdfs_storaged start
root@fastdfs:# data] vim /etc/rc.local
1 #!/bin/bash
2 # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
3 #
4 # It is highly advisable to create own systemd services or udev rules
5 # to run scripts during boot instead of using this file.
6 #
7 # In contrast to previous versions due to parallel execution during boot
8 # this script will NOT be run after all other services.
9 #
10 # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
11 # that this script will be executed during boot.
12
13 touch /var/lock/subsys/local
14 /etc/init.d/fdfs_trackerd start
15 /etc/init.d/fdfs_storaged start # 添加這一行
第十四步 修改 client.conf 配置文件
- 進入到 /etc/fdfs 目錄下並複製一份 client.conf.sample 並更名爲 client.conf
[root@fastdfs:# data] cd /etc/fdfs
[root@fastdfs:# fdfs] cp client.conf.sample client.conf
[root@fastdfs:# fdfs] vim /etc/fdfs/client.conf
......
9 # the base path to store log files
10 base_path=/opt/fdfs/tracker # 修改這裏,這是一個目錄,等下需創建
11
12 # tracker_server can ocur more than once, and tracker_server format is
13 # "host:port", host can be hostname or ip address
14 tracker_server=fastdfs.com:22122 # 指定虛擬機IP, 跟先前一樣,讀者視實際情況修改
......
第十五步 測試上傳文件
筆者這裏準備了一張圖片用來測試文件上傳
[root@fastdfs:# software] ls
12.jpg fastdfs-nginx-module libfastcommon-1.0.7.tar.gz
fastdfs-5.05 fastdfs-nginx-module_v1.16.tar.gz nginx-1.6.2
fastdfs-5.05.tar.gz libfastcommon-1.0.7 nginx-1.6.2.tar.gz
[root@fastdfs:# software]
- 使用 fdfs_upload_file 腳本進行文件上傳操作
[root@fastdfs:# software] /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/software/12.jpg
group1/M00/00/00/wKhDhFwbRyaAEAaRAAYghmrYqsg575.jpg
[root@fastdfs:# software]
可見,已經上傳成功,返回的是文件存儲的位置
第十六步 安裝 nginx
-
準備 nginx 安裝包,這裏筆者已經準備好了
-
解壓縮
[root@fastdfs:# software] tar -xvf nginx-1.6.2.tar.gz
- 先安裝依賴
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
- 預編譯 & 安裝
[root@fastdfs:# software] cd nginx-1.6.2
[root@fastdfs:# nginx-1.6.2] ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 3.10.0-862.14.4.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
......
[root@fastdfs:# nginx-1.6.2] make && make install
- 啓動 nginx
[root@fastdfs:# software] /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=2416
[root@fastdfs:# nginx-1.6.2] ps -ef | grep nginx # 查看 nginx 是否啓動
root 2417 1 0 15:43 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 2418 2417 0 15:43 ? 00:00:00 nginx: worker process
root 4318 2388 0 15:51 pts/1 00:00:00 grep --color=auto nginx
[root@fastdfs:# nginx-1.6.2]
第十七步 安裝 FastDFS 與 Nginx 的依賴包
-
準備安裝包 fastdfs-nginx-module_v1.16.tar.gz
筆者已經在前面的百度盤連接中提供,有其他需要的讀者可以自行百度
-
同樣,解壓,安裝,這裏直接上過程,不做多餘贅述
[root@fastdfs:# software] tar -xvf fastdfs-nginx-module_v1.16.tar.gz
...
[root@fastdfs:# software] cd fastdfs-nginx-module/src/
[root@fastdfs:# src] ls
common.c common.h config mod_fastdfs.conf ngx_http_fastdfs_module.c
[root@fastdfs:# src]
- 修改 config 文件,把文件的第四行配置中的 /usr/local/include 都改爲*/usr/include*,共兩處。
[root@fastdfs:# src] vim config
1 ngx_addon_name=ngx_http_fastdfs_module
2 HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
3 NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
4 CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
5 CORE_LIBS="$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient"
6 CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_M
OD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
~
- 爲了將 nginx 與 fastdfs 相結合,我們先把這個 /usr/local/nginx 目錄刪除掉
[root@fastdfs:# src] cd /usr/local/
[root@fastdfs:# local] ls | grep nginx
nginx
[root@fastdfs:# local] rm -rf nginx
- 進入到 nginx-1.6.2/ 目錄下並執行配置和編譯安裝
[root@fastdfs:# local] cd ~/software/nginx-1.6.2
[root@fastdfs:# nginx-1.6.2] ls
[root@fastdfs:# nginx-1.6.2] ./configure --add-module=/root/software/fastdfs-nginx-module/src # 就是依賴包的位置
[root@fastdfs:# nginx-1.6.2] make && make install
- 複製 fastdfs-nginx-module 中的配置文件,到 /etc/fdfs 目錄中
[root@fastdfs:# src] cd /root/software/fastdfs-nginx-module/src
[root@fastdfs:# src] ls
common.c common.h config mod_fastdfs.conf ngx_http_fastdfs_module.c
[root@fastdfs:# src] cp mod_fastdfs.conf /etc/fdfs/
-
複製之後修改 /etc/fdfs/mod_fastdfs.conf
需要修改的項如下,其中第一項是超時時長,第二項是IP和端口,第三項是允許外界通過 http 方式訪問資源
1 # connect timeout in seconds
2 # default value is 30s
3 connect_timeout=10
......
37 # FastDFS tracker_server can ocur more than once, and tracker_server format is
38 # "host:port", host can be hostname or ip address
39 # valid only when load_fdfs_parameters_from_tracker is true
40 tracker_server=fastdfs.com:22122 # 視讀者實際情況填寫
......
49 # if the url / uri including the group name
50 # set to false when uri like /M00/00/00/xxx
51 # set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
52 # default value is false
53 url_have_group_name = true
......
59 # store_path#, based 0, if store_path0 not exists, it's value is base_path
60 # the paths must be exist
61 # must same as storage.conf
62 store_path0=/opt/fdfs/storage # 視讀者實際情況填寫
......
- 複製 FastDFS 裏的 2 個文件,到 /etc/fdfs 目錄中
[root@fastdfs:# conf] cd /root/software/fastdfs-5.05/conf
[root@fastdfs:# conf] ls
anti-steal.jpg http.conf storage.conf tracker.conf
client.conf mime.types storage_ids.conf
[root@fastdfs:# conf] cp http.conf mime.types /etc/fdfs/
- 創建一個軟連接, 在 /opt/fdfs/storage 文件存儲目錄下創建軟連接, 將其鏈接到
實際存放數據 的目錄
[root@fastdfs:# storage] ln -s /opt/fdfs/storage/data/ /opt/fdfs/storage/data/M00
第十八步 修改 nginx 的配置文件
[root@fastdfs:# storage] cd /usr/local/nginx/conf/
[root@fastdfs:# conf] vim nginx.conf
......
35 server {
36 listen 8888; # 修改這裏
37 server_name localhost;
38
39 #charset koi8-r;
40
41 #access_log logs/host.access.log main;
42
43 location ~/group([0-9])/M00 { # 修改這裏
44 ngx_fastdfs_module;
45 root html;
46 index index.html index.htm;
47 }
48
......
- 設置 nginx 開機自啓動,這樣下次重啓設備之後,tracker 、storage 、nginx 都自動啓動了,直接就可以使用服務
[root@fastdfs:# conf] vim /etc/rc.d/rc.local
1 #!/bin/bash
2 # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
3 #
4 # It is highly advisable to create own systemd services or udev rules
5 # to run scripts during boot instead of using this file.
6 #
7 # In contrast to previous versions due to parallel execution during boot
8 # this script will NOT be run after all other services.
9 #
10 # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
11 # that this script will be executed during boot.
12
13 touch /var/lock/subsys/local
14 /etc/init.d/fdfs_trackerd start
15 /etc/init.d/fdfs_storaged start
16 /usr/local/nginx /sbin/nginx
~
- 啓動 nginx
[root@fastdfs:# conf] /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=4718
-
在通過 8888 端口訪問圖片之前先配置下防火牆,允許外界訪問 8888 端口
-
配置完防火牆後重啓防火牆
OK! 大功告成!!!
現在,可以在你的Windows的瀏覽器中觀察是否可以看到剛纔上傳的圖片
在瀏覽器中輸入:http://fastdfs.com:8888/group1/M00/00/00/wKhDhFwbRyaAEAaRAAYghmrYqsg575.jpg
MarkDown文件已上傳至 github:https://github.com/lymboy/FASTDFS/blob/master/FastDFS服務器單機搭建.md