一、实验环境说明
操作系统: Centos 6.6 x64
FastDFS 相关版本: fastdfs-5.05 fastdfs-nginx-module-v1.16 libfastcommon-v1.0.7
web 服务器软件: nginx-1.7.8
角色分配: 2 个 tracker, 地址分别为:10.1.1.243 10.1.1.244 两块磁盘
2个 group:
G1:10.1.1.245 10.1.1.246 三块磁盘
G2:10.1.1.247 10.1.1.248 三块磁盘
Client:10.1.1.249
目录规划:
/dev/sdb1 /data1 日志元数据目录
/dev/sdc1 /data2 数据目录
关闭selinux和iptables
做时间同步,最好添加定时任务
配置好yum源,推荐国内源
分区格式化磁盘并创建挂载目录进行挂载
二、安装 libfastcommon 和 fastdfs(所有机器均安装)
1.下载所需软件(链接: https://pan.baidu.com/s/1eSvSRPg 密码: 7k66)
unzip libfastcommon-1.0.7.zipcd libfastcommon-1.0.7./make.sh./make.sh installcd ..tar xf FastDFS_v5.05.tar.gzcd FastDFSsed -i 's#/usr/local/bin/#/usr/bin/#g' init.d/fdfs_storagedsed -i 's#/usr/local/bin/#/usr/bin/#g' init.d/fdfs_trackerd./make.sh./make.sh install
三、配置 tracker 10.1.1.243 10.1.1.244 (配置相同)
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vim /etc/fdfs/tracker.conf
disabled=falsebind_addr=port=22122connect_timeout=30network_timeout=60base_path=/data1/max_connections=256accept_threads=1work_threads=4store_lookup=2store_group=group2store_server=0store_path=0download_server=0reserved_storage_space = 10%log_level=inforun_by_group=run_by_user=allow_hosts=*sync_log_buff_interval = 10check_active_interval = 120thread_stack_size = 64KBstorage_ip_changed_auto_adjust = truestorage_sync_file_max_delay = 86400storage_sync_file_max_time = 300use_trunk_file = falseslot_min_size = 256slot_max_size = 16MBtrunk_file_size = 64MBtrunk_create_file_advance = falsetrunk_create_file_time_base = 02:00trunk_create_file_interval = 86400trunk_create_file_space_threshold = 20Gtrunk_init_check_occupying = falsetrunk_init_reload_from_binlog = falsetrunk_compress_binlog_min_interval = 0use_storage_id = falsestorage_ids_filename = storage_ids.confid_type_in_filename = ipstore_slave_file_use_link = falserotate_error_log = falseerror_log_rotate_time=00:00rotate_error_log_size = 0log_file_keep_days = 0use_connection_pool = falseconnection_pool_max_idle_time = 3600http.server_port=8080http.check_alive_interval=30http.check_alive_type=tcphttp.check_alive_uri=/status.html
启动 tracker 服务:
service fdfs_trackerd start
chkconfig fdfs_trackerd on
四、配置组G1 10.1.1.245 10.1.1.246 (配置相同)
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vim /etc/fdfs/storage.conf
disabled=falsegroup_name=G1bind_addr=client_bind=trueport=23000connect_timeout=30network_timeout=60heart_beat_interval=30stat_report_interval=60base_path=/data1/max_connections=256buff_size = 256KBaccept_threads=1work_threads=4disk_rw_separated = truedisk_reader_threads = 1disk_writer_threads = 1sync_wait_msec=50sync_interval=0sync_start_time=00:00sync_end_time=23:59write_mark_file_freq=500store_path_count=1store_path0=/data2subdir_count_per_path=256tracker_server=10.1.1.243:22122tracker_server=10.1.1.244:22122log_level=inforun_by_group=run_by_user=allow_hosts=*file_distribute_path_mode=0file_distribute_rotate_count=100fsync_after_written_bytes=0sync_log_buff_interval=10sync_binlog_buff_interval=10sync_stat_file_interval=300thread_stack_size=512KBupload_priority=10if_alias_prefix=check_file_duplicate=0file_signature_method=hashkey_namespace=FastDFSkeep_alive=0use_access_log = falserotate_access_log = falseaccess_log_rotate_time=00:00rotate_error_log = falseerror_log_rotate_time=00:00rotate_access_log_size = 0rotate_error_log_size = 0log_file_keep_days = 0file_sync_skip_invalid_record=falseuse_connection_pool = falseconnection_pool_max_idle_time = 3600http.domain_name=http.server_port=8888
配置组G2 10.1.1.247 10.1.1.248 配置相同
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vim /etc/fdfs/storage.conf
disabled=falsegroup_name=G2bind_addr=client_bind=trueport=23000connect_timeout=30network_timeout=60heart_beat_interval=30stat_report_interval=60base_path=/data1max_connections=256buff_size = 256KBaccept_threads=1work_threads=4disk_rw_separated = truedisk_reader_threads = 1disk_writer_threads = 1sync_wait_msec=50sync_interval=0sync_start_time=00:00sync_end_time=23:59write_mark_file_freq=500store_path_count=1store_path0=/data2subdir_count_per_path=256tracker_server=10.1.1.243:22122tracker_server=10.1.1.244:22122log_level=inforun_by_group=run_by_user=allow_hosts=*file_distribute_path_mode=0file_distribute_rotate_count=100fsync_after_written_bytes=0sync_log_buff_interval=10sync_binlog_buff_interval=10sync_stat_file_interval=300thread_stack_size=512KBupload_priority=10if_alias_prefix=check_file_duplicate=0file_signature_method=hashkey_namespace=FastDFSkeep_alive=0use_access_log = falserotate_access_log = falseaccess_log_rotate_time=00:00rotate_error_log = falseerror_log_rotate_time=00:00rotate_access_log_size = 0rotate_error_log_size = 0log_file_keep_days = 0file_sync_skip_invalid_record=falseuse_connection_pool = falseconnection_pool_max_idle_time = 3600http.domain_name=http.server_port=8888
启动 storage 服务:(四台)
service fdfs_storaged start
chkconfig fdfs_storaged on
五、storage server 安装 nginx
编译安装nginx,添加fastdfs nginx 模块
tar xf fastdfs-nginx-module_v1.16.tar.gzvim fastdfs-nginx-module/src/configCORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/" #去掉local添加软连接ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.soln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.soln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.soln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so tar xf nginx-1.7.8.tar.gzcd nginx-1.7.8./configure --add-module=../fastdfs-nginx-module/src/ --prefix=/usr/local/nginx --user=nobody --group=nobody --with-http_gzip_static_module --with-http_gunzip_modulemakemake install
nginx 启动脚本
#!/bin/bash## nginx - this script starts and stops the nginx daemon## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse# proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/nginx/conf/nginx.conf# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0NGINX_HOME="/usr/local/nginx/"nginx=$NGINX_HOME"sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE=$NGINX_HOME"conf/nginx.conf"[ -f /etc/sysconfig/nginx ] && /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxstart() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6echo -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retvalkillall -9 nginx}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $nginx -HUPRETVAL=$?echo}force_reload() {restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2esac
配置 nginx(添加一个server)
cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/cp fastdfs-5.05/conf/{anti-steal.jpg,http.conf,mime.types} /etc/fdfs/touch /var/log/mod_fastdfs.logchown nobody.nobody /var/log/mod_fastdfs.logserver {listen 80;server_name 本机IP地址;location /G1/M00{root /data2/;ngx_fastdfs_module;}}
配置 模块配置文件 mod_fastdfs.conf
G1 组配置:vim /etc/fdfs/mod_fastdfs.confconnect_timeout=2network_timeout=30base_path=/tmpload_fdfs_parameters_from_tracker=truestorage_sync_file_max_delay = 86400use_storage_id = falsestorage_ids_filename = storage_ids.conftracker_server=10.1.1.243:22122tracker_server=10.1.1.244:22122storage_server_port=23000group_name=G1url_have_group_name = truestore_path_count=1store_path0=/data2/log_level=infolog_filename=/var/log/mod_fastdfs.logresponse_mode=proxyif_alias_prefix=#include http.confflv_support = trueflv_extension = flvgroup_count = 2[group1]group_name=G1storage_server_port=23000store_path_count=1store_path0=/data2/[group2]group_name=G2storage_server_port=23000store_path_count=1store_path0=/data2/G2 组配置:vim /etc/fdfs/mod_fastdfs.confconnect_timeout=2network_timeout=30base_path=/tmpload_fdfs_parameters_from_tracker=truestorage_sync_file_max_delay = 86400use_storage_id = falsestorage_ids_filename = storage_ids.conftracker_server=10.1.1.243:22122tracker_server=10.1.1.244:22122storage_server_port=23000group_name=G2url_have_group_name = truestore_path_count=1store_path0=/data2/log_level=infolog_filename=/var/log/mod_fastdfs.logresponse_mode=proxyif_alias_prefix=#include http.confflv_support = trueflv_extension = flvgroup_count = 2[group1]group_name=G1storage_server_port=23000store_path_count=1store_path0=/data2/[group2]group_name=G2storage_server_port=23000store_path_count=1store_path0=/data2/service nginx reloadchkconfig nginx on
六、测试
客户端配置/etc/fdfs/client.conf
connect_timeout=30network_timeout=60base_path=/tmptracker_server=10.1.1.243:22122tracker_server=10.1.1.244:22122log_level=infouse_connection_pool = falseconnection_pool_max_idle_time = 3600load_fdfs_parameters_from_tracker=falseuse_storage_id = falsestorage_ids_filename = storage_ids.confhttp.tracker_server_port=80客户端测试上传下载(其他命令暂时不测试,这里仅为测试服务是否正常):fdfs_upload_file /etc/fdfs/client.conf /etc/passwd #上传文件G1/M00/00/00/CgEB9Vnwv1SAXm1HAAAEeivAsqE3998928 md5sum /etc/passwd6923374d09e7f8c9a296fdf2e84764a1 /etc/passwdfdfs_download_file /etc/fdfs/client.conf G1/M00/00/00/CgEB9Vnwv1SAXm1HAAAEeivAsqE3998928 #下载文件 md5sum CgEB9Vnwv1SAXm1HAAAEeivAsqE39989286923374d09e7f8c9a296fdf2e84764a1 CgEB9Vnwv1SAXm1HAAAEeivAsqE3998928上传下载文件内容一致,未发生改变
上传一张图片自定义meizhi.jpg
fdfs_upload_file /etc/fdfs/client.conf meizhi.jpgG1/M00/00/00/CgEB9lnwwTaAFqHZAAC9pQqjlp0604.jpg
访问效果:
#前端可以设置nginx服务器做反向代理,对后端做负载均衡,这里附上一份参考示例:
http {include mime.types;default_type application/octet-stream;#设置主机名哈希表的表项长度默认 32|64|128server_names_hash_bucket_size 128;#设置请求缓冲区大小, 默认使用 client_header_buffer_size 来读取 header 值,#如果 header 过大,会使用 large_client_header_buffers 来读取client_header_buffer_size 32k;large_client_header_buffers 4 32k;#定义最大允许上传的文件大小client_max_body_size 300m;#开启高效文件传输模式, sendfile 指令指定 nginx 是否调用 sendfile 函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,以平衡磁盘与网络 I/O 处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成 offsendfile on;# 防止网络堵塞tcp_nopush on;#长连接超时时间keepalive_timeout 120;#gzip 模块设置gzip on; #开启 gzip 压缩输出gzip_min_length 1k; #最小压缩文件大小gzip_buffers 4 16k; #压缩缓冲区gzip_http_version 1.0; #压缩版本(默认 1.1,前端如果是 squid2.5 请使用 1.0)gzip_comp_level 2; #压缩等级(1~9)gzip_types text/plain application/x-javascript text/css application/xml;#压缩类型,默认就已经包含 text/htmlgzip_vary on; #根据 HTTP 头来判断是否需要压缩gzip_disable “MSIE[1-6].”#IE6 以下对 gzip 的支持不好, 在 IE6 下禁用 gzipproxy_redirect off;#当后端单台 web 服务器上也配置有多个虚拟主机时,需要使用该 Header 来区分反向代理哪个主机名proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;#后端的 Web 服务器可以通过 X-Real-IP/X-Forwarded-For 获取用户真实 IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#后端服务器连接的超时时间(发起握手等候响应的超时时间)proxy_connect_timeout 90;#后端服务器数据回传时间(规定此时间内后端服务器必须传完所有数据)proxy_send_timeout 90;#等待后端服务器处理请求的时间proxy_read_timeout 90;#nginx 从被代理的服务器读取响应时,使用该缓冲区保存响应的开始部分,该值默认等于proxy_buffers 设置的一块缓冲区大小,但可以被设置更小proxy_buffer_size 16k;#为每个连接设置缓冲区的数量为 number,每块缓冲区的大小为 size,用于保存从被代理服务器读取的响应proxy_buffers 4 64k;proxy_busy_buffers_size 128k;#在开启缓冲后端服务器响应到临时文件的功能后,设置 nginx 每次写数据到临时文件的 size(大小)限制proxy_temp_file_write_size 128k;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 /usr/local/nginx/access.log main;#设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:500mmax_size=10g inactive=30d;proxy_temp_path /var/cache/nginx/proxy_cache/tmp;upstream group1 {server 10.1.1.245:80;server 10.1.1.246:80;}upstream group2 {server 10.1.1.247:80;server10.1.1.248:80;}server {listen 80;server_name 10.1.1.249;location /G1/M00 { #设置 group1 的负载均衡参数#如果后端的服务器返回 502、 504、执行超时等错误,自动将请求转发到 upstream 负载均衡池中的另一台服务器,实现故障转移proxy_next_upstream http_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_pass http://group1;expires 30d;}location ~* /G2/(M00|M01) { #设置 group2 的负载均衡参数proxy_next_upstream http_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_pass http://group2;expires 30d;}}