1.fastdfs 公司资源使用
部署在公司服务器 123.456.789.116 [单机部署]
占用端口 tracker 22122 storage 23000
需要使用时配置文件如下 【116服务器 22122 端口未开通外网访问 本地无法连接 部署到 116服务器服务可以连接】
fsstore:
url: http://abc.def:1180/group1/
bucket: group1
fdfs:
connect-timeout: 601
so-timeout: 1501
tracker-list:
- 123.456.789.116:22122
#防火墙需要关闭
systemctl stop firewalld.service #关闭
#编译环境
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
2.环境准备
基于官方给的wiki进行安装,但是踩了不少的坑。根据部署过程遇到的问题以及解决方案,整合官方wiki修改后加入更详细的内容。
官方wiki地址 https://github.com/happyfish100/fastdfs/wiki/安装步骤
环境准备
名称 | 说明 |
---|---|
服务器 | centos 7.x 【本次安装 tracker storage nginx 都在 123.456.789.116】 |
nginx | nginx 1.16.0 【可以不用提前安装】 |
libfastcommon | FastDFS分离出的一些公用函数包 |
FastDFS | FastDFS本体 |
fastdfs-nginx-module | FastDFS和nginx的关联模块 |
安装包 | 位置 /usr/local/src |
数据存储位置 | 位置 /home/dfs/ |
3.安装步骤
1.创建及进入目录
#创建数据存储目录
mkdir /home/dfs
#切换到安装目录准备下载安装包
cd /usr/local/src
2.安装libfastcommon
git clone https://github.com/happyfish100/libfastcommon.git --depth 1
cd libfastcommon/
#编译安装
./make.sh && ./make.sh install
3.安装FastDFS
#返回上一级目录
cd ../
git clone https://github.com/happyfish100/fastdfs.git --depth 1
cd fastdfs/
#编译安装
./make.sh && ./make.sh install
#配置文件准备
#供nginx访问使用
cp /usr/local/src/fastdfs/conf/http.conf /etc/fdfs/
#供nginx访问使用
cp /usr/local/src/fastdfs/conf/mime.types /etc/fdfs/
4.安装fastdfs-nginx-module
cd ../ #返回上一级目录
git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
5.加入fastdfs-nginx-module 模块
5.1 已安装nginx【116已经提前安装过nginx】
因为已经安装过nginx 所以需要重新编译加入fastdfs-nginx-module 模块。
- 备份nginx,备份nginx防止配置错误以备还原
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak_20220413
- 查看nginx的版本号以及加载模块的情况
cd /usr/local/nginx/sbin
#configure arguments 模块信息后面要用
./nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx
- 进入到原始编译目录
cd /usr/local/nginx-1.16.0
- 配置追加 【–prefix=/usr/local/nginx 就是前面的 configure arguments 模块,后面的 -add-module 添加fastdfs模块】
./configure \
--prefix=/usr/local/nginx \
--add-module=/usr/local/src/fastdfs-nginx-module/src
-
编译
make
-
将新生成的nginx 拷贝到运行目录
cp rfp objs/nginx /usr/local/nginx/sbin/nginx
踩坑开始
一开始使用的是 cp objs/nginx /usr/local/nginx/sbin/nginx 然后报错 cp:cannot create regular file `/usr/local/nginx/sbin/nginx': Text file busy
- 检查下nginx是否正常
cd /usr/local/nginx/sbin/nginx ./nginx -t
-
5.2未安装nginx
-
安装nginx
wget http://nginx.org/download/nginx-1.16.0.tar.gz #下载nginx压缩包 tar -zxvf nginx-1.16.0.tar.gz #解压 cd nginx-1.16.0/ #添加fastdfs-nginx-module模块 ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/ make && make install #编译安装
6.配置修改
tracker配置
vim /etc/fdfs/tracker.conf
base_path=/home/dfs # 存储日志和数据的根目录
storage 配置
vim /etc/fdfs/storage.conf
base_path=/home/dfs # 数据和日志文件存储根目录
store_path0=/home/dfs # 第一个存储目录
tracker_server=123.456.789.116:22122 # tracker服务器IP和端口
http.server_port=18080 # http访问文件的端口(和nginx中对外的端口保持一致 116 是 18080)
mod_fastdfs.conf 修改
vim /etc/fdfs/mod_fastdfs.conf
url_have_group_name = true
# 后面url访问发现404 回来查看配置才发现改成的是trues 所以配置文件修改细节要注意 坑往往是自己埋的
# 当时的报错nginx日志如下
ERROR - file: ../common/fdfs_global.c, line: 52, the format of filename "group1/M00/00/00/wKi1hF1mMACAJTDkAAHHR5CQLhc364.png" is invalid
store_path0=/home/dfs
tracker_server=123.456.789.116:22122
client 修改
vim /etc/fdfs/client.conf
#需要修改的内容如下
base_path=/home/dfs
tracker_server=123.456.789.116:22122 #tracker服务器IP和端口
http.tracker_server_port = 18080 # http访问文件的端口(和nginx中对外的端口保持一致 116 是 18080)
7.nginx 配置
123.456.789.116 和123.456.789.115均有配置 115做外面访问转发,也是域名那台服务器
#123.456.789.115
location /group1/ {
proxy_pass http://123.456.789.116:18080/group1/;
}
# 123.456.789.116
location /group1/M00 {
root /home/dfs;
ngx_fastdfs_module;
}
需要重启 nginx,而非 reload 又踩坑了 访问图片的时候404 nginx log 报错如下
[emerg] 67870#0: unknown directive "ngx_fastdfs_module" in /usr/local/nginx/conf/nginx.conf:70
#重启nginx
systemctl restart nginx
#然而。。。。。 新的错误出现了
Failed to reload nginx.service: Unit not found
#是因为nginx没有有添加到系统服务
vim /etc/init.d/nginx
# 将下面内容复制到里面 保存退出
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
cd /etc/init.d
chmod 755 /etc/init.d/nginx
chkconfig –-add nginx
#先停止后启动
service nginx stop
service nginx start
8.启动服务并测试上传
cd /usr/bin
#启动
fdfs_trackerd /etc/fdfs/tracker.conf
fdfs_storaged /etc/fdfs/storage.conf
#检查服务是否启动
netstat -apn|grep 22122
netstat -apn|grep 23000
#测试上传
fdfs_test /etc/fdfs/client.conf upload /xxx.jpg
#会返回 exam file url 放到浏览器看是否能访问 如果404 或400 查看nginx log 分析问题
# eg. http://123.456.789:18080/group1/M00/00/00/CgoKdGJWk6-ARfmNAAGWQd5yXU0272.jpg
确认没问题后修改项目对应配置,测试项目上传是否正常。