Ubuntu20.0安装FastDFS

环境基础:
https://blog.csdn.net/qq_37596010/article/details/114408135

安装FastDFS是根据:
FastDFS入门一篇就够
本文根据这篇文章写的

1、安装GCC编译器:

sudo apt install build-essential
参考文章:如何在Ubuntu 18.04上安装GCC编译器
#apt install lrzsz 这个是基于finalShell 上传文件要的

2、

下载的包都存放在~/caibh/fdfs-package目录下。

下载libfastcommon、fastdfs、fastdfs-nginx-module:

wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -SO libfastcommon.tar.gz
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -SO fastdfs.tar.gz
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -SO fastdfs-nginx-module.tar.gz

解压
tar -xf xxx.tar.gz

安装libfastcommon

cd ~/caibh/fdfs-package
cd libfastcommon-1.0.39
./make.sh
./make.sh install

安装 fastdfs

cd ~/caibh/fdfs-package
cd fastdfs-5.11
./make.sh
./make.sh install

安装好后,程序是在/usr/bin目录下:

$ which fdfs_trackerd
/usr/bin/fdfs_trackerd

而配置文件是在/etc/fdfs目录下:

$ cd ~/caibh/fdfs-package/fastdfs-5.11
$ ls /etc/fdfs
client.conf.sample storage_ids.conf.sample  tracker.conf.sample storage.conf.sample

但是这些配置文件是不全的,而且都是模板,所以需要从fastdfs包中拷贝过来,并修改配置:

