CentOS-7配置fastDFS文件服务器和安装Nginx

配置步骤实在是很繁琐,听我慢慢道来!

主要是配置管理(tracker)和存储(storage)
返回地址样式 --> 域名/组名/磁盘名/目录名/文件名
image


- 1.获取安装包

fastDFS
https://github.com/happyfish100/fastdfs/tags

libfastcommon
https://github.com/happyfish100/libfastcommon/tags

fastdfs-nginx-module
https://github.com/happyfish100/fastdfs-nginx-module/tags

nginx
https://github.com/nginx/nginx/tags

文件包全部上传到 /usr/local/src 下(我是通过xftp上传的,具体看你业务需求,上传到指定其他地方也可以)


2.安装gcc环境

yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake

yum -y install wget httpd-tools vim


3.安装libfastcommon

解压libfastcommon-1.0.55.tar.gz
tar -zxvf libfastcommon-1.0.55.tar.gz

进入解压目录编译、安装
cd libfastcommon-1.0.55

编译: ./make.sh

安装: ./make.sh install

完成后 /usr/lib下就会有libfastcommon.so文件,如果没有就将libfastcommon.so文件复制到 /usr/lib 下
PS:(这个libfastcommon-1.0.55路径根据你的需求来指定,我的位置是这个 --> /usr/local/src/

cp /usr/local/src/libfastcommon-1.0.55/src/libfastcommon.so /usr/lib


4.安装FastDFS

解压fastdfs-6.07.tar.gz
tar -zxvf fastdfs-6.07.tar.gz

编译:./make.sh

安装:./make.sh install

安装成功后将conf内的文件复制到 /etc/fdfs

cp /usr/local/src/fastdfs-6.07/conf/* /etc/fdfs


5.安装tracker

进入 /etc/fdfs 目录,修改tracker.conf文件,如果文件不存在,就复制tracker.conf.sampletracker.conf

修该base_path = /home/yuqing/fastdfsbase_path = /home/logs/fastdfs(日志文件,也可自己指定位置)(指定位置时要在指定位置创建相应的文件目录

端口位置:http.server_port = 8080 修改成 http.server_port = 80(修改成80端口,更方便)

组名:store_group = group2 修改成 store_group = group1

启动tracker:fdfs_trackerd /etc/fdfs/tracker.conf start(start换成restart是重启)

查看tracker状态:netstat -unltp | grep tracker

image


6.安装storage

还是在 /etc/fdfs 目录下,修改storage.conf(如果文件不存在复制storage.conf.samplestorage.conf)

日志路径:base_path = /home/yuqing/fastdfs 修改为 base_path = /home/logs/storage指定位置时要在指定位置创建相应的文件目录

实际文件存储路径(能配置多个路径):store_path0 = /home/yuqing/fastdfs修改为 store_path0 = /home/data/storage指定位置时要在指定位置创建相应的文件目录

tracker_server:tracker_server = 192.168.209.121:22122 修改为 tracker_server = 192.168.136.131:22122(服务器IP地址,ifconfig查询)

组名:group_name = group1(如果和tracker组名一样,不用修改)

端口:http.server_port = 8888修改为http.server_port = 80(都要保持一致的端口80)

启动storage:fdfs_storaged /etc/fdfs/storage.conf start

查看storage状态:netstat -unltp | grep storage
image

然后查看tracker和storage是否互相通信:fdfs_monitor /etc/fdfs/storage.conf

如果出现ACTIVE,则证明服务启动正常,可以上传文件测试。
image


7.测试图片上传

FastDFS提供了一个文件上传命令:/usr/bin/fdfs_test 测试文件上传。测试上传需要连接tracker服务器,连接storage服务器。因此需要指定一个配置文件:client.conf配置文件,通过client.conf连接tracker服务器。

还是在** /etc/fdfs**目录下修改client.conf文件

日志路径:base_path = /home/yuqing/fastdfs修改为base_path = /home/logs/client指定位置时要在指定位置创建相应的文件目录

tracker_server:tracker_server = 192.168.0.197:22122修改为tracker_server = 192.168.136.131:22122(tracker端口,还是和IP地址保持一致,ifconfig查询)

测试命令:/usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg(需要在 /etc/fdfs 目录下使用该命令)

可以看到出现了example file url:.......

图片上传成功!
image


8.安装nginx

gcc安装:
yum install gcc-c++

pcre pcre-devel 安装:
yum install -y zlib zlib-devel

zlib 安装:
yum install -y zlib zlib-devel

OpenSSL 安装:
yum install -y openssl openssl-devel

解压nginx:tar -zxvf nginx-1.20.2.tar.gz

进入解压目录:cd nginx-1.20.2/

./configure编译后出现Makefile文件

然后在编译安装:make && make install


9.安装fastdfs-nginx-module

解压fastdfs-nginx-module-1.22.tar.gz
tar -zxvf fastdfs-nginx-module-1.22.tar.gz

修改/fastdfs-nginx-module/src/config文件,用#注释掉含有local关键字的代码行

image

复制/usr/lib64目录下的文件libfdfsclient.so
cp /usr/lib64/libfdfsclient.so /usr/lib

在nginx安装目录下执行如下命令:把module添加nginx中,通过设置安装参数方式添加模块
cd /usr/local/src/nginx-1.20.2/

./configure --add-module=../fastdfs-nginx-module-1.22/src(这是我的nginx和fastdfs-nginx-module-1.22处于同级目录下,如果你的不在,请填写正确目录)
image


成功后如图:
image

重新编译安装:make && make install

查看Nginx模块:/usr/local/nginx/sbin/nginx -V
image

复制fastdfs-nginx-module源码中的配置文件到/etc/fdfs目录
cd /usr/local/src/fastdfs-nginx-module-1.22/src
cp mod_fastdfs.conf /etc/fdfs/

进入/etc/fdfs修改mod_fastdfs.conf文件

连接超时:connect_timeout=10设置为10秒

teacker_server:tracker_server=tracker:22122修改为tracker_server=192.168.136.131:22122(还是和IP地址保持一致,ifconfig查询)

端口:storage_server_port=23000(默认是23000,是就不用改)

如果文件ID的uri中包含/group**,则要设置为true
url_have_group_name = false修改为url_have_group_name = true

Storage配置的store_path0路径,必须和storage.conf中的一致
store_path1=/home/yuqing/fastdfs修改为store_path0=/home/data/storage
base_path=/temp修改为base_path=/home/logs/storage


配置nginx:进入/usr/local/nginx/conf目录下修改nginx.conf

这个nginx安装目录就统一了。

修改nginx.conf:

#user nobody改成user root
image
下面的server {
listen 80;
server_name localhost;
.....localtion / {
....
.. }
}
在80端口下添加fastdfs-nginx模块
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}

(也就是把之前的location替换掉)
image

注意:
  listen 80 端口值是要与 /etc/fdfs/storage.conf 中的 http.server_port=80 (前面改成80了)相对应。如果改成其它端口,则需要统一,同时在防火墙中打开该端口
  location 的配置,如果有多个group则配置location ~/group([0-9])/M00 ,没有则不用配group。
在文件存储目录下创建软连接,将其链接到实际存放数据的目录,注意这个文件存储的位置后面多个data目录是系统自动生成的
ln -s /home/data/storage/data /home/data/storage/data/M00

10.启动nginx

/usr/local/nginx/sbin/nginx

查看nginx状态:netstat -nap | grep nginx
image

设置开机自启:
vim /etc/rc.d/rc.local

加入:/usr/local/nginx/sbin/nginx

使/etc/rc.d/rc.local变成可执行文件

chmod +x /etc/rc.d/rc.local

11.通过linux本地浏览器查看图片

因为没有设置防火墙等所以无法再windows下的浏览器查看
上传图片后点开url地址 即可看到图片:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg(图片测试,需要在/etc/fdfs目录下使用此命令)

posted @ 2022-02-06 16:20  zzuli-ycz  阅读(290)  评论(0编辑  收藏  举报