项目环境的整个搭建过程中FastDFS(分布式文件系统)是坑我最深最久的一个软件,主要里面夹着着nginx转发模块,所以现在要详细的写一篇博客来记录在搭建过程中遇到的问题,同时也是对自己花这么长时间和精力在上面的一个成果的浓缩。
FastDFS本身的架构其实也是奔着集群去的,也就是说最正宗的做法应该是某几台机器只做Tracker(即Tracker群),某几台机器只做Storage(即Storage群),但是目前所搭建的开发环境根本没有那样的条件让我去搞,就只有两台云主机,然后其中一台上已经有了nginx作转发,因此在实际搭建过程中我的部署思路是这样的,其中一台主机上只跑Tracker服务(因为要跑Storage服务就需要装fastdfs-nginx-module作转发,会与已经存在的nginx发生冲突),而另外一台主机上既跑Tracker服务,又跑Storage服务,如此相当于两个Tracker服务,一个Storage服务。
1.创建fdfs用户组和用户
groupadd fdfs useradd fdfs -g fdfs
2.创建运行日志及数据存储目录
mkdir -p /home/fdfs/data/tracker # tracker运行日志及记录storage汇报心跳等dat文件 mkdir -p /home/fdfs/data/storage # storage运行日志及数据存储目录 mkdir -p /home/fdfs/data/nginx_fdfs # 为storage作转发的nginx运行日志存储目录) mkdir -p /home/fdfs/data/client # 客户端访问运行日志存储目录
3.FastDFS编译需依赖libevent,要求版本1.4.x+,通过命令查看是否已经安装
rpm -qa | grep libevent
4.如果没有安装需要先准备好libevent-2.0.21-stable.tar.gz安装包,并通过如下命令安装;如果检测到已经安装且版本高于1.4,则跳过该步骤
#编译安装libevent tar -zxvf libevent-2.0.21-stable.tar.gz cd libevent-2.0.21-stable ./configure --prefix=/usr/local/libevent make clean make && make install ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5 # 创建软连接
5.准备好并上传所需要的软件
FastDFS_v4.06.tar.gz
fastdfs-nginx-module_v1.15.tar.gz
nginx-1.6.0.tar.gz
6.tracker和storage都需要安装的部分
tar -zxvf FastDFS_v4.06.tar.gz cd FastDFS #因为使用了单独的fdfs用户来运行fastdfs,所以需要修改make.sh vi make.sh 修改 TARGET_PREFIX=/usr/local --> TARGET_PREFIX=/home/fdfs/app 修改 TARGET_CONF_PATH=/etc/fdfs --> TARGET_CONF_PATH=/home/fdfs/app/conf #编译安装 ./make.sh C_INCLUDE_PATH=/usr/local/libevent/include LIBRARY_PATH=/usr/local/libevent/lib ./make.sh install
7.storage需要安装的部分
yum -y install pcre pcre-devel gcc gcc-c++ zlib-devel openssl-devel # 需要监测并安装的类库(因为要安装nginx-module) #安装nginx-module tar -zxvf fastdfs-nginx-module_v1.15.tar.gz cd fastdfs-nginx-module #因为使用了单独的fdfs用户来运行fastdfs,所以需要修改config vi src/config 修改所有 /usr/local -->/home/fdfs/app 修改所有 /etc/fdfs -->/home/fdfs/app/conf #复制mod_fastdfs.conf到/home/fdfs/app/conf/目录下 cp /home/fdfs/soft/fastdfs-nginx-module/src/mod_fastdfs.conf /home/fdfs/app/conf/ #安装nginx tar -zxvf nginx-1.6.0.tar.gz cd nginx-1.6.0/ #编译安装 ./configure --prefix=/home/fdfs/nginx_fdfs --add-module=/home/fdfs/soft/fastdfs-nginx-module/src make && make install
8.配置tracker server
cd /home/fdfs/app/conf vi tracker.conf #存储的目录地址 base_path=/home/yuqing/fastdfs -> base_path=/home/fdfs/data/tracker #运行fastdfs的用户和组,默认为空,即为当前用户 run_by_group= -> run_by_group=fdfs run_by_user= -> run_by_user=fdfs #日志级别,默认为info,相比各种java项目产生的log,debug级别产生的log简直就是可以忽略,大胆的置为debug吧 log_level=info -> log_level=debug #使用ip或者sid模式,这三个选项是fastdfs的无索引特性决定的。v4之前文件名是由storage server生成,里头包含了源storage server ip,可以通过反解文件名出ip,从而定位到文件。但是一旦机房搬迁或者ip发生变化,反解出的ip就定位不到了,因此v4针对此问题做了优化,加入sid的概念,增加了配置文件配置sid跟ip的关系,将sid加入到文件名的生成中,反解后根据对应关系即可定位到ip,只要保证整个集群内sid是唯一的即可,可选值:ip、id use_storage_id=false -> use_storage_id=true storage_ids_filename=storage_ids.conf -> storage_ids_filename=/home/fdfs/app/conf/storage_ids.conf id_type_in_filename=ip -> id_type_in_filename=id #设置是否使用连接池 use_connection_pool=false -> use_connection_pool=true #将源安装包中的storage_ids.conf,移动到安装后的配置文件夹,编辑storage server id和ip地址的对应关系 cp -r /usr/src/soft/FastDFS/conf/storage_ids.conf /home/fdfs/app/conf cd /home/fdfs/app/conf vi storage_ids.conf #1000001 group1 117.79.146.5
9.启动tracker server
chown -R fdfs:fdfs /home/fdfs # 改变目录所有者 cd /home/fdfs/app/bin #启动tracker ./fdfs_trackerd /home/fdfs/app/conf/tracker.conf #查看tracker log,检查是否安装成功 tail -100f /home/fdfs/data/tracker/logs/trackerd.log
10.配置storage server
vi /home/fdfs/app/conf/storage.conf #存储数据和log基目录 base_path=/home/yuqing/fastdfs -> /home/fdfs/data/storage #设置上传数据的挂载点,count的个数和storage_path的个数一致,从0开始,一个一行 storage_path_count=1 storage_path0=/home/yuqing/fastdfs -> /home/fdfs/data/storage #设置tracker地址(需分2行写2个tracker的地址) tracker_server=117.79.146.5:22122 #设置运行FastDFS的用户组 run_by_group= -> run_by_group=fdfs run_by_user= -> run_by_user=fdfs #日志级别设置,依旧大胆的设置成debug吧 log_level=info ->log_level=debug #是否使用连接池 use_connection_pool=false -> use_connection_pool=true
11.启动storage server
cd /home/fdfs/app/bin #启动storage ./fdfs_storaged /home/fdfs/app/conf/storage.conf #如果屏幕上打印了一堆如下,那就ok了 data path:/home/fdfs/storage/data,mkdir sub dir... mkdir data path: 00 ... mkdir data path: 01 ... mkdir data path: 02 ... mkdir data path: 03 ... ....................... data path: /home/fastdfs/storage/data, mkdir sub dir done.
12.配置客户端client
vi /home/fdfs/app/conf/client.conf #配置client客户端存储log的地址 base_path=/home/yuqing/fastdfs -> base_path=/home/fdfs/data/client #设置tracker地址 tracker_server=192.168.209.121:22122 -> tracker_server=117.79.146.5:22122 #日志级别设置 log_level=info -> log_level=debug #是否使用连接池 use_connection_pool=false -> use_connection_pool=true #从tracker.conf 复制相同的配置项 load_fdfs_paramters_from_tracker=false -> load_fdfs_paramters_from_tracker=true
13.测试上传文件
cd /home/fdfs/app/bin ./fdfs_test /home/fdfs/app/conf/client.conf upload test.txt
14.配置nginx_module
cd /home/fdfs/app/conf vi mod_fastdfs.conf #配置存放module产生log的位置 base_path=/home/fdfs/data/nginx_fdfs #从tracker.conf 复制相同的配置项 load_fdfs_parameters_from_tracker=false -> load_fdfs_parameters_from_tracker=true #tracker地址,一行一个地址 tracker_server=192.168.25.11:22122 -> tracker_server=117.79.146.5:22122 #url中是否包含组名,该项与nginx配置的location相关 url_have_group_name = false -> url_have_group_name = true #同storage.conf中的配置(至关重要,我就是被这个坑掺了,曾因此一度怀疑自己的能力) store_path_count=1 store_path0=/home/fdfs/data/storage #log文件名 log_filename=/home/fdfs/data/nginx_fdfs/mod_fastdfs.log #日志级别设置 log_level=info -> log_level=debug #扩展content_type http.need_find_content_type=false -> http.need_find_content_type=true
15.配置nginx
cd /home/fdfs/nginx_fdfs/conf vi nginx.conf
user root; # 这里一定要设置,否则会出现权限不足 worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 7070; server_name localhost; charset koi8-r; access_log logs/7070server.log main; location ~* /group1/(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ { root /home/fdfs/data/storage/data/; set $h $2; set $w $3; if ($h = "0") { rewrite /group1/(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ /group1/$1.$4 last; } if ($w = "0") { rewrite /group1/(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ /group1/$1.$4 last; } if ($h != "0"){ rewrite /group1/(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ /group1/$1.$4 break; } if ($w != "0"){ rewrite /group1/(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ /group1/$1.$4 break; } #image_filter resize $h $w; #image_filter_buffer 2M; try_files /group1/$1.$4 /group1/notfound.jpg; } location ~* /group1 { root /home/fdfs/data/storage/data/; ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
16.启动nginx
/home/fdfs/nginx_fdfs/sbin/nginx
17.如果报错如下:
./nginx: error while loading shared libraries: libfastcommon.so: cannot open shared object file: No such file or directory
解决方案:
ln -s /home/fdfs/app/lib/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /home/fdfs/app/lib/libfastcommon.so /usr/lib64/libfastcommon.so
如果报错如下:
./nginx: error while loading shared libraries: libfdfsclient.so: cannot open shared object file: No such file or directory
解决方案:
ln -s /home/fdfs/app/lib/libfdfsclient.so /usr/lib/libfdfsclient.so
ln -s /home/fdfs/app/lib/libfdfsclient.so /usr/lib64/libfdfsclient.so
18.至此,FastDFS的相关安装及配置结束,相关的安装软件我会上传至附件。真心希望能够帮助到大家!