本地虚拟机搭建了一下FastDFS的环境,总结一番,其中很多不足还望大家指正。
俩VM虚拟机
10.240.37.87
10.240.37.90
既是tracker也是storage
tracker 8081 #http端口
storage 8080 #http端口
一.VM设置网桥模式,共用实际的物理网卡
1.VM网卡配置 cat /etc/sysconfig/network-scripts/ifcfg-eth0
(1)10.240.37.87的网卡配置
DEVICE=eth0
HWADDR=00:0C:29:CA:8F:65
NETMASK=255.255.255.0
IPADDR=10.240.37.87
GATEWAY=10.240.37.1
TYPE=Ethernet
UUID=3481be4a-0c13-4de2-ac7a-15136740f9e0
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
PEERDNS=yes
(2)10.240.37.90的网卡配置
DEVICE=eth0
HWADDR=00:0C:29:CA:8F:65
NETMASK=255.255.255.0
IPADDR=10.240.37.87
GATEWAY=10.240.37.1
TYPE=Ethernet
UUID=3481be4a-0c13-4de2-ac7a-15136740f9e0
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
PEERDNS=yes
(3)DNS服务器
more /etc/resolv.conf
nameserver 192.168.100.10
nameserver 192.168.100.11
二、安装
所需要下载的压缩包有:
FastDFS_v5.01.tar.gz
nginx-1.8.0.tar.gz
pcre-8.32.tar.gz
zlib-1.2.8.tar.gz
openssl-1.0.1c.tar.gz
fastdfs-nginx-module_v1.15.tar.gz
simpl-ngx_devel_kit.tar.gz
lua-nginx-module-0.9.15.tar.gz
LuaJIT-2.0.2.tar.gz
echo-nginx-module.tar.gz
ngx_cache_purge-2.3.tar.gz
1.FastDFS的安装
cd /usr/local
tar zxvf FastDFS_v5.01.tar.gz
cd FastDFS
./make.sh
./make.sh install
2.nginx的安装(加入lua的目的是nginx能够有更好的扩展性)
(1).安装PCRE库
cd /usr/local/
tar -zxvf pcre-8.32.tar.gz
cd pcre-8.21
./configure --prefix=/usr/local/pcre
make
make install
(2).安装zlib库
cd /usr/local/
tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
./configure --prefix = /usr/local/zlib
make
make install
(3).安装openssl
cd /usr/local/
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
./config --prefix = /usr/local/openssl
make
make install
(4).下载编译LuaJIT
wget -c http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar xzvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make install PREFIX=/usr/local/luajit
echo "/usr/local/luajit/lib" > /etc/ld.so.conf.d/usr_local_luajit_lib.conf
ldconfig
#注意环境变量!
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
(5).解压如下软件
tar xzvf lua-nginx-module-0.9.15.tar.gz
tar xzvf echo-nginx-module.tar.gz
tar zxvf simpl-ngx_devel_kit.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz
tar zxvf fastdfs-nginx-module_v1.15.tar.gz
7.编译安装nginx
tar -zxvf nginx-1.8.0.tar.gz
cd ginx-1.8.0
./configure --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --prefix=/usr/local/nginx --with-
pcre=/usr/local/pcre-8.32 --with-zlib=/usr/local/zlib-1.2.8 --with-openssl=/usr/local/openssl-1.0.1c --add-module=/usr/local/simpl-ngx_devel_kit-b62f5a3 --
add-module=/usr/local/lua-nginx-module-0.9.15 --add-module=/usr/local/openresty-echo-nginx-module-7bc71d2 --add-module=/usr/local/ngx_cache_purge-2.3 --add-
module=/usr/local/fastdfs-nginx-module/src
三、配置
1.配置tracker
vim /etc/fdfs/tracker.conf
10.240.37.87的tracker如下:
disabled=false #启用配置文件
port=22122 #设置tracker的端口号
base_path=/data1/tracker #设置tracker的数据文件和日志目录(需预先创建)
http.server_port=8081 #设置http端口号
如需要进行性能调优,可以参照附录的配置文件的详细说明。
10.240.37.90的tracker如下:
disabled=false #启用配置文件
port=22122 #设置tracker的端口号
base_path=/data2/tracker #设置tracker的数据文件和日志目录(需预先创建)
http.server_port=8081 #设置http端口号
如需要进行性能调优,可以参照附录的配置文件的详细说明。
2.配置storage
vim /etc/fdfs/storage.conf
10.240.37.87的storage如下:
disabled=false #启用配置文件
group_name=G1 #组名,根据实际情况修改
port=23000 #设置storage的端口号
base_path=/data1/storage #设置storage的日志目录(需预先创建)
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/data1/storage #存储路径
tracker_server=10.240.37.87:22122 #tracker服务器的IP地址和端口号
tracker_server=10.240.37.90:22122 #tracker服务器的IP地址和端口号
http.server_port=8080 #设置http端口号
10.240.37.90的storage如下:
disabled=false #启用配置文件
group_name=G2 #组名,根据实际情况修改
port=23000 #设置storage的端口号
base_path=/data2/storage #设置storage的日志目录(需预先创建)
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/data2/storage #存储路径
tracker_server=10.240.37.87:22122 #tracker服务器的IP地址和端口号
tracker_server=10.240.37.90:22122 #tracker服务器的IP地址和端口号
http.server_port=8080 #设置http端口号
3.配置mod_fastdfs.conf
cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
10.240.37.87的mod_fastdfs.conf配置如下:
base_path=/data1/storage #保存日志目录
tracker_server=10.240.37.87:22122 #tracker服务器的IP地址以及端口号
tracker_server=10.240.37.90:22122
storage_server_port=23000 #storage服务器的端口号
group_name=G1 #当前服务器的group名
url_have_group_name = true #文件url中是否有group名
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/data1/storage #存储路径
http.need_find_content_type=true #从文件扩展名查找文件类型(nginx时为true)
#include http.conf #开启http服务
10.240.37.90的mod_fastdfs.conf配置如下:
base_path=/data2/storage #保存日志目录
tracker_server=10.240.37.87:22122 #tracker服务器的IP地址以及端口号
tracker_server=10.240.37.90:22122
storage_server_port=23000 #storage服务器的端口号
group_name=G2 #当前服务器的group名
url_have_group_name = true #文件url中是否有group名
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/data2/storage #存储路径
http.need_find_content_type=true #从文件扩展名查找文件类型(nginx时为true)
#include http.conf #开启http服务
4.配置nginx(nginx的listen端口一定要和storage的http端口相同)
vim /usr/local/nginx/conf/nginx.conf
10.240.37.87的nginx.conf配置如下:
user nobody;
worker_processes 4;
worker_rlimit_nofile 65535;
error_log /usr/local/nginx/logs/all_error.log;
pid /usr/local/nginx/logs/nginx.pid;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
error_page 400 403 500 502 503 504 /50x.html;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
gzip off;
gzip_static on;
gzip_min_length 1k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_disable "MSIE [1-6].";
gzip_vary 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;
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/logs/all_access.log main;
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:5m
max_size=100m inactive=12h;
proxy_temp_path /var/cache/nginx/proxy_cache/tmp;
server {
listen 8080;
server_name localhost;
server_name_in_redirect off;
access_log logs/storage_access.log main;
error_log logs/storage_error.log;
location /G1/M00 {
root /data1/storage/data;
ngx_fastdfs_module;
}
}
}
10.240.37.90的nginx.conf基本配置不变,只需修改server里边location的root路径就行:
server {
listen 8080;
server_name localhost;
server_name_in_redirect off;
access_log logs/storage_access.log main;
error_log logs/storage_error.log;
location /G2/M00 {
root /data2/storage/data;
ngx_fastdfs_module;
}
}
5.建立M00至存储目录的符号连接
10.240.37.87执行 ln -s /data1/storage/data /data1/storage/data/M00
10.240.37.90执行 ln -s /data2/storage/data /data2/storage/data/M00
6.启动顺序(注意关闭防火墙和seslinux):
/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
7.测试文件是否可以上传下载:
(1)修改一台机器的client.conf配置文件:
base_path=/data1/tracker #日志存放路径
tracker_server=10.240.37.87:22122 #tracker服务器IP地址和端口号
http.tracker_server_port=8081 #tracker服务器的http端口号
(2)执行如下操作:
[root@localhost fdfs]# /usr/local/bin/fdfs_test /etc/fdfs/client.conf upload /usr/local/test/2007119124413448_2.jpg
This is FastDFS client test program v5.01
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.
[2015-07-06 18:44:52] DEBUG - base_path=/tmp, 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=10.240.37.90, port=23000
group_name=G2, ip_addr=10.240.37.90, port=23000
storage_upload_by_filename
group_name=G2, remote_filename=M00/00/00/CvAlWlWaXCSAXfcKAAF3qJ0P44M093.jpg
source ip address: 10.240.37.90
file timestamp=2015-07-06 18:44:52
file size=96168
file crc32=2635064195
example file url: http://10.240.37.90:8081/G2/M00/00/00/CvAlWlWaXCSAXfcKAAF3qJ0P44M093.jpg
storage_upload_slave_by_filename
group_name=G2, remote_filename=M00/00/00/CvAlWlWaXCSAXfcKAAF3qJ0P44M093_big.jpg
source ip address: 10.240.37.90
file timestamp=2015-07-06 18:44:52
file size=96168
file crc32=2635064195
example file url: http://10.240.37.90:8081/G2/M00/00/00/CvAlWlWaXCSAXfcKAAF3qJ0P44M093_big.jpg
然后下载的话 把返回的80801改成8080即可访问,因为tracker的http端口如果不做负载均衡的话是不提供http服务的。
浏览器访问地址为:http://10.240.37.90:8080/G2/M00/00/00/CvAlWlWaXCSAXfcKAAF3qJ0P44M093_big.jpg
8.注意事项与所遇到的错误:
(1)清掉防火墙规则
sudo iptables -F
(2)注意include和#之间不能有空格
#include http.conf
(3)测试文件是否可以上传下载
/usr/local/bin/fdfs_test /etc/fdfs/client.conf upload /usr/local/test/2007119124413448_2.jpg
下载返回的地址:
wget http://10.240.37.90:8080/G2/M00/00/00/CvAlWlWWTqqAeg8WAAF3qJ0P44M256_big.jpg
测试报错:
HTTP request sent, awaiting response... 500 Internal Server Error
在linux 下 使用这个命令增加进程打开的文件句柄。
ulimit -SHn 51200
并修改nginx配置文件的worker_connections 10240;
测试配置文件是否修改正确
/usr/local/nginx/sbin/nginx -t
重启nginx
/usr/local/nginx/sbin/nginx -s reload
查看nginx 的错误日志 /usr/local/nginx/logs/error.log
/usr/local/nginx/html/50x.html" failed (24: Too many open files)
cento 中 ulimit -n 为1024, 当Nginx连接数超过1024时,error.log中就出现以上错误
解决方法是:
修改/etc/security/limits.conf
在 /etc/security/limits.conf 中加入以下内容:
* soft nofile 655360
* hard nofile 655360
星号代表全局, soft为软件,hard为硬件,nofile为<type>这里指可打开文件数。
另外,要使 limits.conf 文件配置生效,必须要确保 pam_limits.so 文件被加入到启动文件中。查看 /etc/pam.d/login 文件中有:
session required /lib/security/pam_limits.so
修改完重新登录就可以见到效果,可以通过 ulimit -n
然后再修改 /usr/local/nginx/conf/nginx.conf
worker_rlimit_nofile 65535;
worker_connections 10240;
然后报400错误
400 Bad Request
Request Header Or Cookie Too Large
查看日志报错如下:
"/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory)
解决方法,增大请求缓冲,编辑nginx.conf,修改下面两个配置项为:
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
重启即可。
备注:如果有多个tracker可配置nginx+keepalived做前端下载上传的负载均衡.