fastdfs-v6.06安裝部署

目錄

一、部署的環境準備

1.1 機器準備

一臺虛擬機:
hostname:		myhost
ip地址:		192.168.2.122

1.2 系統環境

[root@myhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core) 
[root@myhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.122  netmask 255.255.255.0  broadcast 10.199.41.255
        inet6 fe80::215:5dff:fe89:cd43  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:89:cd:43  txqueuelen 1000  (Ethernet)
        RX packets 1365008  bytes 289565779 (276.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 213902  bytes 207232091 (197.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 7772  bytes 435190 (424.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7772  bytes 435190 (424.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
		
[root@myhost ~]# getenforce
Enforcing
[root@myhost ~]# setenforce 0
[root@myhost ~]# getenforce
Permissive
[root@myhost ~]# sed -i "s#enforcing#disabled#g"  /etc/sysconfig/selinux 

[root@myhost ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_seq=1 ttl=54 time=31.6 ms
64 bytes from 14.215.177.38: icmp_seq=2 ttl=54 time=32.0 ms
64 bytes from 14.215.177.38: icmp_seq=3 ttl=54 time=31.6 ms
64 bytes from 14.215.177.38: icmp_seq=4 ttl=54 time=31.0 ms

二、fastdfs安裝及配置

2.0 安裝軟件及版本

  • libfastcommon-1.0.43.tar.gz
  • fastdfs-6.06.tar.gz
  • fastdfs-nginx-module-1.22.tar.gz
  • nginx-1.16.1.tar.gz

2.1 安裝依賴包

[root@myhost ~]# yum groupinstall "Development Tools" "Server platform Development" -y
[root@myhost ~]# yum install -y libao*

2.2 安裝libfastcommon

2.2.1 準備libfastcommon軟件安裝包

[root@myhost ~]# cd /home/tools/
[root@myhost tools]# ll
總用量 5526
-rw-r--r--. 1 root root    166526 6月   8 13:27 libfastcommon-1.0.43.tar.gz

2.2.2 解壓libfastcommon軟件安裝包

[root@myhost tools]# tar xf libfastcommon-1.0.43.tar.gz
[root@myhost tools]# cd libfastcommon-1.0.43
[root@myhost libfastcommon-1.0.43]# ll
總用量 40
drwxrwxr-x. 2 root root  4096 12月 25 20:35 doc
-rw-rw-r--. 1 root root 10301 12月 25 20:35 HISTORY
-rw-rw-r--. 1 root root   674 12月 25 20:35 INSTALL
-rw-rw-r--. 1 root root  1607 12月 25 20:35 libfastcommon.spec
-rwxrwxr-x. 1 root root  3253 12月 25 20:35 make.sh   #### 編譯安裝腳本 ####
drwxrwxr-x. 2 root root  4096 12月 25 20:35 php-fastcommon
-rw-rw-r--. 1 root root  2776 12月 25 20:35 README
drwxrwxr-x. 3 root root  4096 12月 25 20:35 src

2.2.3 編譯安裝libfastcommon

[root@myhost libfastcommon-1.0.43]#  ./make.sh && ./make.sh install
[root@myhost libfastcommon-1.0.43]# echo $?
0
[root@myhost libfastcommon-1.0.43]# ls /usr/lib64|grep libfastcommon
libfastcommon.so
[root@myhost libfastcommon-1.0.43]#  ls /usr/lib|grep libfastcommon
libfastcommon.so

#至此libfastcommon就已經安裝成功了,
#但注意一下: libfastcommon.so 默認安裝到了/usr/lib64/libfastcommon.so以及/usr/lib/libfastcommon.so裏面了。

2.3 安裝fastdfs主程序

2.3.1 準備fastdfs軟件安裝包

[root@myhost libfastcommon-1.0.43]# cd ../
[root@myhost tools]# ll
總用量 37560
-rwxr-xr-x.  1 root root    809328 6月   9 14:03 fastdfs-6.06.tar.gz
drwxrwxr-x.  5 root root      4096 6月   9 14:14 libfastcommon-1.0.43
-rwxr-xr-x.  1 root root    166526 6月   9 14:03 libfastcommon-1.0.43.tar.gz

2.3.2 解壓fastdfs軟件安裝包

[root@myhost tools]# tar xf fastdfs-6.06.tar.gz

[root@myhost tools]# cd fastdfs-6.06
[root@myhost fastdfs-6.06]# ll
總用量 148
drwxrwxr-x. 3 root root  4096 12月 31 07:36 client
drwxrwxr-x. 2 root root  4096 12月 31 07:36 common
drwxrwxr-x. 2 root root  4096 12月 31 07:36 conf
-rw-rw-r--. 1 root root 35067 12月 31 07:36 COPYING-3_0.txt
drwxrwxr-x. 4 root root    54 12月 31 07:36 docker
-rw-rw-r--. 1 root root  3170 12月 31 07:36 fastdfs.spec
-rw-rw-r--. 1 root root 36881 12月 31 07:36 HISTORY
drwxrwxr-x. 2 root root    26 12月 31 07:36 images
drwxrwxr-x. 2 root root    46 12月 31 07:36 init.d
-rw-rw-r--. 1 root root  8344 12月 31 07:36 INSTALL
-rwxrwxr-x. 1 root root  5517 12月 31 07:36 make.sh  #### 編譯安裝腳本 ####
drwxrwxr-x. 2 root root  4096 12月 31 07:36 php_client
-rw-rw-r--. 1 root root  2448 12月 31 07:36 README.md
-rw-rw-r--. 1 root root  1700 12月 31 07:36 README_zh.md
-rwxrwxr-x. 1 root root  1768 12月 31 07:36 restart.sh
-rwxrwxr-x. 1 root root   763 12月 31 07:36 setup.sh
-rwxrwxr-x. 1 root root  1680 12月 31 07:36 stop.sh
drwxrwxr-x. 4 root root  4096 12月 31 07:36 storage
drwxrwxr-x. 2 root root  4096 12月 31 07:36 test
drwxrwxr-x. 2 root root  4096 12月 31 07:36 tracker

2.3.3 編譯安裝fastdfs主程序

[root@myhost fastdfs-6.06]# ./make.sh && ./make.sh install
[root@myhost fastdfs-6.06]#  echo $?
0
[root@myhost fastdfs-6.06]# ls /usr/bin|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_regenerate_filename
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file

2.3.4 進入fastdfs的配置文件目錄/etc/fdfs

[root@myhost fastdfs-6.06]# cd /etc/fdfs/
[root@myhost fdfs]# ll
總用量 32
-rw-r--r--. 1 root root  1909 6月   8 13:40 client.conf.sample
-rw-r--r--. 1 root root 10246 6月   8 13:40 storage.conf.sample
-rw-r--r--. 1 root root   620 6月   8 13:40 storage_ids.conf.sample
-rw-r--r--. 1 root root  9138 6月   8 13:40 tracker.conf.sample

fastdfs主程序安裝成功後就會生成如上的3個.sample文件(示例配置文件),我們再分別拷貝出3個以備後面用正式配置的時候用。

[root@myhost fdfs]# cp ./client.conf.sample   ./client.conf
[root@myhost fdfs]# cp ./storage.conf.sample  ./storage.conf
[root@myhost fdfs]# cp ./tracker.conf.sample  ./tracker.conf
[root@myhost fdfs]# ll
總用量 60
-rw-r--r--. 1 root root  1909 6月   8 13:42 client.conf
-rw-r--r--. 1 root root  1909 6月   8 13:40 client.conf.sample
-rw-r--r--. 1 root root 10246 6月   8 13:42 storage.conf
-rw-r--r--. 1 root root 10246 6月   8 13:40 storage.conf.sample
-rw-r--r--. 1 root root   620 6月   8 13:40 storage_ids.conf.sample
-rw-r--r--. 1 root root  9138 6月   8 13:42 tracker.conf
-rw-r--r--. 1 root root  9138 6月   8 13:40 tracker.conf.sample

至此,fastdfs已經安裝完畢,接下來的工作就是依次配置tracker和storage了。

2.4 fastdfs關聯tracker配置

2.4.1 創建tracker服務器文件存放路徑

  • 在配置tracker之前,首先需要創建tracker服務器的文件路徑,即用於存儲tracker的數據文件和日誌文件等,
  • 這裏我選擇在/home/fastdfs目錄下創建一個fastdfs_tracker目錄用於存放tracker服務器的相關文件。
[root@myhost fdfs]#  mkdir /home/fastdfs
[root@myhost fdfs]#  cd /home/fastdfs/
[root@myhost fastdfs]# mkdir  fastdfs_tracker
[root@myhost fastdfs]# ll
總用量 0
drwxr-xr-x. 2 root root 6 6月   8 13:43 fastdfs_tracker

2.4.2 編輯安裝fastdfs軟件時產生的tracker.conf配置文件

[root@myhost fdfs]# vi /etc/fdfs/tracker.conf
[root@myhost fdfs]# egrep -v "^$|#" /etc/fdfs/tracker.conf
disabled = false 	#啓用配置文件(默認啓用)
bind_addr =
port = 22122 #設置tracker的端口號,通常採用22122這個默認端口
connect_timeout = 30
network_timeout = 60
base_path = /home/fastdfs/fastdfs_tracker #設置tracker的數據文件和日誌目錄
max_connections = 1024
accept_threads = 1
work_threads = 4
min_buff_size = 8KB
max_buff_size = 128KB
store_lookup = 2
store_group = group2
store_server = 0
store_path = 0
download_server = 0
reserved_storage_space = 20%
log_level = info
run_by_group=
run_by_user =
allow_hosts = *
sync_log_buff_interval = 1
check_active_interval = 120
thread_stack_size = 256KB
storage_ip_changed_auto_adjust = true
storage_sync_file_max_delay = 86400
storage_sync_file_max_time = 300
use_trunk_file = false 
slot_min_size = 256
slot_max_size = 1MB
trunk_alloc_alignment_size = 256
trunk_free_space_merge = true
delete_unused_trunk_files = false
trunk_file_size = 64MB
trunk_create_file_advance = false
trunk_create_file_time_base = 02:00
trunk_create_file_interval = 86400
trunk_create_file_space_threshold = 20G
trunk_init_check_occupying = false
trunk_init_reload_from_binlog = false
trunk_compress_binlog_min_interval = 86400
trunk_compress_binlog_interval = 86400
trunk_compress_binlog_time_base = 03:00
trunk_binlog_max_backups = 7
use_storage_id = false
storage_ids_filename = storage_ids.conf
id_type_in_filename = id
store_slave_file_use_link = false
rotate_error_log = false
error_log_rotate_time = 00:00
compress_old_error_log = false
compress_error_log_days_before = 7
rotate_error_log_size = 0
log_file_keep_days = 0
use_connection_pool = true
connection_pool_max_idle_time = 3600
http.server_port = 89   #設置tracker服務的http端口號,和tracker-nginx的http端口相關
http.check_alive_interval = 30
http.check_alive_type = tcp
http.check_alive_uri = /status.html

2.4.3 啓動fdfs_trackerd服務

[root@myhost fdfs]# /etc/init.d/fdfs_trackerd start
Reloading systemd:                                         [  確定  ]
Starting fdfs_trackerd (via systemctl):                    [  確定  ]

[root@myhost fdfs]# /etc/init.d/fdfs_trackerd status
● fdfs_trackerd.service - LSB: FastDFS tracker server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_trackerd; bad; vendor preset: disabled)
   Active: active (running) since 一 2020-06-08 13:51:29 CST; 4s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1413 ExecStart=/etc/rc.d/init.d/fdfs_trackerd start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/fdfs_trackerd.service
           └─1416 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

6月 08 13:51:29 myhost systemd[1]: Starting LSB: FastDFS tracker server...
6月 08 13:51:29 myhost fdfs_trackerd[1413]: Starting FastDFS tracker server:
6月 08 13:51:29 myhost systemd[1]: Started LSB: FastDFS tracker server.

如果啓動命令執行成功,那麼同時在剛纔創建的tracker文件目錄/home/fastdfs/fastdfs_tracker中就可以看到啓動後新生成的data和logs目錄,tracker服務的端口也應當被正常監聽,最後再通過netstat命令查看一下端口監聽情況:

[root@myhost fdfs]# netstat -lntup |grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      1416/fdfs_trackerd  

2.5 fastdfs關聯storage配置

2.5.1 創建storage服務器文件存放路徑
同理,步驟基本與配置tracker一致,首先是創建storage服務器的文件目錄,
需要注意的是同tracker相比多建了一個目錄,因爲storage還需要一個文件存儲路徑,用於存放接收的文件。

[root@myhost fdfs]#  cd /home/fastdfs/
[root@myhost fastdfs]# mkdir  fastdfs_storage  fastdfs_storage_data
[root@myhost fastdfs]# ll
總用量 0
drwxr-xr-x. 2 root root 6 6月   8 13:43 fastdfs_storage
drwxr-xr-x. 2 root root 6 6月   8 13:43 fastdfs_storage_data
drwxr-xr-x. 2 root root 6 6月   8 13:43 fastdfs_tracker

2.5.2 編輯安裝fastdfs軟件時產生的storage.conf配置文件

[root@myhost fdfs]# vi storage.conf
[root@myhost fdfs]# egrep -v "^$|#"  /etc/fdfs/storage.conf
disabled=false  #啓用配置文件(默認啓用)
group_name=group1  #組名,根據實際情況修改
bind_addr=
client_bind=true
port=23000   	#設置storage的端口號,默認是23000,同一個組的storage端口號必須一致
connect_timeout=30
network_timeout=60
heart_beat_interval=30
stat_report_interval=60
base_path=/home/fastdfs/fastdfs_storage  #設置storage數據文件和日誌目錄
max_connections=256
buff_size = 256KB
accept_threads=1
work_threads=4
disk_rw_separated = true
disk_reader_threads = 1
disk_writer_threads = 1
sync_wait_msec=50
sync_interval=0
sync_start_time=00:00
sync_end_time=23:59
write_mark_file_freq=500
store_path_count=1  #存儲路徑個數,需要和store_path個數匹配
store_path0=/home/fastdfs/fastdfs_storage_data  #圖片文件實際存儲路徑
subdir_count_per_path=256
tracker_server=192.168.2.122:22122
#tracker 服務器的 IP地址和端口號,
#如果是單機搭建,IP不要寫127.0.0.1,否則啓動不成功(此處的ip是我的CentOS虛擬機ip,也不是映射的公網地址)
log_level=info
run_by_group=
run_by_user=
allow_hosts=*
file_distribute_path_mode=0
file_distribute_rotate_count=100
fsync_after_written_bytes=0
sync_log_buff_interval=10
sync_binlog_buff_interval=10
sync_stat_file_interval=300
thread_stack_size=512KB
upload_priority=10
if_alias_prefix=
check_file_duplicate=0
file_signature_method=hash
key_namespace=FastDFS
keep_alive=0
use_access_log = false
rotate_access_log = false
access_log_rotate_time=00:00
rotate_error_log = false
error_log_rotate_time=00:00
rotate_access_log_size = 0
rotate_error_log_size = 0
log_file_keep_days = 0
file_sync_skip_invalid_record=false
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.domain_name=
http.server_port=88   #設置storage的http 端口號,和stroage-nginx的http端口相關

2.5.4 啓動fdfs_storaged服務器

[root@myhost fdfs]# /etc/init.d/fdfs_storaged start
Starting fdfs_storaged (via systemctl):                    [  確定  ]

[root@myhost fdfs]# /etc/init.d/fdfs_storaged status
● fdfs_storaged.service - LSB: FastDFS storage server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_storaged; bad; vendor preset: disabled)
   Active: active (running) since 二 2020-06-09 14:21:28 CST; 5h 35min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 21849 ExecStart=/etc/rc.d/init.d/fdfs_storaged start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/fdfs_storaged.service
           └─21852 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf

6月 09 14:21:28 dscq-122 systemd[1]: Starting LSB: FastDFS storage server...
6月 09 14:21:28 dscq-122 fdfs_storaged[21849]: Starting FastDFS storage server:
6月 09 14:21:28 dscq-122 systemd[1]: Started LSB: FastDFS storage server.

說明:storage服務下的data下有256個1級目錄,每級目錄下又有256個2級子目錄,總共65536個文件,新寫的文件會以hash的方式被路由到其中某個子目錄下,然後將文件數據直接作爲一個本地文件存儲到該目錄中。那麼最後我們再看一下storage服務的端口監聽情況。

[root@myhost fdfs]# netstat -lntup |grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      1523/fdfs_trackerd  
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      1552/fdfs_storaged  

2.6 檢查fastdfs的tracker與storage是否整合成功

上面可以看到此時已經正常監聽tracker的22122端口和storage的23000端口,至此storage服務器就已經配置完成,確定了storage服務器啓動成功後,還有一項工作就是看看storage服務器是否已經登記到tracker服務器(也可以理解爲tracker與storage是否整合成功),運行以下命令:

[root@myhost fdfs]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2020-06-08 13:58:56] DEBUG - base_path=/home/fastdfs/fastdfs_storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

server_count=1, server_index=0

tracker server is 192.168.2.122:22122

group count: 1

Group 1:
group name = group1
disk total space = 148,991 MB
disk free space = 148,279 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 88
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

        Storage 1:
                id = 192.168.2.122
                ip_addr = 192.168.2.122  ACTIVE
                http domain = 
                version = 6.06
                join time = 2020-06-08 13:56:08
                up time = 2020-06-08 13:56:08
                total storage = 148,991 MB
                free storage = 148,279 MB
                upload priority = 10
                store_path_count = 1
                subdir_count_per_path = 256
                storage_port = 23000
                storage_http_port = 88
                current_write_path = 0
                source storage id = 
                if_trunk_server = 0
                connection.alloc_count = 256
                connection.current_count = 0
                connection.max_count = 0
                total_upload_count = 0
                success_upload_count = 0
                total_append_count = 0
                success_append_count = 0
                total_modify_count = 0
                success_modify_count = 0
                total_truncate_count = 0
                success_truncate_count = 0
                total_set_meta_count = 0
                success_set_meta_count = 0
                total_delete_count = 0
                success_delete_count = 0
                total_download_count = 0
                success_download_count = 0
                total_get_meta_count = 0
                success_get_meta_count = 0
                total_create_link_count = 0
                success_create_link_count = 0
                total_delete_link_count = 0
                success_delete_link_count = 0
                total_upload_bytes = 0
                success_upload_bytes = 0
                total_append_bytes = 0
                success_append_bytes = 0
                total_modify_bytes = 0
                success_modify_bytes = 0
                stotal_download_bytes = 0
                success_download_bytes = 0
                total_sync_in_bytes = 0
                success_sync_in_bytes = 0
                total_sync_out_bytes = 0
                success_sync_out_bytes = 0
                total_file_open_count = 0
                success_file_open_count = 0
                total_file_read_count = 0
                success_file_read_count = 0
                total_file_write_count = 0
                success_file_write_count = 0
                last_heart_beat_time = 2020-06-08 13:58:39
                last_source_update = 1970-01-01 08:00:00
                last_sync_update = 1970-01-01 08:00:00
                last_synced_timestamp = 1970-01-01 08:00:00 	

看到192.168.2.122 ACTIVE 字樣即可說明storage服務器已經成功登記到了tracker服務器,同理別忘了添加開機啓動,打開/etc/rc.d/rc.local並將如下配置追加到文件中:

/etc/init.d/fdfs_trackerd start 
/etc/init.d/fdfs_storaged start

至此,我們就已經完成了fastdfs的全部配置,此時也就可以用客戶端工具進行文件上傳下載的測試了。

2.7 測試fastdfs客戶端工具進行文件上傳下載

2.7.1 編輯客戶端client.conf配置文件

測試時需要設置客戶端的配置文件,編輯/etc/fdfs目錄下的client.conf 文件

[root@myhost fdfs]# vi /etc/fdfs/client.conf
[root@myhost fdfs]# egrep -v '^$|#' /etc/fdfs/client.conf
connect_timeout = 5
network_timeout = 60
base_path = /home/fastdfs/fastdfs_tracker   #tracker服務器文件路徑
tracker_server = 192.168.2.122:22122   #tracker服務IP地址和端口號
log_level = info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker = false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port = 89   
#tracker 服務的 http.server_port 端口號,必須和tracker配置文件裏的設置對應起來

2.7.2 準備需要上傳的圖片
配置完成後就可以模擬文件上傳了,先給/home/tools/目錄下放一張圖片(202004115.png)

[root@myhost tools]# pwd
/home/tools
[root@myhost tools]# ll  202004115.png
總用量 654264
-rw-r--r--.  1 root root     10467 6月   8 13:27 202004115.png
[root@myhost tools]# ll /usr/bin/fdfs*
-rwxr-xr-x. 1 root root  362380 6月   8 13:40 /usr/bin/fdfs_appender_test
-rwxr-xr-x. 1 root root  362157 6月   8 13:40 /usr/bin/fdfs_appender_test1
-rwxr-xr-x. 1 root root  349045 6月   8 13:40 /usr/bin/fdfs_append_file
-rwxr-xr-x. 1 root root  348657 6月   8 13:40 /usr/bin/fdfs_crc32
-rwxr-xr-x. 1 root root  349072 6月   8 13:40 /usr/bin/fdfs_delete_file
-rwxr-xr-x. 1 root root  349807 6月   8 13:40 /usr/bin/fdfs_download_file
-rwxr-xr-x. 1 root root  349766 6月   8 13:40 /usr/bin/fdfs_file_info
-rwxr-xr-x. 1 root root  369199 6月   8 13:40 /usr/bin/fdfs_monitor
-rwxr-xr-x. 1 root root  349276 6月   8 13:40 /usr/bin/fdfs_regenerate_filename
-rwxr-xr-x. 1 root root 1280576 6月   8 13:40 /usr/bin/fdfs_storaged
-rwxr-xr-x. 1 root root  372292 6月   8 13:40 /usr/bin/fdfs_test
-rwxr-xr-x. 1 root root  367413 6月   8 13:40 /usr/bin/fdfs_test1
-rwxr-xr-x. 1 root root  512595 6月   8 13:40 /usr/bin/fdfs_trackerd
-rwxr-xr-x. 1 root root  349999 6月   8 13:40 /usr/bin/fdfs_upload_appender
-rwxr-xr-x. 1 root root  351019 6月   8 13:40 /usr/bin/fdfs_upload_file

2.7.3 通過執行客戶端上傳命令嘗試上傳圖片

[root@myhost tools]# /usr/bin/fdfs_upload_file   /etc/fdfs/client.conf /home/tools/202004115.png 
group1/M00/00/00/CscpQF7d1gGAFNLvAAAo4zbLT5g645.png

如果給我們返回了一個路徑,就表示我們的文件已經上傳成功了,當文件存儲到某個子目錄後,即認爲該文件存儲成功,接下來會爲該文件生成一個文件名,文件名由group、存儲目錄、兩級子目錄、fileid、文件後綴名(由客戶端指定,主要用於區分文件類型)拼接而成。同時在之前配置的storage服務器的實際文件存儲路徑中也可以根據返回的路徑找到實際文件。

[root@myhost 00]# pwd
/home/fastdfs/fastdfs_storage_data/data/00/00
[root@myhost 00]# ll
總用量 12
-rw-r--r--. 1 root root 10467 6月   8 14:09 CscpQF7d1gGAFNLvAAAo4zbLT5g645.png

2.7.4 查看上傳的圖片新的信息

[root@myhost 00]# /usr/bin/fdfs_file_info  /etc/fdfs/client.conf group1/M00/00/00/CscpQF7d1gGAFNLvAAAo4zbLT5g645.png
GET FROM SERVER: false

file type: normal
source storage id: 0
source ip address: 192.168.2.122
file create timestamp: 2020-06-08 14:09:05
file size: 10467
file crc32: 919293848 (0x36cb4f98)

2.7.5 下載圖片

不需要跟保存路徑,默認下載到當前目錄

[root@myhost 00]# cd /home/tools
[root@myhost tools]# /usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/CscpQF7d1gGAFNLvAAAo4zbLT5g645.png
[root@myhost tools]# ll
總用量 654276
-rw-r--r--.  1 root root     10467 6月   8 13:27 202004115.png
-rw-r--r--.  1 root root     10467 6月   8 14:13 CscpQF7d1gGAFNLvAAAo4zbLT5g645.png

2.7.6 查看存儲節點狀態

[root@myhost tools]# /usr/bin/fdfs_monitor /etc/fdfs/client.conf
[2020-06-08 14:20:02] DEBUG - base_path=/home/fastdfs/fastdfs_tracker, connect_timeout=5, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

server_count=1, server_index=0

tracker server is 192.168.2.122:22122

group count: 1

Group 1:
group name = group1
disk total space = 148,991 MB
.
.
.
.
.
.

三、安裝fastdfs-nginx-module

3.1 準備fastdfs-nginx-module軟件安裝包

[root@myhost tools]# pwd
/home/tools
[root@myhost tools]# ll
總用量 377856
-rwxr-xr-x.  1 root root     10467 6月   9 14:03 202004115.png
drwxrwxr-x. 12 root root      4096 12月 31 07:36 fastdfs-6.06
-rwxr-xr-x.  1 root root    809328 6月   9 14:03 fastdfs-6.06.tar.gz
-rwxr-xr-x.  1 root root     19952 6月   9 14:03 fastdfs-nginx-module-1.22.tar.gz
drwxrwxr-x.  5 root root      4096 6月   9 14:14 libfastcommon-1.0.43
-rwxr-xr-x.  1 root root    166526 6月   9 14:03 libfastcommon-1.0.43.tar.gz

3.2 解壓fastdfs-nginx-module軟件安裝包

[root@myhost tools]# tar xf fastdfs-nginx-module-1.22.tar.gz
[root@myhost tools]# cd fastdfs-nginx-module-1.22
[root@myhost fastdfs-nginx-module-1.22]# ll
總用量 8
-rw-rw-r--. 1 root root 3036 11月 19 2019 HISTORY
-rw-rw-r--. 1 root root 2001 11月 19 2019 INSTALL
drwxrwxr-x. 2 root root  104 11月 19 2019 src
[root@myhost fastdfs-nginx-module-1.22]# cd src/
[root@myhost src]# pwd
/home/tools/fastdfs-nginx-module-1.22/src
[root@myhost src]# ll
總用量 84
-rw-rw-r--. 1 root root 43507 11月 19 2019 common.c
-rw-rw-r--. 1 root root  3995 11月 19 2019 common.h
-rw-rw-r--. 1 root root   848 11月 19 2019 config
-rw-rw-r--. 1 root root  3725 11月 19 2019 mod_fastdfs.conf
-rw-rw-r--. 1 root root 28668 11月 19 2019 ngx_http_fastdfs_module.c

3.3 修改fastdfs-nginx-module軟件安裝包裏面的config文件

[root@myhost src]# ll /usr/local/include/fast*
ls: 無法訪問/usr/local/include/fast*: 沒有那個文件或目錄
[root@myhost src]# ll -d /usr/include/fast*
drwxr-xr-x. 2 root root 4096 6月   8 13:36 /usr/include/fastcommon
drwxr-xr-x. 2 root root 4096 6月   8 13:40 /usr/include/fastdfs

[root@myhost src]# cat config 
ngx_addon_name=ngx_http_fastdfs_module

if test -n "${ngx_module_link}"; then
    ngx_module_type=HTTP
    ngx_module_name=$ngx_addon_name
    ngx_module_incs="/usr/local/include"
    ngx_module_libs="-lfastcommon -lfdfsclient"
    ngx_module_srcs="$ngx_addon_dir/ngx_http_fastdfs_module.c"
    ngx_module_deps=
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
    . auto/module
else
    HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
    CORE_INCS="$CORE_INCS /usr/local/include"    ########需要修改這一行,修改成:/usr/include ##########
    CORE_LIBS="$CORE_LIBS -lfastcommon -lfdfsclient"
    CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
fi

四、配置nginx爲storage server提供http訪問接口

4.1 安裝nginx(nginx_storage)的依賴庫

在安裝nginx之前需要先安裝一些模塊依賴的lib庫

[root@myhost src]# cd /home/tools/
[root@myhost tools] # yum -y install pcre pcre-devel zlib zlib-devel  openssl openssl-devel

4.2 創建nginx用戶

[root@myhost nginx-1.16.1]# useradd nginx -s /sbin/nologin -M
[root@myhost nginx-1.16.1]# id nginx
uid=1004(nginx) gid=1004(nginx) 組=1004(nginx)

4.3 準備並解壓nginx(nginx_storage)安裝包

[root@myhost tools]# tar xf nginx-1.16.1.tar.gz 
[root@myhost tools]# cd nginx-1.16.1
[root@myhost nginx-1.16.1]# ll
總用量 756
drwxr-xr-x. 6 test 1001   4096 6月   8 14:29 auto
-rw-r--r--. 1 test 1001 296463 8月  13 2019 CHANGES
-rw-r--r--. 1 test 1001 452171 8月  13 2019 CHANGES.ru
drwxr-xr-x. 2 test 1001   4096 6月   8 14:29 conf
-rwxr-xr-x. 1 test 1001   2502 8月  13 2019 configure
drwxr-xr-x. 4 test 1001     68 6月   8 14:29 contrib
drwxr-xr-x. 2 test 1001     38 6月   8 14:29 html
-rw-r--r--. 1 test 1001   1397 8月  13 2019 LICENSE
drwxr-xr-x. 2 test 1001     20 6月   8 14:29 man
-rw-r--r--. 1 test 1001     49 8月  13 2019 README
drwxr-xr-x. 9 test 1001     84 6月   8 14:29 src

4.4 編譯安裝nginx(nginx_storage)

[root@myhost nginx-1.16.1]# 
./configure --user=nginx  \
--group=nginx  \
--prefix=/usr/local/nginx_storage  \
--with-http_ssl_module  \
--with-http_stub_status_module  \
--add-module=/home/tools/fastdfs-nginx-module-1.22/src

[root@myhost nginx-1.16.1]# echo $?
0
[root@myhost nginx-1.16.1]# make && make install
[root@myhost nginx-1.16.1]# echo $?
0

4.5 編輯修改nginx(nginx_storage)配置文件

[root@myhost nginx-1.16.1]# cd /usr/local/nginx_storage/conf/
[root@myhost conf]# vi nginx.conf
[root@myhost conf]# egrep -v "^#|^$" nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       88;    #這裏修改成storage服務的http.server_port=8888
        server_name  localhost;
        location / {
            if ($args ~ type=download){
                add_header Content-Disposition "attachment;";
            }
             root /home/fastdfs/fastdfs_storage_data/data;   #文件實際存儲路徑
             ngx_fastdfs_module;   #模塊名稱
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

4.6 拷貝fastdfs的配置文件

將Fastdfs軟件包裏面的http.conf和mime.types拷貝到/etc/fdfs目錄下。

[root@myhost conf]# cd /home/tools
[root@myhost tools]# cp -r /home/tools/fastdfs-6.06/conf/http.conf   /etc/fdfs/
[root@myhost tools]# cp -r /home/tools/fastdfs-6.06/conf/mime.types  /etc/fdfs/

4.7 拷貝fastdfs-nginx-module的配置文件mod_fastdfs.conf

[root@myhost tools]# cp -r /home/tools/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf  /etc/fdfs/
[root@myhost src]# cd /etc/fdfs/
[root@myhost fdfs]# ll
總用量 84
-rw-r--r--. 1 root root   337 6月   8 14:01 client.conf
-rw-r--r--. 1 root root  1909 6月   8 13:40 client.conf.sample
-rw-r--r--. 1 root root   965 6月   8 14:40 http.conf
-rw-r--r--. 1 root root 31172 6月   8 14:41 mime.types
-rw-r--r--. 1 root root  3725 6月   8 14:44 mod_fastdfs.conf
-rw-r--r--. 1 root root  1287 6月   8 13:54 storage.conf
-rw-r--r--. 1 root root 10246 6月   8 13:40 storage.conf.sample
-rw-r--r--. 1 root root   620 6月   8 13:40 storage_ids.conf.sample
-rw-r--r--. 1 root root  1621 6月   8 13:55 tracker.conf
-rw-r--r--. 1 root root  9138 6月   8 13:40 tracker.conf.sample

4.8 編輯拷貝的mod_fastdfs.conf配置文件

[root@myhost fdfs]# vi /etc/fdfs/mod_fastdfs.conf
[root@myhost fdfs]# egrep -v '^$|#'  /etc/fdfs/mod_fastdfs.conf
#include http.conf   #這一行必須存在,導入引用文件
connect_timeout=2
network_timeout=30
base_path=/home/fastdfs/fastdfs_storage  #設置storage數據文件和日誌目錄
load_fdfs_parameters_from_tracker=true
storage_sync_file_max_delay = 86400
use_storage_id = false
storage_ids_filename = storage_ids.conf
tracker_server=192.168.2.122:22122    #tracker服務的IP地址以及端口號
storage_server_port=23000  #storage服務的端口號
group_name=group1
url_have_group_name = true  #文件url 中是否有 group 名
store_path_count=1
store_path0=/home/fastdfs/fastdfs_storage_data   #文件實際存儲路徑
log_level=info
log_filename=
response_mode=proxy
if_alias_prefix=
flv_support = true
flv_extension = flv
group_count = 3   #設置組的個數,默認爲0,事實上這次只使用了group1
#接下來就需要在文件尾部追加這3個group setting:
[group1]
group_name=group1
storage_server_port=23000 
store_path_count=1
store_path0=/home/fastdfs/fastdfs_storage_data
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/home/fastdfs/fastdfs_storage_data
[group3]
group_name=group3
storage_server_port=23000
store_path_count=1
store_path0=/home/fastdfs/fastdfs_storage_data

4.9 創建文件實際存儲目錄的軟鏈接

訪問後面的目錄就相當於訪問前面的目錄

#ln  -s  /home/fastdfs/fastdfs_storage_data/data  /home/fastdfs/fastdfs_storage_data/data/M00

ln  -s /home/fastdfs/fastdfs_storage_data   /home/fastdfs/fastdfs_storage_data/M00

4.10 啓動nginx_storage

[root@myhost local]#  /usr/local/nginx_storage/sbin/nginx -t		#檢查語法
ngx_http_fastdfs_set pid=21835
nginx: the configuration file /usr/local/nginx_storage/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx_storage/conf/nginx.conf test is successful
[root@myhost local]# /usr/local/nginx_storage/sbin/nginx 
ngx_http_fastdfs_set pid=5049
[root@myhost local]# netstat -lntup |grep nginx
tcp        0      0 0.0.0.0:88              0.0.0.0:*               LISTEN      5050/nginx: master  

4.11 測試storage的http訪問:瀏覽器訪問ip+http端口號

[root@myhost local]# firewall-cmd --zone=public --permanent --add-port=23000/tcp  && firewall-cmd --reload    
success
success
[root@myhost local]# firewall-cmd --zone=public --permanent --add-port=22122/tcp  && firewall-cmd --reload    
success
success
[root@myhost local]# firewall-cmd --zone=public --permanent --add-port=88/tcp  && firewall-cmd --reload    
success
success
[root@myhost local]# firewall-cmd --zone=public --permanent --add-port=89/tcp  && firewall-cmd --reload    
success
success

訪問地址:http://192.168.2.122:88/group1/M00/00/00/CscpQF7d1gGAFNLvAAAo4zbLT5g645.png

五、配置nginx爲tracker server提供http訪問接口

5.1 安裝nginx(nginx_tracker)的依賴庫(單機不需要這一步)

[root@myhost ~]# yum -y install pcre pcre-devel zlib zlib-devel  openssl openssl-devel

5.2 重新解壓一個nginx(nginx_tracker)安裝包

[root@myhost src]# cd /home/tools/
[root@myhost tools]# tar xf nginx-1.16.1.tar.gz 
[root@myhost tools]# cd nginx-1.16.1
[root@myhost nginx-1.16.1]# ll
總用量 756
drwxr-xr-x. 6 test 1001   4096 6月   8 14:29 auto
-rw-r--r--. 1 test 1001 296463 8月  13 2019 CHANGES
-rw-r--r--. 1 test 1001 452171 8月  13 2019 CHANGES.ru
drwxr-xr-x. 2 test 1001   4096 6月   8 14:29 conf
-rwxr-xr-x. 1 test 1001   2502 8月  13 2019 configure
drwxr-xr-x. 4 test 1001     68 6月   8 14:29 contrib
drwxr-xr-x. 2 test 1001     38 6月   8 14:29 html
-rw-r--r--. 1 test 1001   1397 8月  13 2019 LICENSE
drwxr-xr-x. 2 test 1001     20 6月   8 14:29 man
-rw-r--r--. 1 test 1001     49 8月  13 2019 README
drwxr-xr-x. 9 test 1001     84 6月   8 14:29 src

5.3 編譯安裝nginx(nginx_tracker)

[root@myhost nginx-1.16.1]# 
./configure --user=nginx  \
 --group=nginx   \
 --prefix=/usr/local/nginx_tracker  \
  --with-http_ssl_module  \
  --with-http_stub_status_module  \
  --add-module=/home/tools/fastdfs-nginx-module-1.22/src

[root@myhost nginx-1.16.1]# echo $?
0
[root@myhost nginx-1.16.1]# make && make install
[root@myhost nginx-1.16.1]# echo $?
0

5.5 編輯修改nginx(nginx_tracker)配置文件

tracker服務的nginx修改listen端口爲tracker配置文件中的http.server_port=89端口,並將upstream指向storage的nginx地址。

[root@myhost nginx-1.16.1]# cd /usr/local/nginx_tracker/conf/
[root@myhost conf]# egrep -v "^#|^$" nginx.conf
worker_processes  1;
events {
    worker_connections  102400;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    upstream fdfs_group1 {
      server 127.0.0.1:88;  #負載均衡是訪問的實際地址是storage的http.server_port:88
    }
    server {
        listen       89; #tracker服務的http端口號,訪問這個端口的時候會自動去訪問上面負載的實際地址storage_nginx的http地址。
        server_name  localhost;
        location /group1/M00 {
           proxy_pass http://fdfs_group1;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
  
   
}

5.6 啓動nginx_tracker

[root@myhost conf]# /usr/local/nginx_tracker/sbin/nginx -t
nginx: the configuration file /usr/local/nginx_tracker/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx_tracker/conf/nginx.conf test is successful
[root@myhost conf]# /usr/local/nginx_tracker/sbin/nginx 
[root@myhost conf]#  netstat -lntup |grep nginx
tcp        0      0 0.0.0.0:88              0.0.0.0:*               LISTEN      3279/nginx: master  
tcp        0      0 0.0.0.0:89              0.0.0.0:*               LISTEN      6458/nginx: master  

5.7 測試tracker的http訪問:瀏覽器訪問ip+http端口號

http://192.168.2.122:89/group1/M00/00/00/CscpQF7d1gGAFNLvAAAo4zbLT5g645.png
http://192.168.2.122:88/group1/M00/00/00/CscpQF7d1gGAFNLvAAAo4zbLT5g645.png
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章