云服务器配置Fastdfs图片存储服务器

云服务器配置Fastdfs图片存储服务器

一、需要下载以下几个东西。

fastdfs-nginx-module :用于FastDFS整合nginx。用nginx进行代理
在这里插入图片描述

二、将这几个压缩文件上传到服务的目录中,本次上传的目录为/opt/.

三、FastDFS–tracker安装

1、检查FastDFS安装环境

FastDFS是C语言开发,建议在linux上运行,本教程使用Centos7.0作为安装环境。

yum install gcc-c++ -y
# 安装redis的时候,可能需要 yum install gcc-c++ -y automake autoconf 这个两个依赖。

2、安装libevent

 yum -y install libevent

3、安装libfastcommon

1、将上传的压缩包文件libfastcommonV1.0.7.tar.gz 减压到 /usr/local目录下
tar -zxvf libfastcommonV1.0.7.tar.gz -C/usr/local/
4、进行libfastcommonV1.0.7目录进行编译
./make.sh
5、如果出现编译perl 不识别 运行下面这段命令
yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget
6、编译完成后执行安装
./make.sh install

注意:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录,所以需要将/usr/lib64下的库文件拷贝至/usr/lib下。

 cp /usr/lib64/libfastcommon.so /usr/lib/

4 、tracker编译安装

1、将FastDFS_v5.05.tar.gz减压到/usr/local/下
tar -zxvf FastDFS_v5.05.tar.gz -C/usr/local
2、还是进入FastDFS的根目录只是编制安装
cd FastDFS
./make.sh   
./make.sh install
3、安装成功之后,将安装目录下的conf下的文件拷贝到/etc/fdfs/下。
cd conf
cp  *  /etc/fdfs/
4、修改配置文件
vim  /etc/fdfs/tracker.conf

在这里插入图片描述

5、因为没有该目录,所以需要创建该目录。创建fastdfs文件夹
mkdir  /opt/fastdfs

5 设置启动项

1、创建启动目录
mkdir  /usr/local/fdfs  
2、拷贝安装目录/usr/local/FastDFS下stop.sh 和restart.sh 到/usr/local/fdfs/
cp restart.sh  /usr/local/fdfs/
cp stop.sh  /usr/local/fdfs/
3、修改启动脚本
vim /etc/init.d/fdfs_trackerd

将bin换成fdfs ,4处
在这里插入图片描述
在这里插入图片描述
把启动脚本中的路径按照上图修改
修改完毕后
注册服务

cd /etc/init.d/
chkconfig --add fdfs_trackerd

可以使用 chkconfig --list 查看是否已经添加成功!
然后可以用启动测试

service fdfs_trackerd start 

显示绿色的Ok或确认提示则安装成功。

四、astDFS–storage安装

1、修改配置文件

vim /etc/fdfs/storage.conf

修改行号:41,109,118
在这里插入图片描述
在这里插入图片描述

巨坑提醒:上面的tracker_server后的ip地址,测试的时候写云服务器本地的地址(以就是私有ip),整合到Java中的时候写公网ip。

2 创建fdfs_storage文件夹

mkdir /opt/fastdfs/fdfs_storage

3 设置启动服务

vim  /etc/init.d/fdfs_storaged

在这里插入图片描述
在这里插入图片描述
启动配置

cd /etc/init.d/
chkconfig  - -add  fdfs_storaged
chkconfig --list

启动服务

service fdfs_storaged start 
/opt/fastdfs/数据文件及日志
/usr/bin/fdfs_trackerd 、 fdfs_storaged启动执行程序
/usr/local/fdfs/stop.sh 、 restart.sh关闭、重启脚本
/usr/bin/fdfs_trackerd、 fdfs_storaged 启动执行程序
/etc/init.d/fdfs_tracked 、fdfs_storaged服务启动脚本
/etc/fdfs/配置文件

上传图片测试
astDFS安装成功可通过/usr/bin/fdfs_test测试上传、下载等操作。
修改/etc/fdfs/client.conf

vim /etc/fdfs/client.conf

base_path=/opt/fastdfs
tracker_server=IP:22122
注意:这是服务器内部测试,需要些私有IP。
测试代码,a.png是手动上传的图片:

 /usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/a.png
