腾讯云centos7下搭建fastDFS+nginx

1.下载所需要的安装包:

在服务器根目录(你也可以在其他地方下载)下载以下安装包:
libfastcommon包下载: wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -O libfastcommon.tar.gz
fastdfs包下载:wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -O fastdfs.tar.gz
nginx包下载:wget http://nginx.org/download/nginx-1.12.0.tar.gz -O nginx.tar.gz
fastdfs-nginx模块包下载:wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -O fastdfs-nginx-module.tar.gz
### -O 表示重命名下载包
### 除了nginx包,其他包都在这里下载:https://github.com/happyfish100
复制代码

2.环境安装

安装gcc环境
yum -y install gcc-c++
安装pcre-devel与openssl-devel
yum -y install pcre-devel openssl openssl-devel
这个命令是解决安装nginx时出现的错误问题:./configure: error: the HTTP rewrite module requires the PCRE library.

3.安装libfastcommon

1.解压libfastcommon包: tar -zxvf libfastcommon.tar.gz
2.进入解压目录: cd libfastcommon-1.0.39
3.安装: ./make.sh
         ./make.sh install
复制代码

4.安装FastDFS

1.解压fastDFS包: tar -zxvf fastdfs.tar.gz
2.进入解压目录: cd fastdfs-5.11
3.安装:./make.sh
        ./make.sh install
如果安装成功,在/etc/fdfs目录下会有四个文件:
-rw-r--r-- 1 root root  1461 Nov 13 09:54 client.conf.sample
-rw-r--r-- 1 root root  7927 Nov 13 09:54 storage.conf.sample
-rw-r--r-- 1 root root   105 Nov 13 09:54 storage_ids.conf.sample
-rw-r--r-- 1 root root  7389 Nov 13 09:54 tracker.conf.sample
进入目录 cd /etc/fdfs,然后将这三个文件复制并去掉.sample:
cp client.conf.sample client.conf
cp storage.conf.sample storage.conf
cp tracker.conf.sample tracker.conf
复制代码

5.配置tracker

1.创建保存tracker的data和log的目录:mkdir -p /usr/local/fast-dfs/fastdfs_tracker
2.进入目录 cd /etc/fdfs
3.修改tracker.conf文件:vim tracker.conf
修改如下:
    disabled=false #默认开启
    port=22122 #默认端口号
    base_path=/usr/local/fast-dfs/fastdfs_tracker #刚刚创建的目录
    http.server_port=6666 #默认端口是8080
修改完后,
启动tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
查看tracker是否有端口:netstat -ntpul |grep fdfs (如果没有出现端口,说明tracker启动失败)
其它命令:
重启tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
停止tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
添加开机启动:
chmod +x /etc/rc.d/rc.local
echo '/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart' >> /etc/rc.d/rc.local
复制代码

6.配置storage

1.创建保存storage的data和log的目录:mkdir /usr/local/fast-dfs/fastdfs_storage
  再创建一个文件存储目录:mkdir /usr/local/fast-dfs/fastdfs_storage_data
2.进入目录:cd /etc/fdfs
3.修改storage.conf文件:vim storage.conf
修改如下:
    disabled=false #默认false
    group_name=group1 #组名,根据实际情况修改
    port=23000 #设置storage的端口号,默认是23000,同一个组的storage端口号必须一致
    base_path=/usr/local/fast-dfs/fastdfs_storage #设置storage数据文件和日志目录
    store_path_count=1 #存储路径个数,需要和store_path个数匹配
    store_path0=/usr/local/fast-dfs/fastdfs_storage_data #实际文件存储路径
    tracker_server=服务器公网ip地址:22122
    http.server_port=8888 #设置 http 端口号
修改保存后创建软引用
ln -s /usr/bin/fdfs_storaged /usr/local/bin
启动storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
查看storage是否有端口:netstat -ntpul |grep fdfs (如果没有出现端口,说明storage启动失败)
其它命令:
重启storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
停止storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
添加开机启动:
echo '/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart' >> /etc/rc.d/rc.local
复制代码

7.校验整合,确定storage是否注册到了tracker中去

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
如下图表示成功注册了

8.配置客户端client

1.进入目录:cd /etc/fdfs
2.修改client.conf: vim client.conf
修改如下:
    base_path=/usr/local/fast-dfs/fastdfs_tracker #tracker服务器文件路径
    tracker_server=服务器公网ip地址:22122 #tracker服务器IP地址和端口号
    http.tracker_server_port=6666 # tracker 服务器的 http端口号,必须和tracker的设置对应起来!
复制代码

9.FastDFS的nginx模块安装

1.进入根目录(下载包存放的目录):cd ~
2.解压fastdfs的Nginx模块包:tar -zvxf fastdfs-nginx-module.tar.gz
3.重命名模块目录:mv fastdfs-nginx-module-1.12.0 fastdfs-nginx-module
4.修改模块配置文件:vim /root/fastdfs-nginx-module/src/config 
修改如下:
    ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
    CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
保存后,建立软链接:ln -s /usr/include/fast* /usr/local/include/
5.1解压nginx包: tar -zxvf nginx.tar.gz
5.2进入nginx解压目录并添加fastdfs的nginx模块:
cd nginx-1.12.0
./configure --prefix=/usr/local/storage-nginx --add-module=/root/fastdfs-nginx-module/src
如果没出错,就依次执行:
make
make install
复制代码

10.配置storage的nginx

