天天看点

linux安装FastDFS+Nginx

安装FastDFS

1. 准备前期需要的压缩包

    下载 FastDFS 的安装包下载地址:https://github.com/happyfish100/FastDFS,本次我安装的是 v5.05 版本的。其他还需要:libfastcommon、fastdfs-nginx-module、nginx。

2. 安装 gcc 环境

    FastDFS 是 C 语言开发的,编译需要依赖 gcc 环境,所以需要安装 gcc:

yum install gcc-c++

,前提是在有网的环境下。

3. 安装 libevent

    FastDFS 依赖 libevent 库:

yum -y install libevent

4. 安装 libfastcommon

    libfastcommon 是 FastDFS 官方提供的,包含了 FastDFS 运行所需要的一些基础库。

    将 libfastcommon 拷贝至

/usr/local

目录下,执行解压缩命令:

tar -zxvf libfastcommonV1.0.7.tar.gz

,进入到解压后的目录中:

cd libfastcommonV1.0.7.tar.gz

,执行

./make.sh

./make.sh install

    注意:libfastcommon 安装好后会自动将库文件拷贝至

/usr/lib64

目录下,由于 FastDFS 程序引用

usr/lib

目录,所以需要手动将 /usr/lib64 下的库文件拷贝至 /usr/lib 下。要拷贝的文件如下:

linux安装FastDFS+Nginx

5. 编译安装 FastDFS

    将 FAstDFS_v5.05.tar.gz 拷贝至 /user/loacl 下解压,进入 FastDFS 目录:执行

./make.sh

./make.sh install

,安装成功后将安装目录下的 conf 下的文件拷贝到 /etc/fdfs 下。

linux安装FastDFS+Nginx

6. 配置 tracker

    进入 /etc/fdfs 目录,拷贝一份新的 tracker 配置文件:

cp tracker.conf.sample tracker.conf

,修改 tracker.conf,base_path = /home/yuqing/FastDFS 改为 base_path = /home/FastDFS。注意:/home/FastDFS 目录是自己手动创建的,此目录是用来存放日志文件的。

7. 配置 storage

    同配置 tracker.conf 的步骤一样,拷贝一份新的 storage.conf.sample:

cp storage.conf.sample storage.conf

,修改 storage.conf 文件:

group_name = group1
base_path = /home/quqing/FastDFS 改为 base_path = /home/FastDFS
store_path0 = /home/yuqing/FastDFS 改为 store_path0 = /home/FastDFS/fdfs_storage
tracker_server = 192.168.25.128:22122 (配置成虚拟机的ip)
           

8. 启动 tracker 和 storage

    启动tracker:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

    启动storage:

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

    初次执行可能需要一小段时间,请耐心等待即可。

如果需要开机自启,只需要将命令添加进文件:

/etc/rc.d/rc.local

9. 上传图片进行测试

    使用 fast_test 程序测试。

    修改

/etc/fdfs/client.conf

    base_path = /home/fastdfs

    tracker_server = 192.168.25.128:22122

例如将 /home 下的图片上传到 FastDFS 中:

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.png

如果上传成功会将地址返回并显示。

linux安装FastDFS+Nginx

FastDFS 和 nginx 整合

1. 首先需要安装 nginx

    ① 安装 PCRE

    PCRE 是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux上安装 pcre 库。

yum install -y pcre pcre-devel

    ② 安装 zlib

    zlib 库提供了很多种压缩和解压缩的方式。

yum install -y zlib zlib-devel

    ③ 安装 openssl

    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

    nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

    ④ 编译安装 nginx

    将 nginx 压缩包 拷贝至 linux 服务器,解压,进入 nginx 目录

    configure:

./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
           

    注意:上边将临时文件目录指定为 /var/temp/nginx,需要在 /var 下创建 temp 以及 nginx 目录

    然后编译安装:

make

make install

    ⑤ 启动 nginx

cd /usr/local/nginx/sbin/
./nginx
           

    ⑥ 开机自启

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL
           

   设置文件的访问权限

chmod a+x /etc/init.d/nginx
           

这样就可以在控制台很容易的操作 nginx 了

linux安装FastDFS+Nginx

/etc/rc.local

中加入一行

/etc/init.d/nginx start
           

2. 安装 FastDFS-nginx-module

    将 FastDFS-nginx-module 传至 /usr/local 下,解压,进入到 FastDFS-nginx-module/src 目录下,修改 config 文件。

    将所有的 /usr/local 改为 /usr/

linux安装FastDFS+Nginx

    将 FastDFS-nginx-module/src 下的 mod_FastDFS.conf 拷贝至 /etc/fdfs/ 下,并修改 mod_FastDFS.conf 的内容:

base_path=/home/FastDFS
tracker_server=192.168.25.128:22122
url_have_group_name=true		#url中包含group名称
store_path0=/home/FastDFS/fdfs_storage   #指定文件存储路径
           

    将 libfdfsclient.so 拷贝至 /usr/lib 下

   创建 nginx/client 目录:

mkdir -p /var/temp/nginx/client

3. nginx 添加 FastDFS-nginx-module 模块

    进入解压后的 nginx 压缩包目录,重新编译、安装

./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 \
--add-module=/root/fastdfs-nginx-module/src
           

    依次执行:

make、make install

4. 最后一步:配置 nginx.conf 文件

    找到 nginx 默认执行的 nginx.conf 配置文件,我安装的时候就是因为没有配置到正确的配置文件中,所以访问图片路径一直是 404。之所以会有两个 nginx.conf 文件,是因为解压后的 nginx 目录中有一个,安装的位置也有一个。

--prefix=/usr/local/nginx \

此编译参数的作用就是指定安装目录,nginx 默认执行的 nginx.conf 文件就是在这个目录中。

    如果不知道 nginx 安装在哪个目录,可以通过以下命令:

ps -ef | grep nginx

linux安装FastDFS+Nginx

如果执行了这个命令之后,后面却没有 nginx 的安装目录,也没关系,我们可以根据 nginx 的进程id 找到 nginx.exe 的所在目录:

linux安装FastDFS+Nginx

这样我们就可以执行

/usr/local/nginx/sbin/nginx -t

命令检查配置文件

linux安装FastDFS+Nginx

    最后,只要在配置文件中新加一个 server 节点就好了。

server {
        listen       80;
        server_name  192.168.25.128;

        location /group1/M00/{
                #root /home/FastDFS/fdfs_storage/data;
                ngx_fastdfs_module;
        }
}
           
linux安装FastDFS+Nginx

好了,执行完以上各个步骤之后,激动人心的时刻就要到来了,在浏览器中输入返回的地址,就可以看到图片了。

linux安装FastDFS+Nginx

大功告成!