Linux安装FastDFS~Nginx~

柔情痞子 提交于 2019-12-02 05:01:02

发布于:2019-07-03 13:57

确保Linux联网

我这里使用的是CentOS7操作,联网教程
https://www.cnblogs.com/taopanfeng/p/10978752.html

先准备所需文件

安装所需的插件
yum -y install vim wget zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl net-tools unzip iptables iptables-services

防火墙最容易出错,我们先关闭防火墙,这一点尤为重要,我已采坑~~~
chkconfig iptables off

创建目录,用于存放文件
mkdir /usr/local/panfeng

进入创建好的目录
cd /usr/local/panfeng/

依次执行下面文件,下载数据
wget http://ncu.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
wget http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.08/FastDFS_v5.08.tar.gz
wget http://nginx.org/download/nginx-1.10.1.tar.gz
wget https://codeload.github.com/happyfish100/libfastcommon/zip/master -O libfastcommon.zip

也可以,利用工具把四个文件传到/usr/local/panfeng/下
所需文件:
fastdfs-nginx-module_v1.16.tar.gz
FastDFS_v5.08.tar.gz
nginx-1.10.1.tar.gz
libfastcommon.zip

文件分享
链接
https://pan.baidu.com/share/init?surl=2t-T4wk-q9jpnp948pM3Zw
密码
xi4y

安装 libfastcommon-master

进入目录
cd /usr/local/panfeng/

解压zip文件
unzip libfastcommon.zip

进入到解压后的目录
cd libfastcommon-master/

编译
./make.sh

安装
./make.sh install

安装 FastDFS

进入目录
cd /usr/local/panfeng/

解压文件
tar -zxvf FastDFS_v5.08.tar.gz

进入解压后的目录
cd /usr/local/panfeng/FastDFS

编译
./make.sh

安装
./make.sh install
如果安装成功,执行下面命令会看到/etc/init.d/下看到提供的脚本文件

ll /etc/init.d/ | grep fdfs

在这里插入图片描述

  • fdfs_storaged是storage启动脚本
  • dfs_trackerd是tracker启动脚本
执行下面命令能够在 /etc/fdfs/ 目录下看到默认的配置文件模板

ll /etc/fdfs/

在这里插入图片描述

  • arcker.conf.sample是tracker的配置文件模板
  • storage.conf.sample是storage的配置文件模板
  • lient.conf.sample是客户端的配置文件模板

配置并启动tarcker服务

将模版文件复制,复制的时候把.sample去掉就是配置文件了
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

修改复制后的配置文件
vim /etc/fdfs/tracker.conf

修改存储日志和数据的根目录,把 base_path=/home/yuqing/fastdfs 改为 base_path=/panfeng/tracker
按 I 插入 编写完成后按Esc 输入:wq 保存退出

新建目录
mkdir -p /panfeng/tracker

启动服务,如果要停止把start换成stop
service fdfs_trackerd start

检查FastDFS Tracker Server是否启动成功
ps -ef | grep fdfs_trackerd

设置tracker服务开机启动
chkconfig fdfs_trackerd on

在这里插入图片描述

配置并启动storage服务

首先将模板文件复制
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

修改复制后的配置文件
vim /etc/fdfs/storage.conf

修改的内容如下:
把41行 base_path=/home/yuqing/fastdfs 改为 base_path=/panfeng/storage
把109行 store_path0=/home/yuqing/fastdfs 改为 store_path0=/panfeng/storage
把119行 tracker_server=192.168.209.121:22122 改为 tracker_server=192.168.1.104:22122 这里我的虚拟机ip为192.168.1.104
按 I 插入 编写完成后按Esc 输入:wq 保存退出

新建目录
mkdir -p /panfeng/storage

启动服务,如果要停止把start换成stop
service fdfs_storaged start

设置storage服务开机启动
chkconfig fdfs_storaged on

检查storage服务是否启动成功
ps -ef | grep fdfs

在这里插入图片描述

为什么需要用Nginx访问?

  • FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但是同组存储服务器之间需要进入文件复制,有同步延迟的问题。
  • 假设Tracker服务器将文件上传到了192.168.4.125,上传成功后文件ID已经返回给客户端。此时FastDFS存储集群机制会将这个文件同步到同组存储192.168.4.126,在文件还没有复制完成的情况下,客户端如果用这个文件ID在192.168.4.126上取文件,就会出现文件无法访问的错误。
  • 而fastdfs-nginx-module可以重定向文件连接到文件上传时的源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误

安装fastdfs-nginx-module

进入目录
cd /usr/local/panfeng/

解压
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

进入src目录
cd /usr/local/panfeng/fastdfs-nginx-module/src/

编辑config
vim config

使用以下底行命令,将所有的/usr/local替换为 /usr,这个才是正确的目录:
:%s+/usr/local/+/usr/+g
输入 :%s+/usr/local/+/usr/+g 回车进行替换,再输入:wq 保存退出

配置nginx与FastDFS关联配置文件

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

修改复制好的文件
vim /etc/fdfs/mod_fastdfs.conf

修改以下配置:
客户端访问文件连接超时时长(单位:秒),把3行 connect_timeout=2 改为 connect_timeout=20
tracker服务IP和端口,把40行 tracker_server=tracker:22122 改为 tracker_server=192.168.1.104:22122 这里我的虚拟机ip为192.168.1.104
访问链接前缀加上组名,把53行 url_have_group_name = false 改为 url_have_group_name = true
文件存储路径,把62行 store_path0=/home/yuqing/fastdfs 改为 store_path0=/panfeng/storage
按 I 插入 编写完成后按Esc 输入:wq 保存退出

进入到/etc/fdfs 目录
cd /usr/local/panfeng/FastDFS/conf/

复制 FastDFS 的部分配置文件到/etc/fdfs 目录
cp http.conf mime.types /etc/fdfs/

安装Nginx的插件

进入目录
cd /usr/local/panfeng/

解压安装包
tar -zxvf nginx-1.10.1.tar.gz

进入目录
cd /usr/local/panfeng/nginx-1.10.1

配置nginx安装包,并指定fastdfs-nginx-model
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/panfeng/fastdfs-nginx-module/src/

编译并安装
make && make install

启动Nginx

配置nginx整合fastdfs-module模块,需要修改nginx配置文件
vim /opt/nginx/conf/nginx.conf
  • 将文件中,原来的server 80{ ...} 部分代码替换为如下代码
    server {
        listen       80;
        server_name  image.panfeng.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;
        }
    }
启动nginx
nginx

查询nginx是否启动成功
ps -ef|grep nginx
  • 设置Nginx开机自启动
创建一个开机启动的脚本
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="/opt/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
  • 添加完成之后
修改权限
chmod 777 /etc/init.d/nginx

添加到服务列表
chkconfig --add /etc/init.d/nginx

设置开机启动
chkconfig nginx on
再依次执行下面两条命令
iptables -L

iptables -F
执行完上面两条命令,可能会造成一些Linux的一些服务失效,需要重启,
比如:Docker的重启命令

systemctl restart docker
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!