您的位置:首页 > 运维架构 > Linux

阿里云服务器 centOS 7.3 配置FastDFS图片服务器

2020-01-14 17:09 85 查看

云服务centOS 7.3 配置FastDFS

新装的阿里云centOS7.3的系统,在这之前没有安装任何东西。所有代码都是我实践验证过的,由于我是先配置好了再回头写的文档,可能会有疏漏。

一、上传所需的文件

二、安装gcc(编译时需要)

FastDFS是C语言开发,安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc

yum install -y gcc gcc-c++

三、安装libfastcommon

libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS运行所需要的一些基础库。

1、解压之前上传的libfastcommonV1.0.7.tar至/usr/local/下:

tar xvf libfastcommonV1.0.7.tar -C /usr/local/

2、目录切换至解压文件下编译并安装:

cd /usr/local/libfastcommon-1.0.7/
./make.sh
./make.sh install

安装成功:

3、拷贝libfastcommon.so至/usr/lib下

libfastcommon安装好后会在/usr/lib64 目录下生成 libfastcommon.so 库文件,由于FastDFS程序引用usr/lib目录所以需要将/usr/lib64下的库文件拷贝至/usr/lib下。

四、安装tracker

1、将FastDFS_v5.05.tar.gz 解压至/usr/local/下

tar xvf FastDFS_v5.05.tar -C /usr/local

2、编译并安装FastDFS

切换目录至/usr/local/FastDFS/ 编译并安装

cd /usr/local/FastDFS/
./make.sh

./make.sh install

3、将安装目录下的conf下的文件拷贝到/etc/fdfs/下

