安装清单如下:
一、安装FastDFS
1. 安装libfastcommon
先解压安装包到目录
# unzip libfastcommon-1.0.36.zip
安装编译工具及环境(后面Nginx也会用到这些依赖环境)
# yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-deve
# yum -y install pcre-devel openssl openssl-devel
# yum -y install zlib zlib-deve
编译libfastcommon软件并安装
# ./make.sh && ./make.sh install
为libcommon 创建软链接到/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. 安装FastDFS
解压安装包
# tar -xvf FastDFS_v5.01.tar.gz
编译并安装
# cd FastDFS
# ./make.sh && ./make.sh install
安装成功后,FastDFS会安装在/etc/fdfs目录下:
[root@localhost FastDFS]# ll /etc/fdfs/ total 56 -rw-r--r--. 1 root root 1461 Aug 13 06:39 client.conf -rw-r--r--. 1 root root 858 Aug 13 06:39 http.conf -rw-r--r--. 1 root root 31172 Aug 13 06:39 mime.types -rw-r--r--. 1 root root 7535 Aug 13 06:39 storage.conf -rw-r--r--. 1 root root 6990 Aug 13 06:39 tracker.conf
FastDFS安装结束
二、安装Tracker
1. 创建tracker工作目录
此目录用于保存tracker 的data和log
# mkdir -p /data/fastdfs_tracker
2.配置tracker
配置 /etc/fdfs目录下tracker.conf
主要实现以下4个配置内容:
disabled=false port=22122 #默认端口号 base_path=/data/fastdfs_tracker #我刚刚创建的目录 http.server_port=8080 #默认端口是8080 bind_addr=0.0.0.0 #监听地址
完整tracker.conf 文件信息如下:
disabled=false bind_addr=0.0.0.0 port=22122 connect_timeout=30 network_timeout=60 base_path=/opt/fastdfs_tracker max_connections=512 accept_threads=1 work_threads=4 min_buff_size=8KB max_buff_size=128KB store_lookup=2 store_group=group2 store_server=0 store_path=0 download_server=0 reserved_storage_space=10% log_level=info run_by_group= run_by_user= allow_hosts=* sync_log_buff_interval=10 check_active_interval=120 thread_stack_size=64KB storage_ip_changed_auto_adjust=true storage_sync_file_max_delay=86400 storage_sync_file_max_time=300 use_trunk_file=false slot_min_size=256 slot_max_size=16MB trunk_file_size=64MB trunk_create_file_advance=false trunk_create_file_time_base=02:00 trunk_create_file_interval=86400 trunk_create_file_space_threshold=20G trunk_init_check_occupying=false trunk_init_reload_from_binlog=false trunk_compress_binlog_min_interval=0 use_storage_id=false storage_ids_filename=storage_ids.conf id_type_in_filename=ip store_slave_file_use_link=false rotate_error_log=false error_log_rotate_time=00:00 rotate_error_log_size=0 log_file_keep_days=0 use_connection_pool=false connection_pool_max_idle_time=3600 http.server_port=8080 http.check_alive_interval=30 http.check_alive_type=tcp http.check_alive_uri=/status.html
3. 将Tracker加入开机启动项
# cp /usr/fastdfs/FastDFS/init.d/fdfs_trackerd /etc/init.d/
# chkconfig --add fdfs_trackerd
# chkconfig fdfs_trackerd on
启动Tracker Server
# service fdfs_trackerd start
4. 查看一下tracker的端口监听情况
# netstat -unltp|grep fdfs
三、安装Storage
1. 配置storage工作目录
由于storage还需要一个目录用来存储数据,因此多建了两个目录fastdfs_storage_data,fastdfs_storage
# mkdir -p /data/fastdfs_storage
# mkdir -p /data/fastdfs_storage_data
2. 配置storage文件
修改 /etc/fdfs 目录下 storage.conf 文件
修改要点如下:
disabled=false group_name=group1 #组名,根据实际情况修改 port=23000 #设置storage的端口号,同一个组的storage端口号必须一致 base_path=/data/fastdfs_storage #设置storage数据文件和日志目录 store_path_count=1 #存储路径个数,需要和store_path个数匹配 store_path0=/data/fastdfs_storage_data #实际文件存储路径 tracker_server=192.168.78.132:22122 #我CentOS7的ip地址 file_distribute_path_mode=1 # 存储方式 0:轮询 1:Hash http.server_port=8888 #设置 http 端口号
完整信息如下:
disabled=false group_name=group1 bind_addr=0.0.0.0 client_bind=true port=23000 connect_timeout=30 network_timeout=60 heart_beat_interval=30 stat_report_interval=60 base_path=/opt/fastdfs_storage max_connections=256 buff_size=256KB accept_threads=1 work_threads=4 disk_rw_separated=true disk_reader_threads=1 disk_writer_threads=1 sync_wait_msec=50 sync_interval=0 sync_start_time=00:00 sync_end_time=23:59 write_mark_file_freq=500 store_path_count=1 store_path0=/opt/fastdfs_storage_data subdir_count_per_path=256 tracker_server=192.168.78.132:22122 log_level=info run_by_group= run_by_user= allow_hosts=* file_distribute_path_mode=0 file_distribute_rotate_count=100 fsync_after_written_bytes=0 sync_log_buff_interval=10 sync_binlog_buff_interval=10 sync_stat_file_interval=300 thread_stack_size=512KB upload_priority=10 if_alias_prefix= check_file_duplicate=0 file_signature_method=hash key_namespace=FastDFS keep_alive=0 use_access_log=false rotate_access_log=false access_log_rotate_time=00:00 rotate_error_log=false error_log_rotate_time=00:00 rotate_access_log_size=0 rotate_error_log_size=0 log_file_keep_days=0 file_sync_skip_invalid_record=false use_connection_pool=false connection_pool_max_idle_time=3600 http.domain_name= http.server_port=8888
3. 将Storage加入开机启动项
# cp /usr/fastdfs/FastDFS/init.d/fdfs_storaged /etc/init.d/
# chkconfig --add fdfs_storaged
# chkconfig fdfs_storaged on
启动Storage Server(默认产生255*255个目录)
# service fdfs_storaged start
4. 查看一下Storage的端口监听情况
# netstat -unltp | grep fdfs
5. 校验整合
到这里,fastdfs的东西都已安装完成,最后我们还要确定一下,storage是否注册到了tracker中去。
查看命令:
# /usr/local/bin/fdfs_monitor /etc/fdfs/storage.conf
成功后可以看到:
ip_addr = 192.168.78.132 ACTIVE
四、测试
1. 配置客户端
修改 /etc/fdfs/目录下的client.conf 文件
修改要点为:
base_path=/data/fastdfs_tracker #tracker服务器文件路径 tracker_server=192.168.78.132:22122 #tracker服务器IP地址和端口号 http.tracker_server_port=8080 # tracker 服务器的 http端口号,必须和tracker的设置对应起来
完整client.conf 文件信息如下:
connect_timeout=30 network_timeout=60 base_path=/data/fastdfs_tracker tracker_server=192.168.78.132:22122 log_level=info use_connection_pool = false connection_pool_max_idle_time = 3600 load_fdfs_parameters_from_tracker=false use_storage_id = false storage_ids_filename = storage_ids.conf http.tracker_server_port=8080
2. 模拟上传
从个人用户目录上传一个图片,进行测试
# /usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf /df.png
group1/M00/73/38/wKhOhFt0uRqAIWxDAAAQKuWUya0709.png
组名:group1
磁盘:M00
目录:73/38
文件名称:wKhOhFt0uRqAIWxDAAAQKuWUya0709.jpg
定位上传的文件位置如下:
[root@localhost FastDFS]# ll /data/fastdfs_storage_data/data/73/38 total 8 -rw-r--r--. 1 root root 4138 Aug 15 16:36 wKhOhFt0uRqAIWxDAAAQKuWUya0709.png
实际文件存储路径下有创建好的多级目录。data下有256个1级目录,每级目录下又有256个2级子目录,总共65536个文件,新写的文件会以hash的方式被路由到其中某个子目录下,然后将文件数据直接作为一个本地文件存储到该目录中。
如果要访问刚上传的图片,我们得需要结合nginx来实现
五、安装Nginx并实现配置
1. 安装Nginx依赖环境
第一步已经安装完成。
2. 安装nginx并添加fastdfs-nginx-module
解压nginx和fastdfs-nginx-module
# tar -xvf nginx-1.14.0.tar.gz
# tar -xvf fastdfs-nginx-module_v1.16.tar.gz
解压后进入nginx目录编译安装nginx,并添加fastdfs-nginx-module
# ./configure --prefix=/usr/local/nginx --add-module=/usr/fastdfs/fastdfs-nginx-module/src #解压后fastdfs-nginx-module所在的位置
之后进行编译和安装
# make && make isntall
安装成功后,nginx会安装在/usr/local/nginx,安装后查看
[root@localhost nginx-1.14.0]# ll /usr/local/nginx/ total 16 drwxr-xr-x. 2 root root 4096 Aug 15 16:57 conf drwxr-xr-x. 2 root root 4096 Aug 15 16:57 html drwxr-xr-x. 2 root root 4096 Aug 15 16:57 logs drwxr-xr-x. 2 root root 4096 Aug 15 16:57 sbin
3. 配置Storage Nginx
修改Nginx 目录下 conf 的配置文件nginx.conf,新增location信息,具体如下:
server {listen 9991;server_name localhost;location / {root html;index index.html index.htm;}location ~/group1/M00 {root /data/fastdfs_storage_data;ngx_fastdfs_module;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }
需要把fastdfs-nginx-module安装目录中src目录下的mod_fastdfs.conf也拷贝到/etc/fdfs目录下:
# cp /usr/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
对刚刚拷贝的mod_fastdfs.conf文件进行修改:
base_path=/data/fastdfs_storage #保存日志目录 tracker_server=192.168.78.132:22122 #tracker服务器的IP地址以及端口号 storage_server_port=23000 #storage服务器的端口号 url_have_group_name = true #文件 url 中是否有 group 名 store_path0=/data/fastdfs_storage_data #存储路径 group_count = 1 #设置组的个数
在文件的最后,设置group
[group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/data/fastdfs_storage_data
创建M00至storage存储目录的符号连接:
ln -s /data/fastdfs_storage_data/data/ /data/fastdfs_storage_data/data/M00
启动Nginx:
# /usr/local/nginx/sbin/nginx
访问Nginx是否启动
[root@localhost fdfs]# curl localhost:9991 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;} </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p> </body> </html>
4. 配置Tracker Nginx
在nginx.conf 文件添加一个虚拟机
upstream fdfs_group1{server 127.0.0.1:9991; } server {listen 80;server_name localhost;location /group1/M00 {proxy_pass http://fdfs_group1; }error_page 500 502 503 504 /50x.htmllocation = /50x.html {root html;} }
完整nginx.conf配置文件如下:
worker_processes 1;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 65;#gzip on;server {listen 9991;server_name localhost;location / {root html;index index.html index.htm;}location ~/group1/M00 {root /data/fastdfs_storage_data;ngx_fastdfs_module;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}upstream fdfs_group1{server 127.0.0.1:9991;}server {listen 80;server_name localhost;location /group1/M00 {proxy_pass http://fdfs_group1; }error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }
将Nginx重新启动
# /usr/local/nginx/sbin/nginx -s reload
访问Nginx是否已经启动
[root@localhost conf]# curl localhost:9991 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;} </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p> </body> </html>
5. HTTP访问图片资源
http://192.168.78.132/group1/M00/AD/73/wKhOhFt0xNmAN7XbAAAQKuWUya0565.png