搭建环境准备: Traker: 192.168.124.84 Storage: 192.168.124.155
nginx(及其依赖包)+fastDFS(及其依赖包)
# 在Traker上上传一系列包在/usr/local/src下
# 在Storage上上传一系列包到/usr/local/src下
一、在Tracker上配置 关闭防火墙 systemctl stop firewalld setenforce 0
1.安装libfastcommon
# 进到/usr/local/src下
cd /usr/local/src
# 解压
tar zxvf libfastcommon-1.0.35.tar.gz -C /data/server
# 进到目标文件目录下
cd /data/server/libfastcommon-1.0.35/
# 编译安装
* ./make.sh
这里会显示报错 ./make.sh: line 14: gcc: command not found ./make.sh: line 15: ./a.out: No such file or directory cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o hash.o hash.c make: cc: Command not found make: *** [hash.o] Error 127
解决方法: yum install -y gcc*(编译时需要gcc环境)
* ./make.sh
* ./make.sh install
# 添加环境变量
vim /etc/profile
在末行添加
export LD_LIBRARY_PATH=/usr/lib64/
# 使环境变量生效
source /etc/profile
# 创建软链接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
2.安装libevent
# 进到/usr/local/src目录下
cd /usr/local/src
# 解压
tar zxvf libevent-2.0.20-stable.tar.gz -C /data/server
# 进到目标文件目录
cd /data/server/libevent-2.0.20-stable
# 编译安装
* ./configure --prefix=/data/server
* 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
#创建目录
mkdir -p /fdfs/tracker
# 进到 /usr/local/src目录下
cd /usr/local/src
# 解压
tar zxvf FastDFS_v5.08.tar.gz -C /data/server
# 进到目标文件目录
cd /data/server/FastDFS
# 编译安装
* ./make.sh
* ./make.sh install
4.配置文件 vim /data/server/FastDFS/conf/tracker.conf
修改如下内容
base_path=/fdfs/tracker #设置tracker的数据文件和日志目录
max_connections=300 #最大连接数
work_threads=1 #工作线程数,最好和cpu核数相同
store_lookup=0 #0为轮询模式
http.server_port=8080 #设置http端口号
5.启动tracker cd /data/server/FastDFS/tracker fdfs_trackerd /data/server/FastDFS/conf/tracker.conf
6.查看是否启动成功 cat /fdfs/tracker/logs/trackerd.log
++++++++++++++++++++++++++++++++++++++++++++
以下内容在在操作完Storage的第1步到第18步之后再操作
7.创建缓存目录 mkdir -p /var/cache/nginx/proxy_cache
8.安装nginx及其插件
# 回到/usr/local/src下
cd /usr/local/src
# 解压这四个包
tar -zxvf nginx-1.11.5.tar.gz -C /data/server
tar zxvf fastdfs-nginx-module_v1.16.tar.gz -C /data/server
tar zxvf pcre-8.34.tar.gz -C /data/server
tar zxvf zlib-1.2.8.tar.gz -C /data/server
# 进到nginx目标文件目录
cd /data/server/nginx-1.11.5/
# 编译安装
* ./configure --prefix=/data/server/nginx --add-module=/data/server/fastdfs-nginx-module/src/ --with-pcre=/data/server/pcre-8.34/ --with-zlib=/data/server/zlib-1.2.8
* make && make install
9.配置nginx文件 vim /data/server/nginx/conf/nginx.conf
添加补充下面内容 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 /data/server/nginx/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.124.155:8080 fail_timeout=30s; # storage的ip }
#设置other的服务器 upstream fdfs_other { server 192.168.124.155:8080 fail_timeout=30s; # storage的ip }
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模块要与server模块对齐 location ~ /purge(/.*) { allow 127.0.0.1; allow 192.168.124.0/24; #改成自己的网段 deny all; proxy_cache_purge http-cache $1$is_args$args; }
+++++++++++++++++++++++++++++++++++++++++
10.修改client.conf文件 vim /data/server/FastDFS/conf/client.conf
修改如下内容:
base_path=/fdfs/tracker #日志存放路径
tracker_server=192.168.124.84:22122
http.tracker_server_port=8080 #tracker服务器的http端口号
+++++++++++++++++++++++++++++++ 去storage上操作第19步 +++++++++++++++++++++++++++++++
11.运行nginx cd /data/server/nginx/sbin ./nginx
访问网页测试一下nginx是否搭好
12.上传图片
# 先上传一张图片到Moba上
# fdfs_upload_file /data/server/FastDFS/conf/client.conf /root/a.jpg # 路径写全
得到返回值
head/M00/00/00/wKh8m11WVYOAQKRwAAGWPojC1gk461.jpg
# 在浏览器上访问
http://192.168.124.155:8080/head/M00/00/00/wKh8m11WVYOAQKRwAAGWPojC1gk461.jpg
# 再上传一个图片
# fdfs_upload_file /data/server/FastDFS/conf/client.conf /root/b.jpg # 路径写全
得到返回值
other/M00/00/00/wKh8m11WVguAbtNMAAKFVWAT42o693.jpg
# 在浏览器上访问
http://192.168.124.155:8080/other/M00/00/00/wKh8m11WVguAbtNMAAKFVWAT42o693.jpg
# 再上传的时候就会 一个上传到head 一个上传到other
轮询模式实现
+++++++++++++++++++++++++++++++++++++++++ 去storage上操作第30步(检测数据是否存储成功) +++++++++++++++++++++++++++++++++++++++++
二、在Storage上配置 关闭防火墙 systemctl stop firewalld setenforce 0
1.安装libfastcommon
# 进到/usr/local/src下
cd /usr/local/src
# 解压
tar zxvf libfastcommon-1.0.35.tar.gz -C /data/server
# 进到目标文件目录下
cd /data/server/libfastcommon-1.0.35/
# 编译安装
* ./make.sh
这里会显示报错 ./make.sh: line 14: gcc: command not found ./make.sh: line 15: ./a.out: No such file or directory cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o hash.o hash.c make: cc: Command not found make: *** [hash.o] Error 127
解决方法: yum install -y gcc*(编译时需要gcc环境)
* ./make.sh
* ./make.sh install
# 添加环境变量
vim /etc/profile
在末行添加
export LD_LIBRARY_PATH=/usr/lib64/
# 使环境变量生效
source /etc/profile
# 创建软链接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
2.安装libevent
# 进到/usr/local/src目录下
cd /usr/local/src
# 解压
tar zxvf libevent-2.0.20-stable.tar.gz -C /data/server
# 进到目标文件目录
cd /data/server/libevent-2.0.20-stable
# 编译安装
* ./configure --prefix=/data/server
* 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.安装storage
# 创建对应存储目录
mkdir -p /fdfs/tracker #tracker目录
mkdir -p /fdfs/head/data #存放头像文件
mkdir -p /fdfs/other/data #存放其他文件
mkdir -p /fdfs/storage #storage数据节点
# 解压
tar zxvf FastDFS_v5.08.tar.gz -C /data/server
# 进到目标文件目录
cd /data/server/FastDFS
# 编译安装
* ./make.sh
* ./make.sh install
4.配置文件 vim /data/server/FastDFS/conf/storage.conf
修改以下内容 disabled=false #启用配置文件 max_connections=300 #最大连接数 work_threads=1 #工作线程数,最好和cpu核数相同 group_name=head #组名, port=23000 #设置storage的端口号 base_path=/fdfs/storage #设置storage的日志目录(事先创建好的那个) store_path_count=1 #存储路径个数,需要和store_path个数匹配 store_path0=/fdfs/head #存储路径 tracker_server=192.168.124.84:22122 #tracker服务器的IP地址和端口号 http.server_port=8080 #设置http端口号
5.运行storage cd /data/server/FastDFS/storage fdfs_storaged /data/server/FastDFS/conf/storage.conf
6.确认是否运行成功 cat /fdfs/storage/logs/storaged.log
7.查看存储目录 cd /fdfs/head/data ls
8.安装nginx及其插件
# 回到/usr/local/src下
cd /usr/local/src
# 解压这四个包
tar -zxvf nginx-1.11.5.tar.gz -C /data/server
tar zxvf fastdfs-nginx-module_v1.16.tar.gz -C /data/server
tar zxvf pcre-8.34.tar.gz -C /data/server
tar zxvf zlib-1.2.8.tar.gz -C /data/server
# 修改相关路径参数
vim /data/server/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/"
# 进到nginx目标文件目录
cd /data/server/nginx-1.11.5/
# 编译安装
* ./configure --prefix=/data/server/nginx --add-module=/data/server/fastdfs-nginx-module/src/ --with-pcre=/data/server/pcre-8.34/ --with-zlib=/data/server/zlib-1.2.8
* make && make install
9.配置nginx文件 vim /data/server/nginx/conf/nginx.conf
添加修改
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; }
10.创建other组 cd /data/server/FastDFS/conf/ cp storage.conf storage_other.conf
11.配置storage other组 vim /data/server/FastDFS/conf/storage_other.conf
在其中修改
group_name=other
port=23001
store_path0=/fdfs/other
12.启动other的storage cd /data/server/FastDFS/storage fdfs_storaged /data/server/FastDFS/conf/storage_other.conf
13.查看存储状况 cd /fdfs/other/data ls
14.配置相关文件 vim /data/server/fastdfs-nginx-module/src/mod_fastdfs.conf
修改
base_path=/fdfs/storage #保存日志目录
tracker_server=192.168.124.84:22122 #tracker的ip
url_have_group_name = true #文件url中是否有group名
group_name=group1 #把这行注释掉
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
15.创建软链接 ln -s /fdfs/head/data /fdfs/head/data/M00 ln -s /fdfs/other/data /fdfs/other/data/M00
16.查看软链接 ll /fdfs/head/data/M00 ll /fdfs/other/data/M00
17.运行nginx cd /data/server/nginx/sbin ./nginx
18.访问网页 在浏览器输入192.168.124.155:8080(storage的ip:8080)
会出现如下页面
在这里会出现无法访问页面的情况
解决方法:
# 查看错误日志
cat /data/server/nginx/logs/error.log
我们会看到时这个报错
ERROR - file: shared_func.c, line: 968, file /etc/fdfs/mod_fastdfs.conf not exist
ERROR - file: /data/server/fastdfs-nginx-module/src//common.c, line: 155, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code: 2
# cd /data/server/FastDFS/conf
# cp http.conf /etc/fdfs/
# cp mime.types /etc/fdfs
# cd /data/server/fastdfs-nginx-module/src
# cp mod_fastdfs.conf /etc/fdfs/
# cd /data/server/nginx/sbin
# ./nginx -s reload
# 访问网页
++++++++++++++++++++++++++++++++++++++++++++++++ 此时,去tracker上操作第7到第9步 ++++++++++++++++++++++++++++++++++++++++++++++++
19.修改client.conf文件 vim /data/server/FastDFS/conf/client.conf
修改如下内容:
base_path=/fdfs/storage #日志存放路径
tracker_server=192.168.124.84:22122
http.tracker_server_port=8080 #tracker服务器的http端口号
++++++++++++++++++++++++ 去tracker上操作第11到第12步 ++++++++++++++++++++++++
20.检测 cd /fdfs/head/data/00/00 ls
cd /fdfs/other/data/00/00 ls