$ cd ~/caibh/fdfs-package/fastdfs-5.11/conf
$ ls
anti-steal.jpg  client.conf  http.conf  mime.types  storage.conf  storage_ids.conf  tracker.conf
$ sudo cp ~/caibh/fdfs-package/fastdfs-5.11/conf/* /etc/fdfs

修改配置

sudo vi /etc/fdfs/tracker.conf:

# the tracker server port
port=22122

# the base path to store data and log files
base_path=/home/caibh/fdfs

# HTTP port on this tracker server
http.server_port=9270

sudo vi /etc/fdfs/storage.conf:

# storage所属的组
group_name=group1

# the storage server port
port=23000

# the base path to store data and log files
base_path=/home/caibh/fdfs

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/home/caibh/fdfs
#store_path1=/home/caibh/fdfs2

# tracker服务器,虽然是同一台机器上,但是不能写127.0.0.1。这项配置可以出现一次或多次
tracker_server=191.8.1.77:22122

# the port of the web server on this storage server
http.server_port=8888
sudo vi /etc/fdfs/client.conf:

# the base path to store log files
base_path=/home/caibh/fdfs/client
# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=191.8.1.77:22122
#HTTP settings
http.tracker_server_port=9270

sudo vi /etc/fdfs/client.conf:

# the base path to store log files
base_path=/home/caibh/fdfs/client
# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=191.8.1.77:22122
#HTTP settings
http.tracker_server_port=9270

sudo vi /etc/fdfs/mod_fastdfs.conf:

# the base path to store log files
base_path=/tmp

# FastDFS tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=191.8.1.77:22122

# the port of the local storage server
# the default value is 23000
storage_server_port=23000

# the group name of the local storage server
group_name=group1

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/home/caibh/fdfs
#store_path1=/home/yuqing/fastdfs1

配置过程中有几点要注意:

  • 确保配置中用到的目录已经创建了。比如/fdfs/client、/fdfs/data、~/fdfs/logs
    确保各种配置文件之间引用的端口一直。比如:

    mod_fastdfs.conf文件中tracker_server的端口应该跟tracker.conf中port一致;
    mod_fastdfs.conf文件中storage_server_port的端口应该跟跟storage.conf中port一致;
    其他配置或文件虽然不用修改,但是fastdfs-nginx-module模块会用到:
    anti-steal.jpg
    http.conf
    mime.types

启动tracker和storage:

# 启动
fdfs_trackerd /etc/fdfs/tracker.conf start
fdfs_storaged /etc/fdfs/storage.conf start
# 查看日志
tail -n10 ~/fdfs/logs/trackerd.log
tail -n10 ~/fdfs/logs/storaged.log
# 如果日志显示有错误信息,需要根据信息来查找错误原因

用fdfs_test测试上传

上传图片到文件夹下

cd ~/caibh/test_images/
rz 

$ fdfs_test /etc/fdfs/client.conf upload ~/caibh/test_images/XinXiJuZhiWang.jpg

安装Nginx

nginx -V
sudo apt install nginx-core
nginx -V

下载Nginx源文件地址:http://nginx.org/download/

cd ~/caibh/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xf nginx-1.18.0.tar.gz
mv nginx-1.18.0 nginx-src
cd ~/caibh/nginx-src
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

为了方便编辑,复制上面信息,在nginx-src目录下创建一个custom_configure.sh文件,把复制的内容拷贝进去,并在最后加上fastdfs-nginx-module的源码:

$ cd ~/caibh/nginx-src
$ cat custom_configure.sh 
./configure \
--with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' \
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' \
--prefix=/usr/share/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-debug \
--with-pcre-jit \
--with-ipv6 \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_addition_module \
--with-http_dav_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module \
--with-http_v2_module \
--with-http_sub_module \
--with-http_xslt_module \
--with-stream \
--with-stream_ssl_module \
--with-mail \
--with-mail_ssl_module \
--with-threads \
--add-module=~/caibh/fdfs-package/fastdfs-nginx-module-1.20/src
chmod +x custom_configure.sh 

执行 ./custom_configure.sh

./custom_configure.sh 



./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module*******

报错了 ./configure: error: the HTTP rewrite module requires the PCRE library. 我执行了下面三条命令

sudo apt-get install libxml2 libxml2-dev libxslt-dev
sudo apt-get  install openssl
sudo apt-get install libpcre3 libpcre3-dev
./custom_configure.sh 

./configure: error: the HTTP rewrite module requires the PCRE library.错误消失
但是又出现

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module******

运行下面代码

apt-install libgeoip-dev
apt-get install libgd-dev
./custom_configure.sh 

运行结果如下

Configuration summary
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/share/nginx"
  nginx binary file: "/usr/share/nginx/sbin/nginx"
  nginx modules path: "/usr/share/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/lib/nginx/body"
  nginx http proxy temporary files: "/var/lib/nginx/proxy"
  nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi"
  nginx http uwsgi temporary files: "/var/lib/nginx/uwsgi"
  nginx http scgi temporary files: "/var/lib/nginx/scgi"

通过上面这些信息可以知道编译出来的nginx会装到哪里,配置文件会放在哪里,错误日志会放在哪里。
停止nginx,并备份原来的nginx执行文件,和配置文件:

$ which nginx
/usr/sbin/nginx
$ sudo cp /usr/sbin/nginx /usr/sbin/nginx_org_bak
$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
继续在nginx-src目录执行make和make install:
$ cd ~/caibh/nginx-src
$ sudo make

# 报错
Fatal error: can't create objs/addon/src/ngx_http_fastdfs_module.o: Permission denied
In file included from /home/xxxx/caibh/fdfs-package/fastdfs-nginx-module-1.20/src/common.c:26:0,
                 from /home/xxxx/caibh/fdfs-package/fastdfs-nginx-module-1.20/src/ngx_http_fastdfs_module.c:6:
/usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No such file or directory

# 解决
# 修改fastdfs-nginx-module的src/config文件
$ cd /home/xxxx/caibh/fdfs-package/fastdfs-nginx-module-1.20/src
$ sudo vi config
# 修改一下两项:
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

# 重新运行 nginx-src/custom_configure.sh
$ pwd
/home/xxxx/caibh/nginx-src
$ sudo ./custom_configure.sh
$ sudo make
$ sudo make install
# make install 会在/usr/share/nginx/sbin目录下生成一个名为nginx的可执行文件
$ sudo ls /usr/share/nginx
html  sbin

关闭系统原有的nginx,并备份好系统原来的nginx文件:

sudo nginx -s stop
cd /usr/sbin
sudo cp mv /usr/sbin/nginx /usr/sbin/nginx_bak

查看Nginx端口情况()

ps aux|grep nginx

在这里插入图片描述
备份好后创建一个软链接,放在/usr/sbin目录下:

# 检查自己编译出来的nginx能否运行
$ ./nginx -v
nginx version: nginx/1.10.3
#创建软链接
$ sudo ln -s /usr/share/nginx/sbin/nginx /usr/sbin/nginx

配置nginx,加入路由规则,映射到fastdfs的数据目录:

# config for fastdfs-nginx-module
server {
    listen 8777;
    location /M00 {
        root ~/fdfs/data;
        ngx_fastdfs_module;
    }
}

开放端口:

sudo iptables -A IN_public_allow -p tcp --dport 8777 -j ACCEPT
sudo iptables -L -n

测试上传图片:

$ fdfs_test /etc/fdfs/client.conf upload ~/caibh/test_images/XinXiJuZhiWang.jpg
This is FastDFS client test program v5.11

Copyright (C) 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-02-22 14:54:33] DEBUG - base_path=/home/xxxx/fdfs/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=191.8.2.203, port=23000

group_name=group1, ip_addr=191.8.2.203, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/vwgCy1xvnKmAIDXDAAEM3XOobI4945.jpg
source ip address: 191.8.2.203
file timestamp=2019-02-22 14:54:33
file size=68829
file crc32=1940417678
example file url: http://191.8.2.203:9270/group1/M00/00/00/vwgCy1xvnKmAIDXDAAEM3XOobI4945.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/vwgCy1xvnKmAIDXDAAEM3XOobI4945_big.jpg
source ip address: 191.8.2.203
file timestamp=2019-02-22 14:54:33
file size=68829
file crc32=1940417678
example file url: http://191.8.2.203:9270/group1/M00/00/00/vwgCy1xvnKmAIDXDAAEM3XOobI4945_big.jpg

访问:

http://191.8.2.203:8777/M00/00/00/vwgCy1xvnKmAIDXDAAEM3XOobI4945_big.jpg
# 如果能看到图片则表示安装fastdfs-nginx-module成功

出现其他错误

1、module “ngx_http_image_filter_module” is already loaded
 nginx: [emerg] module "ngx_http_image_filter_module" is already loaded in /etc/nginx/modules-enabled/50-mod-http-image-filter.conf:1

上面错误修改 /etc/nginx/nginx.conf
把第四行

include /etc/nginx/modules-enabled/*.conf;

改成

include /usr/share/nginx/modules*.so;

2、open()"/usr/share/nginx/html/group1

open()"/usr/share/nginx/html/group1/M00/00/00/wKglgWBFy2iAaKDoAABRgz9wftg966_big.jpg" 

原因:具体原因不明,可能是在做alias匹配的时候路径出现的问题
修复方案1:修改nginx.conf中的location配置为:
location/group1/M00/ {
alias /home/FastDFS/data;
ngx_fastdfs_module;
}
修复方案2:
在server中添加
location ~/group[0-9]/ {
ngx_fastdfs_module;
}

3、ERROR - file:…/common/fdfs_global.c,

ERROR - file: ../common/fdfs_global.c, line: 52, the format of filename "group1/M00/00/00/wKglgWBGE1CAaQ4qAABRgz9wftg259_big.jpg" is invalid

原因:nginx的fdfs的MOD里面默认没有开启url_have_group_name
修复:开启mod_fastdfs.conf文件中的url_have_group_name选项,后重启nginx

附加

其他问题可参考:
FastDFS+Nginx问题及修复

安装了一天的FastDFS头皮发麻:

最后感谢:
https://segmentfault.com/u/caibaohong
这篇文章的作者

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值