致敬:FastDFS的开发者/fastdfs-nginx-module提供者——淘宝的架构师余庆老师
环境声明:
OS -> Ubuntu 16.04.3 LTS FastDFS -> 5.11 NginX -> 1.10.3
目的
搭建 FastDFS 文件服务器,并使用 NginX 提供负载均衡等高性能的服务,弥补 FastDFS HTTP服务的不足。
准备工作
- 创建两个工作目录 /opt/fastdfs-source(存放 FastDFS 的资源文件), /opt/nginx-source(存放 NginX 的资源文件) 和 文件存储目录 /home/fastdfs/data
- 下载并解压
libfastcommon
、fastdfs
、fastdfs-nginx-module
cd /opt/fastdfs-source wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -SO libfastcommon.tar.gz wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -SO fastdfs.tar.gz wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -SO fastdfs-nginx-module.tar.gz tar -xf libfastcommon.tar.gz tar -xf fastdfs.tar.gz tar -xf fastdfs-nginx-module.tar.gz
组件安装
libfastcommon
cd /opt/fastdfs-source/libfastcommon-1.0.39 ./make.sh ./make.sh install
fastdfs
cd /opt/fastdfs-source/fastdfs-5.11 ./make.sh ./make.sh install
程序安装在 /usr/bin/fdfs_trackerd
配置文件在 /etc/fdfs(这里的配置文件都是以.sample结尾的模板文件)
FastDFS配置
- 把 FastDFS 资源包中的配置文件拷贝到 /etc/fdfs 中
cp /opt/fastdfs-source/fastdfs-5.11/conf/* /etc/fdfs cp /opt/fastdfs-source/fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs ----------------------------------tracker---------------------------------------------- vim /etc/fdfs/tracker.conf # the tracker server port port=22122 # the base path to store data and log files base_path=/home/fastdfs # HTTP port on this tracker server http.server_port=8080 ----------------------------------storage---------------------------------------------- vim /etc/fdfs/storage.conf # the storage server port port=23000 # the base path to store data and log files base_path=/home/fastdfs # store_path#, based 0, if store_path0 not exists, it's value is base_path # the paths must be exist store_path0=/home/fastdfs #store_path1=/home/yuqing/fastdfs2 # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address ##这里的host需要配置成公网ip,否则外部客户端无法上传文件 tracker_server={hostname or ip address}:22122 # the port of the web server on this storage server http.server_port=8888 ----------------------------------client---------------------------------------------- vim /etc/fdfs/client.conf # the port of the web server on this storage server http.server_port=8888 # the base path to store log files base_path=/home/fastdfs # the base path to store log files base_path=/home/fastdfs # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address tracker_server={hostname or ip address}:22122 #HTTP settings http.tracker_server_port=8080 ----------------------------------mod_fastdfs---------------------------------------------- vim /etc/fdfs/mod_fastdfs.conf # FastDFS tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address # valid only when load_fdfs_parameters_from_tracker is true tracker_server={hostname or ip address}:22122 # the port of the local storage server # the default value is 23000 storage_server_port=23000 # the group name of the local storage server group_name=group1 # store_path#, based 0, if store_path0 not exists, it's value is base_path # the paths must be exist # must same as storage.conf store_path0=/home/fastdfs #store_path1=/home/yuqing/fastdfs1
FastDFS启动
FastDFS服务端有两个角色:跟踪器(tracker)、存储节点(storage)。
tracker 主要做调度工作,在访问上起负载均衡的作用。
storage 存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,同时对文件的 metadata 进行管理。
metadata是文件的元数据,K-V的形式保存着文件的一些属性,如:
group_name=group1, remote_filename=M00/00/00/rBgI8V2C9TiAOBYLAAADupul6pU900_big.png, timestamp=2019-09-19 11:25:44, size=954, crc32=2611341973
# 启动 fdfs_trackerd /etc/fdfs/tracker.conf start fdfs_storaged /etc/fdfs/storage.conf start # 查看日志 tail -f /home/fastdfs/logs/trackerd.log tail -f /home/fastdfs/logs/storaged.log # 测试 # 先准备一个图片/opt/light-icon.png fdfs_test /etc/fdfs/client.conf upload /opt/light-icon.png This is FastDFS client test program v5.11 Copyright (C) 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2019-09-19 11:25:44] DEBUG - base_path=/home/fastdfs, 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 tracker_query_storage_store_list_without_group: server 1. group_name=, ip_addr={hostname or ip address}, port=23000 group_name=group1, ip_addr={hostname or ip address}, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/rBgI8V2C9TiAOBYLAAADupul6pU900.png source ip address: {hostname or ip address} file timestamp=2019-09-19 11:25:44 file size=954 file crc32=2611341973 example file url: http://{hostname or ip address}:8080/group1/M00/00/00/rBgI8V2C9TiAOBYLAAADupul6pU900.png storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/rBgI8V2C9TiAOBYLAAADupul6pU900_big.png source ip address: {hostname or ip address} file timestamp=2019-09-19 11:25:44 file size=954 file crc32=2611341973 example file url: http://{hostname or ip address}:8080/group1/M00/00/00/rBgI8V2C9TiAOBYLAAADupul6pU900_big.png
NginX配置
- 安装
apt-get update apt-get install -y nginx
- 添加模块
fastdfs-nginx-module
预先下载相同版本的源码包到/opt/nginx-source
nginx -v #nginx version: nginx/1.10.3
#查看编译参数 nginx -V nginx version: nginx/1.10.3 built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5) built with OpenSSL 1.0.2g 1 Mar 2016 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threadscd /opt/nginx-source/nginx-1.10.3
./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=/opt/fastdfs-source/fastdfs-nginx-module-1.20/src make make install
- 编译时可能出现的问题及解决
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. apt-get install libpcre3 libpcre3-dev ------------------------------------------------ ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option. apt-get install openssl libssl-dev ----------------------------------------- ./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries. apt-get install libgd-dev ------------------------------ ./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries. apt-get install libxml2 libxml2-dev libxslt-dev ------------------------------ ./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. apt-get install libgeoip-dev
- make时可能出现的问题及解决
Fatal error: can't create objs/addon/src/ngx_http_fastdfs_module.o: Permission denied
In file included from /home/xxxx/caibh/fdfs-package/fastdfs-nginx-module-1.20/src/common.c:26:0,
from /home/xxxx/caibh/fdfs-package/fastdfs-nginx-module-1.20/src/ngx_http_fastdfs_module.c:6:
/usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No such file or directory
# 修改fastdfs-nginx-module的src/config文件
vim /opt/fastdfs-source/fastdfs-nginx-module-1.20/src/config
# 修改以下两项:
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
#修改后重新运行 $./configure $make $make install
- 关闭原始nginx,并备份好原始的启动文件,启用新的启动文件(在./configure后控制台会显示)
nginx -s stop cd /usr/sbin mv /usr/sbin/nginx /usr/sbin/nginx_bak #备份好后,把新的启动文件软链接到原始的启动文件目录,放在 /usr/sbin #创建软链接 ln -s /usr/share/nginx/sbin/nginx /usr/sbin/nginx #配置新的路由规则,加入到配置文件 /etc/nginx/nginx.conf server { listen 8777; location /M00 { root /home/fastdfs/data; ngx_fastdfs_module; } }
整体测试
在浏览器测试刚才上传的图片 http://xxx.xx.xx.xx:8777/M00/00/00/rBgI8V2C9TiAOBYLAAADupul6pU900.png
至此一切归为平淡,peace~