#### **介绍**
**实验使用两台centos7机器搭建fastdfs,两台机器使用不同的group,使用阿里云SLB做负载均衡,nginx做反向代理,部署架构如下:** 
**配置两台ECS机器的host,能互相解析主机名**
```bash
$ cat /etc/hosts
```
```
172.18.176.147 n2 n2.mytest.loc
172.18.176.146 n1 n1.mytest.loc
```
### **[ 172.18.176.146 ]**
#### 1.安装依赖库以及环境
```bash
$ yum install gcc gcc-c++ libevent libstdc++-devel pcre-devel zlib-devel make unzip
```
#### 2.安装配置libfastcommon
```bash
$ wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.zip
$ tar xvf V1.0.7.zip
$ cd libfastcommon-1.0.7
$ ./make.sh && ./make.sh install
```
libfastcommon.so 安装到了/usr/lib64/libfastcommon.so,但是FastDFS主程序设置的lib目录是/usr/local/lib,所以需要创建软链接。
```bash
$ ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
$ ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
$ ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
$ ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
```
#### 3.安装配置FastDFS
下载FastDFS
```bash
$ wget https://github.com/happyfish100/fastdfs/archive/V5.05.zip
$ tar xvf V5.05.tar.gz
$ cd fastdfs-5.05
$ ./make.sh && ./make.sh install
```
#### 4.配置tracker
```bash
$ cd /etc/fdfs
$ cp tracker.conf.sample tracker.conf
$ cat tracker.conf
```
```bash
disabled=false
bind_addr=
port=22122 #tracker端口号
connect_timeout=30
network_timeout=60
base_path=/data/fastdfs/tracker #tracker的日志和数据存储目录
max_connections=256
accept_threads=1
work_threads=4
store_lookup=2
store_server=0
store_path=0
download_server=0
reserved_storage_space = 10%
log_level=info
run_by_group=
run_by_user=
allow_hosts=*
sync_log_buff_interval = 10
check_active_interval = 120
thread_stack_size = 64KB
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 = 16MB
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 = 0
use_storage_id = false
storage_ids_filename = storage_ids.conf
id_type_in_filename = ip
store_slave_file_use_link = false
rotate_error_log = false
error_log_rotate_time=00:00
rotate_error_log_size = 0
log_file_keep_days = 0
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.server_port=8080
http.check_alive_interval=30
http.check_alive_type=tcp
http.check_alive_uri=/status.html
```
#### 5.配置storage
```bash
$ cd /etc/fdfs
$ cp storage.conf.sample storage.conf
$ cat storage.conf
```
```bash
disabled=false
group_name=group1 #配置group1卷组
bind_addr=
client_bind=true
port=23000 #storage端口号
connect_timeout=30
network_timeout=60
heart_beat_interval=30
stat_report_interval=60
base_path=/data/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_path0=/data/fastdfs/storage #storage文件存储路径
#store_path_count=2 #有几个存储路径,就写几个
#store_path1=/data/fastdfs/storage #storage文件存储路径
subdir_count_per_path=256
tracker_server=n1.mytest.loc:22122 #配置tracker
tracker_server=n2.mytest.loc:22122 #配置tracker
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=80
```
#### 6.启动tracker
```bash
$ /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
```
#### 7.启动storage
```bash
$ /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
```
#### 8.安装nginx及fastdfs-nginx-module模块(下载源码nginx编译nginx-fastdfs模块,然后替换yum安装的nginx二进制文件,你也可以直接使用源码编译的nginx)
8.1安装nginx
```bash
$ yum install -y nginx
$ nginx -v
```
8.2查看安装的nginx文件的参数
```bash
$ nginx -V
```
```
nginx version: nginx/1.12.2 (CentOS)
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --build=CentOS --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic
```
8.3 下载fastdfs-nginx-module模块
```bash
$ wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip
$ unzip master.zip
```
8.4 下载源码nginx-1.12.2.tar.gz
```bash
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ tar xvr http://nginx.org/download/nginx-1.12.2.tar.gz
$ cd nginx-1.12.2
```
8.5 源码编译nginx
```bash
$ ./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=CentOS \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--add-module=../fastdfs-nginx-module-master/src #添加fastdfs-nginx-module-master模块
$ make #编译nginx
```
8.6 替换yum安装的nginx
```bash
$ cp /usr/sbin/nginx /usr/sbin/nginx_old #备份原来的nginx
$ cp objs/nginx /usr/sbin/nginx #替换yum安装的nginx
```
#### 9.配置fastdfs-nginx-module模块和nginx
```bash
$ cd /etc/fdfs/
$ cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf .
$ cat mod_fastdfs.conf
```
```bash
connect_timeout=2
network_timeout=30
base_path=/tmp
load_fdfs_parameters_from_tracker=true
storage_sync_file_max_delay = 86400
use_storage_id = false
storage_ids_filename = storage_ids.conf
tracker_server=n1.mytest.loc:22122
tracker_server=n2.mytest.loc:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/data/fastdfs/storage
log_level=info
log_filename=
response_mode=proxy
if_alias_prefix=
flv_support = true
flv_extension = flv
group_count = 1
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/storage
```
9.1拷贝http.conf,mime.types文件(nginx的fastdfs-nginx-module模块需要用到)
```bash
$ cp /root/fastdfs/conf/http.conf /root/fastdfs/conf/mime.types /etc/fdfs/
```
9.2配置nginx
```bash
$ cat fastdfs.mytest.cn.conf
```
```
upstream fdfs_group1{
server n1.mytest.loc:18080 weight=1 max_fails=2 fail_timeout=30s;
}
upstream fdfs_group2{
server n2.mytest.loc:18080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name fastdfs.mytest.cn;
access_log /var/log/nginx/fastdfs.mytest.cn.access.log main;
location ~ /group1/M00 {
add_header Strict-Transport-Security max-age=86400;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://fdfs_group1;
}
location ~ /group2/M00 {
add_header Strict-Transport-Security max-age=86400;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://fdfs_group2;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html/;
}
}
#本机器ngx_fastdfs_module模块只会处理group1的读写请求
server {
listen 18080;
server_name 172.18.176.146;
location ~ /group1/M00 {
#add_header Strict-Transport-Security max-age=86400;
alias /data/fastdfs/storage/data;
ngx_fastdfs_module;
}
}
```
#### 10.启动nginx
```bash
$ systemctl start nginx
```
#### 11.测试fastdfs文件服务器
11.1配置fdfs客户端文件
```bash
$ cat /etc/fdfs/client.conf
```
```bash
connect_timeout=30
network_timeout=60
base_path=/data/fastdfs/client
tracker_server=n1.mytest.loc:22122
tracker_server=n2.mytest.loc:22122
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=80
```
11.2增加测试文件test.html
```bash
$ cat test.html
hello,fastdfs!
```
11.3上传文件
```bash
$ fdfs_upload_file /etc/fdfs/client.conf test.html
group1/M00/00/00/rBKwk1tmpJaAbf3CAAAADxawCsc58.html
```
11.4下载文件
```bash
$ fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/rBKwk1tmpJaAbf3CAAAADxawCsc58.html test2.html
```
11.5监控monitor
```bash
$ fdfs_monitor /etc/fdfs/client.conf
```
### **[ 172.18.176.147 ]**
#### **安装libfastcommon,FastDFS,nginx,fastdfs-nginx-module模块的步骤与172.18.176.146一样,storage.conf和nginx的配置有差异**
#### 12.storage的配置
```bash
$ cat /etc/fdfs/storage.conf
```
```bash
disabled=false
group_name=group2 #配置group2卷组
bind_addr=
client_bind=true
port=23000 #storage端口号
connect_timeout=30
network_timeout=60
heart_beat_interval=30
stat_report_interval=60
base_path=/data/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_path0=/data/fastdfs/storage #storage文件存储路径
#store_path_count=2 #有几个存储路径,就写几个
#store_path1=/data/fastdfs/storage #storage文件存储路径
subdir_count_per_path=256
tracker_server=n1.mytest.loc:22122 #配置tracker
tracker_server=n2.mytest.loc:22122 #配置tracker
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=80
```
#### 13.nginx的配置
```bash
$ cat /etc/nginx/conf.d/fastdfs.mytest.cn.conf
```
```
upstream fdfs_group1{
server n1.mytest.loc:18080 weight=1 max_fails=2 fail_timeout=30s;
}
upstream fdfs_group2{
server n2.mytest.loc:18080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name fastdfs.mytest.cn;
#charset koi8-r;
access_log /var/log/nginx/fastdfs.mytest.cn.access.log main;
location ~ /group1/M00 {
add_header Strict-Transport-Security max-age=86400;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://fdfs_group1;
}
location ~ /group2/M00 {
add_header Strict-Transport-Security max-age=86400;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://fdfs_group2;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html/;
}
}
#本机器ngx_fastdfs_module模块只会处理group2的读写请求
server {
listen 18080;
server_name 172.18.176.147;
location ~ /group2/M00 {
alias /data/fastdfs/storage/data;
ngx_fastdfs_module;
}
}
```
#### 14.启动tracker
```bash
$ /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
```
#### 15.启动storage
```bash
$ /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
```
#### 16.启动nginx
```bash
$ systemctl start nginx
```
#### 17. 测试fastdfs文件服务器
17.1上传文件
```bash
$ fdfs_upload_file /etc/fdfs/client.conf test.jpg
group2/M00/00/00/rBKwk1tmrBCAXY8gAAFMEccTGrw633.jpg
```
17.2下载文件
```bash
$ fdfs_download_file /etc/fdfs/client.conf group2/M00/00/00/rBKwk1tmrBCAXY8gAAFMEccTGrw633.jpg test2.jpg
```
17.3监控monitor
```bash
$ fdfs_monitor /etc/fdfs/client.conf
```
#### 18.配置阿里云SLB

#### 19.浏览器访问
