FastDFS+Nginx搭建图片服务器(亲测OK)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fu123123fu/article/details/85415391

前言

操作环境:CentOS7 X64,以下操作都是单机环境。
我把所有的安装包下载到/softpackage/下,解压到当前目录。

先做一件事,修改hosts,将文件服务器的ip与域名映射(单机TrackerServer环境),因为后面很多配置里面都需要去配置服务器地址,ip变了,就只需要修改hosts即可。

# vim /etc/hosts
增加如下一行,这是我的IP
192.168.63.138 file.yunxin.com
如果要本机访问虚拟机,在C:\Windows\System32\drivers\etc\hosts中同样增加一行

安装FastDFS环境

1、安装libevent

# yum -y install libevent

2、下载安装 libfastcommon
① 进入到/softpackage/目录

# cd /softpackage

② 下载libfastcommon

# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

③ 解压

# tar -zxvf V1.0.7.tar.gz
# cd libfastcommon-1.0.7

扫描二维码关注公众号,回复: 4850171 查看本文章

④ 编译、安装

# ./make.sh
# ./make.sh install

⑤ 拷贝 libfastcommon.so到 /usr/lib/下

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

3、下载安装FastDFS
① 进入到/softpackage/目录

# cd /softpackage

② 下载FastDFS

# wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz

③ 解压

# tar -zxvf V5.05.tar.gz
# cd fastdfs-5.05

④ 编译、安装

# ./make.sh
# ./make.sh install

⑤ 拷贝 /softpackage/fastdfs-5.05/conf下所有文件到 /etc/fdfs/下

# cd /softpackage/fastdfs-5.05/conf
# cp * /etc/fdfs/

4、配置Tracker服务
① 进入 /etc/fdfs下,编辑 tracker.conf

# cd /etc/fdfs/
# vim tracker.conf

② 修改以下部分(指定自己的路径),其余默认

base_path=/fastdfs/tracker

③ 创建上一步指定的路径,即 base_path对应的路径

# cd /
# mkdir /fastdfs/tracker -p

④ 将client和storage也一并创建出来

# cd /fastdfs/
# mkdir client
# mkdir storage

⑤ 启动Tracker服务

# cd /usr/bin
# fdfs_trackerd /etc/fdfs/tracker.conf

查看 FastDFS Tracker 是否已成功启动 ,22122端口正在被监听,则算是Tracker服务安装成功。

# netstat -unltp|grep fdfs

⑥ 设置Tracker开机启动

# chkconfig fdfs_trackerd on

5、配置 FastDFS 存储 (Storage)
① 进入 /etc/fdfs下,编辑 storage.conf

# cd /etc/fdfs/
# vim storage.conf

② 修改以下部分(指定自己的路径),其余默认

# group_name表示组织名,访问图片时需要加上这个才能访问,组织名自己随意定义
group_name=taofut
base_path=/fastdfs/storage
store_path0=/fastdfs/storage
tracker_server=file.yunxin.com:22122(最开始hosts配置的域名,也就是本机的ip地址)

③ 启动Storage服务

# cd /usr/bin
# fdfs_storaged /etc/fdfs/storage.conf

查看Storage是否成功启动,23000端口正在被监听,就算Storage启动成功。

# netstat -unltp|grep fdfs

④ 设置Storage开机启动

# chkconfig fdfs_storaged on

6、文件上传测试
① 修改客户端配置文件

# cd /etc/fdfs
# vim client.conf

② 修改以下部分,其余默认

base_path=/fastdfs/client
tracker_server=file.yunxin.com:22122

③ 上传测试,我事先在/home下放了一张图片 hello.png

# cd /home
# /usr/bin/fdfs_test /etc/fdfs/client.conf upload hello.png

上传成功后返回的结果:

This is FastDFS client test program v5.05

Copyright © 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-01-01 15:20:37] DEBUG - base_path=/fastdfs/client,
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=192.168.63.138, port=23000

group_name=taofut, ip_addr=192.168.63.138, port=23000
storage_upload_by_filename group_name=taofut,
remote_filename=M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189.png source ip
address: 192.168.63.138 file timestamp=2019-01-01 15:20:37 file
size=46195 file crc32=1118939218 example file url:
http://192.168.63.138/taofut/M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189.png
storage_upload_slave_by_filename group_name=taofut,
remote_filename=M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189_big.png
source ip address: 192.168.63.138 file timestamp=2019-01-01 15:20:37
file size=46195 file crc32=1118939218 example file url:
http://192.168.63.138/taofut/M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189_big.png

查看上传的文件

# cd /fastdfs/storage/data/00/00

安装Nginx

1、安装Nginx所需环境
① gcc 安装

# yum install gcc-c++

② pcre-devel 安装

# yum install -y pcre pcre-devel

③ zlib 安装

# yum install -y zlib zlib-devel

④ OpenSSL 安装

# yum install -y openssl openssl-devel

3、安装Nginx
① 上传 nginx包到 /softpackage/下

② 解压

# cd /softpackage
# tar -zxvf nginx-1.8.0.tar.gz

③ 创建临时目录/var/temp/nginx

mkdir /var/temp/nginx -p

④ 添加nginx配置

# cd nginx-1.8.0/

将以下内容粘贴到命令行执行

./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

⑤ 编译、安装

# make
# make install

⑥ 修改 nginx.conf 文件

# cd /usr/local/nginx/conf
# vim nginx.conf

添加如下配置:

server {
  listen        88;
   server_name   file.yunxin.com;
   location /taofut/M00 {
        ngx_fastdfs_module;
   }
}

4、配置fastdfs-nginx-module
① 上传 fastdfs-nginx-module_v1.16.tar.gz包到 /softpackage(也可以wget + 地址 直接网上下载)

② 解压

# cd /softpackage
# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

③ 修改config文件

# cd fastdfs-nginx-module/src
# vim config

将以下三个路径里的local去掉

/usr/local/include/fastdfs
/usr/local/include/fastcommon/
/usr/local/lib -lfastcommon -lfdfsclient
修改成:
/usr/include/fastdfs
/usr/include/fastcommon/
/usr/lib -lfastcommon -lfdfsclient

④ 复制并修改 mod_fastdfs.conf 文件

# cd /softpackage/fastdfs-nginx-module/src
# cp mod_fastdfs.conf /etc/fdfs/
# cd /etc/fdfs
# vim mod_fastdfs.conf

修改以下内容,其余默认

base_path=/fastdfs/tmp
tracker_server=file.yunxin.com:22122
group_name=taofut
url_have_group_name = true
store_path0=/fastdfs/storage

创建base_path指定的目录

# cd /fastdfs
# mkdir tmp

⑤ 添加模块

  • 先进入解压包目录

# cd /softpackage/nginx-1.8.0

  • 添加模块

# ./configure --add-module=/softpackage/fastdfs-nginx-module/src

⑥ 重新编译、安装

# make
# make install

⑦ 测试Nginx

  • 进入到nginx安装目录

# cd /usr/local/nginx/sbin
# ./nginx -t

  • 测试成功返回结果

ngx_http_fastdfs_set pid=10804 ngx_http_fastdfs_set pid=10804 nginx:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is
successful

⑧ 启动Nginx服务

# ./nginx

⑨ 重启Tracker、Storage、Nginx服务

# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# cd /usr/local/nginx/sbin/
# ./nginx -s reload

⑩ 访问我之前上传的文件
输入访问地址:http://file.yunxin.com:88/taofut/M00/00/00/wKg_ilwrFMWAdsQ3AAC0c0KxqFI189.png
访问成功!

猜你喜欢

转载自blog.csdn.net/fu123123fu/article/details/85415391