[root@iZ9dl1lxaqlso5Z /]# /usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/a.png
This is FastDFS client test program v5.05

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/ 
for more detail.

[2019-11-02 16:49:55] DEBUG - base_path=/opt/fastdfs, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

tracker_query_storage_store_list_without_group: 
        server 1. group_name=, ip_addr=xxx.xx.xxx.xx, port=23000

group_name=group1, ip_addr=xxx.xx.xxx.xx, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/rBHvE129QzOAeCXAAAGvZQOfHw4183.png
source ip address: xxx.xx.xx.xx
file timestamp=2019-11-02 16:49:55
file size=110437
file crc32=60759822
example file url: http://xxx.xx.xxx.xx/group1/M00/00/00/rBHvE129QzOAeCXAAAGvZQOfHw4183.png
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/rBHvE129QzOAeCXAAAGvZQOfHw4183_big.png
source ip address: xxx.xx.xxx.xx
file timestamp=2019-11-02 16:49:55
file size=110437
file crc32=60759822
example file url: http://xxx.xx.xxx.xx/group1/M00/00/00/rBHvE129QzOAeCXAAAGvZQOfHw4183_big.png
[root@iZ9dl1lxaqlso5Z /]# 
xxx.xx.xxx.xx:这个ip,我替换掉了

对应的上传路径:
/opt/fastdfs/fdfs_storage/data
00/00/rBHvE129QzOAeCXAAAGvZQOfHw4183.png

五、FastDFS整合nginx

1 安装前配置fastdfs-nginx-module

1、减压

tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C/usr/local

2、编辑配置文件:修改config文件将/usr/local/路径改为/usr/ 3处

vim fastdfs-nginx-module/src/config

在这里插入图片描述

3、将FastDFS-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下

cp mod_fastdfs.conf /etc/fdfs/

修改mod_fastdfs.conf的内容:

vim /etc/fdfs/mod_fastdfs.conf

行号 10,40 ,53,62
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4、将libfdfsclient.so拷贝至/usr/lib下

cp /usr/lib64/libfdfsclient.so /usr/lib/

六、安装nginx

1、安装nginx 需要先配置nginx 的运行环境

第一步:安装C语言编译环境

gcc:yum -y install gcc-c++

第二步:安装第三方的开发包。

PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 Perl 兼容的正则表达式库。Nginx的HTTP模块使用PCRE来解析正则表达式,所以需要在Linux上安装PCRE库。

yum install -y pcre pcre-devel

注:pcre-devel是使用PCRE开发的一个二次开发库。Nginx也需要这个库。
zlib
zlib库提供了很多种压缩和解压缩的方式,Nginx使用zlib对HTTP包的内容进行gzip,所以需要在Linux上安装zlib库。

yum install -y zlib zlib-devel

OpenSSL
OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

2、安装nginx

1、解压到/usr/local下

tar -zxvf nginx-1.12.2.tar.gz -C/usr/local

2、创建nginx/client目录

mkdir -p /var/temp/nginx/client

3、编译安装nginx

configure: 表示自动配置nginx 的相关目录!

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs-nginx-module/src

编译,需要进到nginx减压的目录

make

安装

make install

四 编辑nginx.conf 代理!

编辑 nginx 的安装路径的nginx.conf

vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述
启动nginx

/usr/local/nginx/sbin/nginx

重启:

/usr/local/nginx/sbin/nginx -s reload

设置开机启动 – os7:需要独立设置

vim /etc/rc.d/rc.local

在这里插入代码片

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
/usr/local/nginx/sbin/nginx

关闭防火墙或者开放端口

systemctl stop firewalld.service

注:/usr/local/nginx/sbin/nginx 自启动失败!centos7.0 版本会失败!权限会高。
整合java就OK了。

注意:切记,开放22122端口,以及23000端口。

如果不开放23000端口,会报连接超时。
配置文件:tracker.conf

tracker_server=xxx.xx.xxx.x:22122
# 连接超时时间,针对socket套接字函数connect,默认为30秒
connect_timeout=30000
# 网络通讯超时时间,默认是60秒
network_timeout=60000

