fastdfs安装路径在:/usr/local/FastDFS/
tracker的数据和日志存放目录:/data/fdfs/tracker
storage数据和日志的存放目录:/data/fdfs/storage
storage上传文件存放目录:/data/fdfs/storage
tracker的地址:192.168.2.14:22122
nginx的tar包解压缩后路径:/usr/local/nginx-1.14.1/
nginx安装路径:/data/nginx
nginx模块fastdfs-nginx安装目录:/usr/local/fastdfs-nginx-module
1、安装FastDFS服务的依赖
1.1、安装GCC依赖
GCC用来对C语言代码进行编译运行
[root@wzy14 soft]# yum install -y gcc
1.2、安装unzip工具
unzip工具可以帮助我们对压缩包进行解压缩
[root@wzy14 soft]# yum install -y unzip zip
1.3、安装libevent
[root@wzy14 soft]# yum install -y libevent
1.4、安装Nginx所需依赖
[root@wzy14 soft]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
1.5、安装libfastcommon-master
[root@wzy14 soft]# unzip libfastcommon-master.zip
[root@wzy14 soft]# mv libfastcommon-master /usr/local/
[root@wzy14 soft]# cd /usr/local/libfastcommon-master/
[root@wzy14 libfastcommon-master]# ./make.sh
[root@wzy14 libfastcommon-master]# ./make.sh install
2、安装FastDFS
2.1、编译安装
[root@wzy14 soft]# tar -zxvf FastDFS_v5.08.tar.gz -C /usr/local/
[root@wzy14 soft]# cd /usr/local/FastDFS/
#编译安装
[root@wzy14 FastDFS]# ./make.sh
[root@wzy14 FastDFS]# ./make.sh install
检查安装情况
[root@wzy14 ~]# ll /etc/init.d/ | grep fdfs
-rwxr-xr-x 1 root root 918 Nov 12 15:41 fdfs_storaged
-rwxr-xr-x 1 root root 920 Nov 12 15:41 fdfs_trackerd
- fdfs_trackerd 是tracker启动脚本;
- fdfs_storaged 是storage启动脚本。
在 /etc/fdfs目录,通过命令查看到以下配置文件模板:
[root@wzy14 ~]# ll /etc/fdfs/
total 20
-rw-r--r-- 1 root root 1461 Nov 12 15:41 client.conf.sample
-rw-r--r-- 1 root root 7927 Nov 12 15:41 storage.conf.sample
-rw-r--r-- 1 root root 7200 Nov 12 15:41 tracker.conf.sample
- tarcker.conf.sample 是tracker的配置文件模板;
- storage.conf.sample 是storage的配置文件模板;
- client.conf.sample 是客户端的配置文件模板。
2.2、启动tracker
FastDFS的tracker和storage在刚刚的安装过程中,都已经被安装了,因此我们安装这两种角色的方式是一样的。不同的是,两种需要不同的配置文件。我们要启动tracker,就修改刚刚看到的tarcker.conf,并且启动fdfs_trackerd脚本即可。
(1)编辑tracker配置
配置的是tracker的数据和日志存放目录。
[root@wzy14 ~]# cd /etc/fdfs/
[root@wzy14 fdfs]# cp tracker.conf.sample tracker.conf
#tracker的数据和日志存放目录
[root@wzy14 ~]# vim /etc/fdfs/tracker.conf
base_path=/data/fdfs/tracker
(2)创建目录
[root@wzy14 ~]# mkdir -p /data/fdfs/tracker
(3)启动tracker
我们可以使用 sh /etc/init.d/fdfs_trackerd 启动,不过安装过程中,fdfs已经被设置为系统服务,我们可以采用熟悉的服务启动方式:
[root@wzy14 ~]# ll /etc/init.d/fdfs_trackerd
-rwxr-xr-x 1 root root 920 Nov 12 15:41 /etc/init.d/fdfs_trackerd
# 启动fdfs_trackerd服务,停止用stop
[root@wzy14 ~]# service fdfs_trackerd start
们可以通过以下命令,设置tracker开机启动:
[root@wzy14 ~]# chkconfig fdfs_trackerd on
2.3、启动storage
(1)编辑storage配置
[root@wzy14 ~]# cd /etc/fdfs/
[root@wzy14 fdfs]# cp storage.conf.sample storage.conf
[root@wzy14 fdfs]# vim storage.conf
base_path=/data/fdfs/storage
store_path0=/data/fdfs/storage
tracker_server=192.168.2.14:22122
base_path=/data/fdfs/storage #storage数据和日志的存放目录
store_path0=/data/fdfs/storage#storage上传文件存放目录
tracker_server=192.168.2.14:22122#tracker的地址
(2)创建storage存放目录
[root@wzy14 fdfs]# mkdir -p /data/fdfs/storage
(3)启动storage
我们可以使用 sh /etc/init.d/fdfs_storaged
启动,同样我们可以用服务启动方式
[root@wzy14 ~]# ll /etc/init.d/fdfs_storaged
-rwxr-xr-x 1 root root 918 Nov 12 15:41 /etc/init.d/fdfs_storaged
#启动fdfs_storaged服务,停止用stop
[root@wzy14 ~]# service fdfs_storaged start
#开机自启
[root@wzy14 ~]# chkconfig fdfs_storaged on
查看进程
[root@wzy14 ~]# ps -ef | grep fdfs
root 26105 1 0 15:54 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root 26185 1 4 16:02 ? 00:00:03 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
root 26218 25412 0 16:03 pts/0 00:00:00 grep --color=auto fdfs
2.3、测试
进入/etc/fdfs/目录,修改client.conf文件
[root@wzy14 tmp]# cd /etc/fdfs/
[root@wzy14 fdfs]# cp client.conf.sample client.conf
[root@wzy14 fdfs]# vim client.conf
base_path=/tmp
tracker_server=192.168.2.14:22122
运行以下上传图片命令
[root@wzy14 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /tmp/0.jpg
group1/M00/00/00/wKgCDlvs4kqAYZqmAACWvXiBxKY978.jpg
上面中返回结果分析:
(1)group1:表示分组;
(2)M00:对应store_path0;
(3)/00/00:表示磁盘路径。
3、安装Nginx及FastDFS模块
3.1、安装FastDFS的Nginx模块
解压缩及配置config文件
[root@wzy14 soft]# tar -zxf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/
# 进入配置目录
[root@wzy14 soft]# cd /usr/local/fastdfs-nginx-module/src/
[root@wzy14 src]# vim config
# 执行下面命令(将配置中的/usr/local改为/usr):
:%s+/usr/local/+/usr/+g
配置mod_fastdfs.conf
[root@wzy14 ~]# cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@wzy14 ~]# vim /etc/fdfs/mod_fastdfs.conf
connect_timeout=10
tracker_server=192.168.2.14:22122
url_have_group_name = true
store_path0=/data/fdfs/storage
connect_timeout=10 # 客户端访问文件连接超时时长(单位:秒)
tracker_server=192.168.2.14:22122 # tracker服务IP和端口
url_have_group_name=true # 访问链接前缀加上组名
store_path0=/data/fdfs/storage # 文件存储路径
复制 FastDFS的部分配置文件到/etc/fdfs目录
[root@wzy14 ~]# cd /usr/local/FastDFS/conf/
[root@wzy14 conf]# cp http.conf mime.types /etc/fdfs/
3.2、安装Nginx
(1)解压缩及配置
[root@wzy14 ~]# mkdir /data/nginx
[root@wzy14 soft]# tar -zxvf nginx-1.14.1.tar.gz -C /usr/local/
[root@wzy14 soft]# cd /usr/local/nginx-1.14.1/
[root@wzy14 nginx-1.14.1]# ./configure --prefix=/data/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/fastdfs-nginx-module/src/
注意其中--add-module指定的是fastdfs-nginx-module压缩目录。
(2)编译安装
[root@wzy14 nginx-1.14.1]# make && make install
(3)配置nginx整合fastdfs-module模块
我们需要修改nginx配置文件,在/data/nginx/config/nginx.conf文件中
[root@wzy14 ~]# vim /data/nginx/conf/nginx.conf
server {
listen 80;
server_name image.wzy.com;
# 监听域名中带有group的,交给FastDFS模块处理
location ~/group([0-9])/ {
ngx_fastdfs_module;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
(4)设置nginx开机启动
[root@wzy14 ~]# vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/bin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/data/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
nginx # 启动
nginx -s stop # 停止
nginx -s reload # 重新加载配置
# 修改权限
[root@wzy14 ~]# chmod 777 /etc/init.d/nginx
# 添加到服务列表
[root@wzy14 ~]# chkconfig --add /etc/init.d/nginx
[root@wzy14 ~]# chkconfig nginx on