阿里云服务器使用FastDFS+Nginx搭建图片服务器
前言
操作环境:CentOS7 X64,把所有的安装包下载到/usr/local/softpackage/下,解压到当前目录。没有softpackage自己新建一个
新建softpackage文件命令:mkdir softpackage
在安装之前先去修改hosts,将文件服务器的ip与域名映射(单机TrackerServer环境),因为后面很多配置里面都需要去配置服务器 地址,ip变了,就只需要修改hosts即可。
# vim /etc/hosts
增加如下一行,ip地址是你安装本机的ip
106.15.180.40 file.yunxin.com
安装FastDFS环境
1、安装libevent
# yum -y install libevent
2、下载安装 libfastcommon
① 进入到/softpackage/目录
# cd /softpackage
② 下载libfastcommon
# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
③ 解压
# tar -zxvf V1.0.7.tar.gz
# cd libfastcommon-1.0.7
④ 编译、安装
# ./make.sh
# ./make.sh install
⑤ 拷贝 libfastcommon.so到 /usr/lib/下
# cd /usr/lib64/
# cp libfastcommon.so /usr/lib
3、下载安装FastDFS
① 进入到/softpackage/目录
# cd /softpackage
② 下载FastDFS
# wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
③ 解压
# tar -zxvf V5.05.tar.gz
# cd fastdfs-5.05
④ 编译、安装
# ./make.sh
# ./make.sh install
⑤ 拷贝 /softpackage/fastdfs-5.05/conf下所有文件到 /etc/fdfs/下
# cd /softpackage/fastdfs-5.05/conf
# cp * /etc/fdfs/
4、配置Tracker服务
① 进入 /etc/fdfs下,编辑 tracker.conf
# cd /etc/fdfs/
# vim tracker.conf
② 修改以下部分(指定自己的路径),其余默认
base_path=/fastdfs/tracker
③ 创建上一步指定的路径,即 base_path对应的路径
# cd /
# mkdir /fastdfs/tracker -p
④ 将client和storage也一并创建出来
# cd /fastdfs/
# mkdir client
# mkdir storage
⑤ 启动Tracker服务
# cd /usr/bin
# fdfs_trackerd /etc/fdfs/tracker.conf
查看 FastDFS Tracker 是否已成功启动 ,22122端口正在被监听,则算是Tracker服务安装成功。
# netstat -unltp|grep fdfs
⑥ 设置Tracker开机启动
# chkconfig fdfs_trackerd on
5、配置 FastDFS 存储 (Storage)
① 进入 /etc/fdfs下,编辑 storage.conf
# cd /etc/fdfs/
# vim storage.conf
② 修改以下部分(指定自己的路径),其余默认
# group_name表示组织名,访问图片时需要加上这个才能访问,组织名自己随意定义
group_name=taofut
base_path=/fastdfs/storage
store_path0=/fastdfs/storage
tracker_server=file.yunxin.com:22122(最开始hosts配置的域名,也就是本机的ip地址)
③ 启动Storage服务
# cd /usr/bin
# fdfs_storaged /etc/fdfs/storage.conf
查看Storage是否成功启动,23000端口正在被监听,就算Storage启动成功。
# netstat -unltp|grep fdfs
④ 设置Storage开机启动
# chkconfig fdfs_storaged on
6、文件上传测试
① 修改客户端配置文件
# cd /etc/fdfs
# vim client.conf
② 修改以下部分,其余默认
base_path=/fastdfs/client
tracker_server=file.yunxin.com:22122
③ 上传测试,我事先在/home下放了一张图片 test.jpg
# cd /home
# /usr/bin/fdfs_test /etc/fdfs/client.conf upload test.jpg
上传成功后返回的结果:
This is FastDFS client test program v5.05
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.[2020-09-22 16:20:53] DEBUG - base_path=/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=106.15.180.40, port=23000group_name=group1, ip_addr=106.15.180.40, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/rBMLY19ps-WAEsDEAAE1Pmp6F-g479.jpg
source ip address: 172.19.11.99
file timestamp=2020-09-22 16:20:53
file size=79166
file crc32=1786386408
example file url: http://106.15.180.40/group1/M00/00/00/rBMLY19ps-WAEsDEAAE1Pmp6F-g479.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/rBMLY19ps-WAEsDEAAE1Pmp6F-g479_big.jpg
source ip address: 172.19.11.99
file timestamp=2020-09-22 16:20:53
file size=79166
file crc32=1786386408
example file url: http://106.15.180.40/group1/M00/00/00/rBMLY19ps-WAEsDEAAE1Pmp6F-g479_big.jpg
[root@izuf6cbvil0rg78oxhgom6z home]# cd /usr/local/
查看上传的文件
# cd /fastdfs/storage/data/00/00
安装Nginx
1、安装Nginx所需环境
① gcc 安装
# yum install gcc-c++
② pcre-devel 安装
# yum install -y pcre pcre-devel
③ zlib 安装
# yum install -y zlib zlib-devel
④ OpenSSL 安装
# yum install -y openssl openssl-devel
3、安装Nginx
① 上传 nginx包到 /softpackage/下
② 解压
# cd /softpackage
# tar -zxvf nginx-1.8.0.tar.gz
③ 创建临时目录/var/temp/nginx
mkdir /var/temp/nginx -p
④ 添加nginx配置
# cd nginx-1.8.0/
将以下内容粘贴到命令行执行
./configure \ --prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \ –lock-path=/var/lock/nginx.lock
\ --error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
执行上面的命令可能会报文件找不到,解决方案见如下链接
https://blog.csdn.net/qq_45017999/article/details/10717347
⑤ 编译、安装
# make
# make install
⑥ 修改 nginx.conf 文件
# cd /usr/local/nginx/conf
# vim nginx.conf
添加如下配置:
server {
listen 88;
server_name file.yunxin.com;
location /taofut/M00 {
ngx_fastdfs_module;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
4、配置fastdfs-nginx-module
① 上传 fastdfs-nginx-module_v1.16.tar.gz包到 /softpackage(也可以wget + 地址 直接网上下载)
② 解压
# cd /softpackage
# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
③ 修改config文件
# cd fastdfs-nginx-module/src
# vim config
将以下三个路径里的local去掉
/usr/local/include/fastdfs
/usr/local/include/fastcommon/
/usr/local/lib -lfastcommon -lfdfsclient
修改成:
/usr/include/fastdfs
/usr/include/fastcommon/
/usr/lib -lfastcommon -lfdfsclient
④ 复制并修改 mod_fastdfs.conf 文件
# cd /softpackage/fastdfs-nginx-module/src
# cp mod_fastdfs.conf /etc/fdfs/
# cd /etc/fdfs
# vim mod_fastdfs.conf
修改以下内容,其余默认
base_path=/fastdfs/tmp
tracker_server=file.yunxin.com:22122
group_name=taofut
url_have_group_name = true
store_path0=/fastdfs/storage
创建base_path指定的目录
# cd /fastdfs
# mkdir tmp
⑤ 添加模块
- 先进入解压包目录
# cd /softpackage/nginx-1.8.0
- 添加模块
# ./configure --add-module=/softpackage/fastdfs-nginx-module/src
⑥ 重新编译、安装
# make
# make install
⑦ 测试Nginx
- 进入到nginx安装目录
# cd /usr/local/nginx/sbin
# ./nginx -t
- 测试成功返回结果
ngx_http_fastdfs_set pid=10804 ngx_http_fastdfs_set pid=10804 nginx:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is
successful
⑧ 启动Nginx服务
# ./nginx
⑨ 重启Tracker、Storage、Nginx服务
# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# cd /usr/local/nginx/sbin/
# ./nginx -s reload
⑩ 访问我之前上传的文件
输入访问地址:http://106.15.180.40:88/group1/M00/00/00/rBMLY19p6RSANqG2AAKtseksq7A095.jpg
访问成功!