html代码用的是富文本编辑器:wangEditor,直接下载就好。这个代码是借用了别人,我没有去写。上传时没问题的。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div id="div1">
		<p>欢迎使用 wangEditor 富文本编辑器</p>
	</div>
	<script src="${PATH}/static/jquery/jquery-2.1.1.min.js"></script>
	<script src="${PATH}/static/ztree/wangEditor.min.js"></script>
	<script type="text/javascript">
		var E = window.wangEditor
		var editor = new E('#div1')

		// 配置服务器端地址
		editor.customConfig.uploadImgServer = '${PATH}/uploadImg'
		editor.customConfig.uploadFileName = 'myFileName'
	    editor.customConfig.debug = location.href.indexOf('wangeditor_debug_mode=1') > 0 		
		
		// 进行下文提到的其他配置
		
		editor.customConfig.uploadImgTimeout = 300000   //默认10秒
		
		editor.customConfig.uploadImgHooks = {
		    before: function (xhr, editor, files) {
		        // 图片上传之前触发
		        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files 是选择的图片文件
		        
		        // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
		        // return {
		        //     prevent: true,
		        //     msg: '放弃上传'
		        // }
		    },
		    success: function (xhr, editor, result) {
		        // 图片上传并返回结果,图片插入成功之后触发
		        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
		        
		        /* alert("success:" + result); */
		       /*  alert(eval('(' + result + ')')); */
		    },
		    fail: function (xhr, editor, result) {
		        // 图片上传并返回结果,但图片插入错误时触发
		        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
		    	alert("fail:" + result);
		    },
		    error: function (xhr, editor) {
		        // 图片上传出错时触发
		        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
		    },
		    timeout: function (xhr, editor) {
		        // 图片上传超时时触发
		        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
		    },
		
		    // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
		    // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
		    customInsert: function (insertImg, result, editor) {
		        // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
		        // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
		
		        // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
		        var url = result.url
		        alert("customInsert:" + url)
		        insertImg(url)
		
		        // result 必须是一个 JSON 格式字符串!!!否则报错
		    
		    }
		}

		editor.create()
	</script>
</body>
</html>

Java代码:controller层

package com.pmc.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pmc.util.UploadUtil;

@Controller
public class FileUploadController {

  	@RequestMapping(value = "uploadImg",method = RequestMethod.POST)
  	public @ResponseBody Object uploadImg(@RequestParam("myFileName")MultipartFile myFileName,HttpSession session,HttpServletRequest request) throws IllegalStateException, IOException{
  		 String imgUrl ="";
  		if (myFileName != null) {
  			imgUrl = UploadUtil.uploadImage(myFileName);
  		}
  		Map<String, Object> resultMap = new HashMap<String, Object>();
  		resultMap.put("url", imgUrl);
  		System.out.println(resultMap);
  		return resultMap;
  	}
}

上传代码:

package com.pmc.util;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class UploadUtil {

    public static String uploadImage(MultipartFile multipartFile){
        String imgUrl = "http://xxx.xx.xxx.x";
        //上传文件到服务器,配置fdfs的全局链接地址
        String tracker = UploadUtil.class.getResource("/tracker.conf").getPath();//获取配置文件的路径
        try {
            ClientGlobal.init(tracker);
        } catch (Exception e) {
            e.printStackTrace();
        }
        TrackerClient trackerClient = new TrackerClient();
        //获得一个Trackerservice实例
        TrackerServer trackerServer = null;
        try {
            trackerServer = trackerClient.getConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //通过tracker获得一个Storage链接客户端
        StorageClient storageClient = new StorageClient(trackerServer,null);
        try {
             byte[] bytes = multipartFile.getBytes();//获得上传的二进制文件
            //获得文件后缀名
            String originalFilename = multipartFile.getOriginalFilename();
            int i = originalFilename.lastIndexOf(".");
            String extName = originalFilename.substring(i+1);
            String[] uploadInfos = storageClient.upload_file(bytes,extName,null);
            for (String uploadInfo:uploadInfos) {
                imgUrl += "/"+uploadInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgUrl;
    }
}

测试上传:
在这里插入图片描述
完了。以上代码若有错误,请留言。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值