上一篇介绍了fastDFS相关术语和原理,本章将先搭建单机版的fastDFS。
环境准备:
虚拟机环境
liunx服务器(一):192.168.31.128
linux服务器(二):192.168.31.129
fastDFS及其nginx相关安装文件
链接:https://pan.baidu.com/s/1yJLDpr5IvAqdf269Hu4GGA 密码:4kva
将对应安装包放到linux环境下,这里我的安装包全部放在/home/fastDFS目录下
安装fastDFS依赖包
1)、安装libfastcommon
cd /usr/local/
mkdir fastDFS
cd /home/fastDFS/
tar -zxvf libfastcommon-1.0.35.tar.gz
mv libfastcommon-1.0.35 /usr/local/fastDFS/libfastcommon
cd /usr/local/fastDFS/libfastcommon/
./make.sh
./make.sh install
设置环境变量和创建软链接
export LD_LIBRARY_PATH=/usr/lib64/
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
2)、安装libevent
cd /home/fastDFS/
tar -zxvf libevent-2.0.20-stable.tar.gz
mv libevent-2.0.20-stable /usr/local/fastDFS/libevent
cd /usr/local/fastDFS/libevent/
./configure --prefix=/usr/local/
make clean
make && make install
创建软链接
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
3)、创建tracker目录(安装tracker时才执行此步骤)
mkdir -p /fdfs/tracker
tracker安装(192.168.31.128)
1)、解压对应安装包到指定目录
cd /home/fastDFS/
tar -zxvf FastDFS_v5.08.tar.gz
mv FastDFS /usr/local/fastDFS/
2)、编译和安装tracker
cd /usr/local/fastDFS/FastDFS/
./make.sh
./make.sh install
3)、配置
cd /etc/fdfs/
删除当前目录下所有文件
拷贝FastDFS安装目录下conf下面所有文件到当前目录
cp /usr/local/fastDFS/FastDFS/conf/* ./
编辑配置文件目录下的tracker.conf,一般只需改动以下几个参数即可:
base_path=/fdfs/tracker #设置tracker的数据文件和日志目录
max_connections=300 #最大连接数
work_threads=1 #工作线程数,最好和cpu核数相同
http.server_port=8080 #设置http端口号
4)、运行
将tracker在防火墙的对应的端口打开
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22122 -j ACCEPT
/etc/init.d/iptables save
启动tracker
/usr/local/fastDFS/FastDFS/tracker/fdfs_trackerd /etc/fdfs/tracker.conf restart
5)、确认tracker是否启动成功
查看tracker启动文件信息
cat /fdfs/tracker/logs/trackerd.log
确认启动成功后将tracker设置为开机自启动
vim /etc/rc.d/rc.local
在最后一行添加
/usr/local/fastDFS/FastDFS/tracker/fdfs_trackerd /etc/fdfs/tracker.conf restart
安装storage(192.168.31.129)
首先按照0步骤中安装对应依赖包libfastcommon和libevent
安装完成后创建对应storage目录
mkdir -p /fdfs/head/data #存放头像文件
mkdir -p /fdfs/other/data #存放其他文件
mkdir -p /fdfs/storage #storage数据节点
1)、解压对应安装包到指定目录
cd /home/fastDFS/
tar -zxvf FastDFS_v5.08.tar.gz
mv FastDFS /usr/local/fastDFS/
2)、编译和安装storage
cd /usr/local/fastDFS/FastDFS/
./make.sh
./make.sh install
3)、配置
cd /etc/fdfs/
删除当前目录下所有文件
拷贝FastDFS安装目录下conf下面所有文件到当前目录
cp /usr/local/fastDFS/FastDFS/conf/* ./
编辑配置文件目录下的storage.conf,一般只需改动以下几个参数即可。
disabled=false #启用配置文件
max_connections=300 #最大连接数
work_threads=1 #工作线程数,最好和cpu核数相同
group_name=head #组名,目前我们有3个(head、item、other)
port=23000 #设置storage的端口号(head、item、other)
base_path=/fdfs/storage #设置storage的日志目录(需预先创建)
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/fdfs/head #存储路径
tracker_server=192.168.31.128:22122 #tracker服务器的IP地址和端口号
http.server_port=8080 #设置http端口号
4)、运行
将storage在防火墙的对应的端口打开
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 23000 -j ACCEPT
/etc/init.d/iptables save
启动storage,会根据配置文件的设置自动创建多级存储目录
cd /fdfs/head/data
ls
如下图所示
5)、确认启动成功
cat /fdfs/storage/logs/storaged.log
如下图所示
启动成功后加入开机自启动
vim /etc/rc.d/rc.local
最后一行加入
/usr/local/fastDFS/FastDFS/storage/fdfs_storaged /etc/fdfs/storage.conf restart
在storage(192.168.31.129)中安装nginx
1)、安装包准备
nginx源码包
nginx-1.11.5.tar.gz
fastDFS相关插件包
fastdfs-nginx-module_v1.16.tar.gz
pcre-8.34.tar.gz
zlib-1.2.8.tar.gz
2)、将nginx和其他插件包全部解压到/usr/local/fastDFS/下
cd /home/fastDFS/
tar -zxvf nginx-1.11.5.tar.gz
mv nginx-1.11.5 /usr/local/fastDFS/
其他插件包也按照同样的方式做即可,
最后如下图所示,在/usr/local/fastDFS/下有对应解压包
3)、修改fastdfs-nginx-module/src下的config文件中的相关路径参数,否则下面的命令
编译不通过。
vi fastdfs-nginx-module/src/config
找到第4行,修改前为
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
修改后为
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
4)、安装nginx
cd /usr/local/fastDFS/nginx-1.11.5/
编译:
./configure --prefix=/usr/local/fastDFS/storageNginx --add-module=/usr/local/fastDFS/fastdfs-nginx-module/src/ --with-pcre=/usr/local/fastDFS/pcre-8.34/ --with-zlib=/usr/local/fastDFS/zlib-1.2.8
安装
make && make install
将FastDFS的nginx插件模块的配置文件copy到FastDFS配置文件目录
cp /usr/local/fastDFS/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
5)、配置nginx
cd /usr/local/fastDFS/storageNginx/conf/
vi nginx.conf
修改配置文件内容如下
worker_processes 1; #当前CPU核数
worker_cpu_affinity 1; #与cpu核数关联,如果是双核 那么配置 01 10
worker_rlimit_nofile 65535; #为nginx工作进程改变打开最多文件描述符数目的限制
error_log /usr/local/fastDFS/storageNginx/logs/error.log;
pid /usr/local/fastDFS/storageNginx/logs/nginx.pid;
events {
worker_connections 65535;
use epoll; #使用epoll的I/O 模型
}
keepalive_timeout 60; #keepalive超时时间
在server段中添加或者修改:
listen 8080; #监听storage的http端口
server_name 192.168.31.129; #storage的IP
location ~/head/M00 { #组head
root /fdfs/head/data;
ngx_fastdfs_module;
}
location ~/other/M00 { #组other
root /fdfs/other/data;
ngx_fastdfs_module;
}
6)、编辑/etc/fdfs配置文件目录下的mod_fastdfs.conf
base_path=/fdfs/storage #保存日志目录
tracker_server=192.168.31.128:22122 #暂时只有一个tracker
url_have_group_name = true #文件url中是否有group名
group_count = 2 #设置组的个数
在末尾增加2个组的具体信息:
[group1]
group_name=head
storage_server_port=23000
store_path_count=1
store_path0=/fdfs/head
[group2]
group_name=other
storage_server_port=23001
store_path_count=1
store_path0=/fdfs/other
**注意:修改完成之后我们可能有个疑问,貌似我们还没有other组**
cd /etc/fdfs/
cp storage.conf storage_other.conf
修改other组的相关配置信息
group_name=other
port=23001
store_path0=/fdfs/other
修改完毕后启动other组的storage
/usr/local/fastDFS/FastDFS/storage/fdfs_storaged /etc/fdfs/storage_other.conf restart
查看other目录下,是否生成了256了子目录,生成了则成功
cd /fdfs/other/data/
ls
如下图所示
记得需要将23001在防火墙中设置开放
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 23001 -j ACCEPT
/etc/init.d/iptables save
添加开机自启动
vim /etc/rc.d/rc.local
最后一行加入
/usr/local/fastDFS/FastDFS/storage/fdfs_storaged /etc/fdfs/storage_other.conf restart
7)、创建相关软链接
ln -s /fdfs/head/data /fdfs/head/data/M00
ln -s /fdfs/other/data /fdfs/other/data/M00
查看软链接目录
ll /fdfs/head/data/M00
ll /fdfs/other/data/M00
8)、运行nginx
打开防火墙中8080端口
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT;
/etc/init.d/iptables save
启动nginx
/usr/local/fastDFS/storageNginx/sbin/nginx
查看ngxin监听端口且是否启动成功
netstat -unltp | grep nginx
也可查看nginx的日志是否启动成功或是否有错误。
cat /usr/local/fastDFS/storageNginx/logs/error.log
在error.log中没有错误,既启动成功
打开浏览器访问:http://192.168.31.129:8080/
将nginx添加到开机启动
添加开机自启动
vim /etc/rc.d/rc.local
最后一行加入
/usr/local/fastDFS/storageNginx/sbin/nginx
在tracker(192.168.31.128)上安装nginx
1)、创建缓存目录
mkdir -p /var/cache/nginx/proxy_cache
2)、安装包准备
nginx源码包
nginx-1.11.5.tar.gz
fastDFS相关插件包
ngx_cache_purge-2.3.tar.gz
pcre-8.34.tar.gz
zlib-1.2.8.tar.gz
2)、将nginx和其他插件包全部解压到/usr/local/fastDFS/下
cd /home/fastDFS/
tar -zxvf nginx-1.11.5.tar.gz
mv nginx-1.11.5 /usr/local/fastDFS/
其他插件包也按照同样的方式做即可,
最后如下图所示,在/usr/local/fastDFS/下有对应解压包
4)、安装nginx
cd /usr/local/fastDFS/nginx-1.11.5/
编译:
./configure --prefix=/usr/local/fastDFS/trackerNginx --add-module=/usr/local/fastDFS/ngx_cache_purge-2.3 --with-pcre=/usr/local/fastDFS/pcre-8.34/ --with-zlib=/usr/local/fastDFS/zlib-1.2.8
安装
make && make install
6)、配置nginx
修改trackerNginx目录下nginx.conf文件(我的配置如下所示)
#user nobody;
worker_processes 1;
worker_cpu_affinity 1;
worker_rlimit_nofile 65535;
error_log /usr/local/fastDFS/trackerNginx/logs/error.log;
pid /usr/local/fastDFS/trackerNginx/logs/nginx.pid;
#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;
use epoll;
}
http {
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
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 /usr/local/fastDFS/trackerNginx/logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip 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=10g inactive=30d;
proxy_temp_path /var/cache/nginx/proxy_cache/tmp;
#设置head的服务器
upstream fdfs_head {
server 192.168.31.129:8080 fail_timeout=30s;
}
#设置other的服务器
upstream fdfs_other {
server 192.168.31.129:8080 fail_timeout=30s;
}
server {
listen 8888;
server_name localhost;
#设置head的负载均衡参数
location /head/M00 {
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://fdfs_head;
expires 30d;
}
#设置other的负载均衡参数
location /other/M00 {
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://fdfs_other;
expires 30d;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.31.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
#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;
# }
#}
}
7)、修改client.conf文件注意:两台机器都要修改
vi /etc/fdfs/client.conf
base_path=/fdfs/tracker #日志存放路径(tracker和storage不同)
tracker_server=192.168.31.128:22122
http.tracker_server_port=8080 #tracker服务器的http端口号
8)、运行nginx
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8888 -j ACCEPT
/etc/init.d/iptables save
启动nginx
/usr/local/fastDFS/trackerNginx/sbin/nginx
查看是否启动成功和监听端口
也可查看nginx的日志是否启动成功或是否有错误。
cat /usr/local/fastDFS/trackerNginx/logs/error.log
9)、上传文件
使用/usr/local/fastDFS/FastDFS/client/fdfs_upload_file上传一个文件, 程序会自动返回文件的URL。
我们在/home/目录下面放入一张图片test.png
执行上传命令
/usr/local/fastDFS/FastDFS/client/fdfs_upload_file /etc/fdfs/client.conf /home/test.png
如下图所示,返回了一个fileid
浏览器访问文件:http://192.168.31.128:8888/head/M00/00/00/wKgfgVrFp6CAaRmBAADUq3HS2v4927.png
查看192.168.31.129上storage中的文件存储信息