参考 https://blog.csdn.net/github_38924695/article/details/89892401
一、准备工作
最近项目中需要用到文件服务,于是就在linux中搭建一个单机的文件服务
安装fastDFS需要分别安装fastdfs-nginx-module,fastdfs,nginx,libfastcommon。我安装的fastDFS版本是v5.11
安装包链接:https://pan.baidu.com/s/1VIViB-CCtvgTNPSawdPa1g
提取码:1qsu
二、安装步骤
将所有的安装包上传到服务器 我的在/aifs01/test
由于后续需要编译,所以需要安装gcc,具体安装参考gcc安装方法
https://blog.csdn.net/a1035082174/article/details/88862806
2.1安装fastdfs首先需要安装libfastcommon
进入安装包存放目录 /aifs01/test
cd /aifs01/test
解压文件:
tar -xvf libfastcommon-1.0.35.tar.gz
进入解压目录:
cd libfastcommon-1.0.35/
执行编译:
./make.sh
安装:
./make.sh install
2.2安装fastDFS
解压文件:
tar -xvf V5.11.tar.gz
进入解压后的目录:cd fastdfs-5.11/
执行编译:
./make.sh
安装:
./make.sh install
安装成功后查看所有可执行的fast命令:
ls -la /usr/bin/fdfs*
2.3配置tracker服务
进入/aifs01/test目录,新建文件tracker用于存放图片以及日志:
cd /aifs01/test
mkdir tracker
cd /etc/fdfs
进入/etc/fdfs目录进行配置,其中有client.conf.sample(配置客户端的配置文件),storage.conf.sample(storage配置文件),tracker.conf.sample(tracker的配置文件)。
拷贝tracker.conf.sample到tracker.conf:
cp tracker.conf.sample tracker.conf
编辑tracker.conf修改相关参数
vim tracker.conf
base_path=/aifs01/test/tracker #tracker存储data和log的跟路径,必须提前创建好
port=22122 #tracker默认22122
http.server_port=8089 #http端口,需要和nginx相同
启动tracker(支持start|stop|restart):
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
到/aifs01/test/tracker/logs中查看trackerd.log,查看日志:
查看端口情况:
netstat -apn|grep fdfs
可能遇到的报错:
/usr/bin/fdfs_trackerd: error while loading shared libraries: libfastcommon.so: cannot open shared object file: No such file or directory
解决方案:建立libfastcommon.so软链接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
2.4配置Storage服务
在/aifs01/test建一个storage文件夹
进入/etc/fdfs,拷贝storage.conf.sample到storage.conf
cd /etc/fdfs
cp storage.conf.sample storage.conf
编辑storage.conf修改配置内容如下:
base_path=/aifs01/test/storage #storage存储data和log的跟路径,必须提前创建好
port=23000 #storge默认23000,同一个组的storage端口号必须一致
group_name=group1 #默认组名,根据实际情况修改
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/aifs01/test/storage #如果为空,则使用base_path
tracker_server=所在服务器的ip:22122 #配置该storage监听的tracker的ip和port
启动storage(支持start|stop|restart):
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
进入/aifs01/test/storage/logs中查看storaged.log日志:
tail -200f storaged.log
查看端口情况:
netstat -apn|grep fdfs
注意:需开放22122.23000端口
通过monitor来查看storage是否成功绑定:
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
2.5安装nginx及fastdfs-nginx-module
进入/aifs01/test中,解压nginx及fastdfs-nginx-module:
tar -xvf nginx-1.15.2.tar.gz
tar -xvf V1.20.tar.gz
进入fastdfs-nginx-module中修改配置文件:
cd /aifs01/test/fastdfs-nginx-module-1.20/src
vim config
修改内容:
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
C
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
2.6安装nginx需要安装pcre、zlib、openssl依赖
yum install pcre-devel -y
yum install zlib-devel -y
yum install openssl-devel -y
2.7配置client
cd /etc/fdfs
cp client.conf.sample client.conf
vim client.conf
修改如下内容
base_path=/aifs01/test/tracker //tracker服务器文件路径
tracker_server=所在服务器的ip:22122 //tracker服务器IP地址和端口号
http.tracker_server_port=8089 # tracker服务器的http端口号,必须和tracker的设置对应起来
进入解压后的nginx文件夹:
cd /aifs01/test/nginx-1.15.2
配置并加载fastdfs-nginx-module模块:
注意:
/aifs01/test/nginx为nginx的安装目录
/aifs01/test/fastdfs-nginx-module-1.20/src为fastdfs-nginx-module解压后的目录
编译Nginx
./configure --prefix=/aifs01/test/nginx --add-module=/aifs01/test/fastdfs-nginx-module-1.20/src/
编译:
make
安装:
make install
2.8 配置nginx:
进入nginx的安装目录进行配置修改:
cd /aifs01/test/nginx/conf
vim nginx.conf
修改端口及location:
location ~/M00{
root /aifs01/test/storage/data;#配置的图片存放的地址
ngx_fastdfs_module;
}
如下:
拷贝文件:
cd /aifs01/test/fastdfs-5.11/conf
cp mime.types http.conf /etc/fdfs/
2.9配置mod_fastdfs.conf:
进入/etc/fdfs/中编辑mod_fastdfs.conf文件(如果没有从/aifs01/test/fastdfs-nginx-module-1.20/src中复制)
base_path=/aifs01/test
tracker_server=所在服务器ip:22122 #tracker的地址
url_have_group_name=true #url是否包含group名称
storage_server_port=23000 #需要和storage配置的相同
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/aifs01/test/storage #文件存储的位置
进入nginx的目录启动nginx:
cd /aifs01/test/nginx/sbin/
./nginx
测试上传文件:
通过在/aifs01中放一个1.jpg文件,进入/aifs01
上传:
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf 1.jpg
访问图片:
http://nginxip:8089/group1/M00/00/00/ChWrWV4CzdKAUlc8AADsrFGbk7A053.jpg
如果可以成功则说明fast安装成功。
删除文件:
/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/ChWrWV4CzdKAUlc8AADsrFGbk7A053.jpg
关闭fast:
killall fdfs_trackerd
killall fdfs_storaged
设置开机自启动:
vim /etc/rc.d/rc.local
在其中加入以下代码:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
修改文件权限(centos7中):
chmod +x /etc/rc.d/rc.local