cp /usr/local/FastDFS/conf/* /etc/fdfs/

五、配置和启动tracker

1、修改配置文件

vim  /etc/fdfs/tracker.conf

我的路径为:/home/picture 本文后面所有的/home/picture 皆可替换成你自己的路径

2、创建储存目录

之前配置的目录并不存在,所以需要创建。

mkdir /home/picture

3、设置启动项

(1)、创建启动目录

mkdir  /usr/local/fdfs

(2)、拷贝/usr/local/FastDFS下的stop.sh 和restart.sh 至/usr/local/fdfs/

cp /usr/local/FastDFS/restart.sh  /usr/local/fdfs/
cp /usr/local/FastDFS/stop.sh  /usr/local/fdfs/

(3)、修改启动脚本

vim /etc/init.d/fdfs_trackerd

将bin替换为fdfs

(4)、注册服务

注册服务:

chkconfig --add fdfs_trackerd

查看是否成功:

chkconfig --list

4、启动tracker

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

注:在/home/picture目录下会生成data和logs两个目录

六、配置并启动storage

由于上面已经安装过FastDFS,这里只需要配置storage。

1、修改配置文件

vim /etc/fdfs/storage.conf



store_path0 的目录是上传文件所储存的目录,名字自定义。

2、创建fdfs_storage目录

mkdir /opt/fastdfs/fdfs_storage

3、配置启动服务

修改的位置跟trackerd相同

vim  /etc/init.d/fdfs_storaged

3、注册启动配置

chkconfig  - -add  fdfs_storaged

4、启动服务

service fdfs_storaged start

七、使用FastDFS自带工具测试

1、修改/etc/fdfs/client.conf

vim /etc/fdfs/client.conf

2、上传测试

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/文件名

上传成功的截图(没有鼠标,触控板打码打得我手疼,将就看吧~):


由于还没配置nginx,还无法使用http访问。

八 、FastDFS和nginx整合

1、配置fastdfs-nginx-module

(1)、解压 fastdfs-nginx-module_v1.16.tar.gz 至/usr/local/

tar xvf fastdfs-nginx-module_v1.16.tar -C/usr/local

(2)、修改配置文件

vim /usr/local/fastdfs-nginx-module/src/config

将文件中的/usr/local/改为/usr/

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

cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

(4)、修改mod_fastdfs.conf

vim /etc/fdfs/mod_fastdfs.conf

修改文件储存的地址和ip,一共四个更改的地方,截图的时候少画了一条红线。

(5)、将libfdfsclient.so拷贝至/usr/lib下

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

2、配置nginx 运行环境

(1)、安装C语言编译环境

yum -y install gcc-c++

由于之前已经安装过,就不用在安装了。

(2)、安装第三方的开发包。

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

3、安装nginx

(1)、解压文件

tar xvf nginx-1.12.2.tar -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的目录

cd /usr/local/nginx-1.12.2/

编译

make

安装

make install

4、编辑nginx.conf 代理

(1)、编辑 nginx 的安装路径的nginx.conf

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

(2)、启动nginx

启动:

/usr/local/nginx/sbin/nginx

重启:

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

(3)关闭防火墙

systemctl stop firewalld

九、把之前的本地ip改为公网ip并重启所有之前配置的启动项。

十、阿里云服务器记得去防火墙开放22122和23000两个端口

安全-》防火墙-》添加规则-》

十一、测试是否成功

之前测试upload的时候成功会返回一个地址:example file url: http:/内网ip/group1/M00/00/00/文件名。
在把之前设置的内网IP改为公网IP之后,在浏览器输入http:/公网ip/group1/M00/00/00/文件名 就能看到之前上传的图片了。

从晚上七八点左右开始弄到深夜十二点多才配置好。
然后就写这文档,现在已经四点了……
困意浓,提不起精神检查文档了,有疏漏的地方请留言。

十二、来补上后续上传的代码

1、后台代码

导入依赖

<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>

在application中加入配置

我使用的是yml

fdfs:
connect-timeout: 1000
so-timeout: 1000
thumb-image:
height: 400
width: 400
tracker-list:
- 服务器ip:22122
// 可以添加多个,依次序往下写

创建工具类

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
@Component
public class FastDFSClientUtil {
private final Logger logger = LoggerFactory.getLogger(FastDFSClientUtil.class);
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;

//上传文件
public String upload(MultipartFile multipartFile) throws Exception{
//文件名
String originalFilename = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
// 文件扩展名
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(),originalFilename , null);

String url = storePath.getFullPath();

return url;
}

/**
* 删除文件
* @Param fileUrl 文件访问地址
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
logger.warn(e.getMessage());
}
}
}

创建controller类

import com.libo.property.server.util.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FIleUploadController {
@Autowired
FastDFSClientUtil fastDFSClientUtil;
@RequestMapping("upload")
@ResponseBody
public String upload(MultipartFile multipartFile){
String url = null;
try {
url = fastDFSClientUtil.upload(multipartFile);
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
}

2、页面代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- 图片文本框 -->
<input type="file" class="form-control" id="file" name="file" th:onchange="javascript:preview(this)">

<!-- 这个是在上传之前回显图片图片展示 -->
<div id="preview">
  <!--这个是为了将页面返回的图片展示出来的.默认隐藏-->
  <img style="width: 100px; height: 100px;display:none" id="imgHidden" />
</div>

<!-- 提交...这里pageIndex和pageSize可传可不传,主要取决于提交之后是否需要回到当前页面. -->
<button type="submit" th:onclick="javascript:submitForm([[${pageIndex}]],[[${pageSize}]])" class="btn btn-primary">提交</button>
<script src="js/jquery-2.1.1.js"></script>
<script>
function submitForm(pageIndex, pageSize) {
var formData = new FormData(); //将需要提交的参数封装起来
var zswb = $("#file").val();    //获取file中的内容,看是否有值
if (zswb == '' || zswb.length < 1) {    //没有file提交的时候走的接口
$.ajax({
url : '/upload',
type : 'post',
data : formData,
processData : false,
contentType : false,
success : function(value) {
var result = JSON.parse(value);
if (result == 'true') {
window.location.href = "/index?pageIndex=" + pageIndex+ "&pageSize=" + pageSize;
} else {
Lobibox.alert('error', {msg : "媒资信息更新失败!!!"});
}
}
});
} else {    //有file提交的时候走的接口
formData.append("multipartFile", $("#file")[0].files[0]);
$.ajax({
url : '/upload',
type : 'post',
data : formData,
processData : false,
contentType : false,
success : function(url) {
console.log(url);
}
});
}
}
//图片回显:
function preview(file) {
$("#imgHidden").css("display", "none");
var prevDiv = document.getElementById('preview');
if (file.files && file.files[0]) {
var reader = new FileReader();
reader.onload = function(evt) {
prevDiv.innerHTML = '<img style="width: 100px;height: 100px;" src="' + evt.target.result + '" />';
}
reader.readAsDataURL(file.files[0]);
} else {
prevDiv.innerHTML = '<div class="img" style="width: 100px;height:100px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' +
file.value + '\'"></div>';
}
}
</script>
</body>
</html>

提交后返回的是图片的url,在前面加上服务器的ip就可以获取图片了。

暂时写到这里。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
他人亦已歌。 发布了5 篇原创文章 · 获赞 0 · 访问量 26 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: