fastDFS & nginx 综合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash

################################
# fastDFS need
################################
# 第一步:安装gcc环境
yum install -y gcc-c++

# 第二步:安装libevent,FastDFS依赖libevent库

yum install -y libevent


################################
# nginx need
################################
# 第三步:安装libfastcommon,libfastcommon是FastDFS官方提供的包,包含了FastDFS运行所需要的一些基础库

#Nginx是使用C语言开发,安装nginx需要先从官网上将源码下载,然后编译,编译需要gcc环境:
yum install -y gcc-c++

#PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安>
装pcre库
yum install -y pcre pcre-devel

#zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库
yum install -y zlib zlib-devel

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


wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/libfastcommonV1.0.7.tar.gz"

# 下载fastdfs #最新版可从github上下载 https://github.com/happyfish100/fastdfs
wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/fastdfs-5.05.tar.gz"

wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/nginx-1.8.0.tar.gz"

wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/fastdfs-nginx-module_v1.16.tar.gz"

# ---- fast dfs ---
if [ ! -d "/usr/local/fastdfs" ]; then
mkdir /usr/local/fastdfs
fi


cp ~/soft/libfastcommonV1.0.7.tar.gz /usr/local/fastdfs
cp ~/soft/fastdfs-5.05.tar.gz /usr/local/fastdfs

cd /usr/local/fastdfs

tar -zxvf libfastcommonV1.0.7.tar.gz

tar -zxvf fastdfs-5.05.tar.gz

cd libfastcommon-1.0.7

./make.sh && ./make.sh install

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


cd /usr/local/fastdfs/fastdfs-5.05

./make.sh && ./make.sh install

# --- 配置 ------
# 拷贝/usr/local/fastdfs/fastdfs-5.05/conf 目录下的文件到/etc/fdfs 目录下

if [ ! -d "/etc/fdfs" ]; then
mkdir /etc/fdfs
fi

cp /usr/local/fastdfs/fastdfs-5.05/conf/* /etc/fdfs/

cd /etc/fdfs


# ★ Tracker配置

sed -i '/^base_path/s/^/#/' /etc/fdfs/tracker.conf

sed -i '/^#base_path/a\\base_path=\/usr\/local\/fastdfs\/tracker' /etc/fdfs/tracker.conf

# ★ Storage配置

filepath="/etc/fdfs/storage.conf"

ipaddr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

names=("group_name" "base_path" "store_path0" "tracker_server")

replace_names=("group_name=group1" "base_path=\/usr\/local\/fastdfs\/storage" "store_path0=\/usr\/local\/fastdfs\/storage" "tracker_server=$ipaddr:22122")

count=${#names[*]}

for (( i=0; i<"$count"; i=i+1 ))

do
#sed -i "" "/^${names[$i]}/s/^/#/" $filepath
sed -i "/^${names[$i]}/s/^/#/" $filepath
#sed -i "" "/^#${names[$i]}/a\
#${replace_names[$i]}" $filepath
sed -i "/^#${names[$i]}/a\\${replace_names[$i]}" $filepath
done

# 创建目录
#mkdir /usr/local/fastdfs/tracker –p
#mkdir /usr/local/fastdfs/storage –p

if [ ! -d "/usr/local/fastdfs/tracker" ]; then
mkdir /usr/local/fastdfs/tracker –p
fi

if [ ! -d "/usr/local/fastdfs/storage" ]; then
mkdir /usr/local/fastdfs/storage –p
fi

ps -ef | grep tracker | awk -F " " '{if($8!="grep")print $2}' | xargs kill -9
ps -ef | grep storage | awk -F " " '{if($8!="grep")print $2}' | xargs kill -9
# --- 启动 ------
#Tracker启动命令:
#/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
#Storage启动命令:
#/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

##############################################
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
#设置开机自动启动:
#vim /etc/rc.d/rc.local

#将运行命令行添加进文件:
#/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
#/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
##############################################

# 安装fastdfs-nginx-module
cp ~/soft/fastdfs-nginx-module_v1.16.tar.gz /usr/local/fastdfs/

cd /usr/local/fastdfs

tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

filepath="/usr/local/fastdfs/fastdfs-nginx-module/src/config"

sed -i "s/\/usr\/local\/include\/fastdfs/\/usr\/include\/fastdfs/g" $filepath
sed -i "s/\/usr\/local\/include\/fastcommon/\/usr\/include\/fastcommon/g" $filepath
sed -i "s/\/usr\/local\/lib/\/usr\/lib/g" $filepath

#将fastdfs-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下
cp /usr/local/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

# 修改mod_FastDFS.conf文件

#base_path=/usr/local/fastdfs/storage
#tracker_server=192.168.242.140:22122
#url中是否包含group名称
#url_have_group_name=true
#指定文件存储路径,访问时使用该路径
#store_path0=/usr/local/fastdfs/storage

cd /etc/fdfs/

modfilepath="mod_fastdfs.conf"

ipaddr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

#ipaddr='192.168.1.101'

names=("base_path" "tracker_server" "url_have_group_name")

replace_names=("base_path=\/usr\/local\/fastdfs\/storage" "tracker_server=$ipaddr:22122" "url_have_group_name=true")

count=${#names[*]}

for (( i=0; i<"$count"; i=i+1 ))

do

sed -i "/^${names[$i]}/s/^/#/" $modfilepath

sed -i "/^#${names[$i]}/a\\${replace_names[$i]}" $modfilepath

done


#"#[group1]"
#
#"store_path0=\/usr\/local\/fastdfs\/storage"

## linux不能使用/r/n
#sed -i "s/^#\\[group1\\]/#\\[group1\\]`echo -e '\r\n'`store_path0=\/usr\/local\/fastdfs\/storage/g" $modfilepath

# 考虑到可能重复执行该脚本, 把之前设置的 注释掉
sed -ie "/^store_path0/s/^/#/" $modfilepath
sed -E -ie "/^#\[group1\]/a\\store_path0=\/usr\/local\/fastdfs\/storage" $modfilepath

# 将libfdfsclient.so拷贝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/

# 创建nginx/client目录
if [ ! -d "/var/temp/nginx/client" ]; then
mkdir -p /var/temp/nginx/client
fi

#-- nginx --

if [ ! -d "/usr/local/nginx" ]; then
mkdir /usr/local/nginx
fi

cp ~/soft/nginx-1.8.0.tar.gz /usr/local/nginx

cd /usr/local/nginx

tar -zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0


#使用nginx的configure命令进行配置,./configure --help查询详细参数
#进行nginx-1.8.0目录执行以下命令(其中prefix参数指定的是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/fastdfs-nginx-module/src

if [ ! -d "/var/temp/nginx" ]; then
mkdir /var/temp/nginx -p
fi

make && make install

echo ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

ls -al /usr/local/nginx


## 修改nginx.conf配置文件

file="/usr/local/nginx/conf/nginx.conf"

## 方法1: 不能重复使用, 端口重复问题
#sed -i '1,/^[[:space:]]*listen.*/s/^[[:space:]]*listen.*/\tlisten 8080;/' $file

#sed -i -e "/#gzip on;/a\\\t}" $file
#sed -i -e "/#gzip on;/a\\\t\t}" $file
#sed -i -e "/#gzip on;/a\\\t\tngx_fastdfs_module;" $file
#sed -i -e "/#gzip on;/a\\\tlocation /group1/M00/{" $file
#sed -i -e "/#gzip on;/a\\\tserver_name localhost;" $file
#sed -i -e "/#gzip on;/a\\\tlisten 80;" $file
#sed -i -e "/#gzip on;/a\\\tserver {" $file

## 方法2: 直接覆盖
echo "#user nobody;
worker_processes 1;

#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;
}


http {
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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;
location /group1/M00/ {
ngx_fastdfs_module;
}
}

server {
listen 8080;
server_name localhost;

#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;
# }
#}

}" > $file


## 启动nginx
/usr/local/nginx/sbin/nginx

# --- 启动 fast DFS------
#Tracker启动命令:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
#Storage启动命令:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

## 测试配置
#第一步:进入/etc/fdfs/ 目录,拷贝一份client.conf文件
cd /etc/fdfs
cp client.conf.sample client.conf

# 第二步:修改client.conf

#base_path=/usr/local/fastdfs/client
#tracker_server=192.168.242.140:22122

ipaddr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

sed -i '/^base_path/s/^/#/' /etc/fdfs/client.conf
sed -i '/^#base_path/a\\base_path=\/usr\/local\/fastdfs\/client' /etc/fdfs/client.conf

