回到家目录
cd ~
新建packages目录(用于存放 下载的包)
mkdir packages
下载libfastcommon、fastdfs、fastdfs-nginx-module
:
# -S print server response
# -O write documents to file
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 ~/packages/libfastcommon-1.0.39
./make.sh
./make.sh install
# 最好本机执行make。远端ssh 执行,可能会报错
安装 fastdfs
cd ~/packages/fastdfs-5.11
./make.sh
./make.sh install
安装好后,程序是在/usr/bin目录下:
$ which fdfs_trackerd
/usr/bin/fdfs_trackerd
而配置文件是在/etc/fdfs目录下:
# 但是这些配置文件是不全的,而且都是模板
$ ls /etc/fdfs
client.conf.sample storage_ids.conf.sample tracker.conf.sample storage.conf.sample
但是这些配置文件是不全的,而且都是模板,需要所以需要拷贝并重命名 模板文件tracker.conf.sample , storage.conf.sample, client.conf.sample
,并修改它们的配置:
sudo cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
sudo cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
修改配置
在家目录下新建tracker, storage, client
目录:
$ cd ~
$ mkdir tracker
$ mkdir storage
$ mkdir client
sudo vi /etc/fdfs/tracker.conf
:
# the tracker server port
port=22122
# the base path to store data and log files
base_path=/home/python/fastdfs/tracker
# HTTP port on this tracker server
http.server_port=8888
sudo vi /etc/fdfs/storage.conf
:
# storage所属的组
group_name=group1
# the storage server port
port=23000
# tracker服务器,虽然是同一台机器上,但是不能写127.0.0.1。这项配置可以出现一次或多次
tracker_server=10.211.55.15:22122
# the port of the web server on this storage server
http.server_port=8888
# the base path to store data and log files
base_path=/home/python/fastdfs/storage
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/home/python/fastdfs/storage
#store_path1=/home/caibh/fdfs2
sudo vi /etc/fdfs/client.conf
:
# the base path to store log files
base_path=/home/python/fastdfs/tracker
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=10.211.55.15:22122
#HTTP settings
http.tracker_server_port=8888
# 切到~/packages/fastdfs-nginx-module-1.20目录:
$ cd ~/packages/fastdfs-nginx-module-1.20
# 拷贝fastdfs-nginx-module-master/src下的mod_fastdfs.conf 到 /etc/fdfs/mod_fastdfs.conf
$ sudo cp src/mod_fastdfs.conf /etc/fdfs/mod_fastdfs.conf
sudo vim /etc/fdfs/mod_fastdfs.conf
:
connect_timeout=10
# the base path to store log files
base_path=/tmp
# FastDFS tracker_server
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=自己虚拟机的ip地址(10.211.55.15):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/python/fastdfs/storage
#store_path1=/home/yuqing/fastdfs1
url_have_group_name=true
配置过程中有几点要注意:
- 确保各种配置文件之间引用的端口一直。比如:
mod_fastdfs.conf文件中tracker_server的端口应该跟tracker.conf中port一致;
mod_fastdfs.conf文件中storage_server_port的端口应该跟跟storage.conf中port一致;
其他配置文件(
anti-steal.jpg http.conf mime.types storage_ids.conf
)虽然不用修改,但是fastdfs-nginx-module模块会用到。因此需要复制fastdfs-nginx-module/src/
下的anti-steal.jpg http.conf mime.types storage_ids.conf
到 /etc/fdfs目录下。-
上面的配置 有错误的地方:除了22122,23000这两个端口,其它(以http开头的端口,诸如http.server_port, http.tracker_server_port)端口要一致(同时,也要和/usr/local/nginx/conf/nginx.conf 中的server - listen 端口一致)。
/usr/local/nginx/conf/nginx.conf
中,要添加一行user root;
(行尾要加上分号;
), 来赋予nginx权限。
注意事项:上图中(上图中以开头的配置项, 实际配置过程 并不带号,只是为了 着重强调这几个配置项) ,以
http开头的端口
配置 应该 保持一致。
tracker.conf:
port = 22122 # the tracker server port
*http.server_port = 8888,(8080默认) # HTTP port on this tracker server
storage.conf
port = 23000 # the storage server port
tracker_server=10.211.55.15:22122 # tracker server
*http.server_port = 8888,(8888默认) # the port of the web server on this storage server
client.conf
tracker_server=10.211.55.15:22122 # tracker server
*http.tracker_server_port = 8888, (80默认) # http settings
mod_fastdfs.conf
tracker_server=10.211.55.15:22122 # FastDFS tracker_server
storage_server_port=23000 # the port of the local storage server
nginx.conf
listen 8888 # ***应该和上面的http端口 一致***。
启动tracker
和storage
:
# 启动
$ fdfs_trackerd /etc/fdfs/tracker.conf start
$ fdfs_storaged /etc/fdfs/storage.conf start
# 停止
$ service fdfs_trackerd stop
$ fdfs_trackerd /etc/fdfs/tracker.conf stop
$ service fdfs_storaged stop
$ fdfs_storaged /etc/fdfs/storage.conf stop
# 查看fdfs_trackerd, fdfs_storaged是否存活
$ ps aux | grep fdfs
# 查看nginx是否存活
$ ps aux | grep nginx
# 查看日志
$ tail -n10 ~/fastdfs/tracker/logs/trackerd.log
$ tail -n10 ~/fastdfs/storage/logs/storaged.log
# 如果日志显示有错误信息,需要根据信息来查找错误原因
用fdfs_tes
t测试上传
$ fdfs_test /etc/fdfs/client.conf upload ~/Downloads/logo_small.png
如果返回类似group1/M00/00/00/rBIK6VcaP0aARXXvAAHrUgHEviQ394.jpg
的文件id则说明文件上传成功
安装nginx
# 解压缩 nginx-1.16.0.tar.gz
$ tar -xf nginx-1.16.0.tar.gz
# 进入nginx-1.16.0目录中
$ cd ~/packages/nginx-1.16.0
# --prefix表示 安装路径,--add-module表示 加入fastdfs-nginx-module-1.20模块
$ sudo ./configure --prefix=/usr/local/nginx/ --add-module=/home/python/packages/fastdfs-nginx-module-1.20/src
################################
# 想要彻底删除 `tar包安装的nginx`,需要删除--prefix目录(即是 目录/usr/local/nginx). 命令$ rm -rf /usr/local/nginx
################################
$ sudo make
$ sudo make install
# 执行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/python/packages/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/"
sudo vim /usr/local/nginx/conf/nginx.conf
###########################################
# 添加下面一行(这行 很重要)
user root;
###########################################
# 在http部分中添加配置信息如下:
# listen 8888:监听端口8888
# server_name localhost: 域名localhost
# location: 浏览器访问路径中含有`group[0-9]`时,`nginx`会从`fastdfs`中 取文件
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
root /home/python/fastdfs/storage/data;
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
启动nginx
# 启动nginx
$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 停止nginx
$ sudo /usr/local/nginx/sbin/nginx -s stop
# 重新加载nginx
sudo /usr/local/nginx/sbin/nginx -s reload
# 检查自己编译出来的nginx能否运行
$ sudo /usr/local/nginx/sbin/nginx -V
# nginx version: nginx/1.16.0
开放端口:
$ firewall-cmd --zone=public --add-port=8888/tcp --permanent # 开放8888端口
$ firewall-cmd --zone=public --add-port=9270/tcp --permanent # 开放9270端口
测试上传图片:
$ fdfs_test /etc/fdfs/client.conf upload ~/Downloads/logo_small.png
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-07-28 22:03:40] DEBUG - base_path=/home/python/fastdfs/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=10.211.55.15, port=23000
group_name=group1, ip_addr=10.211.55.15, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/CtM3D109qzyAIOTRAAAX7nDuhUA387.png
source ip address: 10.211.55.15
file timestamp=2019-07-28 22:03:40
file size=6126
file crc32=1894679872
example file url: http://10.211.55.15:9270/group1/M00/00/00/CtM3D109qzyAIOTRAAAX7nDuhUA387.png
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/CtM3D109qzyAIOTRAAAX7nDuhUA387_big.png
source ip address: 10.211.55.15
file timestamp=2019-07-28 22:03:40
file size=6126
file crc32=1894679872
example file url: http://10.211.55.15:9270/group1/M00/00/00/CtM3D109qzyAIOTRAAAX7nDuhUA387_big.png
访问:
http://10.211.55.15:9270/group1/M00/00/00/CtM3D109qzyAIOTRAAAX7nDuhUA387_big.png
# 如果能看到图片则表示安装fastdfs-nginx-module成功
本文参考了https://segmentfault.com/a/1190000018251300?utm_source=tag-newest