一:环境介绍
10.63.13.138 tracker,nginx proxy
10.63.13.143 storage,nginx
二:资源包准备
FastDFS官方论坛:http://www.csource.org
资源下载路径:http://sourceforge.net/projects/fastdfs/files/
本实验学习环境使用CENTOS 6.4 X64
需要下载的资源包:
FastDFS源代码:FastDFS_v5.05.tar.gz
nginx模块源代码:fastdfs-nginx-module_v1.15.tar.gz
nginx服务器源代码:nginx-1.4.7.tar.gz
nginx cache purge插件源代码:ngx_cache_purge-2.1.tar.gz
三:tracker 安装
在10.63.13.138上安装tracker,使用的资源包是:FastDFS_v5.05.tar.gz
在V5版本后,通过查看INSTALL 方式好像不需要libevent资源依赖了,但需要安装libfastcomm,否则会在make.sh时,提示如下的错误:
make: *** [../common/fdfs_global.o] Error 1
1.libfastcomm 包安装
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon/ && ./make.sh &&./make.sh install
但是FastDFS主程序默认设置的lib目录是/usr/local/lib
所以需要创建软链接.
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
2.tracker的安装
cd fastdfs-5.05
./make.sh && ./make.sh install
编辑tracker.conf
一般修改以下几个参数:
bind_addr=10.63.13.138
port=22122
base_path=/opt/tracker/fastdfs
store_group=group1
http.server_port=8080
3.tracker的启动方式
fdfs_trackerd /etc/fdfs/tracker.conf start
如果启动报错,看详细的logs,在base_path路径下/opt/tracker/fastdfs/logs/
设置为开机启动:usr/local/bin/fdfs_trackerd/etc/fdfs/tracker.conf restart
四:安装STORAGE
同样的方式安装storage,需先安装libfastcomm
1.libfastcomm 包安装
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon/ && ./make.sh &&./make.sh install
但是FastDFS主程序默认设置的lib目录是/usr/local/lib
所以需要创建软链接.
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
2.storager的安装
cd fastdfs-5.05
./make.sh && ./make.sh install
base_path=/opt/fdfs/storage安装完成后,可执行文件在/usr/local/bin
配置文件在/etc/fdfs
3.编辑storage.conf
disabled=false
group_name=group1根据实际的情况配置
bind_addr=10.63.13.14
port=23000
base_path=/opt/fdfs/storage
store_path_count=1#存储路径的个数
store_path0=/opt/storage/fdfs
tracker_server=10.63.13.138:22122
4.storage的启动方式
fdfs_storaged /etc/fdfs/storage.conf start
如果启动报错,看详细的logs,在base_path路径下/opt/fdfs/storage/logs/
设置为开机启动:usr/local/bin/fdfs_storaged /etc/fdfs/storage.confrestart
5.查看storage是否连接到tracker
通过fdfs_monitor /etc/fdfs/storage.conf查看storage的运行状态,是否为ACTIVE
之后,再检查具体的LOG
五:在storage上安装nginx,在storage上安装nginx,主要是为了提供http的访问服务,同时解决group中storage服务器的同步延迟问题。(需要安装ngx_cache_purge-2.1.tar.gz)
使用的版本号为:nginx 1.4.7,fastfdfs module为V1.15
yum 安装nginx安装所需要依赖的rpm包
yum -y install zlib-devel pcre-devel openssl-devel lcmslcms-devel libtiff libtiff-devel freetype freetype-devel jasper jasper-devellibpng libpng-devel libwmf libwmf-devel libSM libSM-devel bzip2 bzip2-develbzip2-libs
2.nginx的安装,在storage上安装nginx需要加载fastdfs的nginx模块
# ./configure --prefix=/usr/local/nginx--with-http_ssl_module --with-http_realip_module --with-http_addition_module--with-http_sub_module --with-http_dav_module --with-http_flv_module--with-http_mp4_module --with-http_gzip_static_module--with-http_random_index_module --with-http_secure_link_module--with-http_stub_status_module --with-mail --with-mail_ssl_module --add-module=../fastdfs-nginx-module/src/
Make时,需要先修改fastdfs-nginx-module/src/config文件,把环境变量修改下
CORE_INCS="$CORE_INCS/usr/include/fastdfs /usr/include/fastcommon/"
否则,会遇到这样的
In file included from../fastdfs-nginx-module/src//ngx_http_fastdfs_module.c:6:
../fastdfs-nginx-module/src//common.c:43:error: expected specifier-qualifier-list before ‘FDFSStorePaths’
../fastdfs-nginx-module/src//common.c: Infunction ‘fdfs_load_groups_store_paths’:
../fastdfs-nginx-module/src//common.c:123:error: ‘GroupStorePaths’ has no member named ‘store_paths’
Make && make install
安装通过后,将fastdfs的nginx模块的配置文件,copy一份到/etc/fdfs下
Cp fastdfs-nginx-module/src/mod_fastdfs.conf/etc/fdfs/
编辑nginx.conf
主要是新增提供通过http方式下载
listen 8080;
location/group1/M00{
root /opt/storage/fdfs/data;
ngx_fastdfs_module;
}
Group1是指定的组名
M00 是指定的目录名
通过/usr/local/nginx/sbin/nginx–t 检查nginx的配置语法
编辑mod_fastdfs.conf
base_path=/opt/fdfs/storage 保存日志目录
tracker_server=10.63.13.138:22122 tracker的IP和port
group_name=group1 当前服务器的group名
url_have_group_name =true 文件url中是否有group名
store_path_count=1 存储路径个数,需要和store_path匹配
store_path0=/opt/storage/fdfs 存储路径
http.need_find_content_type=true 从文件扩展名查找文件
group_count = 1 设置的组的个数
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/storage/fdfs
5.运行nginx
以上配置都没问题后,就可以启动nginx,启动nginx前,你的tracker和storage要在运行状态
/usr/local/nginx/sbin/nginx 即可启动
通过error.log 看是否有错误
Nginx正常启动后,应该是这样的提示:
[2015-10-21 14:05:12] INFO - fastdfs apache/ nginx module v1.15, response_mode=proxy, base_path=/tmp,url_have_group_name=1, group_count=1, connect_timeout=2, network_timeout=30,tracker_server_count=1, if_alias_prefix=, local_host_ip_count=2,need_find_content_type=1, default_content_type=application/octet-stream,anti_steal_token=0, token_ttl=0s, anti_steal_secret_key length=0,token_check_fail content_type=, token_check_fail buff length=0,load_fdfs_parameters_from_tracker=1, storage_sync_file_max_delay=86400s,use_storage_id=0, storage server id count=0, flv_support=1, flv_extension=flv
[2015-10-21 14:05:12] INFO - group 1.group_name=group1, storage_server_port=23000, path_count=1,store_path0=/opt/storage/fdfs
如果遇到这样的错误,解决办法:
[2015-10-21 13:59:11] ERROR - file:shared_func.c, line: 961, open file /etc/fdfs/mime.types fail, errno: 2, errorinfo: No such file o
r directory
到nginx 的安装包下,在conf下面的mine.types文件copy到/etc/fdfs下即可
ln -s /home/storage/opt/storage/fdfs/data/M00
也是可以将nginx 加入开机启动
五:在tracker上安装nginx
在tracker上安装nginx,结合ngx_cache主要提供http访问的反向代理、负责均衡以及缓存服务(在当前的测试环境中,测试反向代理和缓存)
nginx 安装前的RPM资源依赖包的安装
yum -y install zlib-devel pcre-devel openssl-devel lcmslcms-devel libtiff libtiff-devel freetype freetype-devel jasper jasper-devellibpng libpng-devel libwmf libwmf-devel libSM libSM-devel bzip2 bzip2-develbzip2-libs
解压ngx_cache_purge-2.1.tar.gz
./configure--prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module--with-http_addition_module --with-http_sub_module --with-http_dav_module--with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module--with-http_random_index_module --with-http_secure_link_module--with-http_stub_status_module --with-mail --with-mail_ssl_module --add-module=../ngx_cache_purge-2.1/
Make && make install
编辑nginx.conf文件
主要增加以下内容:
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user[$time_local] "$request" '
# '$status $body_bytes_sent"$http_referer" '
# '"$http_user_agent""$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
tcp_nopush on;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP$remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#设置了缓存存储路径、存储方式、分配的内存大小、磁盘最大空间、缓存期限等
proxy_cache_path/var/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:500m max_size=10ginactive=30d;
proxy_temp_path/var/cache/nginx/proxy_cache/tmp;
#keepalive_timeout 0;
keepalive_timeout 35;
#gzip on;
upstream fdfs_group1{
server 10.63.13.143:8080weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /group1/M00{
proxy_next_upstreamhttp_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key$uri$is_args$args;
proxy_passhttp://fdfs_group1;
expires 30d;
}
location ~ /purge(/.*) {
allow127.0.0.1;
allow10.4.42.99;
deny all;
proxy_cache_purgehttp-cache $1$is_args$args;
}
通过/usr/local/nginx/sbin/nginx –t检查语法是否有误
启动:/usr/local/nginx/sbin/nginx
六:测试文件上传
在上传文件之前,先修改上传文件的client
Vi /etc/fdfs/client.conf
base_path=/opt/tracker/client
tracker_server=10.63.13.138:22122
http.tracker_server_port=8080
#include http.conf
编辑一个文件 test.html
TestfastDFS
file timestamp=2015-10-21 17:36:17
file size=13
file crc32=3334831908
example file url: http://10.63.13.143:8080/group1/M00/00/00/Cj8Nj1YnXJGAbaNtAAAADcbFfyQ26_big.html
group1 组名 M00磁盘目录 00/00随机生成的目录,后面是文件名
可以根据返回的URL,访问到我们上上传的文件
13.143 是storage上的提供http访问的,看如何通过tracker上的nginx来访问,需要注意的是,你的文件是直接上传到storage的
看看是不是有缓存,缓存了这个文件
这个是tracker主要实现的功能
直接上传文件:
# fdfs_upload_file /etc/fdfs/client.conf1.txt
group1/M00/00/00/Cj8Nj1YnXmqAGR4KAAAAEHzRfgo470.txt
访问方式:
http://10.63.13.138:8080/group1/M00/00/00/Cj8Nj1YnXmqAGR4KAAAAEHzRfgo470.txt
是直接下载
这样,一个简单的fastdfs就搭建OK了,后面将进一步学习多tracker和storage的环境学习
转载于:https://blog.51cto.com/caibird/1704953