sed -i '/^tracker_server/s/^/#/' /etc/fdfs/client.conf
sed -i "/^#tracker_server/a\\tracker_server=$ipaddr:22122" /etc/fdfs/client.conf

if [ ! -d "/usr/local/fastdfs/client" ]; then
mkdir /usr/local/fastdfs/client -p
fi

###############################################

上传图片

1
2
3
4
#图片上传
scp /Users/stone/Pictures/sucai/android/9.jpg root@172.16.124.137:/home/tomcat.jpg

# 返回图片地址 浏览器访问

测试上传

1
2
3
4
5
6
7
8
9
#home目录下得有tomcat.png图片...
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.jpg

# 上传成功的话 返回图片下载路径
# 由于现在还没有和nginx整合无法使用http下载

#http://192.168.101.3/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png就是文件的下载访问路径。
#对应storage服务器上的磁盘路径:
#/usr/local/fastdfs/storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件

解决VMware克隆虚拟机无法上网

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

sudo sed -i '/eth0/s/^/#/' /etc/udev/rules.d/70-persistent-net.rules

sudo sed -i '/eth1/s/eth1/eth0/' /etc/udev/rules.d/70-persistent-net.rules

#sudo reboot

cat /etc/udev/rules.d/70-persistent-net.rules | grep eth0

success="可以重启了, 执行命令 sudo reboot"
echo -e "\033[0;32;1m$success\033[0m"

html测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<body>
<form action="${pageContext.request.contextPath }/pic/upload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="uploadFile" width="120px">
<input type="submit" value="上传">
</form>
<hr>
<%--<form action="${pageContext.request.contextPath }/file/down.do" method="get">--%>
<%--<input type="submit" value="下载">--%>
<%--</form>--%>
</body>

</body>
</html>

PicController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.stone.sncodetools.web.controller;


import com.stone.sncodetools.utils.FastDFSClient;
import com.stone.sncodetools.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
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;

import java.util.HashMap;
import java.util.Map;

/**
* Created by stone on 2018/2/11.
*/
@Controller
@RequestMapping("/pic")
public class PicController {
@Value("${imageBaseUrl}")
private String imageBaseUrl;

/*@RequestMapping(value = "/upload")
@ResponseBody
public Map<String, Object> upload(MultipartFile uploadFile) {

Map<String, Object> map = new HashMap<String, Object>();

if (uploadFile == null) {
map.put("error", 1);
map.put("message", "文件对象为空");
return map;
}

String originalFilename = uploadFile.getOriginalFilename();

if (StringUtils.isBlank(originalFilename)) {
map.put("error", 1);
map.put("message", "文件对象为空");
return map;
}

// 获取文件扩展名 (扩展名不带点)
String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

// 执行上传
try {
String uploadFileId = FastDFSClient.uploadFile(uploadFile.getBytes(), extName);

//组装完整图片访问URL
String url = imageBaseUrl + uploadFileId;

System.out.println(url);

map.put("error", 0);
map.put("url", url);

return map;

} catch (Exception e) {
e.printStackTrace();
map.put("error", 1);
map.put("message", "上传失败");

return map;
}

}*/

//为了ie 火狐 兼容性 不能返回json
@RequestMapping(value = "/upload")
@ResponseBody // ResponseBody作用 对象转json , raw数据返回, 不去找资源
public String upload(MultipartFile uploadFile) {

Map<String, Object> map = new HashMap<String, Object>();

if (uploadFile == null) {
map.put("error", 1);
map.put("message", "文件对象为空");
return JsonUtils.objectToJson(map);
}

String originalFilename = uploadFile.getOriginalFilename();

if (StringUtils.isBlank(originalFilename)) {
map.put("error", 1);
map.put("message", "文件对象为空");
return JsonUtils.objectToJson(map);
}

// 获取文件扩展名 (扩展名不带点)
String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
System.out.println(uploadFile);

// 执行上传
try {
String uploadFileId = FastDFSClient.uploadFile(uploadFile.getBytes(), extName);

//组装完整图片访问URL
String url = imageBaseUrl + uploadFileId;

System.out.println(url);

map.put("error", 0);
map.put("url", url);

System.out.println(JsonUtils.objectToJson(map));

return JsonUtils.objectToJson(map);

} catch (Exception e) {
e.printStackTrace();
map.put("error", 1);
map.put("message", "upload faild");

return JsonUtils.objectToJson(map);
}

}
}

Alt text