原文:https://cloud.tencent.com/developer/article/1459349
nginx日志管理 可参考https://www.cnblogs.com/crazylqy/p/6891991.htmlhtml
系统: Centos7
环境和依赖
按照后面标注的方式下载或者安装好须要的依赖java
系统: centos7
yum install -y gcc-c++ #须要依赖gcc
yum install -y pcre #用于支持rewrite模块 pcre-8.32-17nginx
yum install -y libevent #FastDFS依赖libevent库 libevent-2.0.21-4.el7.x86_64c++
yum install -y zlib zlib-devel #用于支持gzip模块 zlib-1.2.7-17.el7.x86_64git
libfastcommon-master.zip #libfastcommon包含了FastDFS运行所须要的一些基础库。https://github.com/happyfish100/libfastcommon/releasesgithub
fastdfs-nginx-module-master.zip # https://codeload.github.com/happyfish100/fastdfs-nginx-module/zip/masterredis
FastDFS版本:fastdfs-5.11.zip #https://github.com/happyfish100/fastdfs/archive/V5.11.zipvim
Nginx版本:nginx-1.14.tar.gz : #官网下载: http://nginx.org/en/download.htmlcentos
安装依赖
能够yum 安装的先直接yum 安装浏览器
将全部下载好依赖上传至服务器/download目录
安装libfastcommon
unzip libfastcommon-1.0.39.zip
mv libfastcommon-1.0.39 /usr/local/libfastcommon
cd /usr/local/libfastcommon/
./make.sh #编译
./make.sh install #安装
# 检查确认/usr/lib64 和 /usr/lib 目录下都生成了 libfastcommon.so (有些版本可能/usr/lib下不会生成)
ls /usr/lib64 | grep libfastcommon.so
ls /usr/lib | grep libfastcommon.so
安装tracker
FastDFS由跟踪服务器(tracker server)、存储服务器(storage server)和客户端组成,
fastdfs的tracker 和storage其实用的是同一个服务,只是配置文件不一样
Tracker是FastDFS的协调者,负责管理全部的storage server和group
storage server 负责存储服务
cd /download
unzip fastdfs-5.11.zip
mv fastdfs-5.11 /usr/local/FastDFS
cd /usr/local/FastDFS/
./make.sh && ./make.sh install #编译以后接着进行安装
#确认安装成功,没有报错后,拷贝配置文件到/etc/fdfs
cp /usr/local/FastDFS/conf/* /etc/fdfs/
#,进入/etc/fdfs 目录, 若是没有tracker.conf就拷贝一份tracker.conf.sample 去掉sample
cp tracker.conf.sample track.conf
配置tracker.conf ;
vi track.conf # 修改如下配置
#===============
base_path=/home/fastdfs #基础目录,之后的data 和日志目录都会放在此目录下
http.server_port=80 #配置http服务端口,这个端口跟后面nginx的监听端口对应
建立目录(目录可能不存在)
mkdir /home/fastdfs
启动tracker,运行以下命令:
/usr/bin/fdfs_trackerd /etc/fdfs/track.conf restart #检查是否启动成功:默认端口22122 ps -ef | grep fdfs #检查/home/fastdfs下是否生成data / logs 两个目录 ls /home/fastdfs/
安装storage
fastdfs的tracker 和storage其实用的是同一个服务,只是配置文件不一样,咱们只须要使用storage的配置文件启动fastdfs便是storage服务
cd /etc/fdfs #若是没有storage.conf 拷贝一份storage.conf.sample 命名为storage.conf cp storage.conf.sample storage.conf #配置storage.conf vi storage.conf #修改以下配置 #=========================== group_name=group1 #配置组名,同一个组的storage 互为备份 base_path=/home/fastdfs #基础目录 #store存放文件的位置(store_path), 能够配置多个, 记得建立路径 store_path0=/home/fdfs_storage #若是有多个挂载磁盘则定义多个store_path,以下 #store_path1=..... #store_path2=...... #配置tracker服务器:IP,阿里云服务器若是须要外部访问请配置公网ip, 不要使用私有ip tracker_server=192.168.112.130:22122 #若是有多个tracker则配置多个tracker #tracker_server=192.168.112.131:22122 #配置http端口 http.server_port=80 #==========完=========== #建立 /home/fdfs_storage 目录 mkdir -p /home/fdfs_storage #启动storage /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart #查看启动是否成功 fdfs_monitor /etc/fdfs/storage.conf #查看fdfs状态 ps -ef | grep fdfs #查看目录是否建立,/home/fdfs_storage/data 应该会自动生成256个文件夹 ls /home/fdfs_storage/data
测试上传文件
fastdfs自带了测试客户端,咱们可使用它测试搭建是否成功
cd /etc/fdfs cp client.conf.sample client.conf vim client.conf #修改以下配置 #================= base_path=/home/fastdfs tracker_server=192.168.112.130:22122 #=======完======== #准备一张图片进行测试 /usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/123.jpg #一切正常的话会返回图片地址等信息 example file url: http://192.168.112.130/group1/M00/00/00/wKhwgltd_uyAZSPfAAJHNmHqPh0482_big.jpg #检查文件是否存在(文件名被存放在/home/fdfs_storage/data/00/00对应的目录中),若是存在便是保存 OK,此时还没法使用http下载 cd /home/fdfs_storage/data/00/00 #查看是否存在wKhwgltd_uyAZSPfAAJHNmHqPh0482_big.jpg
FastDFS 和Nginx整合
单独的Fastdfs 对http支持很差,因此做为图片服务器,咱们通常都会整合Nginx来支持图片的http访问
#解压 fastdfs-nginx-module 到 /usr/local目录下; cd /download unzip fastdfs-nginx-module-master.zip mv fastdfs-nginx-module-master /usr/local/fastdfs-nginx-module cd /usr/local/fastdfs-nginx-module/src
修改config文件,
# 将文件中的全部 /usr/local/ 路径改成 /usr/ , #若是后面步骤中编译nginx报错把下面两项作如下修改 ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/" CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
拷贝配置文件到/etc/fdfs
cp mod_fastdfs.conf /etc/fdfs/ #修改 /etc/fdfs/mod_fastdfs.conf ; vi /etc/fdfs/mod_fastdfs.conf #======修改如下内容======== base_path=/home/fastdfs #保存日志的路径 tracker_server=192.168.112.130:22122 #track_server配置的服务端口 url_have_group_name=true #url中是否包含group名称 store_path0=/home/fdfs_storage #指定文件存储路径(必须和storage.conf配置相同)
检查/usr/lib 和usr/lib64下是否有 libfdfsclient.so
ls /usr/lib | grep libfdfsclient.so ls /usr/lib64 | grep libfdfsclient.so
建立nginx/client目录
mkdir -p /var/temp/nginx/client
安装nginx
tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/
编译nginx,加入fastdfs模块, 若是以前装过nginx, 须要从新安装
#进入解压目录, 日志目录可本身定义,主要是add-modoule cd /usr/local/nginx-1.14.0 #添加模块 ./configure \ --prefix=/usr/local/nginx \ --add-module=/usr/local/fastdfs-nginx-module/src
编译并安装
#在nginx1.14.0目录下执行 make && make install
编译后在/usr/local下生成nginx 目录
修改nginx 配置文件
vi /usr/local/nginx/conf/nginx.conf #======添加配置=========== server { listen 80; server_name 192.168.112.130; location /group1/M00/ { root /home/fdfs_storage/data; ngx_fastdfs_module; } }
启动nginx
/usr/local/nginx/sbin/nginx #检查是否启动成功,不成功就去查看错误日志/var/log/nginx/error.log ps -ef | grep nginx
关闭防火墙或者添加对应端口
systemctl stop firewalld.service
再次上传一张图片
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/123.jpg
获得一个地址
example file url: http://192.168.112.130/group1/M00/00/00/wKhwgltekE6AeekMAAJHNmHqPh0069_big.jpg
在浏览器中访问该图片:
http://192.168.112.130/group1/M00/00/00/wKhwgltekE6AeekMAAJHNmHqPh0069_big.jpg
若是可以正常浏览图片那就大功告成,
#查看nginx 日志能够看到访问记录
tail -f -n 100 /var/log/nginx/access.log
PS: nginx fastfds的安装最坑的就是一些版本中动态连接的问题, 在使用以前一些版本的时候常常会出现各类问题,若是确实须要使用以前的版本可能就须要本身去改一些动态连接的指向了,若是没有特别版本要求,使用本文的版本是没有问题的
项目中如何使用fastdfs
使用方式比较简单,咱们就直接拿来一个示例代码了
导入依赖(也能够本身去git编译一个jar包出来)
<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
配置文件: fdfs.conf
# 链接tracker服务器超时时长 connect_timeout = 10 # socket链接超时时长 network_timeout = 30 # 文件内容编码 charset = UTF-8 # tracker服务器端口 http.tracker_http_port = 80 http.anti_steal_token = no #密码 http.secret_key = 123456 # tracker服务器IP和端口(能够写多个) tracker_server = 192.168.58.128:22122
测试代码
public class FastdfsTest { @Test public void testUpload() throws Exception { //一、把FastDFS提供的jar包添加到工程中 //二、初始化全局配置。加载一个配置文件。 ClientGlobal.init("F:\\wolf-shop\\src\\main\\resources\\fdfs.conf"); //三、建立一个TrackerClient对象。 TrackerClient trackerClient = new TrackerClient(); //四、建立一个TrackerServer对象。 TrackerServer trackerServer = trackerClient.getConnection(); //五、声明一个StorageServer对象,null。 StorageServer storageServer = null; //六、得到StorageClient对象。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //七、直接调用StorageClient对象方法上传文件便可。 String[] result = storageClient.upload_file("F:\\redis_demo\\src\\main\\resources\\logo.png", "png", null); StringBuilder sb = new StringBuilder("http://192.168.112.130/"); sb.append(result[0]).append("/").append(result[1]); System.out.println("图片访问地址: "+sb.toString()); } }
项目中根据本身的实际使用状况抽取一个工具类用起来就方便了