文章目录

  • FastDFS单机环境安装与配置
  • 安装并配置Nginx使FastDFS可通过HTTP协议访问


FastDFS单机环境安装与配置

系统环境:UbuntuLiveServer-21.04-arm64

安装步骤:

  1. 从华为云开源镜像站下载UbuntuLiveServer-21.04-arm64.iso,用于搭建服务器环境
    地址:https://repo.huaweicloud.com/ubuntu-releases/21.04/ubuntu-21.04-live-server-amd64.iso
  2. 使用VMware 15.5 WorkStation Pro 安装UbuntuLiveServer,一切按默认方式安装即可,但网络适配器要改为桥接模式
  3. 等待安装完成后,更新系统软件
  1. 将apt下载源换为阿里源
cd /etc/apt
# 备份初始文件
sudo cp ./sources.list ./sources.list_backup
sudo vim sources.list

向文件中写入如下内容:

# 阿里源

deb http://mirrors.aliyun.com/ubuntu/ hirsute main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-backports main restricted universe multiverse

  1. 更新系统软件
sudo apt update
sudo apt upgrade
  1. 安装gcc、g++与make
    FastDFS基于C/C++开发,所以部署时需要安装C/C++的编译环境
# build-essential中包含了gcc、g++和make
sudo apt install build-essential
  1. 去Github或Gitee下载FastDFS的源码
    首先要切换到root用户,并进入/usr/local/src/目录下
su
cd /usr/local/src

之后去GitHub或者Gitee下载源码(国内建议使用Gitee下载)

  • Github:
# 依赖文件
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
# nginx模块
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -SO fastdfs-nginx-module.tar.gz

下载好之后使用tar命令解压到当前目录即可

  • Gitee:
    注意这里需要手动把压缩包名字改下,因为gitee打包后的程序都是以master.zip命名的
# 依赖文件
wget https://gitee.com/fastdfs100/fastdfs-nginx-module/repository/archive/master.zip
mv master.zip fastdfs-nginx-module.zip
# 主程序包
wget https://gitee.com/fastdfs100/fastdfs/repository/archive/master.zip
mv master.zip fastdfs.zip
# nginx模块
wget https://gitee.com/fastdfs100/libfastcommon/repository/archive/master.zip
mv master.zip libfastcommon.zip

下载好之后使用unzip命令解压到当前目录即可

  1. 安装与配置
  1. 安装FastDFS依赖文件
cd libfastcommon-master
./make.sh
./make.sh install
  1. 安装FastDFS主程序
cd ../fastdfs-master
./make.sh
./make.sh install

安装好后,程序是在/usr/bin目录下,而配置文件是在/etc/fdfs目录下

  1. 配置FastDFS
    将官方提供的配置模板文件复制到/etc/fdfs目录下
cp /usr/local/src/fastdfs-master/conf/* /etc/fdfs

修改配置文件:

  • tracker.conf:
# the tracker server port
port=22122

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

# HTTP port on this tracker server
http.server_port=9270
  • storage.conf:
# storage所属的组
group_name=group1

# the storage server port
port=23000

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

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

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

# the port of the web server on this storage server
http.server_port=8888
  • 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.168.1.6:22122
#HTTP settings
http.tracker_server_port=9270
  1. 创建文件夹,否则FastDFS启动会报错说找不到文件夹
mkdir /var/fdfs/client
mkdir /var/fdfs/logs
mkdir /var/fdfs/data
  1. 测试上传
    使用Xftp或者lrzsz的rz命令随便上传一个文件到/home/user目录下,之后运行:
fdfs_test /etc/fdfs/client.conf upload /home/user/background.jpeg

运行时候出现以下内容表示上传成功:

group_name=group1, remote_filename=M00/00/00/wKgBBmEqQJWAZo9NAAExqgEgsTU13.jpeg
source ip address: 192.168.1.6
file timestamp=2021-08-28 13:56:37
file size=78250
file crc32=18919733
example file url: http://192.168.1.6:9270/group1/M00/00/00/wKgBBmEqQJWAZo9NAAExqgEgsTU13.jpeg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgBBmEqQJWAZo9NAAExqgEgsTU13_big.jpeg
source ip address: 192.168.1.6
file timestamp=2021-08-28 13:56:37
file size=78250
file crc32=18919733
example file url: http://192.168.1.6:9270/group1/M00/00/00/wKgBBmEqQJWAZo9NAAExqgEgsTU13_big.jpeg

至此FastDFS主程序就安装完毕了

安装并配置Nginx使FastDFS可通过HTTP协议访问

  1. 安装Nginx
sudo apt install nginx-core
nginx -V
  1. 安装Nginx所需的依赖
sudo apt-get install libxml2 libxml2-dev libxslt-dev
sudo apt-get  install openssl
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install libgeoip-dev
sudo apt-get install libgd-dev
  1. 下载Nginx源文件,地址:http://nginx.org/download/nginx-1.18.0.tar.gz
cd /home/user/
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 nginx-src
  1. 在nginx-src目录下创建一个custom_config.sh文件,用于配置Nginx
vim custom_config.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=/usr/local/src/fastdfs-nginx-module-master/src

之后为这个文件加上可执行权限并执行

chmod +x custom_config.sh
./custom_config.sh

出现以下内容表示配置成功:

Configuration summary
  + using threads
  + using system PCRE library
  + using OpenSSL library: /usr/bin/openssl
  + 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"
  1. 备份nginx的可执行文件和配置文件
sudo cp /usr/sbin/nginx /usr/sbin/nginx_org_bak
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
  1. 编译并安装Nginx
cd /home/user/nginx-src
sudo make
sudo make install
  1. 关闭系统原有的nginx,并备份好系统原来的nginx文件
sudo nginx -s stop
cd /usr/sbin
sudo cp /usr/sbin/nginx /usr/sbin/nginx_bak
  1. 备份好后创建一个软链接,放在/usr/sbin目录下
# 检查自己编译出来的nginx能否运行
/usr/share/nginx/sbin/nginx -v

#创建软链接
sudo ln -s /usr/share/nginx/sbin/nginx /usr/sbin/nginx
  1. 配置Nginx
    在/etc/fdfs目录下创建mod_fastdfs.conf文件,写入如下内容:
connect_timeout=2
network_timeout=30
base_path=/tmp
load_fdfs_parameters_from_tracker=true
storage_sync_file_max_delay = 86400
use_storage_id = false
storage_ids_filename = storage_ids.conf
tracker_server=192.168.1.6:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/var/fdfs
log_level=info
log_filename=
response_mode=proxy
if_alias_prefix=
flv_support = true
flv_extension = flv
group_count = 0

修改/etc/nginx/nginx.conf文件,添加如下内容:

location ~ /group[1-9]/M0[0-9] {
       ngx_fastdfs_module;
}

执行命令:

nginx -c /etc/nginx/nginx.conf -t

出现以下内容表示nginx配置成功:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

在浏览器中通过url访问之前测试上传的图片:

http://192.168.1.6:9270/group1/M00/00/00/wKgBBmEqQJWAZo9NAAExqgEgsTU13.jpeg

发现图片可正常显示,自此Nginx+FastDFS环境搭建完成。