Ubuntu16.04 安装fastDFS
1、环境准备
# 更新软件库
sudo apt update
# 安装gcc环境
sudo apt install gcc
# 编译安装需要make工具
sudo apt install make
# fastDFS依赖库
sudo apt install libevent-dev
2、下载并安装FastDFS 5.x版本
1、为了方便管理,创建一个文件夹专门放所需软件
sudo mkdir /usr/local/fastdfs
2、下载安装包(fastdfs源码包、libfastcommon源码包)
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
3、创建安装目录(-p是遍历创建目录)
sudo mkdir -p /usr/local/fastdfs/{storage,tracker,client}
4、安装libfastcommon库(到指定文件夹下)
sudo tar -xzvf V1.0.7.tar.gz -C /usr/local/fastdfs
cd /usr/local/fastdfs/libfastcommon-1.0.7
sudo ./make.sh
sudo ./make.sh install
注意:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,所以可以设置软链接
sudo ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
sudo ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
sudo ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
sudo ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
5、安装FastDFS
sudo tar -zxvf V5.05.tar.gz
cd usr/local/fastdfs/fastdfs-5.05
sudo ./make.sh
sudo ./make.sh install
/usr/bin 存放有编译出来的文件
/etc/fdfs 存放有配置文件
将配置文件复制一份用于编辑
sudo cp storage.conf.sample storage.conf
sudo cp tracker.conf.sample tracker.conf
sudo cp client.conf.sample client.conf
6、配置tracker
sudo vim /etc/fdfs/tracker.conf
# 修改,修改前确保文件夹已存在
base_path=/home/durian/fastdfs/tracker
7、启动tracker
# 启动
sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
# 停止
sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
# 重启
sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# 查看tracker进程
ps -aux | grep tracker
8、配置storage
sudo vim /etc/fdfs/storage.conf
# 修改,修改前确保文件夹已存在
base_path=/home/durian/fastdfs/storage
group_name=group1
# tracker服务地址,有多个tracker server则配置多个
tracker_server=192.168.113.131:22122
# 图片实际存放路径,如果有多个挂载,可以有配置多行
store_path0=/usr/local/fastdfs/storage/images0
store_path1=/usr/local/fastdfs/storage/images1
store_path_count=2
9、启动storage
# 启动
sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
# 停止
sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
# 重启
sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# 查看tracker进程
ps -aux | grep storage
10、配置client
sudo vim /etc/fdfs/client.conf
# 修改,修改前确保文件夹已存在
base_path=/home/durian/fastdfs/client
# 指定tracker server地址,有多个则配置多行
tracker_server=192.168.113.131:22122
11、使用client测试
sudo /usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/durian/test.jpg
# 出现如下信息表示图片上传成功
group_name=group1, remote_filename=M00/00/00/wKgchVqh9U6AClDVAACFzpgzGxg08920.h
source ip address: 192.168.113.131
file timestamp=2018-03-09 10:45:34
file size=34254
file crc32=2553486104
example file url: http://192.168.113.131/group1/M00/00/00/wKgchVqh9U6AClDACFzpgz08920.jpg
storage_upload_slave_by_filename
但是此时使用example file url不能访问到图片,需要搭配nginx。
安装Nginx和FastDFS模块
单独安装nginx代理服务,他的作用是代理访问storage上的文件,实现负载均衡。
1、下载Nginx的FastDFS模块
wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
2、解压Nginx的FastDFS模块带目录 /usr/local/fastdfs/fastdfs-nginx-module
sudo tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fastdfs/fastdfs-nginx-module
3、安装nginx并添加fastDFS模块
# 预设几个文件夹,方便等下安装的时候有些文件可以进行存放:
sudo mkdir -p /usr/local/nginx /var/log/nginx /var/temp/nginx /var/lock/nginx
cd /usr/local/nginx
./configure
--prefix=/usr/local/nginx # 指定安装目录
--pid-path=/var/local/nginx/nginx.pid
--lock-path=/var/lock/nginx/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 # 添加fastDFS模块
# 编译安装
sudo make && sudo make install
4、配置FastDFS模块
4-1、复制**fastdfs-nignx**模块的配置文件 mod_fastdfs.conf 到 /etc/fdfs/
cd /usr/local/fastdfs/fastdfs-nginx-module/src
sudo cp mod_fastdfs.conf /etc/fdfs/
4-2、 复制**fastdfs**中的http.conf、mime.types文件到/etc/fdfs
cd /usr/local/fastdfs/fastdfs-5.05
sudo cp http.conf mime.types /etc/fdfs
4-3、修改mod_fastdfs.conf
sudo vim /etc/fdfs/mod_fastdfs.conf
# 日志记录路径
base_path=/home/durian/fastdfs/nginx-fast-module
# tracker server 地址
tracker_server=192.168.113.131:22122
# http带group名字
url_have_group_name = true
# 和storage.conf 一样
store_path0=/home/durian/fastdfs/storage/images0
store_path1=/home/durian/fastdfs/storage/images1
# 参数值必须与store_path数量一致
store_path_count=2
4-4、编辑 Nginx 模块的配置文件
sudo vim /usr/local/fastdfs/fastdfs-nginx-module/src/config
# 修改该行
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/local/include/fastcommon/
4-5、 修改storage.conf
#端口号必须和nginx的端口好一样
http.server_port=80
4-6、编辑nginx.conf
vim /usr/local/nginx/conf/nginx.conf
# 拦截包含 /group1/M00 请求,使用 fastdfs 这个 Nginx 模块进行转发
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
4-7、启动nginx
sudo /usr/local/nginx/sbin/nginx