上面的nginx安装完后,作为storage的nginx,进行配置
1.修改nginx的配置文件:vim /root/nginx-1.12.0/conf/nginx.conf
修改监听端口 listen 9999, 新增location(由于nginx编码严格,不要复制,手动输入,否则会出现nginx: [emerg] unknown directive " " in /usr/local/nginx-1.12.0-storage/conf/nginx.conf:49):
server {
        listen       9999;
        server_name  localhost;


        location / {
            root   html;
            index  index.html index.htm;
        }


        location ~/group1/M00 {
            root /usr/local/fast-dfs/fastdfs_storage_data/data;
            ngx_fastdfs_module;
        }


}
然后进入FastDFS安装时的解压过的目录,将http.conf和mime.types拷贝到/etc/fdfs目录下:
cd  /root/fastdfs-5.11/conf
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/
还需要把fastdfs-nginx-module安装目录中src目录下的mod_fastdfs.conf也拷贝到/etc/fdfs目录下:
cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
再对刚复制的mod_fastdfs配置:
vim /etc/fdfs/mod_fastdfs.conf
如下:
 base_path=/usr/local/fast-dfs/fastdfs_storage  #保存日志目录
 tracker_server=服务器公网ip地址:22122 #tracker服务器的IP地址以及端口号
 storage_server_port=23000 #storage服务器的端口号
 url_have_group_name = true #文件 url 中是否有 group 名
 store_path0=/usr/local/fast-dfs/fastdfs_storage_data   #存储路径
 group_count = 3 #设置组的个数,事实上这次只使用了group1
 在文件的最后,设置group
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data

[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data

[group3]
group_name=group3
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data
保存后,创建M00至storage存储目录的符号连接:
ln  -s  /usr/local/fast-dfs/fastdfs_storage_data/data/ /usr/local/fast-dfs/fastdfs_storage_data/data/M00
启动nginx:
/usr/local/storage-nginx/sbin/nginx -c /root/nginx-1.12.0/conf/nginx.conf
查看端口是否启动:
netstat -ntpul |grep nginx #出现9999端口的nignx説明启动成功
复制代码

11.配置tracker的nginx

1.再解压一个nginx,然后进入解压目录添加fastdfs-nginx模块:
./configure --prefix=/usr/local/tracker-nginx --add-module=/root/fastdfs-nginx-module/src
没出现错误,就依次执行:
make
make install
然后修改nginx解压目录下的/conf/nginx.conf:
 upstream fdfs_group1 {
        server 127.0.0.1:9999;
    }
    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location /group1/M00 {
            proxy_pass http://fdfs_group1;
        }
        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}


        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
保存后,启动tracker的nginx:
/usr/local/tracker-nginx/sbin/nginx -c /root/tracker-nginx/nginx-1.12.0/conf/nginx.conf
查看端口是否有80和9999:
netstat -ntpul |grep nginx
至此FastDFS服务部署完成。
下面配置下防火墙:
1.开启所有配置的端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent #开启80端口号
firewall-cmd --zone=public --add-port=23000/tcp --permanent #开启23000端口号
firewall-cmd --zone=public --add-port=22122/tcp --permanent #开启22122端口号
firewall-cmd --zone=public --add-port=9999/tcp --permanent #开启9999端口号
firewall-cmd --zone=public --add-port=6666/tcp --permanent #开启6666端口号
firewall-cmd --zone=public --add-port=8888/tcp --permanent #开启8888端口号
2.重启防火墙

CentOS7 防火墙相关命令:
firewall-cmd --zone=public --list-ports #查看开启的端口
firewall-cmd state   #查看防火墙是否开启
systemctl enable firewalld.service    #开启防火墙
systemctl stop firewalld.service     #关闭防火墙(开机会仍会启动)
systemctl disable firewalld.service  #禁用防火墙(开机后不再启动)
复制代码

12.测试

在idea上测试,操作如下:
1.拉取fastdfs的java客户端包:File-->New-->project from version control-->github
2.填上Url:https://github.com/happyfish100/fastdfs-client-java.git ,项目所在目录、名称,点击clone
3.等项目初次化完后,设置下你的maven的安装目录:settings-->搜索maven-->改变maven home directory的目录
4.打开右边Maven Project窗口,进行安装fastdfs的java客户端:lifecycle-->install
5.至此你的fastdfs的maven依赖安装完成,在你的测试项目的pom.xml文件加入依赖就可以测试了
<dependency>
      <groupId>org.csource</groupId>
      <artifactId>fastdfs-client-java</artifactId>
      <version>1.27-SNAPSHOT</version>
</dependency>

例子:
在resources下创建fdfs_client.conf配置文件,添加内容:tracker_server=服务器公网ip地址:22122
创建一个demo.java:
public class Demo {
    public static void main(String[] args) throws Exception{
        //加载配置文件,配置文件中的内容就是 tracker 服务的地址
        ClientGlobal.init("D:/myWork/fasDFS-demo/src/main/resources/fdfs_client.conf");
        //创建一个 TrackerClient 对象。直接 new 一个
        TrackerClient trackerClient=new TrackerClient();
        //使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
        TrackerServer trackerServer=trackerClient.getConnection();
        //创建一个 StorageServer 的引用,值为 null
        StorageServer storageServer=null;
        //创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
        StorageClient storageClient=new StorageClient(trackerServer,storageServer);
        //使用 StorageClient 对象上传图片。
        //扩展名不带“.”
        String[] strings=storageClient.upload_file("C:/Users/Public/Pictures/Sample Pictures/test.jpg","jpg",null);
        //遍历数组打印包含组名和图片的路径。
        for (String string:strings){
            System.out.println(string);
        }
    }
}
最后在浏览器上访问你上传的图片:
http://服务器公网ip地址/group1/M00/00/00/rBsABFvqfIyACR3WAAl5WIkOlj8480.jpg
复制代码

参考文章:

blog.csdn.net/weixin_4037… cloud.tencent.com/developer/a…

转载于:https://juejin.im/post/5bea84e3e51d450d050236bd

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值